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

Implementation of Request POST Methods #457

Closed
wants to merge 40 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
f6e3913
Update models.py
gebenalimert Oct 31, 2023
542c53a
Implemented Workspace class and unit tests
gebenalimert Nov 7, 2023
a5d1c2e
Added User class to Admin
gebenalimert Nov 7, 2023
1815038
Changed relations between classes
gebenalimert Nov 7, 2023
bda5137
Finalized tests and workspace model
gebenalimert Nov 8, 2023
b71763e
Update models.py
hakanaktas0 Nov 10, 2023
8d09b5f
Revert "Update models.py"
hakanaktas0 Nov 10, 2023
c042805
Update models.py
gebenalimert Nov 11, 2023
7c89377
Added tests
gebenalimert Nov 11, 2023
5063b98
Update models.py
gebenalimert Nov 13, 2023
8e39cc3
Merge branch 'backend' into entryClass
gebenalimert Nov 13, 2023
de14dc1
Update models.py
gebenalimert Nov 13, 2023
a70d91d
Forgetten "," added
gebenalimert Nov 13, 2023
81f5f3d
Merge branch 'backend' into entryClass
gebenalimert Nov 13, 2023
5f53ed2
Merge branch 'backend' into workspace-model
ArslanArdavic Nov 14, 2023
4983214
Add CollabRequest POST
Bera0422 Nov 15, 2023
5319717
Implemented Status Update
Bera0422 Nov 15, 2023
476b35c
Implement Send Review Request
Bera0422 Nov 15, 2023
d9858ef
Update models.py
gebenalimert Nov 15, 2023
e09e91e
Update models.py
gebenalimert Nov 15, 2023
369aedb
Merge pull request #413 from bounswe/workspace-model
gebenalimert Nov 15, 2023
27a3d44
Merge branch 'backend' into entryClass
gebenalimert Nov 15, 2023
95d38b2
Update models.py
gebenalimert Nov 15, 2023
3aab8ee
Update tests.py
gebenalimert Nov 15, 2023
e88c5bf
Merge pull request #429 from bounswe/entryClass
gebenalimert Nov 15, 2023
1fbe4c0
Implementation
hakanaktas0 Nov 16, 2023
82e5617
tests are implemented
hakanaktas0 Nov 16, 2023
aec05a6
test fix
hakanaktas0 Nov 16, 2023
65fc52c
test fix
hakanaktas0 Nov 16, 2023
2320dae
Update tests.py
hakanaktas0 Nov 16, 2023
06b079d
Update tests.py
hakanaktas0 Nov 16, 2023
9bd9298
Update tests.py
hakanaktas0 Nov 16, 2023
18900c6
Update tests.py
hakanaktas0 Nov 16, 2023
dfc1605
Update tests.py
hakanaktas0 Nov 16, 2023
807d246
Merge pull request #459 from bounswe/backend-workspace-get-api
hakanaktas0 Nov 16, 2023
39115a2
Add CollabRequest POST
Bera0422 Nov 15, 2023
5cb4926
Implemented Status Update
Bera0422 Nov 15, 2023
2930e89
Implement Send Review Request
Bera0422 Nov 15, 2023
e75ac33
Change id reference for workspace
Bera0422 Nov 16, 2023
043cfc8
Merge branch 'request-post-api' of https://github.com/bounswe/bounswe…
Bera0422 Nov 16, 2023
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
126 changes: 122 additions & 4 deletions project/backend/api/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
from rest_framework import status
from rest_framework.test import APIClient
from django.contrib.auth.models import User
from database.models import BasicUser,Contributor,Node,Question, Proof, Theorem
from database.models import *
from database.models import *
from rest_framework.authtoken.models import Token
from database.serializers import RegisterSerializer, UserSerializer
from database import models
import datetime
# Create your tests here for each class or API call.

Expand Down Expand Up @@ -147,8 +147,8 @@ def setUp(self):
user = User.objects.create_user(id=1, email='[email protected]', username='[email protected]', first_name='User',
last_name='Test')
# basic_user = BasicUser.objects.create(user=user, bio='Hello')
cont = models.Contributor.objects.create(user=user, bio='Hello',id=1)
node = models.Node.objects.create(node_title='test',
cont = Contributor.objects.create(user=user, bio='Hello',id=1)
node = Node.objects.create(node_title='test',
theorem=None,
publish_date="2023-01-01",
is_valid=True,
Expand Down Expand Up @@ -447,3 +447,121 @@ def test_get_contributor_from_id(self):
}
)


