Skip to content

admin

variable.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
75
76
77
78
79
80
81
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
66
67
68
69
70
71
72
73
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
59
60
61
62
63
64
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
23
24
25
26
27
28
29
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
31
32
33
34
35
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
37
38
39
40
41
42
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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)

SensorInstallation ¤

Bases: PermissionsBase

Represents an installation of a Sensor at a Station, which measures a Variable.

It includes metadata for installation and finishing date, as well as state (active or not).

Attributes:

Name Type Description
sensorinstallation_id AutoField

Primary key.

variable ForeignKey

Variable measured by the sensor.

station ForeignKey

Station where the sensor is installed.

sensor ForeignKey

Sensor used for the measurement.

start_date DateField

Start date of the installation.

end_date DateField

End date of the installation.

state BooleanField

Is the sensor active?

Functions¤
get_absolute_url() ¤

Get the absolute URL of the object.

Source code in variable\models.py
248
249
250
def get_absolute_url(self) -> str:
    """Get the absolute URL of the object."""
    return reverse("variable:sensorinstallation_detail", kwargs={"pk": self.pk})

SensorInstallationAdmin ¤

Bases: PermissionsBaseAdmin

Admin class for the SensorInstallation model.

SensorTypeAdmin ¤

Bases: PermissionsBaseAdmin

Admin class for the Unit model.

Unit ¤

Bases: PermissionsBase

Unit of measurement with a name and a symbol.

Attributes:

Name Type Description
unit_id AutoField

Primary key.

name CharField

Name of the unit, eg. meters per second.

initials CharField

Symbol for the unit, eg. m/s.

Functions¤
__str__() ¤

Return the string representation of the object.

Source code in variable\models.py
42
43
44
def __str__(self) -> str:
    """Return the string representation of the object."""
    return str(self.initials)
get_absolute_url() ¤

Get the absolute URL of the object.

Source code in variable\models.py
46
47
48
def get_absolute_url(self) -> str:
    """Get the absolute URL of the object."""
    return reverse("variable:unit_detail", kwargs={"pk": self.pk})

Variable ¤

Bases: PermissionsBase

A variable with a physical meaning.

Such as precipitation, wind speed, wind direction, soil moisture, including the associated unit. It also includes metadata to help identify what is a reasonable value for the data, to flag outliers and to help with the validation process.

The nature of the variable can be one of the following:

  • sum: Cumulative value over a period of time.
  • average: Average value over a period of time.
  • value: One-off value.

Attributes:

Name Type Description
variable_id AutoField

Primary key.

variable_code CharField

Code of the variable, eg. airtemperature.

name CharField

Human-readable name of the variable, eg. Air temperature.

unit ForeignKey

Unit of the variable.

maximum DecimalField

Maximum value allowed for the variable.

minimum DecimalField

Minimum value allowed for the variable.

diff_error DecimalField

If two sequential values in the time-series data of this variable differ by more than this value, the validation process can mark this with an error flag.

outlier_limit DecimalField

The statistical deviation for defining outliers, in times the standard deviation (sigma).

null_limit DecimalField

The max % of null values (missing, caused by e.g. equipment malfunction) allowed for hourly, daily, monthly data. Cumulative values are not deemed trustworthy if the number of missing values in a given period is greater than the null_limit.

nature CharField

Nature of the variable, eg. if it represents a one-off value, the average over a period of time or the cumulative value over a period

Attributes¤
is_cumulative: bool property ¤

Return True if the nature of the variable is sum.

Functions¤
__str__() ¤

Return the string representation of the object.

Source code in variable\models.py
165
166
167
def __str__(self) -> str:
    """Return the string representation of the object."""
    return str(self.name)
clean() ¤

Validate the model fields.

Source code in variable\models.py
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
def clean(self) -> None:
    """Validate the model fields."""
    if self.maximum < self.minimum:
        raise ValidationError(
            {
                "maximum": "The maximum value must be greater than the minimum "
                "value."
            }
        )
    if not self.variable_code.isidentifier():
        raise ValidationError(
            {
                "variable_code": "The variable code must be a valid Python "
                "identifier. Only letters, numbers and underscores are allowed, and"
                " it cannot start with a number."
            }
        )
    return super().clean()
get_absolute_url() ¤

Get the absolute URL of the object.

Source code in variable\models.py
169
170
171
def get_absolute_url(self) -> str:
    """Get the absolute URL of the object."""
    return reverse("variable:variable_detail", kwargs={"pk": self.pk})

VariableAdmin ¤

Bases: PermissionsBaseAdmin

Admin class for the Variable model.