from rest_framework import viewsets, routers, serializers
from rest_framework.decorators import detail_route
from rest_framework.response import Response

from models import Applicant
import django_filters


class ApplicantSerializer(serializers.ModelSerializer):
    class Meta:                
        model = Applicant


class ApplicantFilter(django_filters.FilterSet):
    
    ## some examples to get you started
    ## more at: http://www.django-rest-framework.org/api-guide/filtering/
    #date_range = django_filters.DateRangeFilter(name="day") 
    #date_from = django_filters.DateFilter(name="day", lookup_type="gte")
    #date_to = django_filters.DateFilter(name="day", lookup_type="lte")
    #customer = django_filters.CharFilter(name="project__customer")
    #month = django_filters.DateFilter(name="day__month")
    
    class Meta:
        model = Applicant
        #fields = ['status', 'day', 'date_range', 'date_from', 'date_to', 'month']


class ApplicantViewSet(viewsets.ModelViewSet):
    """
    api/v1/applicant/ 
    ----

    """
    
    model = Applicant
    serializer_class=ApplicantSerializer
    filter_class = ApplicantFilter
    filter_fields = ()
    
    
    def get_queryset(self):
        '''
        * Limit queries to only those owned by the current user
        * Admin can see all items 
        * Not logged in is empty list
        '''

        user = self.request.user
        if not user.is_authenticated():
            return Applicant.objects.none()

        if user.is_superuser:
            return Applicant.objects.all()
        else:
            return Applicant.objects.filter(user=user.id)
    
## endpoints should be plural:
router = routers.DefaultRouter()
router.register('applicants', ApplicantViewSet)


