import ast 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, permission_required from django.db.models import Min, Sum from django.http import HttpResponse from django.utils.decorators import method_decorator class LocationIndexView(generic.ListView): template_name = 'billard/location_index.html' 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') 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 Accounting.objects.filter(billed=False).exclude(time_to__isnull=True).order_by('time_from') 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 @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: Accounting.objects.filter(id__in=acc_ids).update(billed=True) Accounting.objects.filter(id__in=acc_ids).update(account_user=request.user.username) resp = redirect('billard:accounting_detail', pk=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) class LocationDataViewSet(viewsets.ModelViewSet): queryset = LocationData.objects.all() serializer_class = LocationDataSerializer def process_location_data(request): process_location_data() return HttpResponse('DONE')