fix processingo of ld objects

This commit is contained in:
Robert Einsle 2017-02-26 12:03:18 +01:00
parent eab72df9ac
commit e22a153201

View File

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