class UserJoinSerializer(serializers.ModelSerializer): user = UserSerializer(read_only=True) group = UserGroupSerializer(read_only=True) class Meta: model = User_Group fields = '__all__'
def post(self, request): username = request.data.get("username", None) password = request.data.get("password", None) user_object = User.objects.filter( username=username).first() or User.objects.filter( email=username).first() if not user_object: return Response(data={ 'title': 'Not found', 'message': 'User with given username and password does not exists', 'error_code': 100 }, status=400) user = authenticate(username=user_object.username, password=password) if user is None: return Response(data={ 'title': 'Not found', 'message': 'User with given username and password does not exists', 'error_code': 100 }, status=400) if not user.is_active: return Response(data={ 'title': 'Deactivated user', 'message': 'That user is deactivated', 'error_code': 101 }, status=400) if user.is_staff: return Response(data={ 'title': 'Invalid admin user', 'message': 'That user is admin user', 'error_code': 102 }, status=400) try: payload = JWT_PAYLOAD_HANDLER(user) jwt_token = JWT_ENCODE_HANDLER(payload) update_last_login(None, user) except User.DoesNotExist: return Response(data={ 'title': 'Not found', 'message': 'User with given username and password does not exists', 'error_code': 100 }, status=400) return Response(data={ 'success': True, 'token': jwt_token, 'user': UserSerializer(user).data }, status=200)
class MusteriGirisiSerializer(serializers.ModelSerializer): sube_adresi = serializers.CharField(source='secili_sube.sube_ismi') calisan = UserSerializer() musteri_bilgiler = MusteriSerializer(source="musteri") class Meta: model = MusteriGirisi fields = [ "calisan", "musteri_bilgiler", "id", "secili_sube", "sube_adresi", "hizmet_turu", "giris_tarih", "cikis_tarih", "ucret", "prim", "odeme_yontemi" ]
def post(self, request, *args, **kwargs): username = self.request.data['username'] email = self.request.data['email'] password = self.request.data['password'] if User.objects.filter(username=username).first(): return Response(data={ 'title': 'Already Exist', 'message': 'There is already user that has same username', 'error_code': 100 }, status=400) if User.objects.filter(email=email).first(): return Response(data={ 'title': 'Already Exist', 'message': 'There is already user that has same email', 'error_code': 101 }, status=400) user = User.objects.create_user( username=username, email=email, password=password, ) try: payload = JWT_PAYLOAD_HANDLER(user) jwt_token = JWT_ENCODE_HANDLER(payload) update_last_login(None, user) except User.DoesNotExist: return Response(data={ 'title': 'Login error', 'message': 'Something went wrong. Please try again later', 'error_code': 102 }, status=400) return Response(data={ 'success': True, 'token': jwt_token, 'user': UserSerializer(user).data }, status=200)
def get(self, request, *args, **kwargs): username = self.request.GET.get('username') user = User.objects.filter(username=username).first() if not user: return Response(data={ 'title': 'No User', 'message': 'There is not user that has same username', 'error_code': 100 }, status=400) if not self.request.user or self.request.user.id != user.id: ViewLog.objects.create(user=user, ) return Response(data=UserSerializer(user, context={ "request": request }).data, status=200)
class PostSerializer(serializers.ModelSerializer): postFrom = UserSerializer(read_only=True) postFromId = serializers.PrimaryKeyRelatedField( queryset=get_user_model().objects.all(), write_only=True) class Meta: model = Post fields = ('id', 'postFrom', 'postFromId', 'content') extra_kwargs = { 'postFrom': { 'read_only': True }, } def create(self, validated_data): validated_data['postFrom'] = validated_data.get('postFromId', None) if validated_data['postFrom'] is None: raise serializers.ValidationError("user not found.") del validated_data['postFromId'] return Post.objects.create(**validated_data)
class BoardJoinSerializer(serializers.ModelSerializer): user = UserSerializer(read_only=True) class Meta: model = Board fields = '__all__'