25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 
 

60 satır
1.9 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 PasswordViewSet(viewsets.ModelViewSet):
  35. serializer_class = serializers.PasswordSerializer
  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.Password.objects.filter(user=self.request.user)
  41. class EntryViewSet(viewsets.ModelViewSet):
  42. serializer_class = serializers.EntrySerializer
  43. permission_classes = (permissions.IsAuthenticated, IsOwner,)
  44. search_fields = ('site', 'email',)
  45. ordering_fields = ('site', 'email', 'created')
  46. def get_queryset(self):
  47. return models.Entry.objects.filter(user=self.request.user)