class UserWorkspacesGETAPITestCase(TestCase):
def setUp(self):
self.client = APIClient()
self.user = User.objects.create_user(id=1, email='[email protected]', username='[email protected]', first_name='User',
last_name='Test')
self.cont = Contributor.objects.create(user=self.user, bio='Hello',id=3)
self.workspace = self.cont.create_workspace('test')
self.url = reverse('get_user_workspaces')

def test_get_workspaces_of_user(self):
response = self.client.get(self.url, {'user_id': self.cont.id})
print(response)

self.assertEqual(response.status_code, 200)
self.assertEqual(response.json()['workspaces'][0]['workspace_id'],self.workspace.workspace_id)
self.assertEqual(response.json()['workspaces'][0]['workspace_title'], self.workspace.workspace_title)
self.assertEqual(response.json()['workspaces'][0]['pending'], False)



class WorkspaceGETAPITestCase(TestCase):
def setUp(self):
self.client = APIClient()
self.user = User.objects.create_user(id=1, email='[email protected]', username='[email protected]', first_name='User',
last_name='Test')
self.cont = Contributor.objects.create(user=self.user, bio='Hello',id=3)
self.workspace = self.cont.create_workspace('test')
self.url = reverse('get_workspace')

def test_get_workspace_from_id(self):
response = self.client.get(self.url, {'workspace_id': self.workspace.workspace_id})
print(response)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json()['workspace_id'],self.workspace.workspace_id)
self.assertEqual(response.json()['workspace_id'], self.workspace.workspace_id)
self.assertEqual(response.json()['contributors'], [{'id':self.cont.id,'first_name':self.user.first_name,'last_name':self.user.last_name,'username':self.user.username}])
self.assertEqual(response.json()['workspace_title'], self.workspace.workspace_title)
self.assertEqual(response.json()['status'], 'workable')
self.assertEqual(response.json()['references'], [])
self.assertEqual(response.json()['pending_contributors'], [])
self.assertEqual(response.json()['num_approvals'], 0)
# self.assertEqual(response.json()['created_at'], self.workspace.created_at)


class CollaborationRequestAPITestCase(TestCase):

def tearDown(self):
Workspace.objects.all().delete()
User.objects.all().delete()
Contributor.objects.all().delete()
CollaborationRequest.objects.all().delete()
print("Test for the CollaborationRequest API is completed!")

def setUp(self):
self.client = APIClient()

self.workspace = Workspace.objects.create()
self.contributor_receiver = Contributor.objects.create(user=User.objects.create(username="receiver"))
self.contributor_sender = Contributor.objects.create(user=User.objects.create(username="sender"))

self.request = CollaborationRequest.objects.create(workspace=self.workspace,receiver=self.contributor_receiver,sender=self.contributor_sender)

self.request_data = {
'sender': self.contributor_sender.id,
'receiver': self.contributor_receiver.id,
'title' : 'Request Test Title',
'body': 'Request Test Body',
'workspace': self.workspace.workspace_id
}

def test_send_collab_request(self):
url = reverse('send_col_req')
response = self.client.post(url, self.request_data, format='json')
self.assertEqual(response.status_code, 201)

def update_collab_request(self):
url = reverse('update_req')
response = self.client.put(url, {'id': self.request_data[id], 'status': 'A'}, format='json')
self.assertEqual(response.status_code, 200)
self.assertEqual(response.data.status, 'A')

class ReviewRequestAPITestCase(TestCase):

def tearDown(self):
Workspace.objects.all().delete()
User.objects.all().delete()
Contributor.objects.all().delete()
ReviewRequest.objects.all().delete()
print("Test for the ReviewRequest API is completed!")

def setUp(self):
self.client = APIClient()

self.workspace = Workspace.objects.create()
self.reviewer_receiver = Contributor.objects.create(user=User.objects.create(username="receiver"))
self.contributor_sender = Contributor.objects.create(user=User.objects.create(username="sender"))

self.request = ReviewRequest.objects.create(workspace=self.workspace,receiver=self.reviewer_receiver,sender=self.contributor_sender)

self.request_data = {
'sender': self.contributor_sender.id,
'receiver': self.reviewer_receiver.id,
'title' : 'Review Request Test Title',
'body': 'Review Request Test Body',
'workspace': self.workspace.workspace_id
}

def test_send_review_request(self):
url = reverse('send_rev_req')
response = self.client.post(url, self.request_data, format='json')
self.assertEqual(response.status_code, 201)

