from billard.serializers import * from .models import LocationData 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 class ClientViewSet(viewsets.ModelViewSet): queryset = LocationData.objects.all() serializer_class = ClientSerializer class LocationViewSet(viewsets.ModelViewSet): queryset = LocationData.objects.all() serializer_class = LocationSerializer class TableViewSet(viewsets.ModelViewSet): queryset = LocationData.objects.all() serializer_class = TableSerializer class LocationDataViewSet(viewsets.ModelViewSet): queryset = LocationData.objects.all() serializer_class = LocationDataSerializer class IndexView(generic.ListView): model = LocationData class LocationDataDetailView(DetailView): model = LocationData @login_required def index(request): if request.method == 'GET': min_loc = Location.objects.filter(users__id=request.user.id).aggregate(Min('id'))['id__min'] loc = None 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)) return resp else: return render(request, 'billard/index.html') 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, 3), 'locations': locations, 'clients': clients, 'location_id': int(loc), } return render(request, 'billard/index.html', context=context) if request.method == 'POST': loc = request.POST['location-selector'] resp = redirect('carom_index') resp['Location'] += '?loc={}'.format(str(loc)) return resp