carom-server/billard/views.py
2017-03-27 21:14:17 +02:00

147 lines
5.1 KiB
Python

from billard.serializers import LocationDataSerializer
from billard.models import LocationData, Location, Client, Accounting
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
from django.db.models import Min, Sum
from django.http import HttpResponse
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
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('carom_index')
@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,
'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)
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):
process_location_data()
return HttpResponse('DONE')