From 7d9ad7601b33b71cf46281c269c2c1641d1b1b02 Mon Sep 17 00:00:00 2001 From: michigg Date: Sun, 8 Oct 2017 17:53:40 +0200 Subject: [PATCH] update food model and required commands --- .../food/management/commands/delete_food.py | 13 ++++++++ ofu_app/apps/food/utils/migrate_data.py | 33 +++++++++++++++---- 2 files changed, 39 insertions(+), 7 deletions(-) create mode 100644 ofu_app/apps/food/management/commands/delete_food.py diff --git a/ofu_app/apps/food/management/commands/delete_food.py b/ofu_app/apps/food/management/commands/delete_food.py new file mode 100644 index 0000000..491119a --- /dev/null +++ b/ofu_app/apps/food/management/commands/delete_food.py @@ -0,0 +1,13 @@ +from django.core.management.base import BaseCommand, CommandError +from apps.food.models import Menu, HappyHour, SingleFood +from apps.food.utils import migrate_data + + +class Command(BaseCommand): + help = "Imports Food from special Websites" + + def add_arguments(self, parser): + pass + + def handle(self, *args, **options): + migrate_data.delete() diff --git a/ofu_app/apps/food/utils/migrate_data.py b/ofu_app/apps/food/utils/migrate_data.py index f95b792..b059670 100644 --- a/ofu_app/apps/food/utils/migrate_data.py +++ b/ofu_app/apps/food/utils/migrate_data.py @@ -39,22 +39,42 @@ def writeStudentenwerkDataInDB(data): def writeFekideDataInDB(data): + print(data) for happyhour_data in data['happyhours']: time = str(happyhour_data['time']).replace(" ", "").split("-") happyhour, new = HappyHour.objects.get_or_create(date=datetime.strptime(data['day'], "%A, %d.%m.%Y"), location=happyhour_data['location'], description=happyhour_data['description'], - starttime=datetime.strptime(time[0], "%H:%M"), - endtime=datetime.strptime(time[1], "%H:%M")) + starttime=datetime.strptime(time[0], "%H:%M").time(), + endtime=datetime.strptime(time[1], "%H:%M").time()) + print("HH: %s, NEW: %s" % (str(happyhour), str(new))) if not new: + print("Update db object " + happyhour.location) happyhour.date = datetime.strptime(data['day'], "%A, %d.%m.%Y") happyhour.location = happyhour_data['location'] happyhour.description = happyhour_data['description'] - happyhour.starttime = datetime.strptime(time[0], "%H:%M") - happyhour.endtime = datetime.strptime(time[1], "%H:%M") + happyhour.starttime = datetime.strptime(time[0], "%H:%M").time() + happyhour.endtime = datetime.strptime(time[1], "%H:%M").time() + happyhour.save() + + +def writeoutDBObjects(): + pprint("SingleFood: " + str(SingleFood.objects.count())) + pprint("Menu: " + str(Menu.objects.count())) + pprint("HappyHour: " + str(HappyHour.objects.count())) + + +def delete(): + happy_hours = HappyHour.objects.all() + print("Deleted following Happy Hours:") + for happy_hour in happy_hours: + print("Happy Hour: Location: %s, Description: %s" % (str(happy_hour.location), str(happy_hour.description))) + happy_hour.delete() def main(): + print("Aktueller Stand:") + writeoutDBObjects() # get food jsons writeStudentenwerkDataInDB(mensa_page_parser.parsePage(LINK_AUSTR_MENSA)) writeStudentenwerkDataInDB(mensa_page_parser.parsePage(LINK_FEKI_MENSA)) @@ -62,9 +82,8 @@ def main(): writeStudentenwerkDataInDB(cafete_page_parser.parsePage(LINK_MARKUS_CAFETE)) writeFekideDataInDB(fekide_happyhour_page_parser.parsePage(LINK_FEKIDE_GUIDE)) - pprint("SingleFood: " + str(SingleFood.objects.count())) - pprint("Menu: " + str(Menu.objects.count())) - pprint("HappyHour: " + str(HappyHour.objects.count())) + print("Neuer Stand:") + writeoutDBObjects() if __name__ == '__main__':