def add_donator_to_db(donator): print("ADD DONATOR") id = donator.m_id user_name = donator.user_name print("USER NAME", user_name) location = donator.m_location donation_count = donator.m_donation_counter donation_level = donator.m_donator_level location_to_db = { 'id': '0', 'longitude': location.longitude, 'latitude': location.latitude } main_db('add_location', location_to_db) donator_to_db = { 'id': id, 'user_name': user_name, 'location_id': get_max_id('location'), 'donation_count': donation_count, 'donation_level': donation_level } main_db('add_donator', donator_to_db) food_to_db = { 'id': '0', 'donator_id': id, 'location_id': donator_to_db['location_id'], 'available': 1, 'number_of_servings': donator.m_food_being_built.m_number_of_servings, 'expiration_date': datetime.timestamp(donator.m_food_being_built.m_expiration_date), 'description': donator.m_food_being_built.m_description, 'food_types': donator.m_food_being_built.m_food_types } print("FOOD TO DB", food_to_db) main_db('add_food', food_to_db) if len(donator.photos) != 0: food_id = get_max_id('food') for photo_id in donator.photos: dir_path = f"Food{food_id}-" photo_path = save_photo_by_path(photo_id, dir_path) main_db('add_photo', {'id': '0', 'path': photo_path}) photo_db_id = get_max_id('photo') main_db('add_food_photos', { 'id': '0', 'food_id': food_id, 'photo_id': photo_db_id }) print("ID", food_id) send_get_message( id, meal_box("❤❤ Thank you for being part of reducing food waste ❤❤"))
def show_food_list(chat_id, receiver): food_list = main_db('get_food_by_types', receiver.food_types) print("FOOD LIST", food_list, receiver) relative_distance = get_relative_distance_for_receiver(receiver, food_list) food_list = {food['id']: food for food in food_list} for id in relative_distance: item = food_list[id] photos = main_db('get_photos_by_food_id', item.get('food_id')) donator = main_db('get_donator_by_id', item['donator_id']) number_of_servings = item['number_of_servings'] food_types = ", ".join(item['food_types']) user_name = '@' + donator['user_name'] location = round(relative_distance[id], 5) des = item['description'] if len(photos) > 0: send_get_message( chat_id, box(id, number_of_servings, food_types, str(location), user_name, des, '\nPhotos of the meal:\n')) bot = telegram.Bot(token=TOKEN) for photo in photos: bot.send_photo(chat_id, open(photo, 'rb')) else: send_get_message( chat_id, box(id, number_of_servings, food_types, str(location), user_name, des))
def get_receiver_by_id(id): rec = main_db('get_receiver_by_id', id) if not rec: return None else: new_rec = Reciver() new_rec.init_reciver_id(id) location = main_db('get_location_by_id', rec['location_id']) new_location = Location() new_location.set_address(location['longitude'], location['latitude']) print("MY LOCATION IN GET", new_location) new_rec.set_location(new_location) new_rec.food_types = main_db('get_receiver_food_types_by_id', id) return new_rec
def handle_exciting_receiver_in_db_responce(message, request, id_obj_map): answer = request['callback_query']['data'] id = message.get_id() if answer == 'Show food': send_get_message( id, f"Here is your relevant food with their contacts 🤗\nFeel free to contact the most appropriate one 😎" ) print(id, id_obj_map) show_food_list(id, id_obj_map[id]) elif answer == 'Restart Process': main_db('delete_receiver_by_id', id) del id_obj_map[id] rec = Reciver() rec.init_reciver_id(id) id_obj_map[id] = rec handle_location(message, request, id_obj_map)
def pokemon_filter(): args = dict(request.args) response = {} if 'type' in args: response = main_db('get_pokemons_by_type', args['type']) if 'error' not in response: response = {'pokemons': response} elif 'trainer' in args: response = main_db('get_pokemons_by_trainer', args['trainer']) if 'error' not in response: response = {'pokemons': response} else: response = {'error': 400, 'details': 'Invalid pokemon filter'} if response and 'error' in response: return Response(json.dumps(response['details'])), response['error'] return Response(json.dumps(response)), 200
def donate_pokemon(): args = dict(request.args) response = {} if 'donator' in args and 'receiver' in args and 'pokemon' in args: response = main_db('donate_pokemon', args['donator'], args['receiver'], args['pokemon']) if response and 'error' in response: return Response(json.dumps(response['details'])), response['error'] return Response(json.dumps({'Response': 'Donated successfully'})), 200
def pokemon_delete(): args = dict(request.args) response = {} if 'pokemon' in args and 'trainer' in args: response = main_db('delete_pokemon_of_trainer', args['trainer'], args['pokemon']) if response and 'error' in response: return Response(json.dumps(response['details'])), response['error'] return Response(json.dumps({'Response': 'Deleted successfully'})), 200
def add_pokemon(): req = request.get_json() attributes = ['id', 'name', 'ownedBy', 'types', 'height', 'weight'] print(req.keys()) if list(req.keys()) == attributes: response = main_db('add_pokemon', req) else: response = {'error': 400, 'details': 'Invalid pokemon object'} if response and 'error' in response: return Response(json.dumps(response['details'])), response['error'] return Response(json.dumps({'Response': 'Added successfully'})), 200
def trainer_filter(): args = dict(request.args) response = {} if 'pokemon' in args: response = main_db('get_trainers_of_pokemon', args['pokemon']) if 'error' not in response: response = {'trainers': response} else: response = {'error': 400, 'detail': 'Invalid trainer filter'} if response and 'error' in response: return Response(json.dumps(response['details'])), response['error'] return Response(json.dumps(response)), 200
def add_recevier_to_db(receiver): id = receiver.telegram_id location = receiver.location food_types = receiver.food_types print("FOOD TYPES", food_types) location_to_db = { 'id': '0', 'longitude': location.longitude, 'latitude': location.latitude } main_db('add_location', location_to_db) main_db('add_location', location_to_db) receiver_to_db = { 'id': id, 'location_id': get_max_id('location'), 'food_types': food_types } main_db('add_receiver', receiver_to_db)
import json from database import main_db with open("data.json", 'r+') as data: data = json.load(data) for poke in data: poke['types'] = [poke['type']] main_db('add_pokemon', poke)
def pokemon_update_types(name): response = main_db('update_types', name) if response and 'error' in response: return Response(json.dumps(response['details'])), response['error'] return Response(json.dumps({'Response': 'Updated successfully'})), 200
def pokemon_evolve(name): response = main_db('evolve_pokemon', name) if response and 'error' in response: return Response(json.dumps(response['details'])), response['error'] return Response(json.dumps({'Response': 'Evolved successfully'})), 200