Skip to content

forms

django_spire.api.forms

ApiAccessCreateForm

Bases: ModelForm

Source code in django_spire/api/forms.py
def __init__(self, *args: object, user: User | None = None, **kwargs: object) -> None:
    super().__init__(*args, **kwargs)
    self.requesting_user = user
    self.fields['user'].empty_label = 'No User'

    if user is None or not user.is_superuser:
        self.fields.pop('has_super_access')

requesting_user = user instance-attribute

Meta

model = ApiAccess class-attribute instance-attribute
exclude = ['hashed_key', 'key_hint'] class-attribute

clean_has_super_access

Source code in django_spire/api/forms.py
def clean_has_super_access(self) -> bool:
    has_super_access = self.cleaned_data['has_super_access']

    if has_super_access and (
        self.requesting_user is None or not self.requesting_user.is_superuser
    ):
        message = 'Only super users may grant super access.'
        raise forms.ValidationError(message)

    return has_super_access