def update_review_request(self):
url = reverse('update_req')
response = self.client.put(url, {'id': self.request_data[id], 'status': 'R'}, format='json')
self.assertEqual(response.status_code, 200)
self.assertEqual(response.data.status, 'R')
5 changes: 5 additions & 0 deletions project/backend/api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,9 @@
path('get_proof/', get_proof_from_id, name='get_proof'),
path('get_theorem/', get_theorem_from_id, name='get_theorem'),
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('send_collab_req/', send_collaboration_request, name='send_col_req'),
path('update_req/', update_request_status, name='update_req'),
path('send_rev_req/', send_review_request, name='send_rev_req'),
]
131 changes: 130 additions & 1 deletion project/backend/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,4 +253,133 @@ def get_contributor_from_id(request):
'name': user.first_name,
'surname': user.last_name,
}
return JsonResponse(data, status=200)
return JsonResponse(data, status=200)

def get_workspaces(request):
id = int(request.GET.get("user_id"))
cont = Contributor.objects.filter(id=id)
if cont.count() == 0:
return JsonResponse({'message':'There is no contributor with this id.'},status=404)
cont = cont[0]
workspace_list = []
for workspace in cont.workspaces.all():

workspace_list.append({'workspace_id':workspace.workspace_id,
'workspace_title':workspace.workspace_title,
'pending':False})
pending = []
for req in CollaborationRequest.objects.filter(receiver=cont):
workspace = req.workspace
pending.append({'workspace_id': workspace.workspace_id,
'workspace_title': workspace.workspace_title,
'pending': True})

return JsonResponse({'workspaces':workspace_list,'pending_workspaces':pending}, status=200)

def get_workspace_from_id(request):
id = int(request.GET.get("workspace_id"))
workspace = Workspace.objects.filter(workspace_id=id)
if workspace.count() == 0:
return JsonResponse({'message':'There is no workspace with this id.'},status=404)
workspace = workspace[0]
entries = []
for entry in workspace.entries.all():
entries.append({'entry_id':entry.entry_id,
'entry_date':entry.entry_date,
'content':entry.content,
'entry_index':entry.entry_index,
'is_theorem_entry':entry.is_theorem_entry,
'is_final_entry':entry.is_final_entry,
'is_proof_entry':entry.is_proof_entry,
'is_editable':entry.is_editable,
'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})
contributors = []
for cont in Contributor.objects.filter(workspaces=workspace):
user = User.objects.get(id=cont.user_id)
contributors.append({"id": cont.id,
"first_name": user.first_name,
"last_name": user.last_name,
"username": user.username})
pending = []
for pend in CollaborationRequest.objects.filter(workspace=workspace):
cont = pend.receiver
user = User.objects.get(id=cont.user_id)
pending.append({"id": cont.id,
"first_name": user.first_name,
"last_name": user.last_name,
"username": user.username})
references = []
for ref in workspace.references.all():
authors = []
for cont in ref.contributors.all():
user = User.objects.get(id=cont.user_id)
authors.append({"id": cont.id,
"first_name": user.first_name,
"last_name": user.last_name,
"username": user.username})
references.append({'node_id':ref.node_id,
'node_title':ref.node_title,
'contributors':authors,
'publish_date':ref.publish_date})
status = 'workable'
if workspace.is_published:
status = 'published'
elif workspace.is_rejected:
status = 'rejected'
elif workspace.is_in_review:
status = 'in_review'
elif workspace.is_finalized:
status = 'finalized'
return JsonResponse({'workspace_id': workspace.workspace_id,
'workspace_title':workspace.workspace_title,
'workspace_entries': entries,
'status':status,
'num_approvals':workspace.num_approvals,
'contributors':contributors,
'pending_contributors':pending,
'references':references,
'created_at':workspace.created_at,
}, status=200)


@api_view(['POST'])
def send_collaboration_request(request):

serializer = CollaborationRequestSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=201)
return Response(serializer.errors, status=400)

@api_view(['PUT'])
def update_request_status(request):
try:
req = Request.objects.get(pk=request.data.get('workspace_id'))
except Request.DoesNotExist:
return Response({"message": "Request not found."}, status=404)

status = request.data.get('status')

if status not in ["P", "A", "R"]:
return Response({"message": "Invalid status value."}, status=400)

req.status = status
req.save()

serializer = RequestSerializer(req)
return Response(serializer.data, status=200)

@api_view(['POST'])
def send_review_request(request):

serializer = ReviewRequestSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=201)
return Response(serializer.errors, status=400)
1 change: 1 addition & 0 deletions project/backend/database/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# Register your models here.

admin.site.register(Workspace)
admin.site.register(Entry)
admin.site.register(BasicUser)
admin.site.register(Contributor)
admin.site.register(Reviewer)
Expand Down
Loading