From dc09be5a43c63b64fa0a039589c7dfdac96274ba Mon Sep 17 00:00:00 2001 From: Robert Einsle Date: Thu, 27 Apr 2017 08:04:44 +0200 Subject: [PATCH 1/9] move code to use generic views --- billard/templates/billard/base.html | 4 +-- billard/templates/billard/location_list.html | 30 ++++++++++++++++++ billard/urls.py | 25 ++++++++++----- billard/views.py | 33 ++++++++++++++++++++ 4 files changed, 83 insertions(+), 9 deletions(-) create mode 100644 billard/templates/billard/location_list.html diff --git a/billard/templates/billard/base.html b/billard/templates/billard/base.html index 137ddb3..b88fc67 100644 --- a/billard/templates/billard/base.html +++ b/billard/templates/billard/base.html @@ -28,9 +28,9 @@ {% endif %} + + + +
{% include 'billard/index_ajax.html' %}
{% endblock %} + {% block js %} +{% endblock %} \ No newline at end of file diff --git a/billard/templates/billard/location_detail_ajax.html b/billard/templates/billard/location_detail_ajax.html new file mode 100644 index 0000000..cef9821 --- /dev/null +++ b/billard/templates/billard/location_detail_ajax.html @@ -0,0 +1,12 @@ +{% load display_client %} +{% if location.clients.all %} +{% for cli in location.clients.all %} +{% for i in "12345678" %} + {{ cli|display_client:i }} +{% endfor %} +{% endfor %} +{% else %} +
+
Keine Tische angelegt!
+
+{% endif %} diff --git a/billard/templates/billard/location_list.html b/billard/templates/billard/location_index.html similarity index 69% rename from billard/templates/billard/location_list.html rename to billard/templates/billard/location_index.html index a568105..2816b8b 100644 --- a/billard/templates/billard/location_list.html +++ b/billard/templates/billard/location_index.html @@ -1,10 +1,11 @@ {% extends 'billard/base.html' %} -{% block title %}Location List{% endblock %} +{% block title %}Standortliste{% endblock %} {% block content %} {% if location_list %} +

Bitte Standort auswählen:

