Commit 9c5c05ab authored by VOISIN Thomas's avatar VOISIN Thomas
Browse files

Add enibar/api/history for Seagull usage

Fix slash url path

Fix

bracket's indentation
parent dd16e9a0
import json
from django.contrib.auth.decorators import login_required
from django.core import serializers
from django.http import Http404
from django.db.models import CharField
from django.db.models import Value
from django.http import JsonResponse
from .models import HistoryLine
from .models import Note
@login_required
def get_history_by_user(request):
try:
notes = Note.objects.filter(mail=request.user.email)
if not notes:
raise Http404
result = []
for note in notes:
result.extend(
obj["fields"]
for obj in json.loads(
serializers.serialize("json", HistoryLine.objects.filter(note_id=note.foreign_id).annotate(
nick=Value(note.nickname, output_field=CharField())
)))
)
return JsonResponse(result, safe=False)
except Exception:
raise Http404 from None
......@@ -105,6 +105,87 @@ class TestEnibarImport(TestCase):
self.assertEqual(response.status_code, 404)
class TestHistoryByUser(TestCase):
user_model = get_user_model()
def setUp(self) -> None:
self.note1 = Note.objects.create(
foreign_id=1,
nickname="Note1",
mail="user@local.test",
note=Decimal("10.00"),
)
self.note2 = Note.objects.create(
foreign_id=2,
nickname="Note 2",
mail="user@local.test",
note=Decimal("10.00"),
)
self.note_autre = Note.objects.create(
foreign_id=3,
nickname="Autre",
mail="other@local.test",
note=Decimal("10.00"),
)
self.user = self.user_model.objects.create_user(
"user", "user@local.test", "password"
)
self.client.force_login(self.user)
for i in range(60):
HistoryLine.objects.create(
foreign_id=i * 3 + 1,
date=timezone.make_aware(datetime(2020, 9, 1, 0, i, 0)),
note=self.note1.nickname,
category="Softs",
product="Produit",
price_name="default",
quantity=1,
liquid_quantity=10,
percentage=Decimal("0.00"),
price=Decimal("1.00"),
note_id=self.note1.foreign_id,
)
HistoryLine.objects.create(
foreign_id=i * 3 + 2,
date=timezone.make_aware(datetime(2020, 9, 1, 0, i, 20)),
note=self.note2.nickname,
category="Softs",
product="Produit",
price_name="default",
quantity=1,
liquid_quantity=10,
percentage=Decimal("0.00"),
price=Decimal("1.00"),
note_id=self.note2.foreign_id,
)
HistoryLine.objects.create(
foreign_id=i * 3 + 3,
date=timezone.make_aware(datetime(2020, 9, 1, 0, i, 40)),
note=self.note_autre.nickname,
category="Softs",
product="Produit",
price_name="default",
quantity=1,
liquid_quantity=10,
percentage=Decimal("0.00"),
price=Decimal("1.00"),
note_id=self.note_autre.foreign_id,
)
def test_all(self) -> None:
"""Tests the request getting all history lines of all notes"""
url = "/enibar/api/history"
response = self.client.get(url)
self.assertNotContains(response, self.note_autre.nickname)
self.assertContains(response, "Produit", count=120)
self.assertContains(response, self.note1.nickname)
self.assertContains(response, self.note2.nickname)
class TestHistoryView(TestCase):
user_model = get_user_model()
......
from django.urls import path
from django.urls import path, include
from . import views
from . import backend
app_name = "enibar"
urlpatterns = [
......@@ -14,4 +15,7 @@ urlpatterns = [
views.show_history,
name="show_history",
),
path('api/', include([
path("history", backend.get_history_by_user, name="get_history")
])),
]
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