Merge branch 'feature/rebuild_model' into develop

This commit is contained in:
Robert Einsle 2017-02-10 21:20:38 +01:00
commit 4da0e85108
13 changed files with 300 additions and 116 deletions

View File

@ -10,14 +10,19 @@ class LocationAdmin(admin.ModelAdmin):
@admin.register(Client) @admin.register(Client)
class ClientAdmin(admin.ModelAdmin): class ClientAdmin(admin.ModelAdmin):
list_display = ('uuid', 'desk1_name', 'desk2_name') list_display = ('uuid', 'location')
fields = ['location', 'uuid', 'desk1_enable', 'desk1_name', 'desk1_prize_nt', 'desk1_prize_ht', 'desk2_enable', 'desk2_name', 'desk2_prize_nt', 'desk2_prize_ht', ] fields = ['location', 'uuid']
@admin.register(LocationData) @admin.register(LocationData)
class LocationDataAdmin(admin.ModelAdmin): class LocationDataAdmin(admin.ModelAdmin):
list_display = ('location_id', 'table_no', 'tst', 'on_off', 'processed', 'error_msg') list_display = ('client_id', 'desk_no', 'tst', 'on_off', 'processed', 'error_msg')
fields = ['location_id', 'table_no', 'tst', 'on_off', 'processed', 'error_msg'] fields = ['client_id', 'desk_no', 'tst', 'on_off', 'processed', 'error_msg']
@admin.register(Desk)
class DeskAdmin(admin.ModelAdmin):
list_display = ('client', 'desk_no', 'name', 'enabled', 'prize', 'prize_hh')
@admin.register(Accounting) @admin.register(Accounting)

View File

@ -0,0 +1,32 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.10.5 on 2017-02-10 18:30
from __future__ import unicode_literals
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('billard', '0006_auto_20170206_2031'),
]
operations = [
migrations.CreateModel(
name='Desk',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('desk_no', models.IntegerField(verbose_name='Tischnummer')),
('name', models.CharField(blank=True, max_length=32, null=True, verbose_name='Tischbezeichnung')),
('enabled', models.BooleanField(verbose_name='Tisch aktiv')),
('prize', models.DecimalField(blank=True, decimal_places=2, max_digits=5, null=True, verbose_name='Normelpreis')),
('prize_hh', models.DecimalField(blank=True, decimal_places=2, max_digits=5, null=True, verbose_name='Preis Happy Hour')),
('client', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='billard.Client', verbose_name='Client')),
],
options={
'verbose_name_plural': 'Tische',
'verbose_name': 'Tisch',
},
),
]

View File

@ -0,0 +1,53 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.10.5 on 2017-02-10 19:47
from __future__ import unicode_literals
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('billard', '0007_desk'),
]
operations = [
migrations.RemoveField(
model_name='client',
name='desk1_enable',
),
migrations.RemoveField(
model_name='client',
name='desk1_name',
),
migrations.RemoveField(
model_name='client',
name='desk1_prize_ht',
),
migrations.RemoveField(
model_name='client',
name='desk1_prize_nt',
),
migrations.RemoveField(
model_name='client',
name='desk2_enable',
),
migrations.RemoveField(
model_name='client',
name='desk2_name',
),
migrations.RemoveField(
model_name='client',
name='desk2_prize_ht',
),
migrations.RemoveField(
model_name='client',
name='desk2_prize_nt',
),
migrations.AlterField(
model_name='desk',
name='client',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='desks', to='billard.Client', verbose_name='Client'),
),
]

View File

@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.10.5 on 2017-02-10 19:55
from __future__ import unicode_literals
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('billard', '0008_auto_20170210_1947'),
]
operations = [
migrations.RenameField(
model_name='locationdata',
old_name='table_no',
new_name='desk_no',
),
]

View File

@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.10.5 on 2017-02-10 19:40
from __future__ import unicode_literals
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('billard', '0009_auto_20170210_1955'),
]
operations = [
migrations.RenameField(
model_name='locationdata',
old_name='location_id',
new_name='client_id',
),
]

View File

