WIP: Affiche un warning si trop d'alcool consommé #727

Draft
lstephan wants to merge 4 commits from Aufinal/alcoolémie into master
2 changed files with 100 additions and 3 deletions
Showing only changes of commit 60831045b5 - Show all commits

View file

@ -1,5 +1,5 @@
import json import json
from datetime import datetime, timedelta from datetime import timedelta
from decimal import Decimal from decimal import Decimal
from unittest import mock from unittest import mock
@ -1365,6 +1365,8 @@ class ArticleCreateViewTests(ViewTestCaseMixin, TestCase):
"category": self.category.pk, "category": self.category.pk,
"stock": 5, "stock": 5,
"price": "2.5", "price": "2.5",
"volume": "33",
"abv": "6.3",
} }
def setUp(self): def setUp(self):
@ -1451,6 +1453,8 @@ class ArticleUpdateViewTests(ViewTestCaseMixin, TestCase):
"price": "3.5", "price": "3.5",
"box_type": "carton", "box_type": "carton",
# 'hidden': not checked # 'hidden': not checked
"volume": "33",
"abv": "6.3",
} }
def setUp(self): def setUp(self):
@ -4220,6 +4224,74 @@ class AccountReadJSONViewTests(ViewTestCaseMixin, TestCase):
def url_expected(self): def url_expected(self):
return "/k-fet/accounts/{}/.json".format(self.accounts["user"].trigramme) return "/k-fet/accounts/{}/.json".format(self.accounts["user"].trigramme)
def setUp(self):
super().setUp()
# A Checkout, curently usable, balance=100
self.checkout = Checkout.objects.create(
created_by=self.accounts["team"],
name="Checkout",
valid_from=self.now - timedelta(days=7),
valid_to=self.now + timedelta(days=7),
balance=Decimal("100.00"),
)
# An Article, price=2.5, stock=20
self.article = Article.objects.create(
category=ArticleCategory.objects.create(name="Category"),
name="Article",
price=Decimal("2.5"),
stock=20,
volume=Decimal("33"),
abv=Decimal("7.3"),
)
now = timezone.localtime(self.now)
ope_party1 = OperationGroup.objects.create(
on_acc=self.accounts["user"],
checkout=self.checkout,
at=now.replace(hour=1),
)
ope_party2 = OperationGroup.objects.create(
on_acc=self.accounts["user"],
checkout=self.checkout,
at=now.replace(day=now.day - 1, hour=22),
)
ope_noparty = OperationGroup.objects.create(
on_acc=self.accounts["user"],
checkout=self.checkout,
at=now.replace(day=now.day - 1, hour=19),
)
Operation.objects.create(
group=ope_party1,
type=Operation.PURCHASE,
article=self.article,
article_nb=2,
)
Operation.objects.create(
group=ope_party2,
type=Operation.PURCHASE,
article=self.article,
article_nb=1,
)
Operation.objects.create(
group=ope_party2,
type=Operation.PURCHASE,
article=self.article,
article_nb=1,
)
Operation.objects.create(
group=ope_noparty,
type=Operation.PURCHASE,
article=self.article,
article_nb=3,
)
def test_ok(self): def test_ok(self):
r = self.client.get(self.url) r = self.client.get(self.url)
self.assertEqual(r.status_code, 200) self.assertEqual(r.status_code, 200)
@ -4243,10 +4315,33 @@ class AccountReadJSONViewTests(ViewTestCaseMixin, TestCase):
"nickname", "nickname",
"promo", "promo",
"trigramme", "trigramme",
"n_units",
] ]
), ),
) )
@mock.patch("django.utils.timezone.now")
def test_without_party(self, mock_now):
now = timezone.localtime(self.now)
mock_now.return_value = now.replace(hour=20).astimezone(timezone.utc)
r = self.client.get(self.url)
self.assertEqual(r.status_code, 200)
content = json.loads(r.content.decode("utf-8"))
self.assertEqual(content["n_units"], 0)
@mock.patch("django.utils.timezone.now")
def test_with_party(self, mock_now):
now = timezone.localtime(self.now)
mock_now.return_value = now.replace(hour=5).astimezone(timezone.utc)
r = self.client.get(self.url)
self.assertEqual(r.status_code, 200)
content = json.loads(r.content.decode("utf-8"))
self.assertAlmostEqual(float(content["n_units"]), 3 * 33 * 7.3 * 0.8 / 100)
class SettingsListViewTests(ViewTestCaseMixin, TestCase): class SettingsListViewTests(ViewTestCaseMixin, TestCase):
url_name = "kfet.settings" url_name = "kfet.settings"

View file

@ -923,13 +923,15 @@ def account_read_json(request, trigramme):
now = timezone.localtime(timezone.now()) now = timezone.localtime(timezone.now())
# une soirée va de XXh à 06h # une soirée va de XXh à 06h
if time(21) <= now.time() or now.time() <= time(6): if time(21) <= now.time() or now.time() <= time(6):
begin_time = datetime.combine(now.date(), time(21)) begin_time = now.replace(hour=21, minute=0, second=0, microsecond=0)
# si on est après minuit, il faut retrancher un jour # si on est après minuit, il faut retrancher un jour
if now.time() <= time(6): if now.time() <= time(6):
begin_time -= timedelta(days=1) begin_time -= timedelta(days=1)
qs = Operation.objects.filter( qs = Operation.objects.filter(
group__on_acc=account, type=Operation.PURCHASE, group__at__gte=begin_time, group__on_acc=account,
type=Operation.PURCHASE,
group__at__gte=begin_time.astimezone(timezone.utc),
).annotate( ).annotate(
units=F("article__volume") * F("article__abv") * alcohol_density / 100 units=F("article__volume") * F("article__abv") * alcohol_density / 100
) )