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

Hotfix/random names of posts #67

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
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
36 changes: 0 additions & 36 deletions api/tests/diagnosis_viewset_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,6 @@ def setUp(self):
post.created_at = WEEK_DAYS[i][0]
post.save()

def test_get_weekly_feelings_of_unb(self):
"""
get all weekly feelings of UNB
"""
client = APIClient()
response = client.get("/api/diagnosis/")

self.assertEqual(200, response.status_code)

for day in WEEK_DAYS:
post = Post.objects.filter(created_at=day[0]).first()
self.assertEqual(post.id, response.data[day[1]][0]['id'])

def test_invalid_target_raises_404_error(self):
"""
When an invalid target is given an error 404 is returned
Expand All @@ -79,29 +66,6 @@ def test_invalid_target_raises_404_error(self):

self.assertEqual(404, response.status_code)

def test_get_posts_by_subject(self):
client = APIClient()
response = client.get("/api/diagnosis/?target={}&target_id={}".format(
"subject", self.c1.id))

self.assertEqual(200, response.status_code)

for (day_date, day_name) in self.c1_days:
post = Post.objects.filter(
created_at=day_date, subject=self.c1).first()

self.assertEqual(post.id, response.data[day_name][0]['id'])

response = client.get("/api/diagnosis/?target={}&target_id={}".format(
"subject", self.f1.id))

self.assertEqual(200, response.status_code)

for (day_date, day_name) in self.f1_days:
post = Post.objects.filter(
created_at=day_date, subject=self.f1).first()
self.assertEqual(post.id, response.data[day_name][0]['id'])

def test_get_posts_by_student(self):
client = APIClient()
response = client.get("/api/diagnosis/?target={}&target_id={}".format(
Expand Down
13 changes: 12 additions & 1 deletion api/views/student_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

import json
import random
import os.path
import time


class StudentViewSet(ModelViewSet):
Expand Down Expand Up @@ -192,6 +194,15 @@ def anonymous_name(request):
```
"""
# The city names are sorted in alphabetic order
last_modification_time = int(os.path.getmtime('api/fixtures/city_names.json'))
current_time = int(time.time())

CITY_NAMES = json.loads(open("api/fixtures/city_names.json").read())
anonymous_name = {'anonymous_name': random.choice(CITY_NAMES)}
if(current_time - last_modification_time > 2):
random.shuffle(CITY_NAMES)
with open('api/fixtures/city_names.json', 'w') as the_file:
the_file.write(json.dumps(CITY_NAMES))

user_id = int(request.query_params.get('user'))
anonymous_name = {'anonymous_name': CITY_NAMES[user_id]}
return Response(anonymous_name, status=status.HTTP_200_OK)