from django.views.generic import DetailView, ListView
from rest_framework.permissions import AllowAny
from rest_framework.viewsets import ModelViewSet
from reservations.permissions import IsOwnerOrReadOnly
from reservations.models import Reservation, Queue
from reservations.serializers import ReservationsSerializer
from django_filters import rest_framework as filters
from datetime import datetime

from website.models import Rule


class QueueDetailView(DetailView):
    model = Queue

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        if Rule.objects.filter(printer_rule=True).exists():
            # Get first rule marked as printer rule
            context['printer_rule'] = Rule.objects.filter(printer_rule=True)[0]
        return context

class QueueListView(ListView):
    model = Queue

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        if self.request.user.is_authenticated:
            context['reservation_list'] = Reservation.objects.filter(user=self.request.user, end__gte=datetime.now())
        else:
            context['reservation_list'] = None
        if Rule.objects.filter(printer_rule=True).exists():
            # Get first rule marked as printer rule
            context['printer_rule'] = Rule.objects.filter(printer_rule=True)[0]
        return context


class SearchDateFilter(filters.FilterSet):
    start = filters.IsoDateTimeFilter(field_name="start", lookup_expr='gt')
    end = filters.IsoDateTimeFilter(field_name="end", lookup_expr='lt')

    class Meta:
        model = Reservation
        fields = ['parent_queue', ]


class ReservationsViewSet(ModelViewSet):
    queryset = Reservation.objects.all()
    filter_backends = (filters.DjangoFilterBackend,)
    filterset_class = SearchDateFilter

    def get_serializer_class(self):
        return ReservationsSerializer

    def get_permissions(self):
        if self.action == 'list':
            permission_classes = [AllowAny]
        else:
            # Kan ikke delete eller patche andre reservasjoner
            permission_classes = [IsOwnerOrReadOnly]
        return [permission() for permission in permission_classes]

@@ -1428,21 +1428,19 @@
 t):%0A    
-start
+end
  = filte
@@ -1497,17 +1497,17 @@
 pr='
-g
+l
 t')%0A    
 end 
@@ -1498,27 +1498,29 @@
 r='lt')%0A    
-end
+start
  = filters.I
@@ -1563,25 +1563,25 @@
 ookup_expr='
-l
+g
 t')%0A%0A    cla

Fix filter to include all events that span over calendar time