diff --git a/project/backend/api/urls.py b/project/backend/api/urls.py index 5cb5f0aa..9f379413 100644 --- a/project/backend/api/urls.py +++ b/project/backend/api/urls.py @@ -17,6 +17,7 @@ path('get_cont/', get_contributor_from_id, name='get_cont'), path('get_user_workspaces/',get_workspaces,name='get_user_workspaces'), path('get_workspace/',get_workspace_from_id,name='get_workspace'), + path('edit_entry/', edit_entry, name='edit_entry'), path('delete_entry/',delete_entry,name='delete_entry'), path('add_entry/',add_entry,name='add_entry'), path('get_random_node_id/',get_random_node_id,name='get_random_node_id'), diff --git a/project/backend/api/views.py b/project/backend/api/views.py index 879a81d4..36eb034f 100644 --- a/project/backend/api/views.py +++ b/project/backend/api/views.py @@ -1,4 +1,5 @@ from django.shortcuts import get_object_or_404 +from django.views.decorators.csrf import csrf_exempt from rest_framework.views import APIView from rest_framework.decorators import api_view, permission_classes from rest_framework.response import Response @@ -299,10 +300,8 @@ def get_workspace_from_id(request): 'entry_number':entry.entry_number,}) semantic_tags = [] for tag in workspace.semantic_tags.all(): - semantic_tags.append({'label':tag.label, - 'desc':tag.desc, - 'parent_tag':tag.parent_tag, - 'created_at':tag.created_at}) + semantic_tags.append({'wid':tag.wid, + 'label':tag.label,}) contributors = [] for cont in Contributor.objects.filter(workspaces=workspace): user = User.objects.get(id=cont.user_id) @@ -351,83 +350,222 @@ def get_workspace_from_id(request): 'references':references, 'created_at':workspace.created_at, }, status=200) +@csrf_exempt def delete_entry(request): - id = int(request.GET.get("entry_id")) - entry = Entry.objects.filter(entry_id=id) + entry_id = request.POST.get("entry_id") + workspace_id = request.POST.get("workspace_id") + if entry_id == None or entry_id == '': + return JsonResponse({'message': 'entry_id field can not be empty'}, status=400) + try: + entry_id = int(entry_id) + except: + return JsonResponse({'message': 'entry_id field has to be a integer'}, status=400) + if workspace_id == None or workspace_id == '': + return JsonResponse({'message': 'workspace_id field can not be empty'}, status=400) + try: + workspace_id = int(workspace_id) + except: + return JsonResponse({'message': 'workspace_id field has to be a integer'}, status=400) + entry = Entry.objects.filter(entry_id=entry_id) if entry.count() == 0: return JsonResponse({'message': 'There is no entry with this id.'}, status=404) - entry.delete() + workspace = Workspace.objects.filter(workspace_id=workspace_id) + if workspace.count() == 0: + return JsonResponse({'message': 'There is no workspace with this id.'}, status=404) + workspace = Workspace.objects.get(workspace_id=workspace_id) + if entry[0] not in workspace.entries.all(): + return JsonResponse({'message': 'There is no entry with this id in this workspace.'}, status=404) + workspace.entries.remove(entry[0]) + workspace.save() + return JsonResponse({'message': 'Entry with this id has been deleted from this workspace successfully.'}, status=200) + +@csrf_exempt def edit_entry(request): - id = int(request.GET.get("entry_id")) - entry = Entry.objects.filter(entry_id=id) - content = (request.GET.get("content")) + entry_id = request.POST.get("entry_id") + content = request.POST.get("content") + if entry_id == None or entry_id == '': + return JsonResponse({'message': 'entry_id field can not be empty'}, status=400) + try: + entry_id = int(entry_id) + except: + return JsonResponse({'message': 'entry_id field has to be a integer'}, status=400) + if content == None: + content = '' + entry = Entry.objects.filter(entry_id=entry_id) + if entry.count() == 0: + return JsonResponse({'message': 'there is no entry with this id'}, status=404) + entry = Entry.objects.get(entry_id=entry_id) entry.content = content + entry.save(update_fields=["content"]) + return JsonResponse({'message': 'entry content is updated successfully'}, status=200) +@csrf_exempt def delete_workspace(request): - id = int(request.GET.get("workspace_id")) - workspace = Workspace.objects.filter(workspace_id=id) + workspace_id = request.POST.get("workspace_id") + contributor_id = request.POST.get("contributor_id") + if workspace_id == None or workspace_id == '': + return JsonResponse({'message': 'workspace_id field can not be empty'}, status=400) + if contributor_id == None or contributor_id == '': + return JsonResponse({'message': 'node_id field can not be empty'}, status=400) + try: + workspace_id = int(workspace_id) + contributor_id = int(contributor_id) + except: + return JsonResponse({'message': 'workspace_id and contributor_id fields have to be a integer'}, status=400) + workspace = Workspace.objects.filter(workspace_id=workspace_id) if workspace.count() == 0: return JsonResponse({'message': 'There is no workspace with this id.'}, status=404) - workspace.delete() - + contributor = Contributor.objects.filter(id=contributor_id) + if contributor.count() == 0: + return JsonResponse({'message': 'There is no contributor with this id.'}, status=404) + if workspace[0] not in contributor[0].workspaces.all(): + return JsonResponse({'message': 'there is no contributor with this id in this workspace.'}, status=404) + contributor[0].workspaces.remove(workspace[0]) + contributor[0].save() + # if workspace[0].contributor_set.all().count() == 0: + # workspace[0].delete() + return JsonResponse({'message': 'workspace deleted successfully.'}, status=200) + +@csrf_exempt def delete_contributor(request): - id = int(request.GET.get("contributor_id")) - workspace_id = int(request.GET.get("workspace_id")) + contributor_id = request.POST.get("contributor_id") + workspace_id = request.POST.get("workspace_id") + if workspace_id == None or workspace_id == '': + return JsonResponse({'message': 'workspace_id field can not be empty'}, status=400) + if contributor_id == None or contributor_id == '': + return JsonResponse({'message': 'node_id field can not be empty'}, status=400) + try: + workspace_id = int(workspace_id) + contributor_id = int(contributor_id) + except: + return JsonResponse({'message': 'workspace_id and contributor_id fields have to be a integer'}, status=400) workspace = Workspace.objects.filter(workspace_id=workspace_id) - contributor = Contributor.objects.filter(contributor_id=id) + contributor = Contributor.objects.filter(id=contributor_id) if contributor.count() == 0: return JsonResponse({'message': 'There is no contributor with this id.'}, status=404) if workspace.count() == 0: return JsonResponse({'message': 'There is no workspace with this id.'}, status=404) - contributor[0].workspaces.delete(workspace_id = workspace_id) - contributor.save() + if workspace[0] not in contributor[0].workspaces.all(): + return JsonResponse({'message': 'there is no contributor with this id in this workspace.'}, status=404) + contributor[0].workspaces.remove(workspace[0]) + contributor[0].save() + return JsonResponse({'message': 'Contributor from workspace deleted successfully.'}, status=200) + +@csrf_exempt def delete_reference(request): - id = int(request.GET.get("workspace_id")) - node = int(request.GET.get("node_id")) - workspace = Workspace.objects.filter(workspace_id=id) + workspace_id = request.POST.get("workspace_id") + node_id = request.POST.get("node_id") + if workspace_id == None or workspace_id == '': + return JsonResponse({'message': 'workspace_id field can not be empty'}, status=400) + if node_id == None or node_id == '': + return JsonResponse({'message': 'node_id field can not be empty'}, status=400) + try: + workspace_id = int(workspace_id) + node_id = int(node_id) + except: + return JsonResponse({'message': 'workspace_id and node_id fields have to be a integer'}, status=400) + workspace = Workspace.objects.filter(workspace_id=workspace_id) if workspace.count() == 0: return JsonResponse({'message': 'There is no workspace with this id.'}, status=404) + if workspace[0].is_finalized == True: + return JsonResponse({'message': ' workspace already finalized'}, status=400) workspace = workspace[0] - reference = workspace.references.all(node = node) - if workspace.count() == 0: - return JsonResponse({'message': 'There is no reference with this id.'}, status=404) - reference.delete() + reference = workspace.references.filter(node_id = node_id) + if reference.count() == 0: + return JsonResponse({'message': 'There is no reference with this id in this workspace.'}, status=404) + workspace.references.remove(reference[0]) + return JsonResponse({'message': 'reference deleted successfully.'}, status=200) + +@csrf_exempt def finalize_workspace(request): - id = int(request.GET.get("workspace_id")) - workspace = Workspace.objects.filter(workspace_id=id) + workspace_id = request.POST.get("workspace_id") + if workspace_id == None or workspace_id == '': + return JsonResponse({'message': 'workspace_id field can not be empty'}, status=400) + try: + workspace_id = int(workspace_id) + except: + return JsonResponse({'message': 'workspace_id field has to be a integer'}, status=400) + workspace = Workspace.objects.filter(workspace_id=workspace_id) if workspace.count() == 0: return JsonResponse({'message': 'There is no workspace with this id.'}, status=404) + if workspace[0].is_finalized == True: + return JsonResponse({'message': ' workspace already finalized'}, status=400) + workspace = workspace[0] workspace.is_finalized = True workspace.is_in_review = False + workspace.save() + return JsonResponse({'message': 'workspace successfully finalized'}, status=200) +@csrf_exempt def add_entry(request): - id = int(request.GET.get("workspace_id")) - content = request.GET.get("entry_content") - workspace = Workspace.objects.filter(workspace_id=id) + workspace_id = request.POST.get("workspace_id") + content = request.POST.get("entry_content") + if workspace_id == None or workspace_id == '': + return JsonResponse({'message': 'workspace_id field can not be empty'}, status=400) + try: + workspace_id = int(workspace_id) + except: + return JsonResponse({'message': 'workspace_id field has to be a integer'}, status=400) + if content == None: + content = '' + workspace = Workspace.objects.filter(workspace_id=workspace_id) if workspace.count() == 0: return JsonResponse({'message': 'There is no workspace with this id.'}, status=404) - entry = Entry.objects.create(content=content) - workspace.entries.add(entry[0]) ## - workspace.save() - + if workspace[0].is_finalized == True: + return JsonResponse({'message': ' workspace already finalized'}, status=400) + entry = Entry.objects.create(content=content,entry_index=0, entry_number=0) # TODO WILL BE PROVIDED IN THE FUTURE + workspace[0].entries.add(entry) ## + workspace[0].save() + return JsonResponse({'message': 'Entry successfully added to workspace', + 'entry_id': entry.entry_id}, status=200) +@csrf_exempt def add_reference(request): - id = int(request.GET.get("workspace_id")) - node_id = int(request.GET.get("node_id")) - workspace = Workspace.objects.filter(workspace_id=id) + workspace_id = request.POST.get("workspace_id") + node_id = request.POST.get("node_id") + if workspace_id == None or workspace_id == '': + return JsonResponse({'message': 'workspace_id field can not be empty'}, status=400) + if node_id == None or node_id == '': + return JsonResponse({'message': 'node_id field can not be empty'}, status=400) + try: + workspace_id = int(workspace_id) + node_id = int(node_id) + except: + return JsonResponse({'message': 'workspace_id and node_id fields have to be a integer'}, status=400) + workspace = Workspace.objects.filter(workspace_id=workspace_id) + if workspace[0].is_finalized == True: + return JsonResponse({'message': ' workspace already finalized'}, status=400) node = Node.objects.filter(node_id=node_id) if workspace.count() == 0: return JsonResponse({'message': 'There is no workspace with this id.'}, status=404) if node.count() == 0: return JsonResponse({'message': 'There is no node with this id.'}, status=404) - workspace.references.add(node[0]) - workspace.save() - + if node[0] in workspace[0].references.all(): + return JsonResponse({'message': 'this reference already exists in this workspace.'}, status=400) + workspace[0].references.add(node[0]) + workspace[0].save() + return JsonResponse({'message': 'reference added to the workspace successfully.'}, status=200) +@csrf_exempt def create_workspace(request): - title = request.GET.get("workspace_title") + title = request.POST.get("workspace_title") + user_id = request.POST.get("user_id") + if title == '' or title == None: + return JsonResponse({'message': 'workspace_title field can not be empty'}, status=400) + if user_id == '' or user_id == None: + return JsonResponse({'message': 'user_id field can not be empty'}, status=400) + try: + creator = int(user_id) + except: + return JsonResponse({'message': 'user_id has to be a integer'}, status=400) + cont = Contributor.objects.filter(id=user_id) + if cont.count() == 0: + return JsonResponse({'message': 'there is no contributor with this user_id'}, status=400) workspace = Workspace.objects.create(workspace_title=title) workspace.save() - + cont[0].workspaces.add(workspace) + return JsonResponse({'message': 'Workspace with title ' + title + ' has been added successfully' , + 'workspace_id' : workspace.workspace_id}, status=200) +@csrf_exempt def get_random_node_id(request): count = int(request.GET.get("count")) node_ids = Node.node_id.all() diff --git a/project/backend/database/models.py b/project/backend/database/models.py index 0743d0d1..3f39767f 100644 --- a/project/backend/database/models.py +++ b/project/backend/database/models.py @@ -58,7 +58,7 @@ class Entry(models.Model): entry_index = models.IntegerField() #workspace_id = models.ForeignKey(Workspace,null=False, blank = False, on_delete=models.CASCADE,related_name='WorkspaceID') content = models.TextField(null=False) - entry_date = models.DateField() + entry_date = models.DateField(auto_now_add=True) is_theorem_entry = models.BooleanField(default=False) is_final_entry = models.BooleanField(default=False) is_proof_entry = models.BooleanField(default=False) @@ -71,7 +71,7 @@ def set_as_final(self): def set_as_theorem(self): self.is_theorem_entry = True def set_entry_content(self,cont): - self.content += cont + self.content = cont # Create your models here. class Workspace(models.Model): #Node and Review Requests may be added later workspace_id = models.AutoField(primary_key=True) diff --git a/project/backend/database/tests.py b/project/backend/database/tests.py index 6ebc8bc0..354c32c8 100644 --- a/project/backend/database/tests.py +++ b/project/backend/database/tests.py @@ -398,14 +398,13 @@ def test_entry_model(self): entry_id = 1, entry_index = 1, content = "This is an entry.", - entry_date = "2023-11-11", is_theorem_entry = True, entry_number = 1, ) self.assertEqual(entry.entry_id, 1) self.assertEqual(entry.entry_number, 1) self.assertEqual(entry.content, "This is an entry.") - self.assertEqual(entry.entry_date, "2023-11-11") + # self.assertEqual(entry.entry_date, "2023-11-11") self.assertEqual(entry.is_theorem_entry, True) # self.assertEqual(entry.is_final_entry,False)