You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

50 lines
1.5 KiB

  1. from api import models, serializers
  2. from api.permissions import IsOwner
  3. from django.contrib.auth import login, authenticate
  4. from rest_framework import status, permissions, viewsets
  5. from rest_framework.response import Response
  6. class AuthViewSet(viewsets.ViewSet):
  7. permission_classes = (permissions.AllowAny,)
  8. @staticmethod
  9. def list(request, format=None):
  10. if request.user.is_authenticated():
  11. user = {
  12. 'id': request.user.id,
  13. 'email': request.user.email,
  14. 'is_admin': request.user.is_staff,
  15. 'is_authenticated': True
  16. }
  17. else:
  18. user = {
  19. 'id': None,
  20. 'email': None,
  21. 'is_admin': False,
  22. 'is_authenticated': False
  23. }
  24. return Response({
  25. 'user': user
  26. })
  27. @staticmethod
  28. def post(request):
  29. user = authenticate(username=request.data.get('username'), password=request.data.get('password'))
  30. if user and user.is_active:
  31. login(request, user)
  32. return Response(status=status.HTTP_201_CREATED)
  33. return Response(status=status.HTTP_401_UNAUTHORIZED)
  34. class EntryViewSet(viewsets.ModelViewSet):
  35. serializer_class = serializers.EntrySerializer
  36. permission_classes = (permissions.IsAuthenticated, IsOwner,)
  37. search_fields = ('site', 'email',)
  38. ordering_fields = ('site', 'email', 'created')
  39. def get_queryset(self):
  40. return models.Entry.objects.filter(user=self.request.user)