from datetime import date, datetime
import time

from rest_framework.reverse import reverse
from rest_framework import viewsets
from rest_framework.response import Response
from rest_framework.decorators import api_view
from django.shortcuts import get_object_or_404
from allauth.socialaccount.models import SocialAccount
import hashlib
from django.http import HttpResponse

from PlanIt.serializers import CardSerializer
from PlanIt.models import Card


class CardsByDayViewSet(viewsets.ModelViewSet):
    serializer_class = CardSerializer
    pagination_class = None

    def get_object(self):
        pk = int(self.kwargs['pk'])
        return get_object_or_404(Card.objects, user=self.request.user, pk=pk)

    def get_queryset(self):
        day = datetime.strptime(self.kwargs['day'], '%Y-%m-%d').date()
        return Card.objects.filter(user=self.request.user, day=day)

    def perform_create(self, serializer):
        day = datetime.strptime(self.kwargs['day'], '%Y-%m-%d').date()
        serializer.save(user=self.request.user, day=day)

    def perform_update(self, serializer):
        day = datetime.strptime(self.kwargs['day'], '%Y-%m-%d').date()
        serializer.save(user=self.request.user, day=day)


class CardsInParkingLotViewSet(viewsets.ModelViewSet):
    serializer_class = CardSerializer
    pagination_class = None

    def get_object(self):
        pk = int(self.kwargs['pk'])
        return get_object_or_404(Card.objects, user=self.request.user, pk=pk)

    def get_queryset(self):
        return Card.objects.filter(user=self.request.user, day=None)

    def perform_create(self, serializer):
        serializer.save(user=self.request.user, day=None)

    def perform_update(self, serializer):
        serializer.save(user=self.request.user, day=None)


@api_view(['GET'])
def api_root(request, format=None):
    return Response({
        'cards-today': reverse(
            'cards-by-day-list', request=request, format=format,
            kwargs=dict(day=datetime.now().strftime('%Y-%m-%d'))),
        'cards-in-parking-lot': reverse(
            'cards-in-parking-lot-list', request=request, format=format)
    })

 
def photo(self):
    fb_uid = SocialAccount.objects.filter(user_id=self.user.id, provider='facebook')
 
    if len(fb_uid):
        return HttpResponse("http://graph.facebook.com/{}/picture?width=40&height=40".format(fb_uid[0].uid))
 
    return "http://www.gravatar.com/avatar/{}?s=40".format(hashlib.md5(self.user.email).hexdigest())

@@ -2145,24 +2145,57 @@
 %7D)%0A%0A
- %0Adef photo(self
+@api_view(%5B'GET'%5D)%0Adef photo(request, format=None
 ):%0A 
@@ -2243,20 +2243,23 @@
 user_id=
-self
+request
 .user.id
@@ -2303,20 +2303,17 @@
 b_uid):%0A
-    
+%09
     retu
@@ -2315,20 +2315,16 @@
  return 
-Http
 Response
@@ -2324,16 +2324,33 @@
 esponse(
+%7B%0A%09%09    'photo': 
 %22http://
@@ -2420,19 +2420,21 @@
 %5B0%5D.uid)
-)%0A 
+%0A%09%09%7D)
 %0A    ret
@@ -2436,16 +2436,45 @@
   return
+ Response (%7B%0A        'photo':
  %22http:/
@@ -2526,20 +2526,23 @@
 lib.md5(
-self
+request
 .user.em
@@ -2559,8 +2559,15 @@
 gest())%0A
+    %7D)%0A

move photo to rest API