Aller au contenu

Models

Club

Bases: Model

The Club class, made as a tree to allow nice tidy organization.

check_loop()

Raise a validation error when a loop is found within the parent list.

Source code in club/models.py
def check_loop(self):
    """Raise a validation error when a loop is found within the parent list."""
    objs = []
    cur = self
    while cur.parent is not None:
        if cur in objs:
            raise ValidationError(_("You can not make loops in clubs"))
        objs.append(cur)
        cur = cur.parent

is_owned_by(user)

Method to see if that object can be super edited by the given user.

Source code in club/models.py
def is_owned_by(self, user):
    """Method to see if that object can be super edited by the given user."""
    if user.is_anonymous:
        return False
    return user.is_board_member

can_be_edited_by(user)

Method to see if that object can be edited by the given user.

Source code in club/models.py
def can_be_edited_by(self, user):
    """Method to see if that object can be edited by the given user."""
    return self.has_rights_in_club(user)

can_be_viewed_by(user)

Method to see if that object can be seen by the given user.

Source code in club/models.py
def can_be_viewed_by(self, user):
    """Method to see if that object can be seen by the given user."""
    sub = User.objects.filter(pk=user.pk).first()
    if sub is None:
        return False
    return sub.was_subscribed

get_membership_for(user)

Return the current membership the given user.

Note

The result is cached.

Source code in club/models.py
def get_membership_for(self, user: User) -> Membership | None:
    """Return the current membership the given user.

    Note:
        The result is cached.
    """
    if user.is_anonymous:
        return None
    membership = cache.get(f"membership_{self.id}_{user.id}")
    if membership == "not_member":
        return None
    if membership is None:
        membership = self.members.filter(user=user, end_date=None).first()
        if membership is None:
            cache.set(f"membership_{self.id}_{user.id}", "not_member")
        else:
            cache.set(f"membership_{self.id}_{user.id}", membership)
    return membership

MembershipQuerySet

Bases: QuerySet

ongoing()

Filter all memberships which are not finished yet.

Source code in club/models.py
def ongoing(self) -> Self:
    """Filter all memberships which are not finished yet."""
    return self.filter(Q(end_date=None) | Q(end_date__gt=timezone.now().date()))

board()

Filter all memberships where the user is/was in the board.

Be aware that users who were in the board in the past are included, even if there are no more members.

If you want to get the users who are currently in the board, mind combining this with the :meth:ongoing queryset method

Source code in club/models.py
def board(self) -> Self:
    """Filter all memberships where the user is/was in the board.

    Be aware that users who were in the board in the past
    are included, even if there are no more members.

    If you want to get the users who are currently in the board,
    mind combining this with the :meth:`ongoing` queryset method
    """
    return self.filter(role__gt=settings.SITH_MAXIMUM_FREE_ROLE)

update(**kwargs)

Refresh the cache for the elements of the queryset.

Besides that, does the same job as a regular update method.

Be aware that this adds a db query to retrieve the updated objects

Source code in club/models.py
def update(self, **kwargs):
    """Refresh the cache for the elements of the queryset.

    Besides that, does the same job as a regular update method.

    Be aware that this adds a db query to retrieve the updated objects
    """
    nb_rows = super().update(**kwargs)
    if nb_rows > 0:
        # if at least a row was affected, refresh the cache
        for membership in self.all():
            if membership.end_date is not None:
                cache.set(
                    f"membership_{membership.club_id}_{membership.user_id}",
                    "not_member",
                )
            else:
                cache.set(
                    f"membership_{membership.club_id}_{membership.user_id}",
                    membership,
                )

delete()

Work just like the default Django's delete() method, but add a cache invalidation for the elements of the queryset before the deletion.

Be aware that this adds a db query to retrieve the deleted element. As this first query take place before the deletion operation, it will be performed even if the deletion fails.

Source code in club/models.py
def delete(self):
    """Work just like the default Django's delete() method,
    but add a cache invalidation for the elements of the queryset
    before the deletion.

    Be aware that this adds a db query to retrieve the deleted element.
    As this first query take place before the deletion operation,
    it will be performed even if the deletion fails.
    """
    ids = list(self.values_list("club_id", "user_id"))
    nb_rows, _ = super().delete()
    if nb_rows > 0:
        for club_id, user_id in ids:
            cache.set(f"membership_{club_id}_{user_id}", "not_member")

Membership

Bases: Model

The Membership class makes the connection between User and Clubs.

Both Users and Clubs can have many Membership objects
  • a user can be a member of many clubs at a time
  • a club can have many members at a time too

A User is currently member of all the Clubs where its Membership has an end_date set to null/None. Otherwise, it's a past membership kept because it can be very useful to see who was in which Club in the past.

is_owned_by(user)

Method to see if that object can be super edited by the given user.

Source code in club/models.py
def is_owned_by(self, user):
    """Method to see if that object can be super edited by the given user."""
    if user.is_anonymous:
        return False
    return user.is_board_member

can_be_edited_by(user)

Check if that object can be edited by the given user.

Source code in club/models.py
def can_be_edited_by(self, user: User) -> bool:
    """Check if that object can be edited by the given user."""
    if user.is_root or user.is_board_member:
        return True
    membership = self.club.get_membership_for(user)
    if membership is not None and membership.role >= self.role:
        return True
    return False

Mailing

Bases: Model

A Mailing list for a club.

Warning

Remember that mailing lists should be validated by UTBM.

MailingSubscription

Bases: Model

Link between user and mailing list.

get_default_owner_group()

Source code in club/models.py
def get_default_owner_group():
    return settings.SITH_GROUP_ROOT_ID