# Path: coffeestats/caffeine_api_v2/serializers.py
# class CaffeineSerializer(serializers.HyperlinkedModelSerializer):
#     ctype = CaffeineField()
# 
#     class Meta:
#         model = Caffeine
#         fields = (
#             'url', 'user', 'date', 'timezone', 'ctype'
#         )
#         extra_kwargs = {
#             'user': {'lookup_field': 'username'},
#         }
# 
#     validators = [
#         NoRecentCaffeineValidator('user', 'ctype', 'date')
#     ]
# 
# class UserCaffeineSerializer(serializers.HyperlinkedModelSerializer):
#     ctype = CaffeineField()
#     user = serializers.HyperlinkedRelatedField(
#         read_only=True, view_name='user-detail', lookup_field='username')
# 
#     class Meta:
#         model = Caffeine
#         fields = (
#             'url', 'date', 'entrytime', 'timezone', 'ctype', 'user'
#         )
#         validators = [
#             NoRecentCaffeineValidator('user', 'ctype', 'date')
#         ]
# 
#     def save(self):
#         user = self.context['view'].view_owner
#         self.validated_data['user'] = user
#         if ('timezone' not in self.validated_data or
#                 not self.validated_data['timezone']):
#             self.validated_data['timezone'] = user.timezone
#         return super(UserCaffeineSerializer, self).save()
# 
# class UserSerializer(serializers.HyperlinkedModelSerializer):
#     caffeines = serializers.HyperlinkedIdentityField(
#         view_name='user-caffeine-list', lookup_field='username',
#         lookup_url_kwarg='caffeine_username')
#     name = serializers.SerializerMethodField()
#     profile = serializers.HyperlinkedIdentityField(
#         view_name='public', lookup_field='username')
#     counts = serializers.SerializerMethodField()
# 
#     class Meta:
#         model = User
#         fields = (
#             'url', 'username', 'location', 'first_name', 'last_name',
#             'name', 'profile', 'counts', 'caffeines',
#         )
#         extra_kwargs = {
#             'url': {'lookup_field': 'username'},
#         }
# 
#     def get_name(self, obj):
#         return obj.get_full_name()
# 
#     def get_counts(self, obj):
#         count_items = Caffeine.objects.total_caffeine_for_user(obj)
#         return count_items
# 
# Path: coffeestats/caffeine_api_v2/permissions.py
# class IsOwnerOrReadOnly(permissions.BasePermission):
#     """
#     Custom permission to only allow owners of an object to edit it.
# 
#     """
#     def has_object_permission(self, request, view, obj):
#         if request.method in permissions.SAFE_METHODS:
#             return True
# 
#         return obj.user == request.user
# 
# class IsOwnCaffeineOrReadOnly(permissions.BasePermission):
#     """
#     Custom permission to only allow operations on own caffeine items.
# 
#     """
#     def has_permission(self, request, view):
#         if request.method in permissions.SAFE_METHODS:
#             return True
#         return view.view_owner == request.user


from django.contrib.auth.mixins import LoginRequiredMixin
from django.views.generic import TemplateView
from rest_framework import permissions, viewsets
from caffeine.models import Caffeine, User
from .serializers import (
    CaffeineSerializer,
    UserCaffeineSerializer,
    UserSerializer,
)
from .permissions import IsOwnerOrReadOnly, IsOwnCaffeineOrReadOnly



class CaffeineViewSet(viewsets.ReadOnlyModelViewSet):
    """
    API endpoint that allows caffeine entries to be viewed.
    """
    queryset = Caffeine.objects.all().order_by('-date')
    serializer_class = CaffeineSerializer


class UserViewSet(viewsets.ReadOnlyModelViewSet):
    """
    API endpoint that allows users to be viewed.
    """
    queryset = User.objects.all().order_by('username')
    serializer_class = UserSerializer
    lookup_field = 'username'


