コード例 #1
0
 def post(self, request, format=None):
     serializer = AccountsSerializer(data=request.data,
                                     context={'request': request})
     if (serializer.is_valid()):
         serializer.save()
         return Response(serializer.data, status=status.HTTP_201_CREATED)
     return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
コード例 #2
0
 def put(self, request, pk, format=None):
     user = self.get_user(pk)
     serializer = AccountsSerializer(user,
                                     data=request.data,
                                     context={'request':
                                              request})  # level 2
     if serializer.is_valid():
         serializer.save()
         return Response(serializer.data)
     return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
コード例 #3
0
class PostSerializer(serializers.ModelSerializer):
    user = AccountsSerializer(read_only=True)
    user_id = serializers.PrimaryKeyRelatedField(
        queryset=Accounts.objects.all(), write_only=True)
    tasks = TaskSerializer(many=True)
    comments = CommentSerializer(many=True, read_only=True)
    favorites = FavoriteSerializer(many=True, read_only=True)

    class Meta:
        model = Post
        fields = [
            'id', 'title', 'detail', 'limit', 'user', 'user_id', 'tasks',
            'comments', 'favorites'
        ]

    def create(self, validated_data):
        tasks_data = validated_data.pop('tasks')
        validated_data['user'] = validated_data.get('user_id', None)
        if validated_data['user'] is None:
            raise serializers.ValidationError("user not found.")
        del validated_data['user_id']

        post = Post.objects.create(**validated_data)
        for task_data in tasks_data:
            Task.objects.create(post=post, **task_data)
        return post
コード例 #4
0
class UserProfileSerializer(serializers.ModelSerializer):

    accounts_linked = AccountsSerializer(many=True)
    cards_linked = CardSerializer(many=True)
    transactions_linked = TransactionsSerializer(many=True)
    tickets_linked = TicketSerializer(many=True)
    wallets_linked = WalletSerializer(many=True)

    class Meta:
        model = UserProfile
        fields = ('first_name', 'last_name', 'email_address', 'phone_number',
                  'alternate_phone', 'kyc_verified', 'profile_pic',
                  'user_joined_date', 'accounts_linked', 'tickets_linked',
                  'transactions_linked', 'wallets_linked', 'cards_linked')
コード例 #5
0
class CommentSerializer(serializers.ModelSerializer):
    user = AccountsSerializer(read_only=True)
    user_id = serializers.PrimaryKeyRelatedField(
        queryset=Accounts.objects.all(), write_only=True)
    post_id = serializers.PrimaryKeyRelatedField(queryset=Post.objects.all(),
                                                 write_only=True)

    class Meta:
        model = Comment
        fields = ['id', 'content', 'user', 'user_id', 'post_id']

    def create(self, validated_data):
        validated_data['user'] = validated_data.get('user_id', None)
        if validated_data['user'] is None:
            raise serializers.ValidationError("user not found.")
        del validated_data['user_id']
        validated_data['post'] = validated_data.get('post_id', None)
        if validated_data['post'] is None:
            raise serializers.ValidationError("post not found.")
        del validated_data['post_id']
        comment = Comment.objects.create(**validated_data)
        return comment
コード例 #6
0
class TaskSerializer(EnumSupportSerializerMixin,
                     serializers.HyperlinkedModelSerializer):  # new one
    # AFTER USING META
    # owner = serializers.ReadOnlyField(source='owner.username')
    owner = AccountsSerializer(read_only=True)

    class Meta:
        model = Task
        fields = [
            'id', 'url', 'owner', 'created', 'title', 'details', 'status',
            'due_date'
        ]

    def create(self, validated_data):
        return Task.objects.create(**validated_data)

    def update(self, instance, validated_data):
        instance.title = validated_data.get('title', instance.title)
        instance.details = validated_data.get('details', instance.details)
        instance.owner = validated_data.get('owner', instance.owner)
        instance.save()

        return instance
コード例 #7
0
 def get(self, request, activation_key=None):
     account = Account.objects.get(activation_key=activation_key)
     if account and account.has_usable_password():
         return HttpResponse("account_already_active/invalid_key")
     serializer = AccountsSerializer(account)
     return Response(serializer.data)
コード例 #8
0
 def get(self, request):
     serializer = AccountsSerializer(request.user)
     return Response(serializer.data)
コード例 #9
0
class StoreSerializer(serializers.ModelSerializer):
    account = AccountsSerializer(required=False)

    class Meta:
        model = Store
        fields = '__all__'
コード例 #10
0
class FavoriteSerializer(serializers.ModelSerializer):
    user = AccountsSerializer(read_only=True)

    class Meta:
        model = Favorite
        fields = ['user']
コード例 #11
0
 def get(self, request, pk, format=None):
     user = self.get_user(pk)
     serializer = AccountsSerializer(user, context={'request': request})
     return Response(serializer.data)
コード例 #12
0
 def get(self, request, format=None):
     users = User.objects.all()
     serializer = AccountsSerializer(users,
                                     many=True,
                                     context={'request': request})
     return Response(serializer.data)