update tasks

This commit is contained in:
Robert Einsle 2017-02-10 21:02:07 +01:00
parent 263bff6540
commit c8c25714f0
3 changed files with 80 additions and 44 deletions

View File

@ -2,10 +2,11 @@ import uuid
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
from billard import utils from billard import utils, tasks
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 celery import shared_task
class LocationData(models.Model): class LocationData(models.Model):
@ -100,4 +101,42 @@ class Accounting(models.Model):
@receiver(post_save, sender=LocationData) @receiver(post_save, sender=LocationData)
def test(sender, **kwargs): 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()

View File

@ -1,42 +1,8 @@
from __future__ import absolute_import, unicode_literals from __future__ import absolute_import, unicode_literals
from celery import shared_task from celery import shared_task
from . import models from billard import utils
from datetime import datetime
from . import utils
@shared_task @shared_task
def process_location_data(): def process_location_data():
data = models.LocationData.objects.filter(processed=False) utils.process_location_data()
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()

View File

@ -1,4 +1,5 @@
from datetime import datetime, date, time from datetime import datetime
#import models
def get_prize_for(start, end, pph=0): def get_prize_for(start, end, pph=0):
@ -16,8 +17,38 @@ def get_prize_for(start, end, pph=0):
return prize return prize
if __name__ == '__main__': def process_location_data():
d = date(2017, 2, 5) pass
t = time(16, 00) #data = models.LocationData.objects.filter(processed=False)
start = datetime.combine(d, t) #for ld in data:
get_prize_for(start=start, pph=8.5) # 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()