<reponame>julianwagle/cookiecutter-django-react<gh_stars>1-10
from django.contrib.auth import get_user_model
from django.utils.translation import ugettext_lazy as _

from allauth.account.views import ConfirmEmailView

from rest_framework import status
from rest_framework.decorators import action
from rest_framework.mixins import ListModelMixin, RetrieveModelMixin, UpdateModelMixin
from rest_framework.permissions import AllowAny
from rest_framework.response import Response
from rest_framework.viewsets import GenericViewSet
from rest_framework.views import APIView


from {{ cookiecutter.project_slug }}.users.api.serializers import UserSerializer

User = get_user_model()

class UserViewSet(RetrieveModelMixin, ListModelMixin, UpdateModelMixin, GenericViewSet):
    serializer_class = UserSerializer
    queryset = User.objects.all()
    lookup_field = "username"

    def get_queryset(self, *args, **kwargs):
        return self.queryset.filter(id=self.request.user.id)

    @action(detail=False, methods=["GET"])
    def me(self, request):
        serializer = UserSerializer(request.user, context={"request": request})
        return Response(status=status.HTTP_200_OK, data=serializer.data)

# https://github.com/iMerica/dj-rest-auth/blob/master/dj_rest_auth/registration/views.py
class VerifyEmailView(APIView, ConfirmEmailView):
    permission_classes = (AllowAny,)
    allowed_methods = ('GET', 'POST', 'OPTIONS', 'HEAD')

    def get(self, *args, **kwargs):
        self.kwargs['key'] = str(self.kwargs['key'])
        confirmation = self.get_object()
        confirmation.confirm(self.request)
        return Response({'detail': _('ok')}, status=status.HTTP_200_OK)