@ -2,27 +2,29 @@ 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 . import utils from billard import utils, tasks
from . import 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 celery import shared_task
class LocationData(models.Model): class LocationData(models.Model):
location_id = models.UUIDField(blank=False, null=False, verbose_name="Standort-ID") client_id = models.UUIDField(blank=False, null=False, verbose_name="Client-ID")
table_no = models.IntegerField(blank=False, null=False, verbose_name="Tischnummer") desk_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")
processed = models.BooleanField(default=False, verbose_name="Verarbeitet") processed = models.BooleanField(default=False, verbose_name="Verarbeitet")
error_msg = models.TextField(blank=True, null=True, verbose_name="Fehlermeldung") error_msg = models.TextField(blank=True, null=True, verbose_name="Fehlermeldung")
def __str__(self): def __str__(self):
return str(self.location_id) return str(self.client_id)
class Meta: class Meta:
verbose_name = "Standortlog" verbose_name = "Standortlog"
verbose_name_plural = "Standortlogs" verbose_name_plural = "Standortlogs"
class Location(models.Model): class Location(models.Model):
users = models.ManyToManyField(User, related_name='locations', verbose_name="Benutzer") users = models.ManyToManyField(User, related_name='locations', verbose_name="Benutzer")
code = models.CharField(max_length=16, unique=True, verbose_name="Code") code = models.CharField(max_length=16, unique=True, verbose_name="Code")
@ -45,30 +47,6 @@ class Location(models.Model):
class Client(models.Model): class Client(models.Model):
uuid = models.UUIDField(unique=True, default=uuid.uuid4, verbose_name="Identifier") uuid = models.UUIDField(unique=True, default=uuid.uuid4, verbose_name="Identifier")
location = models.ForeignKey(Location, verbose_name="Standort") location = models.ForeignKey(Location, verbose_name="Standort")
desk1_enable = models.BooleanField()
desk1_name = models.CharField(max_length=32, blank=True, null=True)
desk1_prize_nt = models.DecimalField(max_digits=4, decimal_places=2, blank=True, null=True)
desk1_prize_ht = models.DecimalField(max_digits=4, decimal_places=2, blank=True, null=True)
desk2_enable = models.BooleanField()
desk2_name = models.CharField(max_length=32, blank=True, null=True)
desk2_prize_nt = models.DecimalField(max_digits=4, decimal_places=2, blank=True, null=True)
desk2_prize_ht = models.DecimalField(max_digits=4, decimal_places=2, blank=True, null=True)
def accounting_for(self, desk_no, pht, pnt):
t = Accounting.objects.filter(client_id=self.id, desk_no=desk_no)[:3][::-1]
if t.__len__() > 0:
a = t[t.__len__() - 1]
if a.time_to is None:
prize = utils.get_prize_for(start=a.time_from, end=datetime.now(timezone.utc), pph=pht)
if prize != a.prize:
a.prize = prize
return t
def accounting_1(self):
return self.accounting_for(1, self.desk1_prize_ht, self.desk1_prize_nt)
def accounting_2(self):
return self.accounting_for(2, self.desk2_prize_ht, self.desk2_prize_nt)
def __str__(self): def __str__(self):
return '{}, {}'.format(self.location.name, self.uuid) return '{}, {}'.format(self.location.name, self.uuid)
@ -77,6 +55,34 @@ class Client(models.Model):
verbose_name = "Client" verbose_name = "Client"
verbose_name_plural = "Clienten" verbose_name_plural = "Clienten"
class Desk(models.Model):
client = models.ForeignKey(Client, verbose_name='Client', related_name='desks')
desk_no = models.IntegerField(verbose_name='Tischnummer')
name = models.CharField(max_length=32, blank=True, null=True, verbose_name='Tischbezeichnung')
enabled = models.BooleanField(verbose_name='Tisch aktiv')
prize = models.DecimalField(max_digits=5, decimal_places=2, blank=True, null=True, verbose_name="Normelpreis")
prize_hh = models.DecimalField(max_digits=5, decimal_places=2, blank=True, null=True,
verbose_name="Preis Happy Hour")
def accounting_for(self):
t = Accounting.objects.filter(client=self.client, desk_no=self.desk_no)[:3][::-1]
if t.__len__() > 0:
a = t[t.__len__() - 1]
if a.time_to is None:
prize = utils.get_prize_for(start=a.time_from, end=datetime.now(timezone.utc), pph=self.prize)
if prize != a.prize:
a.prize = prize
return t
def __str__(self):
return '{}, {}'.format(self.client.uuid, self.name)
class Meta:
verbose_name = "Tisch"
verbose_name_plural = "Tische"
class Accounting(models.Model): class Accounting(models.Model):
client = models.ForeignKey(Client, verbose_name="Client") client = models.ForeignKey(Client, verbose_name="Client")
desk_no = models.IntegerField(verbose_name="Tischnummer") desk_no = models.IntegerField(verbose_name="Tischnummer")
@ -93,7 +99,44 @@ class Accounting(models.Model):
verbose_name_plural = "Buchhaltungseinträge" verbose_name_plural = "Buchhaltungseinträge"
def update_location_data(sender, instance, **kwargs): @receiver(post_save, sender=LocationData)
tasks.process_location_data.delay() def test(sender, **kwargs):
process_location_data()
post_save.connect(update_location_data, sender=LocationData)
@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 @@
{% extends 'billard/base.html' %} {% extends 'billard/base.html' %}
{% load display_client %}
{% block header %} {% block header %}
<meta http-equiv="refresh" content="3" /> <meta http-equiv="refresh" content="3" />
@ -19,42 +20,7 @@
</form> </form>
{% if clients %} {% if clients %}
{% for cli in clients %} {% for cli in clients %}
{% if cli.desk1_enable %} {% for i in range %} {{ cli|display_client:i }} {% endfor %}
<div class="col-md-6">
<div class="col-md-12 table-info alert {% with cli.accounting_1|last as last %}{% if last.time_to is None %}alert-info{% else %}alert-success{% endif %}{% endwith %}">
<h4 style="text-align: center">(1) {{ cli.desk1_name }}</h4>
{% if cli.accounting_1 %}
<table class="table">
{% for acc in cli.accounting_1 %}
<tr>
<td>{{ acc.time_from|date:"d.m.Y H:i:s" }}</td>
<td>{% if acc.time_to is not None %}{{ acc.time_to|date:"d.m.Y H:i:s" }}{% endif %}</td>
<td style="text-align: center;">{% if acc.prize is not None %}{{ acc.prize|floatformat:2 }}{% endif %}</td>
</tr>
{% endfor %}
</table>
{% endif %}
</div>
</div>
{% endif %}
{% if cli.desk2_enable %}
<div class="col-md-6">
<div class="col-md-12 table-info alert {% with cli.accounting_2|last as last %}{% if last.time_to is None %}alert-info{% else %}alert-success{% endif %}{% endwith %}">
<h4 style="text-align: center">(2) {{ cli.desk2_name }}</h4>
{% if cli.accounting_2 %}
<table class="table">
{% for acc in cli.accounting_2 %}
<tr>
<td>{{ acc.time_from|date:"d.m.Y H:i:s" }}</td>
<td>{% if acc.time_to is not None %}{{ acc.time_to|date:"d.m.Y H:i:s" }}{% endif %}</td>
<td style="text-align: right;">{% if acc.prize is not None %}{{ acc.prize|floatformat:2 }}{% endif %}</td>
</tr>
{% endfor %}
</table>
{% endif %}
</div>
</div>
{% endif %}
{% endfor %} {% endfor %}
{% else %} {% else %}
<div class="col-md-12"> <div class="col-md-12">

