Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

user semantic tag add/remove implemented #711

Merged
merged 10 commits into from
Dec 25, 2023
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions project/backend/api/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1207,21 +1207,21 @@ def tearDown(self):
print("All tests for setting/removing workspace proof are completed!")


def test_set_workspace_proof(self):
url = reverse('set_workspace_proof')
data = {
'workspace_id': self.workspace.workspace_id,
'entry_id': self.entry.entry_id,
'is_disproof': True,
}

response = self.client.post(url, data=data)

self.assertEqual(response.status_code, 200, response.json())
self.assertEqual(Workspace.objects.get(pk=self.workspace.workspace_id).proof_entry.entry_id, self.entry.entry_id)
self.assertEqual(Entry.objects.get(pk=self.entry.entry_id).is_proof_entry, True)
self.assertEqual(Entry.objects.get(pk=self.entry.entry_id).is_disproof_entry, True)
# def test_set_workspace_proof(self):
# url = reverse('set_workspace_proof')
# data = {
# 'workspace_id': self.workspace.workspace_id,
# 'entry_id': self.entry.entry_id,
# 'is_disproof': True,
# }
#
# response = self.client.post(url, data=data)
#
# self.assertEqual(response.status_code, 200, response.json())
#
# self.assertEqual(Workspace.objects.get(pk=self.workspace.workspace_id).proof_entry.entry_id, self.entry.entry_id)
# self.assertEqual(Entry.objects.get(pk=self.entry.entry_id).is_proof_entry, True)
# self.assertEqual(Entry.objects.get(pk=self.entry.entry_id).is_disproof_entry, True)


def test_remove_workspace_proof(self):
Expand Down
1 change: 1 addition & 0 deletions project/backend/api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
path('add_user_semantic_tag/', AddUserSemanticTag.as_view(), name='add_user_semantic_tag'),
path('add_semantic_tag/', SemanticTagAPIView.as_view(), name='add_semantic_tag'),
path('remove_workspace_tag/', remove_workspace_tag, name='remove_workspace_tag'),
path('remove_user_tag/', remove_user_tag, name='remove_user_tag'),
path('get_related_nodes/', get_related_nodes, name='get_related_nodes'),
path('reset_workspace_state/', reset_workspace_state, name='reset_workspace_state'),

Expand Down
63 changes: 52 additions & 11 deletions project/backend/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,12 +197,51 @@ def remove_workspace_tag(request):

if is_workspace_contributor(request):
workspace = Workspace.objects.get(workspace_id=workspace_id)
tag = SemanticTag.objects.filter(id=tag_id)
if not tag.exists():
return JsonResponse({'message': 'There is no tag with this id.'}, status=404)
if tag[0] not in workspace.semantic_tags.all():
return JsonResponse({'message': 'There is no tag with this id in this workspace.'}, status=404)
workspace.semantic_tags.remove(tag_id)
workspace.save()
return JsonResponse({'message': 'Tag is successfully removed from workspace.'}, status=200)
else:
return JsonResponse({'message': "You don't have permission to do this!"}, status=403)


@csrf_exempt
def remove_user_tag(request):
tag_id = request.POST.get("tag_id")
res = BasicUserDetailAPI.as_view()(request)
try:
user = BasicUser.objects.filter(id=request.user.basicuser.id)
except:
return JsonResponse({'message': "invalid credentials."}, status=403)
if not user.exists():
return JsonResponse({'message': "invalid credentials."}, status=403)
if tag_id == None or tag_id == '':
return JsonResponse({'message': 'tag_id field can not be empty'}, status=400)
try:
tag_id = int(tag_id)
except:
return JsonResponse({'message': 'tag_id field has to be an integer'}, status=400)
tag = SemanticTag.objects.filter(id=tag_id)
if not tag.exists():
return JsonResponse({'message': 'There is no tag with this id.'}, status=404)
tag = tag[0]
if tag not in request.user.basicuser.semantic_tags.all():
return JsonResponse({'message': 'This user does not have this tag.'}, status=404)
request.user.basicuser.semantic_tags.remove(tag)
request.user.basicuser.save()
return JsonResponse({'message': 'Tag is successfully removed from user.'}, status=200)








def search(request):
res = BasicUserDetailAPI.as_view()(request)
try:
Expand Down Expand Up @@ -339,7 +378,7 @@ def search(request):
if not node.removed_by_admin or is_admin:
nodes.append(node.node_id)
for rel_node in related_nodes_q:
if not node.removed_by_admin or is_admin:
if not rel_node.removed_by_admin or is_admin:
nodes.append(rel_node.node_id)

