Merge branch 'develop' of git.einsle.de:carom/carom-server into develop

Conflicts:
	billard/models.py
This commit is contained in:
Alexander Werner 2017-02-06 20:28:26 +01:00
commit 46c9cdb2d5
6 changed files with 107 additions and 4 deletions

View File

@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.10.5 on 2017-02-06 18:26
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('billard', '0004_accounting'),
]
operations = [
migrations.AlterField(
model_name='locationdata',
name='location_id',
field=models.UUIDField(),
),
]

View File

@ -1,12 +1,15 @@
import uuid 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 from datetime import datetime, timezone
from . import utils from . import utils
from . import tasks
from django.contrib.auth.models import User
from django.db.models.signals import post_save
class LocationData(models.Model): class LocationData(models.Model):
location_id = models.CharField(max_length=32, blank=False, null=False, verbose_name="Standort-ID") location_id = models.UUIDField(blank=False, null=False, verbose_name="Standort-ID")
table_no = models.IntegerField(blank=False, null=False, verbose_name="Tischnummer") table_no = models.IntegerField(blank=False, null=False, verbose_name="Tischnummer")
tst = models.DateTimeField(blank=False, null=False, verbose_name="Zeitstempel") tst = models.DateTimeField(blank=False, null=False, verbose_name="Zeitstempel")
on_off = models.BooleanField(blank=False, null=False, verbose_name="Ein/Ausgebucht") on_off = models.BooleanField(blank=False, null=False, verbose_name="Ein/Ausgebucht")
@ -55,7 +58,7 @@ class Client(models.Model):
t = Accounting.objects.filter(client_id=self.id, desk_no=desk_no)[:3][::-1] t = Accounting.objects.filter(client_id=self.id, desk_no=desk_no)[:3][::-1]
a = t[t.__len__() - 1] a = t[t.__len__() - 1]
if a.time_to is None: if a.time_to is None:
prize = utils.get_prize_for(start=a.time_from, end=datetime.now(), pph=pht) prize = utils.get_prize_for(start=a.time_from, end=datetime.now(timezone.utc), pph=pht)
if prize != a.prize: if prize != a.prize:
a.prize = prize a.prize = prize
return t return t
@ -87,3 +90,9 @@ class Accounting(models.Model):
ordering = ['-time_from'] ordering = ['-time_from']
verbose_name = "Buchhaltungseintrag" verbose_name = "Buchhaltungseintrag"
verbose_name_plural = "Buchhaltungseinträge" verbose_name_plural = "Buchhaltungseinträge"
def update_location_data(sender, instance, **kwargs):
tasks.process_location_data.delay()
post_save.connect(update_location_data, sender=LocationData)

31
billard/tasks.py Normal file
View File

@ -0,0 +1,31 @@
from __future__ import absolute_import, unicode_literals
from celery import shared_task
from . import models
from datetime import datetime
from . import utils
@shared_task
def process_location_data():
data = models.LocationData.objects.filter(processed=False)
for ld in data:
cli = models.Client.objects.get(uuid=ld.location_id)
ac = models.Accounting.objects.filter(client=cli).order_by('time_from').reverse()[0]
if ld.on_off:
if ac.time_to is None:
ac.time_to = datetime.now()
ac.save()
ac = models.Accounting(
client=cli,
desk_no=ld.table_no,
time_from=ld.tst,
)
ac.save()
ld.processed=True
ld.save()
else:
ac.time_to = ld.tst
ac.prize = utils.get_prize_for(start=ac.time_from, end=ac.time_to, pph=cli.desk1_prize_ht)
ac.save()
ld.processed=True
ld.save()

View File

@ -0,0 +1,7 @@
from __future__ import absolute_import, unicode_literals
# This will make sure the app is always imported when
# Django starts so that shared_task will use this app.
from .celery import app as celery_app
__all__ = ['celery_app']

27
caromserver/celery.py Normal file
View File

@ -0,0 +1,27 @@
from __future__ import absolute_import, unicode_literals
import os
import django
from celery import Celery
# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'caromserver.settings')
app = Celery('caromserver')
# Using a string here means the worker don't have to serialize
# the configuration object to child processes.
# - namespace='CELERY' means all celery-related configuration keys
# should have a `CELERY_` prefix.
app.config_from_object('django.conf:settings')
# Load task modules from all registered Django app configs.
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "caromserver.settings")
django.setup()
app.autodiscover_tasks(['billard'])
@app.task(bind=True)
def debug_task(self):
print('Request: {0!r}'.format(self.request))

View File

@ -138,6 +138,15 @@ LOGOUT_URL = 'logout'
LOGIN_REDIRECT_URL = 'carom_index' LOGIN_REDIRECT_URL = 'carom_index'
LOGOUT_REDIRECT_URL = 'carom_index' LOGOUT_REDIRECT_URL = 'carom_index'
# CELERY STUFF
BROKER_URL = 'redis://localhost:6379'
CELERY_RESULT_BACKEND = 'redis://localhost:6379'
CELERY_ACCEPT_CONTENT = ['application/json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
CELERY_TIMEZONE = 'Europe/Berlin'
STATIC_ROOT = "/srv/carom/carom-server/static/" STATIC_ROOT = "/srv/carom/carom-server/static/"
try: try: