class RestaurantSerializer(serializers.ModelSerializer): id = serializers.IntegerField(read_only=True) user = UserSerializer(read_only=True) class Meta: model = Restaurant fields = '__all__'
class TaskSerializer(serializers.ModelSerializer): id = serializers.IntegerField(read_only=True) task_list = TaskListSerializer(read_only=True) owner = UserSerializer(read_only=True) class Meta: model = Task fields = '__all__'
class OffersPurchasesSerializer(serializers.ModelSerializer): id = serializers.IntegerField(read_only=True) owner = UserSerializer(read_only=True) id_offer = OffersSerializer(read_only=True) class Meta: model = OffersPurchases fields = '__all__'
class TaskShortSerializer(serializers.ModelSerializer): is_deleted = serializers.BooleanField(read_only=True) project_id = serializers.IntegerField(write_only=True) executor = UserSerializer(read_only=True) creator = serializers.HiddenField(default=serializers.CurrentUserDefault()) class Meta: model = Task fields = ('id', 'name', 'document', 'status', 'is_deleted', 'project_id', 'executor', 'creator')
def signup(self, request): if request.method == 'POST': user_data = request.data new_user = User.objects.create(email=user_data['email'], username=user_data['username']) new_user.set_password(user_data['password']) new_user.save() serializer = UserSerializer(new_user) logger.debug(f'New user signed up, ID: {serializer.instance}') logger.info(f'New user signed up, ID: {serializer.instance}') return Response(serializer.data)
class TaskListSerializer(serializers.Serializer): id = serializers.IntegerField(read_only=True) name = serializers.CharField(required=True) owner = UserSerializer(read_only=True) def create(self, validated_data): task_list = TaskList(**validated_data) task_list.save() return task_list def update(self, instance, validated_data): instance.name = validated_data.get('name', instance.name) instance.save() return instance
def create_manager(self, request): if request.method == 'POST': user_data = request.data new_user = User.objects.create_superuser( email=user_data['email'], username=user_data['username'], password=user_data['password'], ) new_user.save() serializer = UserSerializer(new_user) logger.debug(f'New manager is created, ID: {serializer.instance}') logger.info(f'New manager is created, ID: {serializer.instance}') return Response(serializer.data)
def post(self, request, *args, **kwargs): try: serializer = RegisterUserSerializer(data=request.data) if serializer.is_valid(raise_exception=True): user = serializer.save() logger.info(f"{user.username} registered") ser = UserSerializer(instance=user) return JsonResponse(ser.data, status=status.HTTP_200_OK) else: logger.error(serializer.errors) return JsonResponse({'errors': str(serializer.errors)}, status=status.HTTP_400_BAD_REQUEST) except Exception as e: logger.error(f"{e} error") return JsonResponse({'error': str(e)}, status=status.HTTP_404_NOT_FOUND)
def signup(request): serializer = UserSerializer(data=request.data) serializer.is_valid(raise_exception=True) serializer.create() return Response(serializer.data)
class ProductReviewSerilalizer(serializers.ModelSerializer): user = UserSerializer(read_only=True) class Meta: model = ProductReview fields = ['id', 'user', 'review', 'rating']
def register_user(request): serializer = RegistrationSerializer(data=request.data) serializer.is_valid(raise_exception=True) user = serializer.register() return Response(UserSerializer(user).data, status=status.HTTP_201_CREATED)
def post(self, request): serializer = UserSerializer(data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def list(self, request): users = User.objects.all() serializer = UserSerializer(users, many=True) return Response(serializer.data, status.HTTP_200_OK)