carom-server/billard/views.py

147 lines
5.1 KiB
Python
Raw Normal View History

2017-03-09 20:32:49 +01:00
from billard.serializers import LocationDataSerializer
from billard.models import LocationData, Location, Client, Accounting
2017-03-09 20:32:49 +01:00
from billard.tasks import process_location_data
2017-01-03 19:55:01 +01:00
from rest_framework import viewsets
2017-02-04 07:35:17 +01:00
from django.shortcuts import render, redirect
from django.views import generic
2017-01-09 20:42:59 +01:00
from django.views.generic.detail import DetailView
2017-02-03 09:42:23 +01:00
from django.contrib.auth.decorators import login_required
2017-03-11 20:20:25 +01:00
from django.db.models import Min, Sum
2017-02-11 18:59:42 +01:00
from django.http import HttpResponse
2017-01-03 19:26:51 +01:00
2017-01-03 19:55:01 +01:00
2017-01-06 20:24:36 +01:00
class LocationDataViewSet(viewsets.ModelViewSet):
queryset = LocationData.objects.all()
serializer_class = LocationDataSerializer
class IndexView(generic.ListView):
model = LocationData
2017-02-20 19:11:48 +01:00
def get_template_names(self):
if self.request.is_ajax():
return ('billard/locationdata_list_ajax.html',)
return super().get_template_names()
2017-01-09 20:42:59 +01:00
2017-01-09 20:42:59 +01:00
class LocationDataDetailView(DetailView):
model = LocationData
2017-02-01 18:57:29 +01:00
2017-02-20 19:11:48 +01:00
def get_template_names(self):
if self.request.is_ajax():
return ('billard/locationdata_detail_ajax.html',)
return super().get_template_names()
2017-02-01 18:57:29 +01:00
2017-03-06 19:22:01 +01:00
@login_required
def accountmodalview(request):
2017-03-09 19:50:39 +01:00
try:
2017-03-27 21:14:17 +02:00
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
2017-03-09 19:50:39 +01:00
except Client.DoesNotExist:
account = None
2017-03-06 19:22:01 +01:00
context = {
2017-03-06 20:05:53 +01:00
'account': account
2017-03-06 19:22:01 +01:00
}
return render(request, 'billard/accountmodal.html', context=context)
2017-02-01 18:57:29 +01:00
2017-03-06 20:05:53 +01:00
@login_required
def accountmodalconfirmview(request, pk):
account = Accounting.objects.get(pk=pk)
account.reporter_uuid = None
account.save()
2017-03-06 20:13:22 +01:00
return redirect('carom_index')
2017-03-06 20:05:53 +01:00
2017-03-11 20:20:25 +01:00
@login_required
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,
2017-03-11 20:48:59 +01:00
'acc_ids': ','.join(str(e) for e in acc_ids),
2017-03-11 20:20:25 +01:00
}
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']
2017-03-11 20:48:59 +01:00
if 'accountings' in request.POST:
acc_ids = request.POST['accountings'].split(',')
Accounting.objects.filter(id__in=acc_ids).update(billed=True)
2017-03-11 20:20:25 +01:00
request.session['loc'] = str(loc)
resp = redirect('accounting')
resp['Location'] += '?loc={}'.format(str(loc))
return resp
2017-02-03 09:42:23 +01:00
@login_required
2017-02-01 18:57:29 +01:00
def index(request):
2017-02-04 07:35:17 +01:00
if request.method == 'GET':
2017-02-20 19:11:48 +01:00
template = 'billard/index.html'
2017-02-26 12:20:50 +01:00
loc = None
2017-02-20 19:11:48 +01:00
if request.is_ajax():
template = 'billard/index_ajax.html'
2017-02-26 12:20:50 +01:00
loc = request.session.get('loc')
2017-02-04 07:35:17 +01:00
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')
2017-02-05 12:58:06 +01:00
if min_loc is not None:
resp['Location'] += '?loc={}'.format(str(min_loc))
2017-02-26 12:20:50 +01:00
request.session['loc'] = str(min_loc)
2017-02-05 12:58:06 +01:00
return resp
else:
2017-02-20 19:11:48 +01:00
return render(request, template)
2017-02-04 07:35:17 +01:00
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 = {
2017-02-10 19:39:21 +01:00
'range': range(1, 9),
2017-02-04 07:35:17 +01:00
'locations': locations,
'clients': clients,
'location_id': int(loc),
}
2017-02-20 19:11:48 +01:00
return render(request, template, context=context)
2017-02-04 07:35:17 +01:00
if request.method == 'POST':
2017-02-01 18:57:29 +01:00
loc = request.POST['location-selector']
2017-02-26 12:20:50 +01:00
request.session['loc'] = str(loc)
2017-02-04 07:35:17 +01:00
resp = redirect('carom_index')
resp['Location'] += '?loc={}'.format(str(loc))
return resp
2017-02-11 18:59:42 +01:00
def process_locationdata(request):
process_location_data()
return HttpResponse('DONE')