Skip to content

admin

sensor.admin ¤

Classes¤

PermissionsBaseAdmin ¤

Bases: GuardedModelAdmin

Base admin class for models that require permissions.

Functions¤
formfield_for_choice_field(db_field, request, **kwargs) ¤

Limit the queryset for choice fields.

Source code in management/admin.py
74
75
76
77
78
79
80
def formfield_for_choice_field(self, db_field, request, **kwargs):
    """Limit the queryset for choice fields."""
    if db_field.name == "visibility":
        kwargs["initial"] = "private"
        if self.limit_visibility_level and not request.user.is_superuser:
            kwargs["disabled"] = True
    return super().formfield_for_choice_field(db_field, request, **kwargs)
formfield_for_foreignkey(db_field, request, **kwargs) ¤

Limit the queryset for foreign key fields.

Source code in management/admin.py
65
66
67
68
69
70
71
72
def formfield_for_foreignkey(self, db_field, request, **kwargs):
    """Limit the queryset for foreign key fields."""
    if db_field.name in self.foreign_key_fields:
        kwargs["queryset"] = _get_queryset(db_field, request.user)
    if db_field.name == "owner" and not request.user.is_superuser:
        kwargs["initial"] = request.user.id
        kwargs["disabled"] = True
    return super().formfield_for_foreignkey(db_field, request, **kwargs)
get_queryset(request) ¤

Return a queryset of the objects that the user has view permissions for.

Source code in management/admin.py
58
59
60
61
62
63
def get_queryset(self, request):
    """Return a queryset of the objects that the user has view permissions for."""
    qs = super().get_queryset(request)
    return get_objects_for_user(
        request.user, f"{self.opts.app_label}.view_{self.opts.model_name}", qs
    )
has_change_permission(request, obj=None) ¤

Check if the user has the correct permission to change the object.

Source code in management/admin.py
22
23
24
25
26
27
28
def has_change_permission(self, request, obj=None):
    """Check if the user has the correct permission to change the object."""
    if obj is not None:
        return request.user.has_perm(
            f"{self.opts.app_label}.change_{self.opts.model_name}", obj
        )
    return True
has_delete_permission(request, obj=None) ¤

Check if the user has the correct permission to delete the object.

Source code in management/admin.py
30
31
32
33
34
def has_delete_permission(self, request, obj=None):
    """Check if the user has the correct permission to delete the object."""
    return request.user.has_perm(
        f"{self.opts.app_label}.delete_{self.opts.model_name}", obj
    )
has_view_permission(request, obj=None) ¤

Check if the user has the correct permission to view the object.

Source code in management/admin.py
36
37
38
39
40
41
def has_view_permission(self, request, obj=None):
    """Check if the user has the correct permission to view the object."""
    if obj is not None:
        return request.user.has_perm(
            f"{self.opts.app_label}.view_{self.opts.model_name}", obj
        )
obj_perms_manage_view(request, object_pk) ¤

Prevents permission scalation at object level.

Only allows users with change permissions for this object to change the object permissions.

Source code in management/admin.py
43
44
45
46
47
48
49
50
51
52
53
54
55
56
def obj_perms_manage_view(self, request, object_pk):
    """Prevents permission scalation at object level.

    Only allows users with change permissions for this object to change the object
    permissions.
    """
    obj = self.get_object(request, object_pk)
    if not request.user.has_perm(
        f"{self.opts.app_label}.change_{self.opts.model_name}", obj
    ):
        post_url = reverse("admin:index", current_app=self.admin_site.name)
        return redirect(post_url)

    return super().obj_perms_manage_view(request, object_pk)

Sensor ¤

Bases: PermissionsBase

Specific sensor details.

Attributes:

Name Type Description
sensor_id int

Primary key, sensor id.

code

(str) sensor code.

sensor_type SensorType

sensor type.

sensor_brand SensorBrand

sensor brand.

model str

specific model of the sensor.

serial str

serial number of the sensor.

status bool

sensor status.

Functions¤
__str__() ¤

Return the sensor code.

Source code in sensor/models.py
112
113
114
def __str__(self) -> str:
    """Return the sensor code."""
    return str(self.code)
get_absolute_url() ¤

Return the absolute url of the sensor.

Source code in sensor/models.py
116
117
118
def get_absolute_url(self) -> str:
    """Return the absolute url of the sensor."""
    return reverse("sensor:sensor_detail", kwargs={"pk": self.pk})

SensorAdmin ¤

Bases: PermissionsBaseAdmin

Admin class for the Sensor model.

SensorBrand ¤

Bases: PermissionsBase

Brand of the sensor, eg. Davis, Texas Electronics, etc.

Attributes:

Name Type Description
brand_id

int, primary key, sensor brand id.

name

str, sensor brand name.

Functions¤
__str__() ¤

Return the brand name.

Source code in sensor/models.py
52
53
54
def __str__(self) -> str:
    """Return the brand name."""
    return str(self.name)
get_absolute_url() ¤

Return the absolute url of the sensor brand.

Source code in sensor/models.py
56
57
58
def get_absolute_url(self) -> str:
    """Return the absolute url of the sensor brand."""
    return reverse("sensor:brand_detail", kwargs={"pk": self.pk})

SensorBrandAdmin ¤

Bases: PermissionsBaseAdmin

Admin class for the SensorBrand model.

SensorType ¤

Bases: PermissionsBase

Type of sensor, eg. pluviometric, wind sensor, etc.

Attributes:

Name Type Description
sensor_type_id

int, primary key, sensor type id.

name

str, sensor type name.

Functions¤
__str__() ¤

Return the sensor type name.

Source code in sensor/models.py
32
33
34
def __str__(self) -> str:
    """Return the sensor type name."""
    return str(self.name)
get_absolute_url() ¤

Return the absolute url of the sensor type.

Source code in sensor/models.py
36
37
38
def get_absolute_url(self) -> str:
    """Return the absolute url of the sensor type."""
    return reverse("sensor:type_detail", kwargs={"pk": self.pk})

SensorTypeAdmin ¤

Bases: PermissionsBaseAdmin

Admin class for the SensorType model.