-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from HE-Arc/dev
add tag modification for the user
- Loading branch information
Showing
16 changed files
with
188 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
from .like import LikeCreateView | ||
from .user import UserView | ||
from .tag import TagListView | ||
from .like import LikeView | ||
from .user import UserView, UserTagView | ||
from .tag import TagView | ||
from .picture import PictureView |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,54 @@ | ||
from rest_framework.views import APIView | ||
from rest_framework.response import Response | ||
from django.shortcuts import get_object_or_404 | ||
from rest_framework import status | ||
from ..models import Tag | ||
from ..serializers import TagSerializer | ||
from ..models import User | ||
|
||
|
||
class TagListView(APIView): | ||
class TagView(APIView): | ||
|
||
def get(self, request): | ||
tags = Tag.objects.all() | ||
serializer = TagSerializer(tags, many=True) | ||
return Response(serializer.data) | ||
|
||
def post(self, request): | ||
|
||
serializer = TagSerializer(data=request.data) | ||
|
||
if not serializer.is_valid(): | ||
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) | ||
|
||
user = request.user | ||
|
||
tag_name = serializer.validated_data['name'] | ||
tag = get_object_or_404(Tag, name=tag_name) | ||
|
||
if user in tag.users.all(): | ||
return Response({'message': 'User already has tag'}, status=status.HTTP_400_BAD_REQUEST) | ||
|
||
tag.users.add(user) | ||
|
||
return Response({'message': 'Tag added to user'}, status=status.HTTP_201_CREATED) | ||
|
||
|
||
def delete(self, request): | ||
|
||
serializer = TagSerializer(data=request.data) | ||
|
||
if not serializer.is_valid(): | ||
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) | ||
|
||
user = request.user | ||
|
||
tag_name = serializer.validated_data['name'] | ||
tag = get_object_or_404(Tag, name=tag_name) | ||
|
||
if user not in tag.users.all(): | ||
return Response({'message': 'User does not have tag'}, status=status.HTTP_400_BAD_REQUEST) | ||
|
||
tag.users.remove(user) | ||
|
||
return Response({'message': 'Tag removed from user'}, status=status.HTTP_204_NO_CONTENT) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.