Better usage of cache for groups and clubs related operations (#634)

* Better usage of cache for group retrieval

* Cache clearing on object deletion or update

* replace signals by save and delete override

* add is_anonymous check in is_owned_by

Add in many is_owned_by(self, user) methods that user is not anonymous. Since many of those functions do db queries, this should reduce a little bit the load of the db.

* Stricter usage of User.is_in_group

Constrain the parameters that can be passed to the function to make sure only a str or an int can be used. Also force to explicitly specify if the group id or the group name is used.

* write test and correct bugs

* remove forgotten populate commands

* Correct test
This commit is contained in:
thomas girod
2023-05-02 12:36:59 +02:00
committed by GitHub
parent 96dede5077
commit ef968f3673
50 changed files with 1315 additions and 699 deletions

View File

@ -228,7 +228,9 @@ class ProductType(models.Model):
"""
Method to see if that object can be edited by the given user
"""
if user.is_in_group(settings.SITH_GROUP_ACCOUNTING_ADMIN_ID):
if user.is_anonymous:
return False
if user.is_in_group(pk=settings.SITH_GROUP_ACCOUNTING_ADMIN_ID):
return True
return False
@ -294,9 +296,11 @@ class Product(models.Model):
"""
Method to see if that object can be edited by the given user
"""
if user.is_anonymous:
return False
if user.is_in_group(
settings.SITH_GROUP_ACCOUNTING_ADMIN_ID
) or user.is_in_group(settings.SITH_GROUP_COUNTER_ADMIN_ID):
pk=settings.SITH_GROUP_ACCOUNTING_ADMIN_ID
) or user.is_in_group(pk=settings.SITH_GROUP_COUNTER_ADMIN_ID):
return True
return False
@ -318,8 +322,8 @@ class Product(models.Model):
"""
if not self.buying_groups.exists():
return True
for group in self.buying_groups.all():
if user.is_in_group(group.name):
for group_id in self.buying_groups.values_list("pk", flat=True):
if user.is_in_group(pk=group_id):
return True
return False
@ -402,18 +406,17 @@ class Counter(models.Model):
return reverse("counter:details", kwargs={"counter_id": self.id})
def is_owned_by(self, user):
if user.is_anonymous:
return False
mem = self.club.get_membership_for(user)
if mem and mem.role >= 7:
return True
return user.is_in_group(settings.SITH_GROUP_COUNTER_ADMIN_ID)
return user.is_in_group(pk=settings.SITH_GROUP_COUNTER_ADMIN_ID)
def can_be_viewed_by(self, user):
if self.type == "BAR":
return True
return (
user.is_in_group(settings.SITH_MAIN_BOARD_GROUP)
or user in self.sellers.all()
)
return user.is_board_member or user in self.sellers.all()
def gen_token(self):
"""Generate a new token for this counter"""
@ -621,6 +624,8 @@ class Refilling(models.Model):
)
def is_owned_by(self, user):
if user.is_anonymous:
return False
return user.is_owner(self.counter) and self.payment_method != "CARD"
def delete(self, *args, **kwargs):
@ -713,6 +718,8 @@ class Selling(models.Model):
)
def is_owned_by(self, user):
if user.is_anonymous:
return False
return user.is_owner(self.counter) and self.payment_method != "CARD"
def can_be_viewed_by(self, user):
@ -953,7 +960,9 @@ class CashRegisterSummary(models.Model):
"""
Method to see if that object can be edited by the given user
"""
if user.is_in_group(settings.SITH_GROUP_COUNTER_ADMIN_ID):
if user.is_anonymous:
return False
if user.is_in_group(pk=settings.SITH_GROUP_COUNTER_ADMIN_ID):
return True
return False
@ -1022,7 +1031,9 @@ class Eticket(models.Model):
"""
Method to see if that object can be edited by the given user
"""
return user.is_in_group(settings.SITH_GROUP_COUNTER_ADMIN_ID)
if user.is_anonymous:
return False
return user.is_in_group(pk=settings.SITH_GROUP_COUNTER_ADMIN_ID)
def get_hash(self, string):
import hashlib

View File

@ -87,8 +87,8 @@ class CounterAdminMixin(View):
edit_club = []
def _test_group(self, user):
for g in self.edit_group:
if user.is_in_group(g):
for grp_id in self.edit_group:
if user.is_in_group(pk=grp_id):
return True
return False
@ -486,14 +486,12 @@ class CounterClick(CounterTabsMixin, CanViewMixin, DetailView):
pid = str(pid)
price = self.get_price(pid)
total = self.sum_basket(request)
product = self.get_product(pid)
can_buy = False
if not product.buying_groups.exists():
can_buy = True
else:
for g in product.buying_groups.all():
if self.customer.user.is_in_group(g.name):
can_buy = True
product: Product = self.get_product(pid)
user: User = self.customer.user
buying_groups = list(product.buying_groups.values_list("pk", flat=True))
can_buy = len(buying_groups) == 0 or any(
user.is_in_group(pk=group_id) for group_id in buying_groups
)
if not can_buy:
request.session["not_allowed"] = True
return False
@ -514,18 +512,17 @@ class CounterClick(CounterTabsMixin, CanViewMixin, DetailView):
):
request.session["not_allowed"] = True
return False
if product.limit_age >= 18 and not self.customer.user.date_of_birth:
if product.limit_age >= 18 and not user.date_of_birth:
request.session["no_age"] = True
return False
if product.limit_age >= 18 and self.customer.user.is_banned_alcohol:
if product.limit_age >= 18 and user.is_banned_alcohol:
request.session["not_allowed"] = True
return False
if self.customer.user.is_banned_counter:
if user.is_banned_counter:
request.session["not_allowed"] = True
return False
if (
self.customer.user.date_of_birth
and self.customer.user.get_age() < product.limit_age
user.date_of_birth and self.customer.user.get_age() < product.limit_age
): # Check if affordable
request.session["too_young"] = True
return False