Commit 6a72750c authored by Thomas VOISIN's avatar Thomas VOISIN
Browse files

Black reformat on enibar urls.py, tests.py and views.py

parent 1b8e0807
...@@ -12,6 +12,7 @@ types-Markdown = "*" ...@@ -12,6 +12,7 @@ types-Markdown = "*"
types-freezegun = "*" types-freezegun = "*"
types-redis = "*" types-redis = "*"
types-requests = "*" types-requests = "*"
black = "*"
[packages] [packages]
Markdown = "*" Markdown = "*"
...@@ -26,7 +27,6 @@ psycopg2-binary = "*" ...@@ -26,7 +27,6 @@ psycopg2-binary = "*"
redis = "*" redis = "*"
requests = "*" requests = "*"
[scripts] [scripts]
test = "./test.sh" test = "./test.sh"
create_recurrent_events = "./manage.py create_recurrent_events" create_recurrent_events = "./manage.py create_recurrent_events"
This diff is collapsed.
...@@ -60,7 +60,9 @@ class TestEnibarImport(TestCase): ...@@ -60,7 +60,9 @@ class TestEnibarImport(TestCase):
Note.objects.create( Note.objects.create(
foreign_id=2, nickname="toto", mail="coucou@test.fr", note=Decimal("10") foreign_id=2, nickname="toto", mail="coucou@test.fr", note=Decimal("10")
) )
response = self.client.delete("/enibar/api/note", '{"token": "changeme", "id": 2}') response = self.client.delete(
"/enibar/api/note", '{"token": "changeme", "id": 2}'
)
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
with self.assertRaises(Note.DoesNotExist): with self.assertRaises(Note.DoesNotExist):
Note.objects.get(foreign_id=2) Note.objects.get(foreign_id=2)
...@@ -187,8 +189,13 @@ class TestHistoryByUser(TestCase): ...@@ -187,8 +189,13 @@ class TestHistoryByUser(TestCase):
self.assertNotContains(response, self.note_autre.nickname) self.assertNotContains(response, self.note_autre.nickname)
self.assertIsInstance(json.loads(response.content), list) self.assertIsInstance(json.loads(response.content), list)
self.assertEqual(len(json.loads(response.content)), 120) self.assertEqual(len(json.loads(response.content)), 120)
assert all(isinstance(value, dict) and all(field.name in value.keys() for field in HistoryLine._meta.get_fields()) assert all(
for value in json.loads(response.content)) isinstance(value, dict)
and all(
field.name in value.keys() for field in HistoryLine._meta.get_fields()
)
for value in json.loads(response.content)
)
self.assertContains(response, self.note1.nickname) self.assertContains(response, self.note1.nickname)
self.assertContains(response, self.note2.nickname) self.assertContains(response, self.note2.nickname)
...@@ -370,6 +377,8 @@ class TestHistoryView(TestCase): ...@@ -370,6 +377,8 @@ class TestHistoryView(TestCase):
class TestGetPhotoPaths(TestCase): class TestGetPhotoPaths(TestCase):
def test_last_update_in_dst_change(self): def test_last_update_in_dst_change(self):
"""Regression test for #43""" """Regression test for #43"""
resp = self.client.get("/enibar/api/photos?last_updated=2020-10-25T02%3A00%3A01") resp = self.client.get(
"/enibar/api/photos?last_updated=2020-10-25T02%3A00%3A01"
)
self.assertEqual(resp.status_code, 200) self.assertEqual(resp.status_code, 200)
...@@ -11,10 +11,19 @@ urlpatterns = [ ...@@ -11,10 +11,19 @@ urlpatterns = [
views.show_history, views.show_history,
name="show_history", name="show_history",
), ),
path('api/', include([ path(
path("note", views.request_note, name="request_note"), "api/",
path("photos", views.get_photo_paths, name="photo_paths"), include(
path("history", views.request_history, name="request_history"), [
path("history-by-user", views.get_history_by_user, name="get_history_by_user") path("note", views.request_note, name="request_note"),
])), path("photos", views.get_photo_paths, name="photo_paths"),
path("history", views.request_history, name="request_history"),
path(
"history-by-user",
views.get_history_by_user,
name="get_history_by_user",
),
]
),
),
] ]
...@@ -99,7 +99,9 @@ request_history = _create_view(HistoryLine) ...@@ -99,7 +99,9 @@ request_history = _create_view(HistoryLine)
@login_required @login_required
def show_history(request: HttpRequest, page, nickname: Optional[str] = None) -> HttpResponse: def show_history(
request: HttpRequest, page, nickname: Optional[str] = None
) -> HttpResponse:
page = page or 1 page = page or 1
notes = Note.objects.filter(mail=request.user.email) # type: ignore[union-attr] # login is required notes = Note.objects.filter(mail=request.user.email) # type: ignore[union-attr] # login is required
personal_notes = set(notes) personal_notes = set(notes)
...@@ -133,8 +135,9 @@ def get_photo_paths(request: HttpRequest) -> JsonResponse: ...@@ -133,8 +135,9 @@ def get_photo_paths(request: HttpRequest) -> JsonResponse:
last_updated_in = request.GET.get("last_updated") last_updated_in = request.GET.get("last_updated")
if last_updated_in is not None: if last_updated_in is not None:
last_updated = datetime.datetime.strptime(last_updated_in, "%Y-%m-%dT%H:%M:%S") last_updated = datetime.datetime.strptime(last_updated_in, "%Y-%m-%dT%H:%M:%S")
last_updated = timezone.make_aware(last_updated, timezone=None, last_updated = timezone.make_aware(
is_dst=True) # Make TZ aware avoiding DST by fetching more last_updated, timezone=None, is_dst=True
) # Make TZ aware avoiding DST by fetching more
users = User.objects.filter( users = User.objects.filter(
profile__last_updated__gt=last_updated profile__last_updated__gt=last_updated
).select_related("profile") ).select_related("profile")
...@@ -159,8 +162,11 @@ def get_history_by_user(request: HttpRequest) -> JsonResponse: ...@@ -159,8 +162,11 @@ def get_history_by_user(request: HttpRequest) -> JsonResponse:
raise Http404 raise Http404
result: List[dict] = [] result: List[dict] = []
for note in notes: for note in notes:
result.extend(HistoryLine.objects.filter(note_id=note.foreign_id) result.extend(
.annotate(nick=Value(note.nickname, output_field=CharField())).values()) HistoryLine.objects.filter(note_id=note.foreign_id)
.annotate(nick=Value(note.nickname, output_field=CharField()))
.values()
)
return JsonResponse(result, safe=False) return JsonResponse(result, safe=False)
except Exception: except Exception:
raise Http404 from None raise Http404 from None
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment