Skip to content

Commit

Permalink
Merge branch 'backend' into smtag-api
Browse files Browse the repository at this point in the history
  • Loading branch information
ArslanArdavic authored Dec 18, 2023
2 parents 7dcebf5 + 1d01ea6 commit a76ce96
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 4 deletions.
28 changes: 26 additions & 2 deletions project/backend/api/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,8 @@ def test_get_user_profile(self):
self.assertEqual(response.json()['asked_questions'][0]['answerer_id'], 1)
self.assertEqual(response.json()['asked_questions'][0]['answerer_mail'], 'test@example.com')

self.assertEqual(response.json()['user_type'], 'contributor')

class ProofGETAPITestCase(TestCase):
def setUp(self):
# Create a sample Proof instance for testing
Expand Down Expand Up @@ -952,12 +954,12 @@ def test_update_question_status(self):
def test_update_user_status(self):

url = reverse('update_content_status')
data = {'context': 'user', 'content_id': self.basic_user.pk, 'hide': False}
data = {'context': 'user', 'content_id': self.basic_user.id, 'hide': False}

response = self.client.put(url, data, format='json')

self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(BasicUser.objects.get(pk=self.basic_user.pk).user.is_active, False)
self.assertEqual(BasicUser.objects.get(pk=self.basic_user.id).user.is_active, False)

def test_no_context_given(self):
url = reverse('update_content_status')
Expand All @@ -967,6 +969,28 @@ 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}

response = self.client.post(url, data, format='json')
self.assertEqual(response.status_code, status.HTTP_201_CREATED, response.data)
self.assertEqual(len(Reviewer.objects.filter(id=self.contributor.id)), 1)

response = self.client.post(url, data, format='json')
self.assertEqual(response.status_code, 409)


url = reverse('demote_reviewer')

response = self.client.delete(f'{url}?reviewer_id={self.contributor.id}')
self.assertEqual(response.status_code, 204, response.data)
self.assertEqual(len(Reviewer.objects.filter(id=self.contributor.id)), 0)

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()
Expand Down
3 changes: 3 additions & 0 deletions project/backend/api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,8 @@
path('ask_question/', AskQuestion.as_view(), name='ask_question'),
path('answer_question/', AnswerQuestion.as_view(), name='answer_question'),
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'),

]
49 changes: 47 additions & 2 deletions project/backend/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,12 +345,21 @@ def get_profile(request):
authors.append({'name': user.first_name, 'surname': user.last_name, 'username': user.username})
node_infos.append({'id':node_id,'title':node.node_title,'date':node.publish_date,'authors':authors})

user_type = 'basic_user'
if Contributor.objects.filter(id=basic_user.id).exists():
user_type = 'contributor'
if Reviewer.objects.filter(id=basic_user.id).exists():
user_type = 'reviewer'
if Admin.objects.filter(id=basic_user.id).exists():
user_type = 'admin'

return JsonResponse({'name':user.first_name,
'surname':user.last_name,
'bio':basic_user.bio,
'nodes': node_infos,
'asked_questions':asked_questions,
'answered_questions':answered_questions},status=200)
'answered_questions':answered_questions,
'user_type': user_type},status=200)

def get_proof_from_id(request):
id = int(request.GET.get("proof_id"))
Expand Down Expand Up @@ -1088,6 +1097,43 @@ 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'])
def promote_contributor(request):
try:
cont_id = request.data.get("cont_id")
cont = Contributor.objects.filter(id=cont_id)
if cont.count():
cont = cont.first()
if Reviewer.objects.filter(id=cont_id).exists():
return Response("Already exists!", status=409)
reviewer = Reviewer(contributor_ptr_id=cont.id)
reviewer.__dict__.update(cont.__dict__)
reviewer.save()
return Response(ReviewerSerializer(reviewer).data, status=201)

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


@authentication_classes((TokenAuthentication,))
@permission_classes((IsAuthenticated, IsAdmin))
@api_view(['DELETE'])
def demote_reviewer(request):
try:
reviewer = Reviewer.objects.filter(id=request.query_params.get('reviewer_id'))
if reviewer.count():
reviewer = reviewer.first()
reviewer.delete(keep_parents=True)

return Response("Reviewer demoted to Contributor successfully", status=status.HTTP_204_NO_CONTENT)

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

class AddUserSemanticTag(APIView):
authentication_classes = (TokenAuthentication,)
Expand All @@ -1112,4 +1158,3 @@ def post(self, 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 a76ce96

Please sign in to comment.