mirror of
https://github.com/ae-utbm/sith.git
synced 2026-06-04 23:29:24 +00:00
feat: request.barmen
This commit is contained in:
+2
-7
@@ -46,7 +46,7 @@ from django.http import HttpRequest
|
|||||||
from ninja_extra import ControllerBase
|
from ninja_extra import ControllerBase
|
||||||
from ninja_extra.permissions import BasePermission
|
from ninja_extra.permissions import BasePermission
|
||||||
|
|
||||||
from counter.models import Counter
|
from counter.utils import is_logged_in_counter
|
||||||
|
|
||||||
|
|
||||||
class IsInGroup(BasePermission):
|
class IsInGroup(BasePermission):
|
||||||
@@ -186,12 +186,7 @@ class IsLoggedInCounter(BasePermission):
|
|||||||
"""Check that a user is logged in a counter."""
|
"""Check that a user is logged in a counter."""
|
||||||
|
|
||||||
def has_permission(self, request: HttpRequest, controller: ControllerBase) -> bool:
|
def has_permission(self, request: HttpRequest, controller: ControllerBase) -> bool:
|
||||||
if "/counter/" not in request.META.get("HTTP_REFERER", ""):
|
return is_logged_in_counter(request)
|
||||||
return False
|
|
||||||
token = request.session.get("counter_token")
|
|
||||||
if not token:
|
|
||||||
return False
|
|
||||||
return Counter.objects.filter(token=token).exists()
|
|
||||||
|
|
||||||
|
|
||||||
CanAccessLookup = IsLoggedInCounter | HasPerm("core.access_lookup")
|
CanAccessLookup = IsLoggedInCounter | HasPerm("core.access_lookup")
|
||||||
|
|||||||
@@ -43,6 +43,7 @@ from core.models import BanGroup, Group, Page, PageRev, SithFile, User
|
|||||||
from core.utils import resize_image
|
from core.utils import resize_image
|
||||||
from counter.models import (
|
from counter.models import (
|
||||||
Counter,
|
Counter,
|
||||||
|
CounterSellers,
|
||||||
Price,
|
Price,
|
||||||
Product,
|
Product,
|
||||||
ProductType,
|
ProductType,
|
||||||
@@ -364,10 +365,10 @@ class Command(BaseCommand):
|
|||||||
Counter.objects.create(name="Carte AE", club=clubs.refound, type="OFFICE")
|
Counter.objects.create(name="Carte AE", club=clubs.refound, type="OFFICE")
|
||||||
|
|
||||||
# Add barman to counter
|
# Add barman to counter
|
||||||
Counter.sellers.through.objects.bulk_create(
|
CounterSellers.objects.bulk_create(
|
||||||
[
|
[
|
||||||
Counter.sellers.through(counter_id=1, user=skia), # MDE
|
CounterSellers(counter_id=1, user=skia, is_regular=True), # MDE
|
||||||
Counter.sellers.through(counter_id=2, user=krophil), # Foyer
|
CounterSellers(counter_id=2, user=krophil, is_regular=True), # Foyer
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -22,14 +22,9 @@
|
|||||||
</form>
|
</form>
|
||||||
<ul class="bars">
|
<ul class="bars">
|
||||||
{% cache 100 "counters_activity" %}
|
{% cache 100 "counters_activity" %}
|
||||||
{# The sith has no periodic tasks manager
|
{# It would be cleaner to handle the timeout with django-celery-beat,
|
||||||
and using cron jobs would be way too overkill here.
|
but doing it here is simpler and less error-prone #}
|
||||||
Thus the barmen timeout is handled in the only place that
|
{% do Counter.objects.filter(type="BAR").handle_timeout() %}
|
||||||
is loaded on every page : the header bar.
|
|
||||||
However, let's be clear : this has nothing to do here.
|
|
||||||
It's' merely a contrived workaround that should
|
|
||||||
replaced by a proper task manager as soon as possible. #}
|
|
||||||
{% set _ = Counter.objects.filter(type="BAR").handle_timeout() %}
|
|
||||||
{% endcache %}
|
{% endcache %}
|
||||||
{% for bar in Counter.objects.annotate_has_barman(user).annotate_is_open().filter(type="BAR") %}
|
{% for bar in Counter.objects.annotate_has_barman(user).annotate_is_open().filter(type="BAR") %}
|
||||||
<li>
|
<li>
|
||||||
|
|||||||
@@ -0,0 +1,64 @@
|
|||||||
|
from typing import TYPE_CHECKING, Callable
|
||||||
|
|
||||||
|
from django.db.models import Exists, OuterRef
|
||||||
|
from django.http import HttpRequest, HttpResponse
|
||||||
|
from django.utils.functional import SimpleLazyObject, empty
|
||||||
|
|
||||||
|
from core.models import User
|
||||||
|
from counter.models import Permanency
|
||||||
|
|
||||||
|
if TYPE_CHECKING:
|
||||||
|
from django.contrib.sessions.backends.base import SessionBase
|
||||||
|
|
||||||
|
|
||||||
|
SESSION_BARMEN_KEY = "barmen_ids"
|
||||||
|
|
||||||
|
|
||||||
|
def get_cached_barmen(request: HttpRequest) -> set[User]:
|
||||||
|
if not hasattr(request, "_cached_barmen"):
|
||||||
|
session: SessionBase = request.session
|
||||||
|
barmen_ids = session.get(SESSION_BARMEN_KEY, [])
|
||||||
|
if barmen_ids:
|
||||||
|
request._cached_barmen = set(
|
||||||
|
User.objects.filter(
|
||||||
|
Exists(Permanency.objects.filter(user=OuterRef("pk"), end=None)),
|
||||||
|
id__in=barmen_ids,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
request._cached_barmen = set()
|
||||||
|
|
||||||
|
return request._cached_barmen
|
||||||
|
|
||||||
|
|
||||||
|
class BarmenMiddleware:
|
||||||
|
"""Inject barmen logged in the current session.
|
||||||
|
|
||||||
|
In a similar fashion as `request.user`, `request.barmen` contains
|
||||||
|
users that are barmen in the current session, and ONLY them ;
|
||||||
|
if a user is logged as a barman on another session,
|
||||||
|
it will not be in `request.barmen`.
|
||||||
|
|
||||||
|
Notes:
|
||||||
|
In case of ended permanence, users will be automatically
|
||||||
|
removed from `request.barmen`.
|
||||||
|
However, in case of newly started permanence, this middleware
|
||||||
|
cannot add new barmen in the session data, so that operation
|
||||||
|
must be explicitly done in the barman login view.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self, get_response: Callable[[HttpRequest], HttpResponse]):
|
||||||
|
self.get_response = get_response
|
||||||
|
|
||||||
|
def __call__(self, request: HttpRequest):
|
||||||
|
request.barmen = SimpleLazyObject(lambda: get_cached_barmen(request))
|
||||||
|
|
||||||
|
response = self.get_response(request)
|
||||||
|
|
||||||
|
if request.barmen._wrapped is not empty and {
|
||||||
|
b.id for b in request.barmen
|
||||||
|
} != set(request.session.get(SESSION_BARMEN_KEY, [])):
|
||||||
|
# update the session data only if `session.barmen`
|
||||||
|
# has been accessed and modified.
|
||||||
|
request.session[SESSION_BARMEN_KEY] = [b.id for b in request.barmen]
|
||||||
|
return response
|
||||||
+7
-14
@@ -20,41 +20,34 @@
|
|||||||
# Place - Suite 330, Boston, MA 02111-1307, USA.
|
# Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||||
#
|
#
|
||||||
#
|
#
|
||||||
|
import random
|
||||||
|
|
||||||
from django.db.models.signals import pre_delete
|
from django.db.models.signals import pre_delete
|
||||||
from django.dispatch import receiver
|
from django.dispatch import receiver
|
||||||
|
|
||||||
from core.middleware import get_signal_request
|
from core.middleware import get_signal_request
|
||||||
from core.models import OperationLog
|
from core.models import OperationLog
|
||||||
from counter.models import Counter, Refilling, Selling
|
from counter.models import Refilling, Selling
|
||||||
|
|
||||||
|
|
||||||
def write_log(instance, operation_type):
|
def write_log(instance: Selling | Refilling, operation_type):
|
||||||
def get_user():
|
def get_user():
|
||||||
request = get_signal_request()
|
request = get_signal_request()
|
||||||
|
|
||||||
if not request:
|
if not request:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
# Get a random barmen if deletion is from a counter
|
if request.barmen:
|
||||||
session = getattr(request, "session", {})
|
return random.choice(list(request.barmen))
|
||||||
session_token = session.get("counter_token", None)
|
|
||||||
if session_token:
|
|
||||||
counter = Counter.objects.filter(token=session_token).first()
|
|
||||||
if counter and len(counter.barmen_list) > 0:
|
|
||||||
return counter.get_random_barman()
|
|
||||||
|
|
||||||
# Get the current logged user if not from a counter
|
# Get the current logged user if not from a counter
|
||||||
if request.user and not request.user.is_anonymous:
|
if request.user.is_authenticated:
|
||||||
return request.user
|
return request.user
|
||||||
|
|
||||||
# Return None by default
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
OperationLog(
|
OperationLog(
|
||||||
label=str(instance),
|
label=str(instance), operator=get_user(), operation_type=operation_type
|
||||||
operator=get_user(),
|
|
||||||
operation_type=operation_type,
|
|
||||||
).save()
|
).save()
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ from bs4 import BeautifulSoup
|
|||||||
from dateutil.relativedelta import relativedelta
|
from dateutil.relativedelta import relativedelta
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.contrib.auth.models import Permission, make_password
|
from django.contrib.auth.models import Permission, make_password
|
||||||
|
from django.contrib.messages import DEFAULT_LEVELS, get_messages
|
||||||
from django.http import HttpResponse
|
from django.http import HttpResponse
|
||||||
from django.shortcuts import resolve_url
|
from django.shortcuts import resolve_url
|
||||||
from django.test import Client, TestCase
|
from django.test import Client, TestCase
|
||||||
@@ -38,6 +39,7 @@ from core.models import BanGroup, Group, User
|
|||||||
from counter.baker_recipes import price_recipe, product_recipe, sale_recipe
|
from counter.baker_recipes import price_recipe, product_recipe, sale_recipe
|
||||||
from counter.models import (
|
from counter.models import (
|
||||||
Counter,
|
Counter,
|
||||||
|
CounterSellers,
|
||||||
Customer,
|
Customer,
|
||||||
Permanency,
|
Permanency,
|
||||||
ProductType,
|
ProductType,
|
||||||
@@ -67,10 +69,14 @@ class TestFullClickBase(TestCase):
|
|||||||
cls.subscriber = subscriber_user.make()
|
cls.subscriber = subscriber_user.make()
|
||||||
|
|
||||||
cls.counter = baker.make(Counter, type="BAR")
|
cls.counter = baker.make(Counter, type="BAR")
|
||||||
cls.counter.sellers.add(cls.barmen, cls.board_admin)
|
|
||||||
|
|
||||||
cls.other_counter = baker.make(Counter, type="BAR")
|
cls.other_counter = baker.make(Counter, type="BAR")
|
||||||
cls.other_counter.sellers.add(cls.barmen)
|
CounterSellers.objects.bulk_create(
|
||||||
|
[
|
||||||
|
CounterSellers(counter=cls.counter, user=cls.barmen),
|
||||||
|
CounterSellers(counter=cls.counter, user=cls.board_admin),
|
||||||
|
CounterSellers(counter=cls.other_counter, user=cls.barmen),
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
cls.yet_another_counter = baker.make(Counter, type="BAR")
|
cls.yet_another_counter = baker.make(Counter, type="BAR")
|
||||||
|
|
||||||
@@ -115,7 +121,10 @@ class TestRefilling(TestFullClickBase):
|
|||||||
) -> HttpResponse:
|
) -> HttpResponse:
|
||||||
used_client = client if client is not None else self.client
|
used_client = client if client is not None else self.client
|
||||||
return used_client.post(
|
return used_client.post(
|
||||||
reverse("counter:refilling_create", kwargs={"customer_id": user.pk}),
|
reverse(
|
||||||
|
"counter:refilling_create",
|
||||||
|
kwargs={"customer_id": user.pk, "counter_id": self.counter.pk},
|
||||||
|
),
|
||||||
{"amount": str(amount), "payment_method": Refilling.PaymentMethod.CASH},
|
{"amount": str(amount), "payment_method": Refilling.PaymentMethod.CASH},
|
||||||
HTTP_REFERER=reverse(
|
HTTP_REFERER=reverse(
|
||||||
"counter:click", kwargs={"counter_id": counter.id, "user_id": user.pk}
|
"counter:click", kwargs={"counter_id": counter.id, "user_id": user.pk}
|
||||||
@@ -139,7 +148,10 @@ class TestRefilling(TestFullClickBase):
|
|||||||
return self.client.post(
|
return self.client.post(
|
||||||
reverse(
|
reverse(
|
||||||
"counter:refilling_create",
|
"counter:refilling_create",
|
||||||
kwargs={"customer_id": self.customer.pk},
|
kwargs={
|
||||||
|
"customer_id": self.customer.pk,
|
||||||
|
"counter_id": self.counter.pk,
|
||||||
|
},
|
||||||
),
|
),
|
||||||
{"amount": "10", "payment_method": "CASH"},
|
{"amount": "10", "payment_method": "CASH"},
|
||||||
)
|
)
|
||||||
@@ -443,9 +455,19 @@ class TestCounterClick(TestFullClickBase):
|
|||||||
|
|
||||||
def test_click_not_connected(self):
|
def test_click_not_connected(self):
|
||||||
force_refill_user(self.customer, 10)
|
force_refill_user(self.customer, 10)
|
||||||
|
|
||||||
|
# trying to click on a bar without being logged should result
|
||||||
|
# in a redirect to the counter page with an error message
|
||||||
res = self.submit_basket(self.customer, [BasketItem(self.snack.id, 2)])
|
res = self.submit_basket(self.customer, [BasketItem(self.snack.id, 2)])
|
||||||
assertRedirects(res, self.counter.get_absolute_url())
|
assertRedirects(res, self.counter.get_absolute_url())
|
||||||
|
messages = list(get_messages(res.wsgi_request))
|
||||||
|
assert len(messages) == 1
|
||||||
|
assert messages[0].level == DEFAULT_LEVELS["ERROR"]
|
||||||
|
assert (
|
||||||
|
messages[0].message == "Vous ne pouvez pas cliquer des gens sur ce comptoir"
|
||||||
|
)
|
||||||
|
|
||||||
|
# trying to click on an office counter without permission should 403
|
||||||
res = self.submit_basket(
|
res = self.submit_basket(
|
||||||
self.customer, [BasketItem(self.snack.id, 2)], counter=self.club_counter
|
self.customer, [BasketItem(self.snack.id, 2)], counter=self.club_counter
|
||||||
)
|
)
|
||||||
@@ -738,6 +760,7 @@ class TestBarmanConnection(TestCase):
|
|||||||
assert last_perm.counter == self.counter
|
assert last_perm.counter == self.counter
|
||||||
assert last_perm.user == self.barman
|
assert last_perm.user == self.barman
|
||||||
assert last_perm.end is None
|
assert last_perm.end is None
|
||||||
|
assert self.barman in response.wsgi_request.barmen
|
||||||
response = self.client.get(
|
response = self.client.get(
|
||||||
self.detail_url, {"username": self.barman.username, "password": "plop"}
|
self.detail_url, {"username": self.barman.username, "password": "plop"}
|
||||||
)
|
)
|
||||||
@@ -754,6 +777,7 @@ class TestBarmanConnection(TestCase):
|
|||||||
)
|
)
|
||||||
assert "HX-Redirect" not in response.headers
|
assert "HX-Redirect" not in response.headers
|
||||||
assert not Permanency.objects.filter(user=not_barman).exists()
|
assert not Permanency.objects.filter(user=not_barman).exists()
|
||||||
|
assert self.barman not in response.wsgi_request.barmen
|
||||||
|
|
||||||
response = self.client.get(self.detail_url)
|
response = self.client.get(self.detail_url)
|
||||||
assert response.context_data.get("barmen") == []
|
assert response.context_data.get("barmen") == []
|
||||||
@@ -762,11 +786,11 @@ class TestBarmanConnection(TestCase):
|
|||||||
|
|
||||||
|
|
||||||
@pytest.mark.django_db
|
@pytest.mark.django_db
|
||||||
def test_barman_timeout():
|
def test_barman_timeout(client: Client):
|
||||||
"""Test that barmen timeout is well managed."""
|
"""Test that barmen timeout is well managed."""
|
||||||
bar = baker.make(Counter, type="BAR")
|
bar = baker.make(Counter, type="BAR")
|
||||||
user = baker.make(User)
|
user = baker.make(User)
|
||||||
bar.sellers.add(user)
|
CounterSellers.objects.create(counter=bar, user=user)
|
||||||
baker.make(Permanency, counter=bar, user=user, start=now())
|
baker.make(Permanency, counter=bar, user=user, start=now())
|
||||||
|
|
||||||
qs = Counter.objects.annotate_is_open().filter(pk=bar.pk)
|
qs = Counter.objects.annotate_is_open().filter(pk=bar.pk)
|
||||||
@@ -782,6 +806,8 @@ def test_barman_timeout():
|
|||||||
bar = qs[0]
|
bar = qs[0]
|
||||||
assert not bar.is_open
|
assert not bar.is_open
|
||||||
assert bar.barmen_list == []
|
assert bar.barmen_list == []
|
||||||
|
res = client.get("")
|
||||||
|
assert res.wsgi_request.barmen == set()
|
||||||
|
|
||||||
|
|
||||||
class TestClubCounterClickAccess(TestCase):
|
class TestClubCounterClickAccess(TestCase):
|
||||||
@@ -831,14 +857,14 @@ class TestClubCounterClickAccess(TestCase):
|
|||||||
|
|
||||||
def test_barman(self):
|
def test_barman(self):
|
||||||
"""Sellers should be able to click on office counters"""
|
"""Sellers should be able to click on office counters"""
|
||||||
self.counter.sellers.add(self.user)
|
CounterSellers.objects.create(counter=self.counter, user=self.user)
|
||||||
self.client.force_login(self.user)
|
self.client.force_login(self.user)
|
||||||
res = self.client.get(self.click_url)
|
res = self.client.get(self.click_url)
|
||||||
assert res.status_code == 200
|
assert res.status_code == 200
|
||||||
|
|
||||||
def test_both_barman_and_board_member(self):
|
def test_both_barman_and_board_member(self):
|
||||||
"""If the user is barman and board member, he should be authorized as well."""
|
"""If the user is barman and board member, he should be authorized as well."""
|
||||||
self.counter.sellers.add(self.user)
|
CounterSellers.objects.create(counter=self.counter, user=self.user)
|
||||||
baker.make(
|
baker.make(
|
||||||
Membership, club=self.counter.club, user=self.user, role=self.board_role
|
Membership, club=self.counter.club, user=self.user, role=self.board_role
|
||||||
)
|
)
|
||||||
@@ -862,16 +888,17 @@ class TestCounterLogout:
|
|||||||
reverse("counter:logout", kwargs={"counter_id": permanence.counter_id}),
|
reverse("counter:logout", kwargs={"counter_id": permanence.counter_id}),
|
||||||
data={"user_id": permanence.user_id},
|
data={"user_id": permanence.user_id},
|
||||||
)
|
)
|
||||||
assertRedirects(
|
assertRedirects(
|
||||||
res,
|
res,
|
||||||
reverse(
|
reverse("counter:details", kwargs={"counter_id": permanence.counter_id}),
|
||||||
"counter:details", kwargs={"counter_id": permanence.counter_id}
|
)
|
||||||
),
|
permanence.refresh_from_db()
|
||||||
)
|
assert permanence.end == permanence.activity
|
||||||
permanence.refresh_from_db()
|
assert permanence.user not in res.wsgi_request.barmen
|
||||||
assert permanence.end == now()
|
|
||||||
|
|
||||||
def test_logout_doesnt_change_old_permanences(self, client: Client):
|
def test_logout_doesnt_change_old_permanences(self, client: Client):
|
||||||
|
# regression test for #1141
|
||||||
|
# https://github.com/ae-utbm/sith/pull/1141
|
||||||
perm_counter = baker.make(Counter, type="BAR")
|
perm_counter = baker.make(Counter, type="BAR")
|
||||||
permanence = baker.make(
|
permanence = baker.make(
|
||||||
Permanency,
|
Permanency,
|
||||||
@@ -892,6 +919,6 @@ class TestCounterLogout:
|
|||||||
data={"user_id": permanence.user_id},
|
data={"user_id": permanence.user_id},
|
||||||
)
|
)
|
||||||
permanence.refresh_from_db()
|
permanence.refresh_from_db()
|
||||||
assert permanence.end == now()
|
assert permanence.end == permanence.activity
|
||||||
old_permanence.refresh_from_db()
|
old_permanence.refresh_from_db()
|
||||||
assert old_permanence.end == old_end
|
assert old_permanence.end == old_end
|
||||||
|
|||||||
+1
-1
@@ -67,7 +67,7 @@ urlpatterns = [
|
|||||||
path("<int:counter_id>/", CounterMain.as_view(), name="details"),
|
path("<int:counter_id>/", CounterMain.as_view(), name="details"),
|
||||||
path("<int:counter_id>/click/<int:user_id>/", CounterClick.as_view(), name="click"),
|
path("<int:counter_id>/click/<int:user_id>/", CounterClick.as_view(), name="click"),
|
||||||
path(
|
path(
|
||||||
"refill/<int:customer_id>/",
|
"<int:counter_id>/refill/<int:customer_id>/",
|
||||||
RefillingCreateView.as_view(),
|
RefillingCreateView.as_view(),
|
||||||
name="refilling_create",
|
name="refilling_create",
|
||||||
),
|
),
|
||||||
|
|||||||
+3
-16
@@ -3,8 +3,6 @@ from urllib.parse import urlparse
|
|||||||
from django.http import HttpRequest
|
from django.http import HttpRequest
|
||||||
from django.urls import resolve
|
from django.urls import resolve
|
||||||
|
|
||||||
from counter.models import Counter
|
|
||||||
|
|
||||||
|
|
||||||
def is_logged_in_counter(request: HttpRequest) -> bool:
|
def is_logged_in_counter(request: HttpRequest) -> bool:
|
||||||
"""Check if the request is sent from a device logged to a counter.
|
"""Check if the request is sent from a device logged to a counter.
|
||||||
@@ -20,24 +18,13 @@ def is_logged_in_counter(request: HttpRequest) -> bool:
|
|||||||
or the request path belongs to the counter app
|
or the request path belongs to the counter app
|
||||||
(eg. the barman went back to the main by missclick and go back
|
(eg. the barman went back to the main by missclick and go back
|
||||||
to the counter)
|
to the counter)
|
||||||
- The current session has a counter token associated with it.
|
- There are barmen logged in the current session
|
||||||
- A counter with this token exists.
|
|
||||||
- The counter is open
|
|
||||||
"""
|
"""
|
||||||
referer_ok = (
|
referer_ok = (
|
||||||
"HTTP_REFERER" in request.META
|
"HTTP_REFERER" in request.META
|
||||||
and resolve(urlparse(request.META["HTTP_REFERER"]).path).app_name == "counter"
|
and resolve(urlparse(request.META["HTTP_REFERER"]).path).app_name == "counter"
|
||||||
)
|
)
|
||||||
has_token = (
|
if not referer_ok and request.resolver_match.app_name != "counter":
|
||||||
(referer_ok or request.resolver_match.app_name == "counter")
|
|
||||||
and "counter_token" in request.session
|
|
||||||
and request.session["counter_token"]
|
|
||||||
)
|
|
||||||
if not has_token:
|
|
||||||
return False
|
return False
|
||||||
|
|
||||||
return (
|
return bool(request.barmen)
|
||||||
Counter.objects.annotate_is_open()
|
|
||||||
.filter(token=request.session["counter_token"], is_open=True)
|
|
||||||
.exists()
|
|
||||||
)
|
|
||||||
|
|||||||
+15
-10
@@ -12,6 +12,7 @@
|
|||||||
# OR WITHIN THE LOCAL FILE "LICENSE"
|
# OR WITHIN THE LOCAL FILE "LICENSE"
|
||||||
#
|
#
|
||||||
#
|
#
|
||||||
|
import random
|
||||||
from collections import defaultdict
|
from collections import defaultdict
|
||||||
|
|
||||||
from django.contrib import messages
|
from django.contrib import messages
|
||||||
@@ -42,7 +43,7 @@ def get_operator(request: HttpRequest, counter: Counter, customer: Customer) ->
|
|||||||
return request.user
|
return request.user
|
||||||
if counter.customer_is_barman(customer):
|
if counter.customer_is_barman(customer):
|
||||||
return customer.user
|
return customer.user
|
||||||
return counter.get_random_barman()
|
return random.choice(list(request.barmen))
|
||||||
|
|
||||||
|
|
||||||
class CounterClick(
|
class CounterClick(
|
||||||
@@ -74,7 +75,7 @@ class CounterClick(
|
|||||||
return kwargs
|
return kwargs
|
||||||
|
|
||||||
def dispatch(self, request, *args, **kwargs):
|
def dispatch(self, request, *args, **kwargs):
|
||||||
self.customer = get_object_or_404(Customer, user__id=self.kwargs["user_id"])
|
self.customer = get_object_or_404(Customer, user_id=self.kwargs["user_id"])
|
||||||
obj: Counter = self.get_object()
|
obj: Counter = self.get_object()
|
||||||
|
|
||||||
if not self.customer.can_buy or self.customer.user.is_banned_counter:
|
if not self.customer.can_buy or self.customer.user.is_banned_counter:
|
||||||
@@ -92,8 +93,8 @@ class CounterClick(
|
|||||||
# or a seller of this counter.
|
# or a seller of this counter.
|
||||||
raise PermissionDenied
|
raise PermissionDenied
|
||||||
|
|
||||||
if obj.type == "BAR" and (
|
if obj.type == "BAR" and not (
|
||||||
not obj.is_open or request.session.get("counter_token", "") != obj.token
|
request.barmen and request.barmen.issubset(set(obj.barmen_list))
|
||||||
):
|
):
|
||||||
messages.error(request, _("You cannot click users on this counter"))
|
messages.error(request, _("You cannot click users on this counter"))
|
||||||
return redirect(obj) # Redirect to counter
|
return redirect(obj) # Redirect to counter
|
||||||
@@ -194,7 +195,7 @@ class CounterClick(
|
|||||||
)
|
)
|
||||||
if self.object.can_refill():
|
if self.object.can_refill():
|
||||||
res["refilling_fragment"] = RefillingCreateView.as_fragment()(
|
res["refilling_fragment"] = RefillingCreateView.as_fragment()(
|
||||||
self.request, customer=self.customer
|
self.request, customer=self.customer, counter=self.object
|
||||||
)
|
)
|
||||||
return res
|
return res
|
||||||
|
|
||||||
@@ -232,11 +233,13 @@ class RefillingCreateView(FragmentMixin, FormView):
|
|||||||
if not is_logged_in_counter(request):
|
if not is_logged_in_counter(request):
|
||||||
raise PermissionDenied
|
raise PermissionDenied
|
||||||
|
|
||||||
self.counter: Counter = get_object_or_404(
|
self.counter: Counter = get_object_or_404(Counter, id=self.kwargs["counter_id"])
|
||||||
Counter, token=request.session["counter_token"]
|
|
||||||
)
|
|
||||||
|
|
||||||
if not self.counter.can_refill():
|
if not (
|
||||||
|
request.barmen
|
||||||
|
and request.barmen.issubset(self.counter.barmen_list)
|
||||||
|
and self.counter.can_refill()
|
||||||
|
):
|
||||||
raise PermissionDenied
|
raise PermissionDenied
|
||||||
|
|
||||||
self.operator = get_operator(request, self.counter, self.customer)
|
self.operator = get_operator(request, self.counter, self.customer)
|
||||||
@@ -245,6 +248,7 @@ class RefillingCreateView(FragmentMixin, FormView):
|
|||||||
|
|
||||||
def render_fragment(self, request, **kwargs) -> SafeString:
|
def render_fragment(self, request, **kwargs) -> SafeString:
|
||||||
self.customer = kwargs.pop("customer")
|
self.customer = kwargs.pop("customer")
|
||||||
|
self.counter = kwargs.pop("counter")
|
||||||
return super().render_fragment(request, **kwargs)
|
return super().render_fragment(request, **kwargs)
|
||||||
|
|
||||||
def form_valid(self, form):
|
def form_valid(self, form):
|
||||||
@@ -259,7 +263,8 @@ class RefillingCreateView(FragmentMixin, FormView):
|
|||||||
def get_context_data(self, **kwargs):
|
def get_context_data(self, **kwargs):
|
||||||
kwargs = super().get_context_data(**kwargs)
|
kwargs = super().get_context_data(**kwargs)
|
||||||
kwargs["action"] = reverse(
|
kwargs["action"] = reverse(
|
||||||
"counter:refilling_create", kwargs={"customer_id": self.customer.pk}
|
"counter:refilling_create",
|
||||||
|
kwargs={"customer_id": self.customer.pk, "counter_id": self.counter.pk},
|
||||||
)
|
)
|
||||||
return kwargs
|
return kwargs
|
||||||
|
|
||||||
|
|||||||
@@ -15,12 +15,12 @@
|
|||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
|
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
|
from django.db.models import F
|
||||||
from django.http import HttpRequest, HttpResponseRedirect
|
from django.http import HttpRequest, HttpResponseRedirect
|
||||||
from django.shortcuts import redirect
|
from django.shortcuts import redirect
|
||||||
from django.urls import reverse
|
from django.urls import reverse
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
from django.utils.safestring import SafeString
|
from django.utils.safestring import SafeString
|
||||||
from django.utils.timezone import now
|
|
||||||
from django.views.decorators.http import require_POST
|
from django.views.decorators.http import require_POST
|
||||||
from django.views.generic import DetailView
|
from django.views.generic import DetailView
|
||||||
from django.views.generic.detail import SingleObjectMixin
|
from django.views.generic.detail import SingleObjectMixin
|
||||||
@@ -49,10 +49,9 @@ class CounterLoginFragment(FragmentMixin, SingleObjectMixin, FormView):
|
|||||||
return super().get_form_kwargs() | {"counter": self.object}
|
return super().get_form_kwargs() | {"counter": self.object}
|
||||||
|
|
||||||
def form_valid(self, form: CounterLoginForm):
|
def form_valid(self, form: CounterLoginForm):
|
||||||
self.object.permanencies.create(user=form.get_user(), start=timezone.now())
|
user = form.get_user()
|
||||||
if not self.object.barmen_list:
|
self.object.permanencies.create(user=user, start=timezone.now())
|
||||||
self.object.gen_token()
|
self.request.barmen.add(user)
|
||||||
self.request.session["counter_token"] = self.object.token
|
|
||||||
self.success_url = reverse(
|
self.success_url = reverse(
|
||||||
"counter:details", kwargs={"counter_id": self.object.id}
|
"counter:details", kwargs={"counter_id": self.object.id}
|
||||||
)
|
)
|
||||||
@@ -92,8 +91,8 @@ class CounterMain(
|
|||||||
def dispatch(self, request, *args, **kwargs):
|
def dispatch(self, request, *args, **kwargs):
|
||||||
self.object: Counter = self.get_object()
|
self.object: Counter = self.get_object()
|
||||||
if self.object.type != "BAR" and self.request.method.upper() == "POST":
|
if self.object.type != "BAR" and self.request.method.upper() == "POST":
|
||||||
# barmen have to log in (thus do a POST request)
|
# barmen have to log in (thus do a POST request) only if it is a bar,
|
||||||
# only if it is a bar.
|
# so a POST on a non-bar counter makes no sense
|
||||||
return self.http_method_not_allowed(request, *args, **kwargs)
|
return self.http_method_not_allowed(request, *args, **kwargs)
|
||||||
if self.object.type == "BAR":
|
if self.object.type == "BAR":
|
||||||
self.object.update_activity()
|
self.object.update_activity()
|
||||||
@@ -115,7 +114,8 @@ class CounterMain(
|
|||||||
kwargs["can_click"] = (
|
kwargs["can_click"] = (
|
||||||
self.object.type == "BAR"
|
self.object.type == "BAR"
|
||||||
and self.object.is_open
|
and self.object.is_open
|
||||||
and self.request.session.get("counter_token", "") == self.object.token
|
and self.request.barmen
|
||||||
|
and self.request.barmen.issubset(set(self.object.barmen_list))
|
||||||
) or (
|
) or (
|
||||||
self.object.type == "OFFICE"
|
self.object.type == "OFFICE"
|
||||||
and (
|
and (
|
||||||
|
|||||||
+6
-3
@@ -34,6 +34,7 @@ https://docs.djangoproject.com/en/1.8/ref/settings/
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
import binascii
|
import binascii
|
||||||
|
import contextlib
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
@@ -41,6 +42,7 @@ from pathlib import Path
|
|||||||
|
|
||||||
import sentry_sdk
|
import sentry_sdk
|
||||||
from dateutil.relativedelta import relativedelta
|
from dateutil.relativedelta import relativedelta
|
||||||
|
from django.utils.deprecation import RemovedInDjango60Warning
|
||||||
from django.utils.translation import gettext_lazy as _
|
from django.utils.translation import gettext_lazy as _
|
||||||
from environs import Env
|
from environs import Env
|
||||||
from sentry_sdk.integrations.django import DjangoIntegration
|
from sentry_sdk.integrations.django import DjangoIntegration
|
||||||
@@ -91,7 +93,8 @@ ALLOWED_HOSTS = ["*"]
|
|||||||
# RemovedInDjango60Warning: It's a transitional setting helpful in early
|
# RemovedInDjango60Warning: It's a transitional setting helpful in early
|
||||||
# adoption of "https" as the new default value of forms.URLField.assume_scheme.
|
# adoption of "https" as the new default value of forms.URLField.assume_scheme.
|
||||||
# Remove this after upgrading to Django 6.x
|
# Remove this after upgrading to Django 6.x
|
||||||
FORMS_URLFIELD_ASSUME_HTTPS = True
|
with contextlib.suppress(RemovedInDjango60Warning):
|
||||||
|
FORMS_URLFIELD_ASSUME_HTTPS = True
|
||||||
|
|
||||||
# Application definition
|
# Application definition
|
||||||
|
|
||||||
@@ -138,13 +141,13 @@ MIDDLEWARE = (
|
|||||||
"django.contrib.sessions.middleware.SessionMiddleware",
|
"django.contrib.sessions.middleware.SessionMiddleware",
|
||||||
"django.middleware.common.CommonMiddleware",
|
"django.middleware.common.CommonMiddleware",
|
||||||
"django.middleware.csrf.CsrfViewMiddleware",
|
"django.middleware.csrf.CsrfViewMiddleware",
|
||||||
"django.contrib.auth.middleware.AuthenticationMiddleware",
|
"core.middleware.AuthenticationMiddleware",
|
||||||
"django.contrib.messages.middleware.MessageMiddleware",
|
"django.contrib.messages.middleware.MessageMiddleware",
|
||||||
"django.middleware.locale.LocaleMiddleware",
|
"django.middleware.locale.LocaleMiddleware",
|
||||||
"django.middleware.clickjacking.XFrameOptionsMiddleware",
|
"django.middleware.clickjacking.XFrameOptionsMiddleware",
|
||||||
"django.middleware.security.SecurityMiddleware",
|
"django.middleware.security.SecurityMiddleware",
|
||||||
"core.middleware.AuthenticationMiddleware",
|
|
||||||
"core.middleware.SignalRequestMiddleware",
|
"core.middleware.SignalRequestMiddleware",
|
||||||
|
"counter.middleware.BarmenMiddleware",
|
||||||
)
|
)
|
||||||
|
|
||||||
ROOT_URLCONF = "sith.urls"
|
ROOT_URLCONF = "sith.urls"
|
||||||
|
|||||||
Reference in New Issue
Block a user