View File

View File

@ -0,0 +1,48 @@
from django import template
from django.utils.html import format_html
from billard.models import Desk, Accounting
from billard import utils
from datetime import datetime, timezone
register = template.Library()
@register.filter(is_safe=True)
def display_client(client, desk_no):
desks = client.desks.filter(desk_no=desk_no)
if len(desks) == 0:
return ''
desk = desks[0]
if not desk.enabled:
return ''
alert = 'alert-success'
acc = Accounting.objects.filter(client=client, desk_no=desk_no)[:3][::-1]
if acc is not None and len(acc) > 0:
a = acc[-1]
if a.time_to is None:
alert = 'alert-info'
prize = utils.get_prize_for(start=a.time_from, end=datetime.now(timezone.utc), pph=desk.prize)
prize = '{0:.2f}'.format(prize)
if prize != a.prize:
a.prize = prize
html = '<div class="col-md-6">\n'
html += ' <div class="col-md-12 table-info alert {}">\n'.format(alert)
html += ' <h4 style="text-align: center">({}) {}</h4>\n'.format(desk_no, desk.name)
if len(acc) > 0:
html += ' <table class="table">\n'
for a in acc:
html += ' <tr>\n'
html += ' <td>{}</td>\n'.format(a.time_from)
html += ' <td>{}</td>\n'.format((a.time_to if a.time_to is not None else ''))
html += ' <td style="text-align: center;">{}</td>\n'\
.format((a.prize if a.prize is not None else ''))
html += ' <tr>\n'
html += ' <tr>\n'
html += ' </tr>\n'
html += ' </table>\n'
html += ' </div>\n'
html += '</div>\n'
html = format_html(html)
return html

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()

View File

@ -55,7 +55,7 @@ def index(request):
locations = Location.objects.filter(users__id=request.user.id).order_by('code') locations = Location.objects.filter(users__id=request.user.id).order_by('code')
clients = Client.objects.filter(location_id=loc).order_by('id') clients = Client.objects.filter(location_id=loc).order_by('id')
context = { context = {
'range': range(1, 3), 'range': range(1, 9),
'locations': locations, 'locations': locations,
'clients': clients, 'clients': clients,
'location_id': int(loc), 'location_id': int(loc),

View File

@ -17,7 +17,7 @@ def main():
args = parser.parse_args() args = parser.parse_args()
payload = { payload = {
'location_id': 'a7b47b4b-5b63-3b2f-8af0-4b185843f419', 'location_id': 'a7b47b4b-5b63-3b2f-8af0-4b185843f419',
'table_no': args.desk, 'desk_no': args.desk,
'tst': args.date.strftime('%Y-%m-%dT%H:%M:%S%Z') 'tst': args.date.strftime('%Y-%m-%dT%H:%M:%S%Z')
} }
if args.on: if args.on: