<commit_before># -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.contrib.auth import logout
from django.contrib.auth.tokens import default_token_generator
from django.utils.encoding import force_text
from django.utils.translation import ugettext_lazy as _
from rest_framework import status
from rest_framework.views import APIView
from rest_framework.generics import GenericAPIView
from rest_framework.permissions import AllowAny
from rest_framework.renderers import TemplateHTMLRenderer, JSONRenderer, BrowsableAPIRenderer
from rest_framework.response import Response
from rest_auth.views import LoginView as OriginalLoginView
from shop.models.cart import CartModel
from shop.rest.auth import PasswordResetSerializer, PasswordResetConfirmSerializer
from shop.middleware import get_user


class AuthFormsView(GenericAPIView):
    """
    Generic view to handle authetication related forms such as user registration
    """
    serializer_class = None
    form_class = None

    def post(self, request, *args, **kwargs):
        form = self.form_class(data=request.DATA, instance=request.user)
        if form.is_valid():
            form.save(request=request)
            return Response(form.data, status=status.HTTP_200_OK)
        return Response(dict(form.errors), status=status.HTTP_400_BAD_REQUEST)


class LoginView(OriginalLoginView):
    def login(self):
        """
        Logs in as the given user, and moves the items from the current to the new cart.
        """
        anonymous_cart = CartModel.objects.get_from_request(self.request)
        super(LoginView, self).login()
        authenticated_cart = CartModel.objects.get_from_request(self.request)
        anonymous_cart.items.update(cart=authenticated_cart)


class LogoutView(APIView):
    """
    Calls Django logout method and delete the auth Token assigned to the current User object.
    """
    permission_classes = (AllowAny,)

    def post(self, request):
        try:
            request.user.auth_token.delete()
        except:
            pass
        logout(request)
        request.user = get_user(request, True)
        return Response({'success': _("Successfully logged out.")}, status=status.HTTP_200_OK)


class PasswordResetView(GenericAPIView):
    """
    Calls Django Auth PasswordResetForm save method.<commit_msg>exempting login View from CSRF protection<commit_after># -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.contrib.auth import logout
from django.contrib.auth.tokens import default_token_generator
from django.views.decorators.csrf import csrf_exempt
from django.utils.encoding import force_text
from django.utils.translation import ugettext_lazy as _
from rest_framework import status
from rest_framework.views import APIView
from rest_framework.generics import GenericAPIView
from rest_framework.permissions import AllowAny
from rest_framework.renderers import TemplateHTMLRenderer, JSONRenderer, BrowsableAPIRenderer
from rest_framework.response import Response
from rest_auth.views import LoginView as OriginalLoginView
from shop.models.cart import CartModel
from shop.rest.auth import PasswordResetSerializer, PasswordResetConfirmSerializer
from shop.middleware import get_user


class AuthFormsView(GenericAPIView):
    """
    Generic view to handle authetication related forms such as user registration
    """
    serializer_class = None
    form_class = None

    def post(self, request, *args, **kwargs):
        form = self.form_class(data=request.DATA, instance=request.user)
        if form.is_valid():
            form.save(request=request)
            return Response(form.data, status=status.HTTP_200_OK)
        return Response(dict(form.errors), status=status.HTTP_400_BAD_REQUEST)


@csrf_exempt
class LoginView(OriginalLoginView):
    def login(self):
        """
        Logs in as the given user, and moves the items from the current to the new cart.
        This View is intentionally exempted from CSRF protection, because a) the Login Form
        retains an expired CSRF token immediately after logging out and b) because a
        malicious client can't abuse the Login Form anyway.
        """
        anonymous_cart = CartModel.objects.get_from_request(self.request)
        super(LoginView, self).login()
        authenticated_cart = CartModel.objects.get_from_request(self.request)
        anonymous_cart.items.update(cart=authenticated_cart)


class LogoutView(APIView):
    """
    Calls Django logout method and delete the auth Token assigned to the current User object.
    """
    permission_classes = (AllowAny,)

    def post(self, request):
        try:
            request.user.auth_token.delete()
        except:
            pass
        logout(request)
        request.user = get_user(request, True)
        return Response({'success': _("Successfully logged out.")}, status=status.HTTP_200_OK)


class PasswordResetView(GenericAPIView):
    """
    Calls Django Auth PasswordResetForm save method.
{'fraction_non_alphanumeric': 0.07264358615169102, 'fraction_numerical': 0.0040024014408645186, 'mean_word_length': 3.866601752677702, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6494968', 'n_tokens_mistral': 1357, 'n_tokens_neox': 1311, 'n_words': 393}