carom-server/billard/views.py

111 lines
3.7 KiB
Python
Raw Normal View History

2017-04-27 13:23:18 +02:00
import ast
2017-08-03 19:52:43 +02:00
import logging
2017-04-27 13:23:18 +02:00
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-04-10 20:30:27 +02:00
from django.contrib.auth.decorators import login_required, permission_required
2017-04-29 11:15:26 +02:00
from django.db.models import Sum
2017-02-11 18:59:42 +01:00
from django.http import HttpResponse
2017-04-27 10:35:10 +02:00
from django.utils.decorators import method_decorator
2017-04-29 11:15:26 +02:00
from django.utils import timezone
2017-01-03 19:26:51 +01:00
2017-01-03 19:55:01 +01:00
2017-08-03 19:52:43 +02:00
log = logging.getLogger(__name__)
2017-04-27 10:35:10 +02:00
class LocationIndexView(generic.ListView):
template_name = 'billard/location_index.html'
2017-04-27 08:04:44 +02:00
context_object_name = 'location_list'
def get_queryset(self):
"""Return the last five published questions."""
return Location.objects.filter(users__id=self.request.user.id).order_by('code')
2017-04-27 10:35:10 +02:00
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)
2017-04-27 14:22:23 +02:00
result = super(LocationDetailView, self).dispatch(request, *args, **kwargs)
result.context_data['pk'] = self.kwargs['pk']
return result
2017-04-27 10:35:10 +02:00
@method_decorator(login_required, name='dispatch')
class AccountingView(generic.ListView):
template_name = 'billard/accounting.html'
context_object_name = 'accounting'
2017-04-27 08:04:44 +02:00
def get_queryset(self):
return Accounting.objects.filter(billed=False).filter(desk__client__location_id=self.kwargs['pk'])\
.exclude(time_to__isnull=True).order_by('time_from')
2017-04-27 08:04:44 +02:00
2017-04-27 12:24:38 +02:00
def dispatch(self, request, *args, **kwargs):
result = super(AccountingView, self).dispatch(request, *args, **kwargs)
acc_sum = self.get_queryset().aggregate(Sum('prize'))
if acc_sum['prize__sum'] is None:
result.context_data['acc_sum'] = 0
else:
result.context_data['acc_sum'] = acc_sum['prize__sum']
result.context_data['acc_ids'] = [acc.id for acc in self.get_queryset().all()]
return result
2017-04-27 08:04:44 +02:00
2017-04-27 13:23:18 +02:00
@login_required
@permission_required('billard.change_accounting')
def accounting_confirm(request, pk):
if request.method == 'POST':
if 'accountings' in request.POST:
acc_ids = ast.literal_eval(request.POST['accountings'])
if len(acc_ids) > 0:
2017-04-29 11:38:52 +02:00
Accounting.objects.filter(id__in=acc_ids).update(
billed=True,
account_user=request.user.username,
account_tst=timezone.now(),
)
2017-04-27 13:23:18 +02:00
resp = redirect('billard:accounting_detail', pk=pk)
return resp
2017-04-27 13:56:18 +02:00
@login_required
2017-04-27 14:22:23 +02:00
def account_modal_view(request, loc_pk):
2017-04-27 13:56:18 +02:00
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 = {
2017-04-27 14:22:23 +02:00
'account': account,
'loc_pk': loc_pk,
2017-04-27 13:56:18 +02:00
}
return render(request, 'billard/accountmodal.html', context=context)
@login_required
2017-04-27 14:22:23 +02:00
def account_modal_confirm_view(request, loc_pk, pk):
2017-04-27 13:56:18 +02:00
account = Accounting.objects.get(pk=pk)
account.reporter_uuid = None
account.save()
2017-04-27 14:22:23 +02:00
return redirect('billard:location_detail', pk=loc_pk)
2017-04-27 13:56:18 +02:00
2017-01-06 20:24:36 +01:00
class LocationDataViewSet(viewsets.ModelViewSet):
queryset = LocationData.objects.all()
serializer_class = LocationDataSerializer
2017-04-27 14:03:57 +02:00
def process_location_data(request):
2017-02-11 18:59:42 +01:00
process_location_data()
return HttpResponse('DONE')