2017-08-03 19:52:43 +02:00
|
|
|
import logging
|
2018-03-31 18:22:23 +02:00
|
|
|
from datetime import datetime
|
2017-04-27 13:23:18 +02:00
|
|
|
|
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
|
2018-02-11 10:56:44 +01:00
|
|
|
from django.shortcuts import render, redirect
|
2018-02-15 15:58:55 +01:00
|
|
|
from django.urls import reverse_lazy
|
2017-04-29 11:15:26 +02:00
|
|
|
from django.utils import timezone
|
2018-02-11 10:56:44 +01:00
|
|
|
from django.utils.decorators import method_decorator
|
|
|
|
from django.views import generic
|
2018-02-13 08:38:17 +01:00
|
|
|
from django.views.generic import UpdateView
|
2018-02-11 10:56:44 +01:00
|
|
|
from rest_framework import viewsets
|
2017-01-03 19:26:51 +01:00
|
|
|
|
2018-02-11 10:56:44 +01:00
|
|
|
from billard.models import LocationData, Location, Client, Accounting
|
2018-02-11 11:27:27 +01:00
|
|
|
from billard.serializers import LocationDataSerializer, ClientUpdateLastSeenSerializer
|
2018-02-13 08:38:17 +01:00
|
|
|
from .forms import UserInformationUpdateForm
|
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):
|
2018-02-11 10:56:44 +01:00
|
|
|
return Accounting.objects.filter(billed=False).filter(desk__client__location_id=self.kwargs['pk']) \
|
2017-05-17 09:57:56 +02:00
|
|
|
.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()]
|
2018-02-10 13:44:14 +01:00
|
|
|
result.context_data['location'] = Location.objects.get(pk=self.kwargs['pk'])
|
2017-04-27 12:24:38 +02:00
|
|
|
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:
|
2018-02-15 15:58:55 +01:00
|
|
|
acc_ids = request.POST.getlist('list_acc_id')
|
2017-04-27 13:23:18 +02:00
|
|
|
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')
|
2017-08-17 20:09:45 +02:00
|
|
|
accounts = Accounting.objects.filter(reporter_uuid__in=uuids)
|
2017-04-27 13:56:18 +02:00
|
|
|
# TODO: support multiple account objects
|
|
|
|
except Client.DoesNotExist:
|
2017-08-17 20:09:45 +02:00
|
|
|
accounts = None
|
2017-04-27 13:56:18 +02:00
|
|
|
context = {
|
2017-08-17 20:09:45 +02:00
|
|
|
'accounts': accounts,
|
2017-04-27 14:22:23 +02:00
|
|
|
'loc_pk': loc_pk,
|
2017-08-17 20:12:22 +02:00
|
|
|
'pks': ','.join([str(account.pk) for account in accounts]),
|
2017-04-27 13:56:18 +02:00
|
|
|
}
|
|
|
|
return render(request, 'billard/accountmodal.html', context=context)
|
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
2017-08-17 20:09:45 +02:00
|
|
|
def account_modal_confirm_view(request, loc_pk, pks):
|
|
|
|
for pk in pks.split(','):
|
|
|
|
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-01-09 10:59:46 +01:00
|
|
|
|
|
|
|
|
2018-02-11 11:27:27 +01:00
|
|
|
class ClientUpdateLastSeenViewSet(viewsets.ModelViewSet):
|
|
|
|
queryset = LocationData.objects.all()
|
|
|
|
serializer_class = ClientUpdateLastSeenSerializer
|
|
|
|
|
|
|
|
|
2018-02-13 08:38:17 +01:00
|
|
|
@method_decorator(login_required, name='dispatch')
|
|
|
|
class UserUpdateView(UpdateView):
|
|
|
|
form_class = UserInformationUpdateForm
|
|
|
|
template_name = 'registration/my_account.html'
|
|
|
|
success_url = reverse_lazy('billard:my_account')
|
|
|
|
|
|
|
|
def get_object(self):
|
|
|
|
return self.request.user
|