class UserCaffeineViewSet(viewsets.ModelViewSet):
    """
    API endpoint that allows working with a users caffeine entries.
    """
    serializer_class = UserCaffeineSerializer
    permission_classes = (permissions.IsAuthenticatedOrReadOnly,

# Path: coffeestats/caffeine_api_v2/serializers.py
# class CaffeineSerializer(serializers.HyperlinkedModelSerializer):
#     ctype = CaffeineField()
# 
#     class Meta:
#         model = Caffeine
#         fields = (
#             'url', 'user', 'date', 'timezone', 'ctype'
#         )
#         extra_kwargs = {
#             'user': {'lookup_field': 'username'},
#         }
# 
#     validators = [
#         NoRecentCaffeineValidator('user', 'ctype', 'date')
#     ]
# 
# class UserCaffeineSerializer(serializers.HyperlinkedModelSerializer):
#     ctype = CaffeineField()
#     user = serializers.HyperlinkedRelatedField(
#         read_only=True, view_name='user-detail', lookup_field='username')
# 
#     class Meta:
#         model = Caffeine
#         fields = (
#             'url', 'date', 'entrytime', 'timezone', 'ctype', 'user'
#         )
#         validators = [
#             NoRecentCaffeineValidator('user', 'ctype', 'date')
#         ]
# 
#     def save(self):
#         user = self.context['view'].view_owner
#         self.validated_data['user'] = user
#         if ('timezone' not in self.validated_data or
#                 not self.validated_data['timezone']):
#             self.validated_data['timezone'] = user.timezone
#         return super(UserCaffeineSerializer, self).save()
# 
# class UserSerializer(serializers.HyperlinkedModelSerializer):
#     caffeines = serializers.HyperlinkedIdentityField(
#         view_name='user-caffeine-list', lookup_field='username',
#         lookup_url_kwarg='caffeine_username')
#     name = serializers.SerializerMethodField()
#     profile = serializers.HyperlinkedIdentityField(
#         view_name='public', lookup_field='username')
#     counts = serializers.SerializerMethodField()
# 
#     class Meta:
#         model = User
#         fields = (
#             'url', 'username', 'location', 'first_name', 'last_name',
#             'name', 'profile', 'counts', 'caffeines',
#         )
#         extra_kwargs = {
#             'url': {'lookup_field': 'username'},
#         }
# 
#     def get_name(self, obj):
#         return obj.get_full_name()
# 
#     def get_counts(self, obj):
#         count_items = Caffeine.objects.total_caffeine_for_user(obj)
#         return count_items
# 
# Path: coffeestats/caffeine_api_v2/permissions.py
# class IsOwnerOrReadOnly(permissions.BasePermission):
#     """
#     Custom permission to only allow owners of an object to edit it.
# 
#     """
#     def has_object_permission(self, request, view, obj):
#         if request.method in permissions.SAFE_METHODS:
#             return True
# 
#         return obj.user == request.user
# 
# class IsOwnCaffeineOrReadOnly(permissions.BasePermission):
#     """
#     Custom permission to only allow operations on own caffeine items.
# 
#     """
#     def has_permission(self, request, view):
#         if request.method in permissions.SAFE_METHODS:
#             return True
#         return view.view_owner == request.user


# Path: coffeestats/caffeine_api_v2/views.py
from django.contrib.auth.mixins import LoginRequiredMixin
from django.views.generic import TemplateView
from rest_framework import permissions, viewsets
from caffeine.models import Caffeine, User
from .serializers import (
    CaffeineSerializer,
    UserCaffeineSerializer,
    UserSerializer,
)
from .permissions import IsOwnerOrReadOnly, IsOwnCaffeineOrReadOnly




class CaffeineViewSet(viewsets.ReadOnlyModelViewSet):
    """
    API endpoint that allows caffeine entries to be viewed.
    """
    queryset = Caffeine.objects.all().order_by('-date')
    serializer_class = CaffeineSerializer


class UserViewSet(viewsets.ReadOnlyModelViewSet):
    """
    API endpoint that allows users to be viewed.
    """
    queryset = User.objects.all().order_by('username')
    serializer_class = UserSerializer
    lookup_field = 'username'


class UserCaffeineViewSet(viewsets.ModelViewSet):
    """
    API endpoint that allows working with a users caffeine entries.
    """
    serializer_class = UserCaffeineSerializer
    permission_classes = (permissions.IsAuthenticatedOrReadOnly,

                          IsOwnCaffeineOrReadOnly,