diff --git a/core/management/commands/populate.py b/core/management/commands/populate.py index 05a650d0..a85da98e 100644 --- a/core/management/commands/populate.py +++ b/core/management/commands/populate.py @@ -747,6 +747,7 @@ class Command(BaseCommand): "add_subscription", "add_membership", "view_hidden_user", + "add_refilling", ] ) ) diff --git a/counter/models.py b/counter/models.py index 7f6db9b4..1ccf1d8f 100644 --- a/counter/models.py +++ b/counter/models.py @@ -669,13 +669,18 @@ class Counter(models.Model): """Update the barman activity to prevent timeout.""" self.permanencies.filter(end=None).update(activity=timezone.now()) + @cached_property def can_refill(self) -> bool: - """Show if the counter authorize the refilling with physic money.""" - if self.type != "BAR": - return False - # at least one of the barmen is in the AE board - ae = Club.objects.get(id=settings.SITH_MAIN_CLUB_ID) - return any(ae.get_membership_for(barman) for barman in self.barmen_list) + """Show if the counter authorize the refilling with physic money. + + Refills are authorized if a user having the required permission + is currently logged in. + """ + return self.type == "BAR" and ( + User.objects.with_perm("counter.add_refilling") + .filter(id__in=[u.id for u in self.barmen_list]) + .exists() + ) def get_top_barmen(self) -> QuerySet: """Return a QuerySet querying the office hours stats of all the barmen of all time diff --git a/counter/views/click.py b/counter/views/click.py index f13444ef..5398eb92 100644 --- a/counter/views/click.py +++ b/counter/views/click.py @@ -204,7 +204,7 @@ class CounterClick( res["student_card_fragment"] = StudentCardFormFragment.as_fragment()( self.request, customer=self.customer ) - if self.object.can_refill(): + if self.object.can_refill: res["refilling_fragment"] = RefillingCreateView.as_fragment()( self.request, customer=self.customer, counter=self.object ) @@ -250,7 +250,7 @@ class RefillingCreateView(FragmentMixin, CreateView): if not ( request.barmen and request.barmen.issubset(self.counter.barmen_list) - and self.counter.can_refill() + and self.counter.can_refill ): raise PermissionDenied