diff --git a/billard/models.py b/billard/models.py index e99a635..e6c4d60 100644 --- a/billard/models.py +++ b/billard/models.py @@ -1,4 +1,5 @@ import uuid +import logging from django.db import models from django.contrib.auth.models import User from datetime import datetime, timezone @@ -6,10 +7,11 @@ from billard import utils from django.contrib.auth.models import User from django.db.models.signals import post_save from django.dispatch import receiver -from django.core.mail import mail_admins from celery import shared_task from .tasks import request_process_location_data +log = logging.getLogger(__name__) + class LocationData(models.Model): client_id = models.UUIDField(blank=False, null=False, verbose_name="Client-ID") @@ -126,7 +128,7 @@ def process_location_data(): ld.processed = True ld.error_msg = 'No client object found. Stopp processing! {}, {}'.format(ld.client_id, ld.desk_no) ld.save() - mail_admins(subject=ld.error_msg, message=ld.error_msg) + log.error(ld.error_msg) else: cli = cli[0] desk = cli.desks.filter(desk_no=ld.desk_no, enabled=True) @@ -134,15 +136,14 @@ def process_location_data(): ld.processed = True ld.error_msg = 'No desk object found. Stopp processing! {}, {}'.format(ld.client_id, ld.desk_no) ld.save() - mail_admins(subject=ld.error_msg, message=ld.error_msg) + 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: - mail_admins(subject='Accounting passt nicht', - message='Vorheriges Accounting nicht abgeschlossen: Desk_id {}, Accounting_id {}' - .format(desk.id, ac[0].id)) + 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( @@ -152,15 +153,21 @@ def process_location_data(): 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, - hh_start=cli.location.happy_hour_start, - hh_end=cli.location.happy_hour_end, - pphh=desk.prize_hh, - ) - acc.save() - ld.delete() + if len(acc) > 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)