mirror of
https://github.com/ae-utbm/sith.git
synced 2026-06-04 23:29:24 +00:00
show barmen logged on current device in counter
This commit is contained in:
+17
-5
@@ -9,6 +9,7 @@ from django import forms
|
||||
from django.core.exceptions import ValidationError
|
||||
from django.db.models import Exists, OuterRef, Q
|
||||
from django.forms import BaseModelFormSet
|
||||
from django.http import HttpRequest
|
||||
from django.utils.timezone import now
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
from django_celery_beat.models import ClockedSchedule
|
||||
@@ -131,9 +132,18 @@ class GetUserForm(forms.Form):
|
||||
|
||||
|
||||
class CounterLoginForm(LoginForm):
|
||||
def __init__(self, *args, counter: Counter, **kwargs):
|
||||
"""LoginForm to log a barman in a counter.
|
||||
|
||||
To be able to log in a counter, a user must :
|
||||
|
||||
- be part of the sellers of the given counter
|
||||
- not being already logged in any counter
|
||||
"""
|
||||
|
||||
def __init__(self, *args, request: HttpRequest, counter: Counter, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
self.counter = counter
|
||||
self.request = request
|
||||
|
||||
def confirm_login_allowed(self, user: User):
|
||||
super().confirm_login_allowed(user)
|
||||
@@ -141,11 +151,13 @@ class CounterLoginForm(LoginForm):
|
||||
raise ValidationError(
|
||||
message=_("You are not a barman of this counter."), code="not_barman"
|
||||
)
|
||||
if user in self.counter.barmen_list:
|
||||
raise ValidationError(
|
||||
message=_("You are already logged in this counter."),
|
||||
code="not_logged_in",
|
||||
if user in self.request.barmen:
|
||||
message = (
|
||||
_("You are already logged in this counter.")
|
||||
if user in self.counter.barmen_list
|
||||
else _("You are already logged in another counter.")
|
||||
)
|
||||
raise ValidationError(message=message, code="already_logged_in")
|
||||
|
||||
|
||||
class RefillForm(forms.ModelForm):
|
||||
|
||||
@@ -44,10 +44,35 @@
|
||||
{% endif %}
|
||||
</div>
|
||||
{% if counter.type == 'BAR' %}
|
||||
<h3>{% trans %}Barman: {% endtrans %}</h3>
|
||||
<h3>{% trans %}Barmen:{% endtrans %}</h3>
|
||||
|
||||
{% if barmen_here %}
|
||||
<div class="row gap-2x">
|
||||
<div>
|
||||
<h4>{% trans %}On this device{% endtrans %}</h4>
|
||||
{% for b in barmen_here %}
|
||||
<p>{{ barman_logout_link(b) }}</p>
|
||||
{% endfor %}
|
||||
</div>
|
||||
<div>
|
||||
<h4>{% trans %}Elsewhere{% endtrans %}</h4>
|
||||
{% if barmen_here|length == barmen|length %}
|
||||
{# all logged barmen are logged in this session #}
|
||||
<p><em>{% trans %}No barman logged elsewhere{% endtrans %}</em></p>
|
||||
{% else %}
|
||||
{% for b in barmen %}
|
||||
{%- if b not in barmen_here -%}
|
||||
<p>{{ barman_logout_link(b) }}</p>
|
||||
{%- endif -%}
|
||||
{% endfor %}
|
||||
{% endif %}
|
||||
</div>
|
||||
</div>
|
||||
{% else %}
|
||||
{% for b in barmen %}
|
||||
<p>{{ barman_logout_link(b) }}</p>
|
||||
{% endfor %}
|
||||
{% endif %}
|
||||
{{ login_fragment }}
|
||||
{% endif %}
|
||||
{% endblock %}
|
||||
|
||||
+10
-5
@@ -46,9 +46,12 @@ class CounterLoginFragment(FragmentMixin, SingleObjectMixin, FormView):
|
||||
return super().dispatch(request, *args, **kwargs)
|
||||
|
||||
def get_form_kwargs(self):
|
||||
return super().get_form_kwargs() | {"counter": self.object}
|
||||
return super().get_form_kwargs() | {
|
||||
"request": self.request,
|
||||
"counter": self.object,
|
||||
}
|
||||
|
||||
def form_valid(self, form: CounterLoginForm):
|
||||
def form_valid(self, form: GetUserForm):
|
||||
user = form.get_user()
|
||||
self.object.permanencies.create(user=user, start=timezone.now())
|
||||
self.request.barmen.add(user)
|
||||
@@ -82,7 +85,7 @@ class CounterMain(
|
||||
"""The public (barman) view."""
|
||||
|
||||
model = Counter
|
||||
queryset = Counter.objects.annotate_is_open().exclude(type="EBOUTIC")
|
||||
queryset = Counter.objects.exclude(type="EBOUTIC")
|
||||
template_name = "counter/counter_main.jinja"
|
||||
pk_url_kwarg = "counter_id"
|
||||
form_class = GetUserForm
|
||||
@@ -111,9 +114,11 @@ class CounterMain(
|
||||
kwargs = super().get_context_data(**kwargs)
|
||||
if self.object.type == "BAR":
|
||||
kwargs["barmen"] = self.object.barmen_list
|
||||
kwargs["barmen_here"] = list(
|
||||
self.request.barmen.intersection(self.object.barmen_list)
|
||||
)
|
||||
kwargs["can_click"] = (
|
||||
self.object.type == "BAR"
|
||||
and self.object.is_open
|
||||
and self.request.barmen
|
||||
and self.request.barmen.issubset(set(self.object.barmen_list))
|
||||
) or (
|
||||
@@ -132,7 +137,7 @@ class CounterMain(
|
||||
)
|
||||
return kwargs
|
||||
|
||||
def form_valid(self, form: CounterLoginForm):
|
||||
def form_valid(self, form: GetUserForm):
|
||||
"""We handle here the redirection, passing the user id of the asked customer."""
|
||||
self.success_url = reverse(
|
||||
"counter:click",
|
||||
|
||||
Reference in New Issue
Block a user