Skip to content

models

management.models ¤

Classes¤

PermissionsBase ¤

Bases: Model

Base model for models that require permissions.

Functions¤
set_model_permissions() classmethod ¤

Set model-level add permissions.

Source code in management\models.py
74
75
76
77
@classmethod
def set_model_permissions(cls):
    """Set model-level add permissions."""
    apply_add_permissions_to_standard_group(cls)
set_object_permissions() ¤

Set object-level delete, change and view permissions.

Source code in management\models.py
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
def set_object_permissions(self):
    """Set object-level delete, change and view permissions."""
    delete, change, view, add = _get_perm_codenames(self.__class__)
    standard_group = Group.objects.get(name="Standard")
    anonymous_user = get_anonymous_user()

    # View permissions based on permissions level
    if self.visibility in ["public", "internal"]:
        assign_perm(view, standard_group, self)
        assign_perm(view, anonymous_user, self)
        if self.owner:
            remove_perm(view, self.owner, self)
    elif self.visibility == "private" and self.owner:
        remove_perm(view, standard_group, self)
        remove_perm(view, anonymous_user, self)
        if self.owner:
            assign_perm(view, self.owner, self)

    # Assign change and delete permissions for owner
    for perm in [change, delete]:
        remove_perm(perm, standard_group, self)
        remove_perm(perm, anonymous_user, self)
        if self.owner:
            assign_perm(perm, self.owner, self)

User ¤

Bases: AbstractUser

Custom user model.

All users are given staff status and added to the standard group.

Functions¤

_get_perm_codenames(model) ¤

Helper function to get delete, change and view permission codenames for a given model.

Source code in management\models.py
83
84
85
86
87
88
89
90
91
92
def _get_perm_codenames(model):
    """Helper function to get delete, change and view permission codenames for a
    given model.
    """
    return (
        f"delete_{model._meta.model_name}",
        f"change_{model._meta.model_name}",
        f"view_{model._meta.model_name}",
        f"add_{model._meta.model_name}",
    )

apply_add_permissions_to_standard_group(model) ¤

Apply model-level add permissions to the standard user group.

Parameters:

Name Type Description Default
model Model

Model to apply permissions to.

required
Source code in management\models.py
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
def apply_add_permissions_to_standard_group(model):
    """Apply model-level add permissions to the standard user group.

    Args:
        model (Model): Model to apply permissions to.

    """
    delete, change, view, add = _get_perm_codenames(model)
    standard_group = Group.objects.get(name="Standard")
    content_type = ContentType.objects.get_for_model(model)
    permission, created = Permission.objects.get_or_create(
        codename=add, content_type=content_type
    )
    standard_group.permissions.add(permission)