<commit_before>import uuid

from django.contrib.auth.hashers import make_password
from django.contrib.auth.models import User

from rest_framework import serializers, viewsets, status, permissions
from rest_framework.generics import CreateAPIView
from rest_framework.response import Response

from zchat.permissions import IsOwnerOrReadOnly
from zmessages.models import Message, MobilePushToken, Room


class UserSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = ('id', 'first_name', 'last_name', 'username', 'email')


class MessageSerializer(serializers.HyperlinkedModelSerializer):
    author = UserSerializer(read_only=True)

    class Meta:
        model = Message
        fields = ('author', 'msg', 'timestamp')


class MyMessageSerializer(serializers.HyperlinkedModelSerializer):

    class Meta:
        model = Message
        fields = ('msg', 'timestamp')


class MyMobilePushTokenSerializer(serializers.HyperlinkedModelSerializer):

    class Meta:
        model = MobilePushToken
        fields = ('token', 'timestamp')


class NewUserSerializer(serializers.ModelSerializer):
    password = serializers.CharField(max_length=254, min_length=5)

    class Meta:
        model = User
        fields = ('first_name', 'last_name', 'username', 'email', 'password')


class UserSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = User
        fields = ('url', 'first_name', 'last_name', 'username', 'email', 'is_staff')


class RoomSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = Room
        fields = '__all__'
        extra_kwargs = {'creator': {'read_only': True}}


class MessageViewSet(viewsets.ReadOnlyModelViewSet):
    queryset = Message.objects.all()
    serializer_class = MessageSerializer
    # permission_classes = (permissions.IsAuthenticatedOrReadOnly, IsOwnerOrReadOnly,)


class MyMessageViewSet(viewsets.ModelViewSet):
    serializer_class = MyMessageSerializer
    # permission_classes = (permissions.IsAuthenticatedOrReadOnly, IsOwnerOrReadOnly,)

    def perform_create(self, serializer):
        msg = serializer.validated_data['msg']
        msg['uuid'] = str(uuid.uuid4())
        serializer.save(author=self.request.user, msg=msg)

    def get_queryset(self):
        queryset = Message.objects.all().filter(author_id=self.request.user.id)
        return queryset


class MyMobilePushTokenViewSet(viewsets.ModelViewSet):
    serializer_class = MyMobilePushTokenSerializer

    def perform_create(self, serializer):
        serializer.save(user=self.request.user)

    def get_queryset(self):
        return (
            self.serializer_class.Meta.
            model.objects.all().
            filter(user_id=self.request.user.id)
        )


class AddUserView(CreateAPIView):
    queryset = User.objects.all()
    serializer_class = NewUserSerializer
    permission_classes = (permissions.AllowAny,)

    def create(self, request, *args, **kwargs):
        serializer = self.get_serializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        self.perform_create(serializer)
        headers = self.get_success_headers(serializer.data)
        return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)

    def perform_create(self, serializer):
        password = <PASSWORD>(serializer.validated_data['password'])
        serializer.save(password=password)


class UserViewSet(viewsets.ReadOnlyModelViewSet):<commit_msg>fix authentification for add user<commit_after>import uuid

from django.contrib.auth.hashers import make_password
from django.contrib.auth.models import User

from rest_framework import serializers, viewsets, status, permissions, authentication
from rest_framework.generics import CreateAPIView
from rest_framework.response import Response

from zchat.permissions import IsOwnerOrReadOnly
from zmessages.models import Message, MobilePushToken, Room


class UserSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = ('id', 'first_name', 'last_name', 'username', 'email')


class MessageSerializer(serializers.HyperlinkedModelSerializer):
    author = UserSerializer(read_only=True)

    class Meta:
        model = Message
        fields = ('author', 'msg', 'timestamp')


class MyMessageSerializer(serializers.HyperlinkedModelSerializer):

    class Meta:
        model = Message
        fields = ('msg', 'timestamp')


class MyMobilePushTokenSerializer(serializers.HyperlinkedModelSerializer):

    class Meta:
        model = MobilePushToken
        fields = ('token', 'timestamp')


class NewUserSerializer(serializers.ModelSerializer):
    password = serializers.CharField(max_length=254, min_length=5)

    class Meta:
        model = User
        fields = ('first_name', 'last_name', 'username', 'email', 'password')


class UserSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = User
        fields = ('url', 'first_name', 'last_name', 'username', 'email', 'is_staff')


class RoomSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = Room
        fields = '__all__'
        extra_kwargs = {'creator': {'read_only': True}}


class MessageViewSet(viewsets.ReadOnlyModelViewSet):
    queryset = Message.objects.all()
    serializer_class = MessageSerializer
    # permission_classes = (permissions.IsAuthenticatedOrReadOnly, IsOwnerOrReadOnly,)


class MyMessageViewSet(viewsets.ModelViewSet):
    serializer_class = MyMessageSerializer
    # permission_classes = (permissions.IsAuthenticatedOrReadOnly, IsOwnerOrReadOnly,)

    def perform_create(self, serializer):
        msg = serializer.validated_data['msg']
        msg['uuid'] = str(uuid.uuid4())
        serializer.save(author=self.request.user, msg=msg)

    def get_queryset(self):
        queryset = Message.objects.all().filter(author_id=self.request.user.id)
        return queryset


class MyMobilePushTokenViewSet(viewsets.ModelViewSet):
    serializer_class = MyMobilePushTokenSerializer

    def perform_create(self, serializer):
        serializer.save(user=self.request.user)

    def get_queryset(self):
        return (
            self.serializer_class.Meta.
            model.objects.all().
            filter(user_id=self.request.user.id)
        )


class AddUserView(CreateAPIView):
    queryset = User.objects.all()
    serializer_class = NewUserSerializer
    permission_classes = (permissions.AllowAny,)
    authentication_classes = (authentication.BasicAuthentication,)

    def create(self, request, *args, **kwargs):
        serializer = self.get_serializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        self.perform_create(serializer)
        headers = self.get_success_headers(serializer.data)
        return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)

    def perform_create(self, serializer):
        password = <PASSWORD>(serializer.validated_data['password'])
        serializer.save(password=password)


class UserViewSet(viewsets.ReadOnlyModelViewSet):
{'fraction_non_alphanumeric': 0.09712280701754386, 'fraction_numerical': 0.002245614035087719, 'mean_word_length': 4.046742209631728, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '6414460', 'n_tokens_mistral': 1999, 'n_tokens_neox': 1947, 'n_words': 392}