Skip to content

handlers

variable.signals.handlers ¤

Attributes¤

User = get_user_model() module-attribute ¤

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)

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})

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})

Functions¤

set_model_permissions(sender, **kwargs) ¤

Set model-level permissions.

Source code in variable\signals\handlers.py
19
20
21
22
23
24
25
26
27
@receiver(post_migrate)
def set_model_permissions(sender, **kwargs):
    """Set model-level permissions."""
    for model in [
        SensorInstallation,
        Unit,
        Variable,
    ]:
        model.set_model_permissions()

set_object_permissions(sender, instance, **kwargs) ¤

Set object-level permissions".

Source code in variable\signals\handlers.py
11
12
13
14
15
16
@receiver(post_save, sender=SensorInstallation)
@receiver(post_save, sender=Unit)
@receiver(post_save, sender=Variable)
def set_object_permissions(sender, instance: PermissionsBase, **kwargs):
    """Set object-level permissions"."""
    instance.set_object_permissions()