if search_type == 'random':
Expand Down Expand Up @@ -579,14 +618,15 @@ def get_workspace_from_id(request):
reviewer = Reviewer.objects.filter(pk=request.user.basicuser)
cont = Contributor.objects.get(pk=request.user.basicuser)
reviewer_flag = True
pending = False
pending_reviewer = False
pending_collab = False
workspace = workspace[0]
if reviewer.exists():
for req in ReviewRequest.objects.filter(receiver=cont):
if req.workspace.workspace_id == workspace.workspace_id and req.status == 'P':
reviewer_flag = False
request_id = req.id
pending = True
pending_reviewer = True
if workspace in reviewer[0].review_workspaces.all():
cont = Contributor.objects.filter(pk=request.user.basicuser)[0]
requests = ReviewRequest.objects.filter(workspace=workspace)
Expand All @@ -599,7 +639,7 @@ def get_workspace_from_id(request):
for req in CollaborationRequest.objects.filter(receiver=cont):
if req.workspace.workspace_id == workspace.workspace_id and req.status == 'P':
collab_flag = False
pending = True
pending_collab = True
request_id = req.id
# collab_comment = req.comment
if reviewer_flag and collab_flag:
Expand Down Expand Up @@ -643,12 +683,12 @@ def get_workspace_from_id(request):
"first_name": user.first_name,
"last_name": user.last_name,
"username": user.username})
pending = []
pending_cont = []
for pend in CollaborationRequest.objects.filter(workspace=workspace):
cont = pend.receiver
user = User.objects.get(id=cont.user_id)
if pend.status == 'P':
pending.append({"id": cont.id,
pending_cont.append({"id": cont.id,
'request_id':pend.id,
"first_name": user.first_name,
"last_name": user.last_name,
Expand Down Expand Up @@ -694,13 +734,14 @@ def get_workspace_from_id(request):
'num_approvals':workspace.num_approvals,
'semantic_tags':semantic_tags,
'contributors':contributors,
'pending_contributors':pending,
'pending_contributors':pending_cont,
'references':references,
'created_at':workspace.created_at,
'from_node_id' : node_id,
'request_id' : request_id,
'comments':comments,
'pending':pending,
'pending_reviewer':pending_reviewer,
'pending_collab':pending_collab,
}, status=200)

def get_semantic_suggestion(request):
Expand Down Expand Up @@ -742,7 +783,7 @@ def change_workspace_title(request):
def set_workspace_proof(request):
entry_id = request.POST.get("entry_id")
workspace_id = request.POST.get("workspace_id")
is_disproof = request.POST.get("is_disproof")
# is_disproof = request.POST.get("is_disproof")
if entry_id == None or entry_id == '':
return JsonResponse({'message': 'entry_id field can not be empty'}, status=400)
try:
Expand Down Expand Up @@ -778,8 +819,8 @@ def set_workspace_proof(request):
workspace.proof_entry.is_proof_entry = False
workspace.proof_entry = entry
entry.is_proof_entry = True
if is_disproof:
entry.is_disproof_entry= True
# if is_disproof:
# entry.is_disproof_entry= True
entry.save()
workspace.save()
return JsonResponse({'message': 'Proof entry is successfully set.'}, status=200)
Expand Down
4 changes: 2 additions & 2 deletions project/backend/database/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@

class SemanticTag(models.Model):
created_at = models.DateTimeField(auto_now_add=True)
wid = models.CharField(max_length=20)
label = models.CharField(max_length=30)
wid = models.CharField(max_length=20,unique=False)
label = models.CharField(max_length=30,unique=False)

@property
def nodes(self):
Expand Down
16 changes: 16 additions & 0 deletions project/backend/database/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,19 @@ def create(self, validated_data):
wid = validated_data.get('wid', None)
label = validated_data.get('label', None)
workspace_id = self.context['request'].data.get('workspace_id', None)
user_id = self.context['request'].data.get('user_id', None)

tag = SemanticTag.objects.create(wid=wid, label=label)

if workspace_id is not None:
workspace = Workspace.objects.get(workspace_id=workspace_id)
workspace.semantic_tags.add(tag)
workspace.save()

if user_id is not None:
user = BasicUser.objects.get(id=user_id)
user.semantic_tags.add(tag)
user.save()

return tag

Expand Down Expand Up @@ -265,6 +272,14 @@ class Meta:
model = Node
fields = ['node_id', 'node_title', 'contributors', 'publish_date']

# Serializer for Semantic Tags
class NodeViewSemanticTagSerializer(serializers.ModelSerializer):
class Meta:
model = SemanticTag
fields = ['wid', 'id', 'label']



# Serializer to Node
class NodeSerializer(serializers.ModelSerializer):
to_referenced_nodes = NodeViewReferenceSerializer(many=True)
Expand All @@ -273,6 +288,7 @@ class NodeSerializer(serializers.ModelSerializer):
theorem = NodeViewTheoremSerializer()
question_set = NodeViewQuestionSerializer(many=True)
contributors = NodeViewBasicUserSerializer(many=True)
semantic_tags = NodeViewSemanticTagSerializer(many=True)
class Meta:
model = Node
fields = ['node_id', 'node_title', 'publish_date', 'is_valid', 'num_visits' , 'theorem', 'contributors',
Expand Down