<commit_before>from rest_framework.views import APIView
from rest_framework.response import Response
from api.serializers import *
from api.models import *
from rest_framework.response import Response
from rest_framework import authentication, generics, viewsets, status, decorators, permissions
from rest_framework.permissions import IsAdminUser, AllowAny
from django.contrib.auth import get_user_model
from rest_framework_jwt.settings import api_settings
from django.contrib.auth import authenticate
from Library.permissions import IsOwnerOrAdmin, IsAuthenticatedOrCreate

jwt_payload_handler = api_settings.JWT_PAYLOAD_HANDLER
jwt_encode_handler = api_settings.JWT_ENCODE_HANDLER
jwt_decode_handler = api_settings.JWT_DECODE_HANDLER
User = get_user_model()


class UserViewSet(viewsets.ModelViewSet):
    """
    create: Create New User And Get Token
    login: Username And Password To ExChange Token
    """
    lookup_field = 'pk'
    queryset = User.objects.all()

    def get_permissions(self):
        if self.action in ('login', 'create'):
            self.permission_classes = [AllowAny, ]
        elif self.action in ('destroy',):
            self.permission_classes = [IsAdminUser, ]
        return super(self.__class__, self).get_permissions()

    def get_serializer_class(self):
        if self.action == 'login':
            return LoginSerializer
        return UserSerializer

    def get_queryset(self):
        if self.request.user.is_staff:
            return User.objects.all()
        else:
            return User.objects.filter(pk=self.request.user.pk)

    def create(self, request, *args, **kwargs):
        serializer = self.get_serializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        user = User(**serializer.data)
        user.save()
        payload = jwt_payload_handler(user)
        data = dict()
        data['token'] = jwt_encode_handler(payload)
        return Response(data=data, status=status.HTTP_201_CREATED)

    @decorators.list_route(methods=['post'])
    def login(self, request):
        serializer = LoginSerializer(data=request.data)
        serializer.is_valid(raise_exception=False)
        # try:
        username = serializer.data['username']
        password = serializer.data['password']
        data = {}
        try:
            user = User.objects.get(username=username)
        except:
            data['error'] = 'No This User'
            return Response(data=data, status=status.HTTP_401_UNAUTHORIZED)
        user = authenticate(username=username, password=password)<commit_msg>fix bug<commit_after>from rest_framework.views import APIView
from rest_framework.response import Response
from api.serializers import *
from api.models import *
from rest_framework.response import Response
from rest_framework import authentication, generics, viewsets, status, decorators, permissions
from rest_framework.permissions import IsAdminUser, AllowAny
from django.contrib.auth import get_user_model
from rest_framework_jwt.settings import api_settings
from django.contrib.auth import authenticate
from Library.permissions import IsOwnerOrAdmin, IsAuthenticatedOrCreate

jwt_payload_handler = api_settings.JWT_PAYLOAD_HANDLER
jwt_encode_handler = api_settings.JWT_ENCODE_HANDLER
jwt_decode_handler = api_settings.JWT_DECODE_HANDLER
User = get_user_model()


class UserViewSet(viewsets.ModelViewSet):
    """
    create: Create New User And Get Token
    login: Username And Password To ExChange Token
    """
    lookup_field = 'pk'
    queryset = User.objects.all()

    def get_permissions(self):
        if self.action in ('login', 'create'):
            self.permission_classes = [AllowAny, ]
        elif self.action in ('destroy',):
            self.permission_classes = [IsAdminUser, ]
        return super(self.__class__, self).get_permissions()

    def get_serializer_class(self):
        if self.action == 'login':
            return LoginSerializer
        return UserSerializer

    def get_queryset(self):
        if self.request.user.is_staff:
            return User.objects.all()
        else:
            return User.objects.filter(pk=self.request.user.pk)

    def create(self, request, *args, **kwargs):
        serializer = self.get_serializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        user = User(**serializer.data)
        user.set_password(serializers.data['password'])
        user.save()
        payload = jwt_payload_handler(user)
        data = dict()
        data['token'] = jwt_encode_handler(payload)
        return Response(data=data, status=status.HTTP_201_CREATED)

    @decorators.list_route(methods=['post'])
    def login(self, request):
        serializer = LoginSerializer(data=request.data)
        serializer.is_valid(raise_exception=False)
        # try:
        username = serializer.data['username']
        password = serializer.data['password']
        data = {}
        try:
            user = User.objects.get(username=username)
        except:
            data['error'] = 'No This User'
            return Response(data=data, status=status.HTTP_401_UNAUTHORIZED)
        user = authenticate(username=username, password=password)
{'fraction_non_alphanumeric': 0.09048902579899884, 'fraction_numerical': 0.0023103581055063534, 'mean_word_length': 3.421276595744681, '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': '6107380', 'n_tokens_mistral': 1479, 'n_tokens_neox': 1451, 'n_words': 321}