Skip to content

Commit

Permalink
Merge pull request #627 from bounswe/smtag-api
Browse files Browse the repository at this point in the history
User can add SM Tag
  • Loading branch information
ArslanArdavic authored Dec 18, 2023
2 parents 1d01ea6 + a76ce96 commit b146177
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 3 deletions.
23 changes: 23 additions & 0 deletions project/backend/api/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -969,6 +969,7 @@ def test_no_context_given(self):

self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)


def test_user_conversion(self):
url = reverse('promote_contributor')
data = {'cont_id': self.contributor.id}
Expand All @@ -990,3 +991,25 @@ def test_user_conversion(self):
response = self.client.delete(f'{url}?reviewer_id={self.contributor.id}')
self.assertEqual(response.status_code, 404)

class AddUserSemanticTagTestCase(TestCase):
def setUp(self):
self.client = APIClient()
self.user = User.objects.create_user(id=1, email='test@example.com', username='test@example.com', first_name='User',
last_name='Test')
self.basic_user = BasicUser.objects.create(user=self.user, bio='Hello')

self.basic_user_token = Token.objects.create(user=self.user)

self.client.credentials(HTTP_AUTHORIZATION=f"Token {self.basic_user_token.key}")

self.sm_tag = SemanticTag.objects.create(
wid="QXXX",
label="Test SM Tag"
)
def test_add_user_semantic_tag(self):
url = reverse('add_user_semantic_tag')
payload = {
'sm_tag_id': self.sm_tag.pk,
}
response = self.client.post(url, payload, format='json')
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
2 changes: 2 additions & 0 deletions project/backend/api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,6 @@
path('update_content_status/', update_content_status, name='update_content_status'),
path('promote_contributor/', promote_contributor, name='promote_contributor'),
path('demote_reviewer/', demote_reviewer, name='demote_reviewer'),
path('add_user_semantic_tag/', AddUserSemanticTag.as_view(), name='add_user_semantic_tag'),

]
29 changes: 26 additions & 3 deletions project/backend/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1097,8 +1097,7 @@ def update_content_status(request):
return Response(BasicUserSerializer(user).data, status=200)
except Exception as e:
return Response({'message': str(e)}, status=status.HTTP_400_BAD_REQUEST)



@authentication_classes((TokenAuthentication,))
@permission_classes((IsAuthenticated, IsAdmin))
@api_view(['POST'])
Expand Down Expand Up @@ -1134,4 +1133,28 @@ def demote_reviewer(request):

return Response("Reviewer does not exist!", status=status.HTTP_404_NOT_FOUND)
except Exception as e:
return Response(str(e), status=500)
return Response(str(e), status=500)

class AddUserSemanticTag(APIView):
authentication_classes = (TokenAuthentication,)
permission_classes = (IsAuthenticated,)

def post(self, request):
sm_tag_id = request.data.get('sm_tag_id')
if not sm_tag_id:
return Response({"error": "Semantic Tag ID is required."}, status=status.HTTP_400_BAD_REQUEST)
try:
user = BasicUser.objects.get(pk=request.user.basicuser.pk)
except:
return Response({"error": "User not found."}, status=status.HTTP_404_NOT_FOUND)
try:
sm_tag = SemanticTag.objects.get(pk=sm_tag_id)
except:
return Response({"error": "Semantic Tag not found."}, status=status.HTTP_404_NOT_FOUND)

if user.semantic_tags.filter(pk=sm_tag_id).exists():
return Response({"error": "User already has this Semantic Tag."}, status=status.HTTP_400_BAD_REQUEST)

user.semantic_tags.add(sm_tag)
return Response({"message": "Semantic Tag successfully added to user."}, status=status.HTTP_201_CREATED)

0 comments on commit b146177

Please sign in to comment.