diff --git a/billard/models.py b/billard/models.py index 3756304..31c570d 100644 --- a/billard/models.py +++ b/billard/models.py @@ -8,7 +8,7 @@ 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 -from .tasks import request_process_location_data +from .tasks import process_location_data log = logging.getLogger(__name__) @@ -116,58 +116,6 @@ class Accounting(models.Model): @receiver(post_save, sender=LocationData) def test(sender, **kwargs): - request_process_location_data.delay() + process_location_data.delay() -@shared_task -def process_location_data(): - data = LocationData.objects.filter(processed=False) - for ld in data: - cli = Client.objects.filter(uuid=ld.client_id, desks__desk_no=ld.desk_no) - if cli.count() < 1: - ld.processed = True - ld.error_msg = 'No client object found. Stopp processing! {}, {}'.format(ld.client_id, ld.desk_no) - ld.save() - log.error(ld.error_msg) - else: - cli = cli[0] - desk = cli.desks.filter(desk_no=ld.desk_no, enabled=True) - if desk.count() != 1: - ld.processed = True - ld.error_msg = 'No desk object found. Stopp processing! {}, {}'.format(ld.client_id, ld.desk_no) - ld.save() - log.error(ld.error_msg) - desk = desk[0] - ac = desk.accounting_set.order_by('time_from').reverse() - if ld.on_off: - acc = None - if ac.count() > 0 and ac[0].time_to is None: - log.error('Vorheriges Accounting nicht abgeschlossen: Desk_id {}, Accounting_id {}' - .format(desk.id, ac[0].id)) - acc = ac[0] - if acc is None: - acc = Accounting( - desk=desk, - time_from=ld.tst, - ) - acc.save() - ld.delete() - else: - if len(ac) > 0: - acc = ac[0] - acc.time_to = ld.tst - acc.prize = utils.get_prize_for( - start=acc.time_from, - end=ld.tst, - pph=desk.prize, - hh_start=cli.location.happy_hour_start, - hh_end=cli.location.happy_hour_end, - pphh=desk.prize_hh, - ) - acc.save() - ld.delete() - else: - ld.processed = True - ld.error_msg = 'No existing accountings found. Stopp processing! {}, {}'.format(ld.client_id, ld.desk_no) - ld.save() - log.error(ld.error_msg) diff --git a/billard/tasks.py b/billard/tasks.py index 97a22ff..a8ee1b6 100644 --- a/billard/tasks.py +++ b/billard/tasks.py @@ -1,9 +1,62 @@ from __future__ import absolute_import, unicode_literals -from celery import shared_task -from django.conf import settings -import requests +import logging +import billard.utils as utils +from celery import shared_task + +from billard.models import LocationData, Client, Accounting + +log = logging.getLogger(__name__) @shared_task -def request_process_location_data(): - requests.get(url=settings.URL_LOCATION_PROCESSOR) +def process_location_data(): + data = LocationData.objects.filter(processed=False) + for ld in data: + cli = Client.objects.filter(uuid=ld.client_id, desks__desk_no=ld.desk_no) + if cli.count() < 1: + ld.processed = True + ld.error_msg = 'No client object found. Stopp processing! {}, {}'.format(ld.client_id, ld.desk_no) + ld.save() + log.error(ld.error_msg) + else: + cli = cli[0] + desk = cli.desks.filter(desk_no=ld.desk_no, enabled=True) + if desk.count() != 1: + ld.processed = True + ld.error_msg = 'No desk object found. Stopp processing! {}, {}'.format(ld.client_id, ld.desk_no) + ld.save() + log.error(ld.error_msg) + desk = desk[0] + ac = desk.accounting_set.order_by('time_from').reverse() + if ld.on_off: + acc = None + if ac.count() > 0 and ac[0].time_to is None: + log.error('Vorheriges Accounting nicht abgeschlossen: Desk_id {}, Accounting_id {}' + .format(desk.id, ac[0].id)) + acc = ac[0] + if acc is None: + acc = Accounting( + desk=desk, + time_from=ld.tst, + ) + acc.save() + ld.delete() + else: + if len(ac) > 0: + acc = ac[0] + acc.time_to = ld.tst + acc.prize = utils.get_prize_for( + start=acc.time_from, + end=ld.tst, + pph=desk.prize, + hh_start=cli.location.happy_hour_start, + hh_end=cli.location.happy_hour_end, + pphh=desk.prize_hh, + ) + acc.save() + ld.delete() + else: + ld.processed = True + ld.error_msg = 'No existing accountings found. Stopp processing! {}, {}'.format(ld.client_id, ld.desk_no) + ld.save() + log.error(ld.error_msg)