Skip to content

filters

variable.filters ¤

Classes¤

FilterVisible(target, model, field=None) ¤

Filter objects the user has permission to view.

Standard filter shows ALL objects available in the database when faced with a ForeignKey field. This filter shows only the objects that appear in the target model objects that the user has permission to view.

If field is None, the name of the model model is used.

Parameters:

Name Type Description Default
target type[Model]

Model containing the objects.

required
model type[Model]

Model of the objects to display.

required
field str | None

Field to filter by. Defaults to None.

None
Source code in management\filters.py
22
23
24
25
26
27
28
def __init__(
    self, target: type[Model], model: type[Model], field: str | None = None
) -> None:
    self.target = target
    self.model = model
    self.field = field or model.__name__.lower()
    self.permission = f"{target._meta.app_label}.view_{target._meta.model_name}"

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

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

SensorInstallationFilter ¤

Bases: FilterSet

Station ¤

Bases: PermissionsBase

Main representation of a station, including several metadata.

Attributes:

Name Type Description
visibility str

Visibility level of the object, including an "internal" option.

station_id int

Primary key.

station_code str

Unique code for the station.

station_name str

Brief description of the station.

station_type StationType

Type of the station.

country Country

Country where the station is located.

region Region

Region within the Country where the station is located.

ecosystem Ecosystem

Ecosystem associated with the station.

institution Institution

Institutional partner responsible for the station.

place_basin PlaceBasin

Place-Basin association.

station_state bool

Is the station operational?

timezone str

Timezone of the station.

station_latitude Decimal

Latitude of the station, in degrees [-90 to 90].

station_longitude Decimal

Longitude of the station, in degrees [-180 to 180].

station_altitude int

Altitude of the station.

influence_km Decimal

Area of influence in km2.

station_file ImageField

Photography of the station.

station_external bool

Is the station external?

variables str

Comma-separated list of variables measured by the station.

Attributes¤
variables_list: list[str] property ¤

Return the list of variables measured by the station.

Only variables with data in the database are returned.

Returns:

Type Description
list[str]

list[str]: List of variables measured by the station.

Functions¤
__str__() ¤

Return the station code.

Source code in station\models.py
458
459
460
def __str__(self) -> str:
    """Return the station code."""
    return str(self.station_code)
get_absolute_url() ¤

Return the absolute url of the station.

Source code in station\models.py
462
463
464
def get_absolute_url(self) -> str:
    """Return the absolute url of the station."""
    return reverse("station:station_detail", kwargs={"pk": self.pk})
set_object_permissions() ¤

Set object-level permissions.

This method is called by the save method of the model to set the object-level permissions based on the visibility level of the object. In addition to the standard permissions for the station, the view_measurements permission is set which controls who can view the measurements associated to the station.

Source code in station\models.py
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
def set_object_permissions(self) -> None:
    """Set object-level permissions.

    This method is called by the save method of the model to set the object-level
    permissions based on the visibility level of the object. In addition to the
    standard permissions for the station, the view_measurements permission is set
    which controls who can view the measurements associated to the station.
    """
    super().set_object_permissions()

    standard_group = Group.objects.get(name="Standard")
    anonymous_user = get_anonymous_user()

    # Assign view_measurements permission based on permissions level
    if self.visibility == "public":
        assign_perm("view_measurements", standard_group, self)
        assign_perm("view_measurements", anonymous_user, self)
        if self.owner:
            remove_perm("view_measurements", self.owner, self)
    elif self.visibility == "internal":
        assign_perm("view_measurements", standard_group, self)
        remove_perm("view_measurements", anonymous_user, self)
        if self.owner:
            remove_perm("view_measurements", self.owner, self)
    elif self.visibility == "private":
        remove_perm("view_measurements", standard_group, self)
        remove_perm("view_measurements", anonymous_user, self)
        if self.owner:
            assign_perm("view_measurements", self.owner, self)

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

VariableFilter ¤

Bases: FilterSet