def get_json(self): data = {} dish = {'chin_name': self.chin_name, 'eng_name': self.eng_name} if self.desc: dish['desc'] = self.desc if self.pinyin: dish['pinyin'] = self.pinyin data['dish'] = [dish] if self.reviews: reviews = [] for review in self.reviews: review_data = { 'username': review.user.username, 'date': format_date(review.date), 'text': review.text, 'restaurant': review.rest_dish.restaurant.name } reviews.append(review_data) # reviews = [{"username": review.user.username, "date": format_date(review.date), "text": review.text, "restaurant": review.rest_dish.restaurant.name} for review in self.reviews] data['reviews'] = reviews if self.images: images = [Image.get_json(image.filename) for image in self.images] data['images'] = images if self.dish_tags: tags_dict = {} for dish_tag in self.dish_tags: name = dish_tag.tag.name tag_id = str(dish_tag.tag.id) if tags_dict.get(name): tags_dict[tag_id]['count'] += 1 else: tags_dict[tag_id] = {'name': name, 'count': 1} tags = [{ 'name': tag_data['name'], 'count': str(tag_data['count']), 'id': tag_id } for tag_id, tag_data in tags_dict.iteritems()] data['tags'] = tags return data
def get_json(self): data = {} dish = { 'chin_name': self.chin_name, 'eng_name': self.eng_name } if self.desc: dish['desc'] = self.desc if self.pinyin: dish['pinyin'] = self.pinyin data['dish'] = [dish] if self.reviews: reviews = [] for review in self.reviews: review_data = { 'username': review.user.username, 'date': format_date(review.date), 'text': review.text, 'restaurant': review.rest_dish.restaurant.name } reviews.append(review_data) # reviews = [{"username": review.user.username, "date": format_date(review.date), "text": review.text, "restaurant": review.rest_dish.restaurant.name} for review in self.reviews] data['reviews'] = reviews if self.images: images = [Image.get_json(image.filename) for image in self.images] data['images'] = images if self.dish_tags: tags_dict = {} for dish_tag in self.dish_tags: name = dish_tag.tag.name tag_id = str(dish_tag.tag.id) if tags_dict.get(name): tags_dict[tag_id]['count'] += 1 else: tags_dict[tag_id] = {'name': name, 'count': 1} tags = [{'name': tag_data['name'], 'count': str(tag_data['count']), 'id': tag_id} for tag_id, tag_data in tags_dict.iteritems()] data['tags'] = tags return data
import requests import base64 import important from access_token import generate_access_token from date import format_date from password import password_generator from requests.auth import HTTPBasicAuth data = format_date() password = password_generator() def lipa_na_mpesa(): access_token = generate_access_token() api_url = "https://sandbox.safaricom.co.ke/mpesa/stkpush/v1/processrequest" headers = {"Authorization": "Bearer %s" % access_token} request = { "BusinessShortCode": important.business_short_code, "Password": password, "Timestamp": data, "TransactionType": "CustomerPayBillOnline", "Amount": "5", "PartyA": important.phone_no, "PartyB": important.business_short_code, "PhoneNumber": important.phone_no, "CallBackURL": "https://ben.com/callback", "AccountReference": "12345678", "TransactionDesc": "school fees " } response = requests.post(api_url, json=request, headers=headers)
def password_generator(): data_to_encode = important.business_short_code + important.pass_key + format_date() encoded_string= base64.b64encode(data_to_encode.encode()) decoded_password= encoded_string.decode('utf-8') return decoded_password
def jsonify(user): reviews = [] if user.reviews != []: reviews = [{'restaurant': review.rest_dish.restaurant.name, 'dish': review.dish.eng_name, 'date': format_date(review.date), 'text': review.text} for review in user.reviews] data = { 'username': user.username, 'reviews': reviews } return data