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

Search and Profile GET API Update #417

Merged
merged 2 commits into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
44 changes: 37 additions & 7 deletions project/backend/api/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ 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')
cont = models.Contributor.objects.create(user=user, bio='Hello',id=1)
node = models.Node.objects.create(node_title='test',
theorem=None,
publish_date="2023-01-01",
Expand Down Expand Up @@ -175,6 +175,7 @@ def test_search(self):
self.assertEqual(response.json()['nodes'][0]['authors'][0]['name'], 'User')
self.assertEqual(response.json()['nodes'][0]['authors'][0]['surname'], 'Test')
self.assertEqual(response.json()['nodes'][0]['authors'][0]['username'], '[email protected]')
self.assertEqual(response.json()['nodes'][0]['authors'][0]['id'], 1)
self.assertEqual(response.json()['authors'], [])
data = {'query': 'test', 'type': 'author'}
response = self.client.get(self.search_url, data, format='json')
Expand All @@ -183,6 +184,7 @@ def test_search(self):
self.assertEqual(response.json()['authors'][0]['name'], 'User')
self.assertEqual(response.json()['authors'][0]['surname'], 'Test')
self.assertEqual(response.json()['authors'][0]['username'], '[email protected]')
self.assertEqual(response.json()['authors'][0]['id'], 1)
data = {'query': 'test', 'type': 'by'}
response = self.client.get(self.search_url, data, format='json')
self.assertEqual(response.status_code, 200)
Expand All @@ -192,6 +194,7 @@ def test_search(self):
self.assertEqual(response.json()['nodes'][0]['authors'][0]['name'], 'User')
self.assertEqual(response.json()['nodes'][0]['authors'][0]['surname'], 'Test')
self.assertEqual(response.json()['nodes'][0]['authors'][0]['username'], '[email protected]')
self.assertEqual(response.json()['nodes'][0]['authors'][0]['id'], 1)
data = {'query': 'test', 'type': 'all'}
response = self.client.get(self.search_url, data, format='json')
self.assertEqual(response.status_code, 200)
Expand All @@ -204,6 +207,7 @@ def test_search(self):
self.assertEqual(response.json()['nodes'][0]['authors'][0]['name'], 'User')
self.assertEqual(response.json()['nodes'][0]['authors'][0]['surname'], 'Test')
self.assertEqual(response.json()['nodes'][0]['authors'][0]['username'], '[email protected]')
self.assertEqual(response.json()['nodes'][0]['authors'][0]['id'], 1)


class ProfileGETAPITestCase(TestCase):
Expand All @@ -212,17 +216,20 @@ 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 = Contributor.objects.create(user=user,bio='Hello')
cont = Contributor.objects.create(user=user,bio='Hello',id=1)
node = Node.objects.create(node_title='test',
node_id = 55,
theorem=None,
publish_date="2023-01-01",
is_valid=True,
num_visits=0,)
Q = Question.objects.create(
node=node,
asker = cont,
question_content = "TEXT",
answerer = cont
question_content = "QUESTION",
answerer = cont,
answer_content = 'ANSWER',

)
node.contributors.add(cont)
self.get_profile_url = reverse('get_profile')
Expand All @@ -239,15 +246,38 @@ def test_get_user_profile(self):
self.assertEqual(response.json()['name'],'User')
self.assertEqual(response.json()['surname'], 'Test')
self.assertEqual(response.json()['bio'], 'Hello')
self.assertEqual(response.json()['nodes'][0]['id'],1)
self.assertEqual(response.json()['nodes'][0]['id'],55)
self.assertEqual(response.json()['nodes'][0]['title'], 'test')
self.assertEqual(response.json()['nodes'][0]['date'], '2023-01-01')
self.assertEqual(response.json()['nodes'][0]['authors'][0]['name'], 'User')
self.assertEqual(response.json()['nodes'][0]['authors'][0]['surname'], 'Test')
self.assertEqual(response.json()['nodes'][0]['authors'][0]['username'], '[email protected]')
# TODO TESTS FAIL HERE
# self.assertEqual(response.json()['answered_questions'][0],1)
# self.assertEqual(response.json()['asked_questions'][0], 1)
self.assertEqual(response.json()['answered_questions'][0]['question_content'],'QUESTION')
self.assertEqual(response.json()['answered_questions'][0]['answer_content'],'ANSWER')
self.assertEqual(response.json()['answered_questions'][0]['asker_name'], 'User')
self.assertEqual(response.json()['answered_questions'][0]['asker_surname'], 'Test')
self.assertEqual(response.json()['answered_questions'][0]['asker_id'], 1)
self.assertEqual(response.json()['answered_questions'][0]['asker_mail'], '[email protected]')
self.assertEqual(response.json()['answered_questions'][0]['node_id'], 55)
self.assertEqual(response.json()['answered_questions'][0]['node_title'], 'test')
self.assertEqual(response.json()['answered_questions'][0]['answerer_name'], 'User')
self.assertEqual(response.json()['answered_questions'][0]['answerer_surname'], 'Test')
self.assertEqual(response.json()['answered_questions'][0]['answerer_id'], 1)
self.assertEqual(response.json()['answered_questions'][0]['answerer_mail'], '[email protected]')

self.assertEqual(response.json()['asked_questions'][0]['question_content'], 'QUESTION')
self.assertEqual(response.json()['asked_questions'][0]['answer_content'], 'ANSWER')
self.assertEqual(response.json()['asked_questions'][0]['asker_name'], 'User')
self.assertEqual(response.json()['asked_questions'][0]['asker_surname'], 'Test')
self.assertEqual(response.json()['asked_questions'][0]['asker_id'], 1)
self.assertEqual(response.json()['asked_questions'][0]['asker_mail'], '[email protected]')
self.assertEqual(response.json()['asked_questions'][0]['node_id'], 55)
self.assertEqual(response.json()['asked_questions'][0]['node_title'], 'test')
self.assertEqual(response.json()['asked_questions'][0]['answerer_name'], 'User')
self.assertEqual(response.json()['asked_questions'][0]['answerer_surname'], 'Test')
self.assertEqual(response.json()['asked_questions'][0]['answerer_id'], 1)
self.assertEqual(response.json()['asked_questions'][0]['answerer_mail'], '[email protected]')

class ProofGETAPITestCase(TestCase):
def setUp(self):
Expand Down
42 changes: 34 additions & 8 deletions project/backend/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,16 +136,16 @@ def search(request):
for cont in contributors:
user = User.objects.get(username=cont)
cont = Contributor.objects.get(user=user)
res_authors.append({'name': User.objects.get(id=cont.user_id).first_name,
'surname': User.objects.get(id=cont.user_id).last_name, 'username': cont.user.username})
res_authors.append({'name': user.first_name,
'surname': user.last_name, 'username': cont.user.username , 'id': cont.id})
node_infos = []
for node_id in nodes:
node = Node.objects.get(node_id=node_id)
authors = []
for cont in node.contributors.all():
user = User.objects.get(id=cont.user_id)
authors.append({'name': User.objects.get(id=cont.user_id).first_name,
'surname': User.objects.get(id=cont.user_id).last_name, 'username': user.username})
authors.append({'name': user.first_name,
'surname': user.last_name, 'username': user.username, 'id': cont.id})
node_infos.append({'id': node_id, 'title': node.node_title, 'date': node.publish_date, 'authors': authors})
return JsonResponse({'nodes' : node_infos , 'authors' :res_authors },status=200)

Expand All @@ -168,10 +168,36 @@ def get_profile(request):
nodes.append(node.node_id)
user_answered_qs = Question.objects.filter(answerer=cont[0].id)
for ans in user_answered_qs:
answered_questions.append(ans.id)
user_asked_qs = Question.objects.filter(asker=user.id)
asker_user = User.objects.get(id=ans.asker.user_id)
answerer_user = User.objects.get(id=ans.answerer.user_id)
answered_questions.append({'id':ans.id, 'ask_date':ans.created_at, 'node_id':ans.node.node_id, 'node_title':ans.node.node_title, 'node_date':ans.node.publish_date,
'asker_id':ans.asker.id,'asker_name':asker_user.first_name,'asker_surname':asker_user.last_name,
'asker_mail':asker_user.username,'answerer_name':answerer_user.first_name,'answerer_surname':answerer_user.last_name,
'answerer_mail':answerer_user.username,
'answerer_id':ans.answerer.id, 'question_content':ans.question_content,
'answer_content':ans.answer_content,'answer_date':ans.answered_at,'is_answered':1})

user_asked_qs = Question.objects.filter(asker=cont[0].id)
for q in user_asked_qs:
asked_questions.append(q.id)
asker_user = User.objects.get(id=q.asker.user_id)
if q.answerer == None:
asked_questions.append(
{'id': q.id, 'ask_date': q.created_at, 'node_id': q.node.node_id, 'node_title': q.node.node_title,
'node_date': q.node.publish_date,
'asker_id': q.asker.id, 'asker_name': asker_user.first_name, 'asker_surname': asker_user.last_name,
'asker_mail': asker_user.username, 'question_content': q.question_content, 'is_answered': 0})
continue
answerer_user = User.objects.get(id=q.answerer.user_id)
asked_questions.append(
{'id': q.id, 'ask_date': q.created_at, 'node_id': q.node.node_id, 'node_title': q.node.node_title,
'node_date': q.node.publish_date,
'asker_id': q.asker.id, 'asker_name': asker_user.first_name, 'asker_surname': asker_user.last_name,
'asker_mail': asker_user.username, 'answerer_name': answerer_user.first_name,
'answerer_surname': answerer_user.last_name,
'answerer_mail': answerer_user.username,
'answerer_id': q.answerer.id, 'question_content': q.question_content,
'answer_content': q.answer_content, 'answer_date': q.answered_at, 'is_answered':1})

node_infos = []
for node_id in nodes:
node = Node.objects.get(node_id=node_id)
Expand All @@ -180,7 +206,7 @@ def get_profile(request):
user = User.objects.get(id=cont.user_id)
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})
# TODO QUESTION RETURNS SHOULD BE CHANGED IN THE FUTURE.

return JsonResponse({'name':user.first_name,
'surname':user.last_name,
'bio':basic_user.bio,
Expand Down