From c8c25714f094d64c8785380e0c57871ef94dd544 Mon Sep 17 00:00:00 2001 From: Robert Einsle Date: Fri, 10 Feb 2017 21:02:07 +0100 Subject: [PATCH] update tasks --- billard/models.py | 43 +++++++++++++++++++++++++++++++++++++++++-- billard/tasks.py | 38 ++------------------------------------ billard/utils.py | 43 +++++++++++++++++++++++++++++++++++++------ 3 files changed, 80 insertions(+), 44 deletions(-) diff --git a/billard/models.py b/billard/models.py index 266651c..41b50d3 100644 --- a/billard/models.py +++ b/billard/models.py @@ -2,10 +2,11 @@ import uuid from django.db import models from django.contrib.auth.models import User from datetime import datetime, timezone -from billard import utils +from billard import utils, tasks from django.contrib.auth.models import User from django.db.models.signals import post_save from django.dispatch import receiver +from celery import shared_task class LocationData(models.Model): @@ -100,4 +101,42 @@ class Accounting(models.Model): @receiver(post_save, sender=LocationData) def test(sender, **kwargs): - print('test') + process_location_data() + + +@shared_task +def process_location_data(): + data = LocationData.objects.filter(processed=False) + for ld in data: + cli = Client.objects.filter(uuid=ld.client_id) + if cli.count() < 1: + ld.processed = True + ld.error_msg = 'No location object found. Stopp processing!' + ld.save() + # TODO Send error eMail to Admin + else: + cli = cli[0] + desk = cli.desks.filter(desk_no=ld.desk_no)[0] + ac = Accounting.objects.filter(client=cli, desk_no=ld.desk_no).order_by('time_from').reverse() + if ld.on_off: + if ac.count() > 0 and ac[0].time_to is None: + ac[0].time_to = datetime.now() + ac[0].save() + # TODO Send error eMail to Admin + acc = Accounting( + client=cli, + desk_no=ld.desk_no, + time_from=ld.tst, + ) + acc.save() + ld.delete() + else: + acc = ac[0] + acc.time_to = ld.tst + acc.prize = utils.get_prize_for( + start=acc.time_from, + end=ld.tst, + pph=desk.prize + ) + acc.save() + ld.delete() diff --git a/billard/tasks.py b/billard/tasks.py index 6bcd39f..46d4a18 100644 --- a/billard/tasks.py +++ b/billard/tasks.py @@ -1,42 +1,8 @@ from __future__ import absolute_import, unicode_literals from celery import shared_task -from . import models -from datetime import datetime -from . import utils +from billard import utils @shared_task def process_location_data(): - data = models.LocationData.objects.filter(processed=False) - for ld in data: - cli = models.Client.objects.filter(uuid=ld.location_id) - if cli.count() < 1: - ld.processed = True - ld.error_msg = 'No location object found. Stopp processing!' - ld.save() - # TODO Send error eMail to Admin - else: - cli = cli[0] - ac = models.Accounting.objects.filter(client=cli, desk_no=ld.table_no).order_by('time_from').reverse() - if ld.on_off: - if ac.count() > 0 and ac[0].time_to is None: - ac[0].time_to = datetime.now() - ac[0].save() - # TODO Send error eMail to Admin - acc = models.Accounting( - client=cli, - desk_no=ld.table_no, - time_from=ld.tst, - ) - acc.save() - ld.delete() - else: - acc = ac[0] - acc.time_to = ld.tst - acc.prize = utils.get_prize_for( - start=acc.time_from, - end=ld.tst, - pph=cli.desk1_prize_ht - ) - acc.save() - ld.delete() + utils.process_location_data() diff --git a/billard/utils.py b/billard/utils.py index 4251112..f6f6d8e 100644 --- a/billard/utils.py +++ b/billard/utils.py @@ -1,4 +1,5 @@ -from datetime import datetime, date, time +from datetime import datetime +#import models def get_prize_for(start, end, pph=0): @@ -16,8 +17,38 @@ def get_prize_for(start, end, pph=0): return prize -if __name__ == '__main__': - d = date(2017, 2, 5) - t = time(16, 00) - start = datetime.combine(d, t) - get_prize_for(start=start, pph=8.5) +def process_location_data(): + pass + #data = models.LocationData.objects.filter(processed=False) + #for ld in data: + # cli = models.Client.objects.filter(uuid=ld.client_id) + # if cli.count() < 1: + # ld.processed = True + # ld.error_msg = 'No location object found. Stopp processing!' + # ld.save() + # # TODO Send error eMail to Admin + # else: + # cli = cli[0] + # ac = models.Accounting.objects.filter(client=cli, desk_no=ld.table_no).order_by('time_from').reverse() + # if ld.on_off: + # if ac.count() > 0 and ac[0].time_to is None: + # ac[0].time_to = datetime.now() + # ac[0].save() + # # TODO Send error eMail to Admin + # acc = models.Accounting( + # client=cli, + # desk_no=ld.table_no, + # time_from=ld.tst, + # ) + # acc.save() + # ld.delete() + # else: + # acc = ac[0] + # acc.time_to = ld.tst + # acc.prize = get_prize_for( + # start=acc.time_from, + # end=ld.tst, + # pph=cli.desk1_prize_ht + # ) + # acc.save() + # ld.delete()