@@ -15,11 +16,11 @@ {% for loc in location_list %} - - - - - + + + + + {% endfor %}
Code
{{ loc.code }}{{ loc.name }}{{ loc.street }}{{ loc.plz }}{{ loc.city }}{{ loc.code|default_if_none:"" }}{{ loc.name|default_if_none:"" }}{{ loc.street|default_if_none:"" }}{{ loc.plz|default_if_none:"" }}{{ loc.city|default_if_none:"" }}
diff --git a/billard/urls.py b/billard/urls.py index 020de52..4a71067 100644 --- a/billard/urls.py +++ b/billard/urls.py @@ -9,7 +9,7 @@ router.register(r'location_data', views.LocationDataViewSet) app_name = 'billard' urlpatterns = [ # ex. /billard/ - url(r'^$', login_required(views.LocationListView.as_view()), name='location_list'), + url(r'^$', login_required(views.LocationIndexView.as_view()), name='location_index'), # ex. /billard/1/ url(r'^(?P[0-9]+)/$', login_required(views.LocationDetailView.as_view()), name='location_detail'), # ex. /billard/1/accounting/ @@ -21,7 +21,7 @@ urlpatterns = [ #url(r'api/v1/', include(router.urls)), #url(r'process_locationdata', views.process_locationdata, name='process_locationdata'), #url(r'^accounting/$', views.AccountingView.as_view(), name='accounting'), - #url(r'accountmodal$', views.accountmodalview, name='accountmodal'), - #url(r'accoutmodal/confirm/(?P[0-9]+)$', views.accountmodalconfirmview, name="accountmodalconfirm") + url(r'^accountmodal$', views.accountmodalview, name='accountmodal'), + url(r'^accoutmodal/confirm/(?P[0-9]+)$', views.accountmodalconfirmview, name="accountmodalconfirm") # (?P[0-9]+) ] diff --git a/billard/views.py b/billard/views.py index 57d658a..6a89687 100644 --- a/billard/views.py +++ b/billard/views.py @@ -8,10 +8,11 @@ from django.views.generic.detail import DetailView from django.contrib.auth.decorators import login_required, permission_required from django.db.models import Min, Sum from django.http import HttpResponse +from django.utils.decorators import method_decorator -class LocationListView(generic.ListView): - template_name = 'billard/location_list.html' +class LocationIndexView(generic.ListView): + template_name = 'billard/location_index.html' context_object_name = 'location_list' def get_queryset(self): @@ -19,13 +20,26 @@ class LocationListView(generic.ListView): return Location.objects.filter(users__id=self.request.user.id).order_by('code') -class LocationDetailView(generic.ListView): - template_name = 'billard/location_list.html' - context_object_name = 'location_list' +class LocationDetailView(generic.DetailView): + model = Location + template_name = 'billard/location_detail.html' + + def dispatch(self, request, *args, **kwargs): + if request.is_ajax(): + context = { + 'location': self.get_object(), + } + return render(request, template_name='billard/location_detail_ajax.html', context=context) + return super(LocationDetailView, self).dispatch(request, *args, **kwargs) + + +@method_decorator(login_required, name='dispatch') +class AccountingView(generic.ListView): + template_name = 'billard/accounting.html' + context_object_name = 'accounting' def get_queryset(self): - """Return the last five published questions.""" - return Location.objects.filter(users__id=self.request.user.id).order_by('code') + return Accounting.objects.filter(billed=False).exclude(time_to__isnull=True) # TODO OLD CODE, CLEAN UP @@ -59,7 +73,7 @@ def accountmodalview(request): try: uuids = Client.objects.filter(report_user=request.user).values_list('uuid') account = Accounting.objects.filter(reporter_uuid__in=uuids).first - #TODO: support multiple account objects + # TODO: support multiple account objects except Client.DoesNotExist: account = None context = { @@ -96,7 +110,7 @@ def accounting(request): if loc is None: loc = min_loc locations = Location.objects.filter(users__id=request.user.id).order_by('code') - acc = Accounting.objects.filter(billed=False).exclude(time_to__isnull=True).\ + acc = Accounting.objects.filter(billed=False).exclude(time_to__isnull=True). \ filter(desk__client__location_id=loc).order_by('-time_from') acc_sum = acc.aggregate(Sum('prize')) acc_ids = list() @@ -164,18 +178,6 @@ def index(request): return resp -#@login_required -#@permission_required('billard.change_accounting') -class AccountingView(generic.ListView): - template_name = 'billard/accounting.html' - context_object_name = 'accounting' - - def get_queryset(self): - return Accounting.objects.filter(billed=False).exclude(time_to__isnull=True)\ - #.\ - #filter(desk__client__location_id=loc).order_by('-time_from') - - def process_locationdata(request): process_location_data() return HttpResponse('DONE') From cae1ee197e760ebc51d2aaa6eeca9f5ed48aa282 Mon Sep 17 00:00:00 2001 From: Robert Einsle Date: Thu, 27 Apr 2017 12:24:38 +0200 Subject: [PATCH 4/9] update accounting dialog --- billard/templates/billard/accounting.html | 12 ------------ billard/templates/billard/base.html | 3 --- billard/templates/billard/location_index.html | 6 ++++++ billard/urls.py | 2 +- billard/views.py | 10 ++++++++++ 5 files changed, 17 insertions(+), 16 deletions(-) diff --git a/billard/templates/billard/accounting.html b/billard/templates/billard/accounting.html index 112beae..b6dedd1 100644 --- a/billard/templates/billard/accounting.html +++ b/billard/templates/billard/accounting.html @@ -4,18 +4,6 @@ {% block title %}Accounting Data{% endblock %} {% block content %} -{% if not locations|length_is:"1" %} -
-{% csrf_token %} -
- -
-
-{% endif %} diff --git a/billard/templates/billard/base.html b/billard/templates/billard/base.html index 4b084df..dded387 100644 --- a/billard/templates/billard/base.html +++ b/billard/templates/billard/base.html @@ -29,9 +29,6 @@ diff --git a/billard/templates/billard/location_detail.html b/billard/templates/billard/location_detail.html index 6925e3f..726bd90 100644 --- a/billard/templates/billard/location_detail.html +++ b/billard/templates/billard/location_detail.html @@ -20,6 +20,12 @@ $(document).ready(function() { function refresh_page() { $('#desk_data').load('#'); + $('#modal-wrapper').load('{% url 'billard:account_modal' %}', function() { + if ( $('#accountsmodal').length ) { + window.clearInterval(interval); + $('#accountsmodal').modal('show'); + } + }); } {% endblock %} \ No newline at end of file diff --git a/billard/urls.py b/billard/urls.py index f06ab1c..09a5f40 100644 --- a/billard/urls.py +++ b/billard/urls.py @@ -16,6 +16,10 @@ urlpatterns = [ url(r'^(?P[0-9]+)/accounting/$', views.AccountingView.as_view(), name='accounting_detail'), # ex. /billard/1/accounting/confirm url(r'^(?P[0-9]+)/accounting/confirm/$', views.accounting_confirm, name='accounting_detail_confirm'), + # ex. /billard/1/account_modal/ + url(r'^account_modal/$', views.account_modal_view, name='account_modal'), + # ex. /billard/1/account_modal/confirm/ + url(r'^account_modal/(?P[0-9]+)/confirm/$', views.account_modal_confirm_view, name='account_modal_confirm'), # rest api url(r'api/v1/', include(router.urls)), @@ -23,7 +27,7 @@ urlpatterns = [ #url(r'^(?P[0-9]+)/$', views.LocationDataDetailView.as_view(), name='detail'), #url(r'process_locationdata', views.process_locationdata, name='process_locationdata'), #url(r'^accounting/$', views.AccountingView.as_view(), name='accounting'), - url(r'^accountmodal$', views.accountmodalview, name='accountmodal'), - url(r'^accoutmodal/confirm/(?P[0-9]+)$', views.accountmodalconfirmview, name="accountmodalconfirm") + #url(r'^accountmodal$', views.accountmodalview, name='accountmodal'), + #url(r'^accoutmodal/confirm/(?P[0-9]+)$', views.accountmodalconfirmview, name="accountmodalconfirm") # (?P[0-9]+) ] diff --git a/billard/views.py b/billard/views.py index 1040b72..d33eedb 100644 --- a/billard/views.py +++ b/billard/views.py @@ -67,6 +67,28 @@ def accounting_confirm(request, pk): return resp +@login_required +def account_modal_view(request): + try: + uuids = Client.objects.filter(report_user=request.user).values_list('uuid') + account = Accounting.objects.filter(reporter_uuid__in=uuids).first + # TODO: support multiple account objects + except Client.DoesNotExist: + account = None + context = { + 'account': account + } + return render(request, 'billard/accountmodal.html', context=context) + + +@login_required +def account_modal_confirm_view(request, pk): + account = Accounting.objects.get(pk=pk) + account.reporter_uuid = None + account.save() + return redirect('billard:location_detail', pk=account.desk.client.location_id) + + # TODO OLD CODE, CLEAN UP @@ -93,26 +115,6 @@ class LocationDataDetailView(DetailView): return super().get_template_names() -@login_required -def accountmodalview(request): - try: - uuids = Client.objects.filter(report_user=request.user).values_list('uuid') - account = Accounting.objects.filter(reporter_uuid__in=uuids).first - # TODO: support multiple account objects - except Client.DoesNotExist: - account = None - context = { - 'account': account - } - return render(request, 'billard/accountmodal.html', context=context) - - -@login_required -def accountmodalconfirmview(request, pk): - account = Accounting.objects.get(pk=pk) - account.reporter_uuid = None - account.save() - return redirect('billard:location_detail', pk=pk) @login_required From 361183966400f4bbd6a1e786ce4caba333c72423 Mon Sep 17 00:00:00 2001 From: Robert Einsle Date: Thu, 27 Apr 2017 13:59:36 +0200 Subject: [PATCH 7/9] cleanup code --- billard/urls.py | 10 +---- billard/views.py | 113 +---------------------------------------------- 2 files changed, 3 insertions(+), 120 deletions(-) diff --git a/billard/urls.py b/billard/urls.py index 09a5f40..721198a 100644 --- a/billard/urls.py +++ b/billard/urls.py @@ -22,12 +22,6 @@ urlpatterns = [ url(r'^account_modal/(?P[0-9]+)/confirm/$', views.account_modal_confirm_view, name='account_modal_confirm'), # rest api url(r'api/v1/', include(router.urls)), - - #url(r'^$', views.index, name='carom_index'), - #url(r'^(?P[0-9]+)/$', views.LocationDataDetailView.as_view(), name='detail'), - #url(r'process_locationdata', views.process_locationdata, name='process_locationdata'), - #url(r'^accounting/$', views.AccountingView.as_view(), name='accounting'), - #url(r'^accountmodal$', views.accountmodalview, name='accountmodal'), - #url(r'^accoutmodal/confirm/(?P[0-9]+)$', views.accountmodalconfirmview, name="accountmodalconfirm") - # (?P[0-9]+) + # ex. /billard/process_location_data/ + url(r'^process_location_data/$', views.process_location_data, name='process_location_data'), ] diff --git a/billard/views.py b/billard/views.py index d33eedb..d93b75c 100644 --- a/billard/views.py +++ b/billard/views.py @@ -89,122 +89,11 @@ def account_modal_confirm_view(request, pk): return redirect('billard:location_detail', pk=account.desk.client.location_id) -# TODO OLD CODE, CLEAN UP - - class LocationDataViewSet(viewsets.ModelViewSet): queryset = LocationData.objects.all() serializer_class = LocationDataSerializer -class IndexView(generic.ListView): - model = LocationData - - def get_template_names(self): - if self.request.is_ajax(): - return ('billard/locationdata_list_ajax.html',) - return super().get_template_names() - - -class LocationDataDetailView(DetailView): - model = LocationData - - def get_template_names(self): - if self.request.is_ajax(): - return ('billard/locationdata_detail_ajax.html',) - return super().get_template_names() - - - - -@login_required -@permission_required('billard.change_accounting') -def accounting(request): - if request.method == 'GET': - template = 'billard/accounting.html' - loc = None - min_loc = Location.objects.filter(users__id=request.user.id).aggregate(Min('id'))['id__min'] - if 'loc' in request.GET: - loc = request.GET['loc'] - if not Location.objects.filter(users__id=request.user.id).filter(id=loc).exists(): - resp = redirect('accounting') - if min_loc is not None: - resp['Location'] += '?loc={}'.format(str(min_loc)) - request.session['loc'] = str(min_loc) - return resp - else: - return render(request, accounting) - if loc is None: - loc = min_loc - locations = Location.objects.filter(users__id=request.user.id).order_by('code') - acc = Accounting.objects.filter(billed=False).exclude(time_to__isnull=True). \ - filter(desk__client__location_id=loc).order_by('-time_from') - acc_sum = acc.aggregate(Sum('prize')) - acc_ids = list() - for a in acc: - acc_ids.append(a.id) - - context = { - 'location_id': int(loc), - 'locations': locations, - 'accounting': acc, - 'acc_ids': ','.join(str(e) for e in acc_ids), - } - if acc_sum['prize__sum'] is None: - context['acc_sum'] = 0 - else: - context['acc_sum'] = acc_sum['prize__sum'] - return render(request, template_name=template, context=context) - if request.method == 'POST': - loc = request.POST['location-selector'] - if 'accountings' in request.POST: - acc_ids = request.POST['accountings'].split(',') - Accounting.objects.filter(id__in=acc_ids).update(billed=True) - Accounting.objects.filter(id__in=acc_ids).update(account_user=request.user.username) - request.session['loc'] = str(loc) - resp = redirect('accounting') - resp['Location'] += '?loc={}'.format(str(loc)) - return resp - - -@login_required -def index(request): - if request.method == 'GET': - template = 'billard/index.html' - loc = None - if request.is_ajax(): - template = 'billard/index_ajax.html' - loc = request.session.get('loc') - min_loc = Location.objects.filter(users__id=request.user.id).aggregate(Min('id'))['id__min'] - if 'loc' in request.GET: - loc = request.GET['loc'] - if not Location.objects.filter(users__id=request.user.id).filter(id=loc).exists(): - resp = redirect('carom_index') - if min_loc is not None: - resp['Location'] += '?loc={}'.format(str(min_loc)) - request.session['loc'] = str(min_loc) - return resp - else: - return render(request, template) - if loc is None: - loc = min_loc - locations = Location.objects.filter(users__id=request.user.id).order_by('code') - clients = Client.objects.filter(location_id=loc).order_by('id') - context = { - 'range': range(1, 9), - 'locations': locations, - 'clients': clients, - 'location_id': int(loc), - } - return render(request, template, context=context) - if request.method == 'POST': - loc = request.POST['location-selector'] - request.session['loc'] = str(loc) - resp = redirect('carom_index') - resp['Location'] += '?loc={}'.format(str(loc)) - return resp - - -def process_locationdata(request): +def process_location_data(request): process_location_data() return HttpResponse('DONE') From 857af8d54d53a13d4ae348cdfd90d50a4c04e9e9 Mon Sep 17 00:00:00 2001 From: Robert Einsle Date: Thu, 27 Apr 2017 14:03:57 +0200 Subject: [PATCH 8/9] cleanup code --- billard/urls.py | 12 ++--- billard/views.py | 113 +---------------------------------------------- 2 files changed, 4 insertions(+), 121 deletions(-) diff --git a/billard/urls.py b/billard/urls.py index 09a5f40..77853a3 100644 --- a/billard/urls.py +++ b/billard/urls.py @@ -20,14 +20,8 @@ urlpatterns = [ url(r'^account_modal/$', views.account_modal_view, name='account_modal'), # ex. /billard/1/account_modal/confirm/ url(r'^account_modal/(?P[0-9]+)/confirm/$', views.account_modal_confirm_view, name='account_modal_confirm'), - # rest api + # ex. /billard/api/v1/ (rest api) url(r'api/v1/', include(router.urls)), - - #url(r'^$', views.index, name='carom_index'), - #url(r'^(?P[0-9]+)/$', views.LocationDataDetailView.as_view(), name='detail'), - #url(r'process_locationdata', views.process_locationdata, name='process_locationdata'), - #url(r'^accounting/$', views.AccountingView.as_view(), name='accounting'), - #url(r'^accountmodal$', views.accountmodalview, name='accountmodal'), - #url(r'^accoutmodal/confirm/(?P[0-9]+)$', views.accountmodalconfirmview, name="accountmodalconfirm") - # (?P[0-9]+) + # ex. /billard/process_location_data/ + url(r'^process_location_data/$', views.process_location_data, name='process_location_data'), ] diff --git a/billard/views.py b/billard/views.py index d33eedb..d93b75c 100644 --- a/billard/views.py +++ b/billard/views.py @@ -89,122 +89,11 @@ def account_modal_confirm_view(request, pk): return redirect('billard:location_detail', pk=account.desk.client.location_id) -# TODO OLD CODE, CLEAN UP - - class LocationDataViewSet(viewsets.ModelViewSet): queryset = LocationData.objects.all() serializer_class = LocationDataSerializer -class IndexView(generic.ListView): - model = LocationData - - def get_template_names(self): - if self.request.is_ajax(): - return ('billard/locationdata_list_ajax.html',) - return super().get_template_names() - - -class LocationDataDetailView(DetailView): - model = LocationData - - def get_template_names(self): - if self.request.is_ajax(): - return ('billard/locationdata_detail_ajax.html',) - return super().get_template_names() - - - - -@login_required -@permission_required('billard.change_accounting') -def accounting(request): - if request.method == 'GET': - template = 'billard/accounting.html' - loc = None - min_loc = Location.objects.filter(users__id=request.user.id).aggregate(Min('id'))['id__min'] - if 'loc' in request.GET: - loc = request.GET['loc'] - if not Location.objects.filter(users__id=request.user.id).filter(id=loc).exists(): - resp = redirect('accounting') - if min_loc is not None: - resp['Location'] += '?loc={}'.format(str(min_loc)) - request.session['loc'] = str(min_loc) - return resp - else: - return render(request, accounting) - if loc is None: - loc = min_loc - locations = Location.objects.filter(users__id=request.user.id).order_by('code') - acc = Accounting.objects.filter(billed=False).exclude(time_to__isnull=True). \ - filter(desk__client__location_id=loc).order_by('-time_from') - acc_sum = acc.aggregate(Sum('prize')) - acc_ids = list() - for a in acc: - acc_ids.append(a.id) - - context = { - 'location_id': int(loc), - 'locations': locations, - 'accounting': acc, - 'acc_ids': ','.join(str(e) for e in acc_ids), - } - if acc_sum['prize__sum'] is None: - context['acc_sum'] = 0 - else: - context['acc_sum'] = acc_sum['prize__sum'] - return render(request, template_name=template, context=context) - if request.method == 'POST': - loc = request.POST['location-selector'] - if 'accountings' in request.POST: - acc_ids = request.POST['accountings'].split(',') - Accounting.objects.filter(id__in=acc_ids).update(billed=True) - Accounting.objects.filter(id__in=acc_ids).update(account_user=request.user.username) - request.session['loc'] = str(loc) - resp = redirect('accounting') - resp['Location'] += '?loc={}'.format(str(loc)) - return resp - - -@login_required -def index(request): - if request.method == 'GET': - template = 'billard/index.html' - loc = None - if request.is_ajax(): - template = 'billard/index_ajax.html' - loc = request.session.get('loc') - min_loc = Location.objects.filter(users__id=request.user.id).aggregate(Min('id'))['id__min'] - if 'loc' in request.GET: - loc = request.GET['loc'] - if not Location.objects.filter(users__id=request.user.id).filter(id=loc).exists(): - resp = redirect('carom_index') - if min_loc is not None: - resp['Location'] += '?loc={}'.format(str(min_loc)) - request.session['loc'] = str(min_loc) - return resp - else: - return render(request, template) - if loc is None: - loc = min_loc - locations = Location.objects.filter(users__id=request.user.id).order_by('code') - clients = Client.objects.filter(location_id=loc).order_by('id') - context = { - 'range': range(1, 9), - 'locations': locations, - 'clients': clients, - 'location_id': int(loc), - } - return render(request, template, context=context) - if request.method == 'POST': - loc = request.POST['location-selector'] - request.session['loc'] = str(loc) - resp = redirect('carom_index') - resp['Location'] += '?loc={}'.format(str(loc)) - return resp - - -def process_locationdata(request): +def process_location_data(request): process_location_data() return HttpResponse('DONE') From a165a0560fe7b1986be257f096889517588b695e Mon Sep 17 00:00:00 2001 From: Robert Einsle Date: Thu, 27 Apr 2017 14:04:56 +0200 Subject: [PATCH 9/9] cleanup code --- billard/views.py | 1 - 1 file changed, 1 deletion(-) diff --git a/billard/views.py b/billard/views.py index d93b75c..be4c464 100644 --- a/billard/views.py +++ b/billard/views.py @@ -6,7 +6,6 @@ from billard.tasks import process_location_data from rest_framework import viewsets from django.shortcuts import render, redirect from django.views import generic -from django.views.generic.detail import DetailView from django.contrib.auth.decorators import login_required, permission_required from django.db.models import Min, Sum from django.http import HttpResponse