from django.shortcuts import render
from datetime import datetime, timedelta
import pytz, random

from .models import *
from .serializers import *

from django.db.models import Q
from django.db.models import Count

from rest_framework import generics
from rest_framework.decorators import authentication_classes, permission_classes
from rest_framework.authentication import SessionAuthentication, BasicAuthentication

from rest_framework.permissions import IsAuthenticated
# from durin.auth import TokenAuthentication

# from durin.views import APIAccessTokenView

from django_filters.rest_framework import DjangoFilterBackend
from rest_framework import filters

from rest_framework.response import Response
from rest_framework_api_key.permissions import HasAPIKey

td = timedelta(hours=7)
odt = datetime.now() - td

# Create your views here.

class ContractAPIView(generics.ListAPIView):
    serializer_class = ContractSerializer
    queryset = Contract.objects.all()[:25]
    filter_backends = [DjangoFilterBackend, filters.SearchFilter]
    filterset_fields = ['id',]
    permission_classes = [HasAPIKey]


class ContractAllAPIView(generics.ListAPIView):
    serializer_class = ContractSerializer
    queryset = Contract.objects.all()
    filter_backends = [DjangoFilterBackend, filters.SearchFilter]
    search_fields = ['notice_id', 'original_contract_number', 'title', 'description', 'company__name', 'us_dept', 'us_dept_sub_tier', 'us_office', 'naics_code', 'prod_svc_code']
    filterset_fields = ['id',]
    permission_classes = [HasAPIKey]


class CompanyAPIView(generics.ListAPIView):
    serializer_class = CompanySerializer
    queryset = Company.objects.all()
    permission_classes = [HasAPIKey]

# class PromoAPIView(generics.ListAPIView):
#     serializer_class = PromoSerializer
#     queryset = Promo.objects.filter(published=True)
#     filterset_fields = ['organization__name', 'calendar__shortcode',]
#     search_fields = ['organization__name', 'calendar__shortcode',]
#     # permission_classes = [HasAPIKey]
    
#     def get_queryset(self):
#         calendar = self.request.GET.get('calendar__shortcode')
#         queryset = Promo.objects.filter(published=True, calendar__shortcode=calendar).order_by('?')
#         return queryset

