mirror of
https://github.com/ae-utbm/sith.git
synced 2025-07-03 08:35:16 +00:00
replace signals by save and delete override
This commit is contained in:
parent
437254d0de
commit
09e1746b30
33
club/apps.py
33
club/apps.py
@ -1,33 +0,0 @@
|
|||||||
# -*- coding:utf-8 -*
|
|
||||||
#
|
|
||||||
# Copyright 2023
|
|
||||||
# - Maréchal <thgirod@hotmail.com>
|
|
||||||
#
|
|
||||||
# Ce fichier fait partie du site de l'Association des Étudiants de l'UTBM,
|
|
||||||
# http://ae.utbm.fr.
|
|
||||||
#
|
|
||||||
# This program is free software; you can redistribute it and/or modify it under
|
|
||||||
# the terms of the GNU General Public License a published by the Free Software
|
|
||||||
# Foundation; either version 3 of the License, or (at your option) any later
|
|
||||||
# version.
|
|
||||||
#
|
|
||||||
# This program is distributed in the hope that it will be useful, but WITHOUT
|
|
||||||
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
||||||
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
|
||||||
# details.
|
|
||||||
#
|
|
||||||
# You should have received a copy of the GNU General Public License along with
|
|
||||||
# this program; if not, write to the Free Sofware Foundation, Inc., 59 Temple
|
|
||||||
# Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
||||||
#
|
|
||||||
#
|
|
||||||
|
|
||||||
from django.apps import AppConfig
|
|
||||||
|
|
||||||
|
|
||||||
class ClubConfig(AppConfig):
|
|
||||||
name = "club"
|
|
||||||
verbose_name = "Management of the clubs and of the memberships in these clubs"
|
|
||||||
|
|
||||||
def ready(self):
|
|
||||||
import club.signals
|
|
@ -175,29 +175,34 @@ class Club(models.Model):
|
|||||||
self.page.parent = self.parent.page
|
self.page.parent = self.parent.page
|
||||||
self.page.save(force_lock=True)
|
self.page.save(force_lock=True)
|
||||||
|
|
||||||
|
@transaction.atomic()
|
||||||
def save(self, *args, **kwargs):
|
def save(self, *args, **kwargs):
|
||||||
with transaction.atomic():
|
old = Club.objects.filter(id=self.id).first()
|
||||||
creation = False
|
creation = old is None
|
||||||
old = Club.objects.filter(id=self.id).first()
|
if not creation and old.unix_name != self.unix_name:
|
||||||
if not old:
|
self._change_unixname(self.unix_name)
|
||||||
creation = True
|
super(Club, self).save(*args, **kwargs)
|
||||||
else:
|
if creation:
|
||||||
if old.unix_name != self.unix_name:
|
board = MetaGroup(name=self.unix_name + settings.SITH_BOARD_SUFFIX)
|
||||||
self._change_unixname(self.unix_name)
|
board.save()
|
||||||
super(Club, self).save(*args, **kwargs)
|
member = MetaGroup(name=self.unix_name + settings.SITH_MEMBER_SUFFIX)
|
||||||
if creation:
|
member.save()
|
||||||
board = MetaGroup(name=self.unix_name + settings.SITH_BOARD_SUFFIX)
|
subscribers = Group.objects.filter(
|
||||||
board.save()
|
name=settings.SITH_MAIN_MEMBERS_GROUP
|
||||||
member = MetaGroup(name=self.unix_name + settings.SITH_MEMBER_SUFFIX)
|
).first()
|
||||||
member.save()
|
self.make_home()
|
||||||
subscribers = Group.objects.filter(
|
self.home.edit_groups.set([board])
|
||||||
name=settings.SITH_MAIN_MEMBERS_GROUP
|
self.home.view_groups.set([member, subscribers])
|
||||||
).first()
|
self.home.save()
|
||||||
self.make_home()
|
self.make_page()
|
||||||
self.home.edit_groups.set([board])
|
cache.set(f"sith_club_{self.unix_name}", self)
|
||||||
self.home.view_groups.set([member, subscribers])
|
|
||||||
self.home.save()
|
def delete(self, *args, **kwargs):
|
||||||
self.make_page()
|
super().delete(*args, **kwargs)
|
||||||
|
# Invalidate the cache of this club and of its memberships
|
||||||
|
for membership in self.members.ongoing().select_related("user"):
|
||||||
|
cache.delete(f"membership_{self.id}_{membership.user.id}")
|
||||||
|
cache.delete(f"sith_club_{self.unix_name}")
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return self.name
|
return self.name
|
||||||
@ -241,7 +246,10 @@ class Club(models.Model):
|
|||||||
if membership == "not_member":
|
if membership == "not_member":
|
||||||
return None
|
return None
|
||||||
if membership is None:
|
if membership is None:
|
||||||
|
print(self.members.all())
|
||||||
|
print(user.memberships.all())
|
||||||
membership = self.members.filter(user=user, end_date=None).first()
|
membership = self.members.filter(user=user, end_date=None).first()
|
||||||
|
print("membership", membership)
|
||||||
if membership is None:
|
if membership is None:
|
||||||
cache.set(f"membership_{self.id}_{user.id}", "not_member")
|
cache.set(f"membership_{self.id}_{user.id}", "not_member")
|
||||||
else:
|
else:
|
||||||
@ -343,6 +351,14 @@ class Membership(models.Model):
|
|||||||
def get_absolute_url(self):
|
def get_absolute_url(self):
|
||||||
return reverse("club:club_members", kwargs={"club_id": self.club.id})
|
return reverse("club:club_members", kwargs={"club_id": self.club.id})
|
||||||
|
|
||||||
|
def save(self, *args, **kwargs):
|
||||||
|
super().save(*args, **kwargs)
|
||||||
|
cache.set(f"membership_{self.club.id}_{self.user.id}", self)
|
||||||
|
|
||||||
|
def delete(self, *args, **kwargs):
|
||||||
|
super().delete(*args, **kwargs)
|
||||||
|
cache.delete(f"membership_{instance.id}_{instance.user.id}")
|
||||||
|
|
||||||
|
|
||||||
class Mailing(models.Model):
|
class Mailing(models.Model):
|
||||||
"""
|
"""
|
||||||
|
@ -1,52 +0,0 @@
|
|||||||
# -*- coding:utf-8 -*
|
|
||||||
#
|
|
||||||
# Copyright 2023
|
|
||||||
# - Maréchal <thgirod@hotmail.com>
|
|
||||||
#
|
|
||||||
# Ce fichier fait partie du site de l'Association des Étudiants de l'UTBM,
|
|
||||||
# http://ae.utbm.fr.
|
|
||||||
#
|
|
||||||
# This program is free software; you can redistribute it and/or modify it under
|
|
||||||
# the terms of the GNU General Public License a published by the Free Software
|
|
||||||
# Foundation; either version 3 of the License, or (at your option) any later
|
|
||||||
# version.
|
|
||||||
#
|
|
||||||
# This program is distributed in the hope that it will be useful, but WITHOUT
|
|
||||||
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
||||||
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
|
||||||
# details.
|
|
||||||
#
|
|
||||||
# You should have received a copy of the GNU General Public License along with
|
|
||||||
# this program; if not, write to the Free Sofware Foundation, Inc., 59 Temple
|
|
||||||
# Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
||||||
#
|
|
||||||
#
|
|
||||||
|
|
||||||
from django.core.cache import cache
|
|
||||||
from django.db.models.signals import pre_delete, post_init, pre_save
|
|
||||||
from django.dispatch import receiver
|
|
||||||
|
|
||||||
from club.models import Club, Membership
|
|
||||||
|
|
||||||
|
|
||||||
@receiver(pre_delete, sender=Club, dispatch_uid="clear_cached_club")
|
|
||||||
def clear_cached_club(sender, instance: Club, **_kwargs):
|
|
||||||
"""
|
|
||||||
When a club is deleted, clear the cache of the memberships
|
|
||||||
associated with this club
|
|
||||||
"""
|
|
||||||
for membership in instance.members.ongoing().select_related("user"):
|
|
||||||
cache.delete(f"membership_{instance.id}_{membership.user.id}")
|
|
||||||
cache.delete(f"sith_club_{instance.unix_name}")
|
|
||||||
|
|
||||||
|
|
||||||
@receiver(
|
|
||||||
[pre_save, pre_delete],
|
|
||||||
sender=Membership,
|
|
||||||
dispatch_uid="clear_cached_membership",
|
|
||||||
)
|
|
||||||
def clear_cached_membership(sender, instance: Membership, **_kwargs):
|
|
||||||
"""
|
|
||||||
When the membership of a user is deleted or edited, clear the associated cache
|
|
||||||
"""
|
|
||||||
cache.delete(f"membership_{instance.id}_{instance.user.id}")
|
|
@ -34,7 +34,6 @@ class SithConfig(AppConfig):
|
|||||||
|
|
||||||
def ready(self):
|
def ready(self):
|
||||||
from forum.models import Forum
|
from forum.models import Forum
|
||||||
import core.signals
|
|
||||||
|
|
||||||
def clear_cached_memberships(**kwargs):
|
def clear_cached_memberships(**kwargs):
|
||||||
Forum._club_memberships = {}
|
Forum._club_memberships = {}
|
||||||
|
@ -90,6 +90,16 @@ class Group(AuthGroup):
|
|||||||
"""
|
"""
|
||||||
return reverse("core:group_list")
|
return reverse("core:group_list")
|
||||||
|
|
||||||
|
def save(self, *args, **kwargs):
|
||||||
|
super().save(*args, **kwargs)
|
||||||
|
cache.set(f"sith_group_{self.id}", self)
|
||||||
|
cache.set(f"sith_group_{self.name.replace(' ', '_')}", self)
|
||||||
|
|
||||||
|
def delete(self, *args, **kwargs):
|
||||||
|
super().delete(*args, **kwargs)
|
||||||
|
cache.delete(f"sith_group_{self.id}")
|
||||||
|
cache.delete(f"sith_group_{self.name.replace(' ', '_')}")
|
||||||
|
|
||||||
|
|
||||||
class MetaGroup(Group):
|
class MetaGroup(Group):
|
||||||
"""
|
"""
|
||||||
|
@ -1,54 +0,0 @@
|
|||||||
# -*- coding:utf-8 -*
|
|
||||||
#
|
|
||||||
# Copyright 2023
|
|
||||||
# - Maréchal <thgirod@hotmail.com>
|
|
||||||
#
|
|
||||||
# Ce fichier fait partie du site de l'Association des Étudiants de l'UTBM,
|
|
||||||
# http://ae.utbm.fr.
|
|
||||||
#
|
|
||||||
# This program is free software; you can redistribute it and/or modify it under
|
|
||||||
# the terms of the GNU General Public License a published by the Free Software
|
|
||||||
# Foundation; either version 3 of the License, or (at your option) any later
|
|
||||||
# version.
|
|
||||||
#
|
|
||||||
# This program is distributed in the hope that it will be useful, but WITHOUT
|
|
||||||
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
||||||
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
|
||||||
# details.
|
|
||||||
#
|
|
||||||
# You should have received a copy of the GNU General Public License along with
|
|
||||||
# this program; if not, write to the Free Sofware Foundation, Inc., 59 Temple
|
|
||||||
# Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
||||||
#
|
|
||||||
#
|
|
||||||
|
|
||||||
|
|
||||||
from django.core.cache import cache
|
|
||||||
from django.db.models.signals import pre_delete, pre_save
|
|
||||||
from django.dispatch import receiver
|
|
||||||
|
|
||||||
from club.models import Club, Membership
|
|
||||||
from core.models import Group
|
|
||||||
|
|
||||||
|
|
||||||
@receiver(pre_delete, sender=Club, dispatch_uid="clear_cached_memberships")
|
|
||||||
def clear_cached_memberships(sender, instance: Club, **_kwargs):
|
|
||||||
"""
|
|
||||||
When a club is deleted, clear the cache of the memberships
|
|
||||||
associated with this club
|
|
||||||
"""
|
|
||||||
for membership in instance.members.ongoing():
|
|
||||||
cache.delete(f"membership_{membership.club.id}_{membership.user.id}")
|
|
||||||
|
|
||||||
|
|
||||||
@receiver(
|
|
||||||
pre_delete,
|
|
||||||
sender=Group,
|
|
||||||
dispatch_uid="clear_cached_user_groups",
|
|
||||||
)
|
|
||||||
def clear_cached_user_groups(sender, instance: Group, **_kwargs):
|
|
||||||
"""
|
|
||||||
When the membership of a user is deleted or edited, clear the associated cache
|
|
||||||
"""
|
|
||||||
cache.delete(f"sith_group_{instance.id}")
|
|
||||||
cache.delete(f"sith_group_{instance.name.replace(' ', '_')}")
|
|
Loading…
x
Reference in New Issue
Block a user