Skip to content

admin

measurement.admin ¤

Classes¤

Measurement ¤

Bases: MeasurementBase

Class to store the measurements and their validation status.

This class holds the value of a given variable and station at a specific time, as well as auxiliary information such as maximum and minimum values, depth and direction, for vector quantities. All of these have a raw version where a backup of the original data is kept, should this change at any point.

Flags to monitor its validation status, if the data is active (and therefore can be used for reporting) and if it has actually been used for that is also included.

Attributes:

Name Type Description
depth int

Depth of the measurement.

direction Decimal

Direction of the measurement, useful for vector quantities.

raw_value Decimal

Original value of the measurement.

raw_maximum Decimal

Original maximum value of the measurement.

raw_minimum Decimal

Original minimum value of the measurement.

raw_direction Decimal

Original direction of the measurement.

raw_depth int

Original depth of the measurement.

is_validated bool

Flag to indicate if the measurement has been validated.

is_active bool

Flag to indicate if the measurement is active. An inactive measurement is not used for reporting

Attributes¤
overwritten: bool property ¤

Indicates if any of the values associated to the entry have been overwritten.

Returns:

Name Type Description
bool bool

True if any raw field is different to the corresponding standard field.

raws: tuple[str, ...] property ¤

Return the raw fields of the measurement.

Returns:

Type Description
tuple[str, ...]

tuple[str]: Tuple with the names of the raw fields of the measurement.

Functions¤
clean() ¤

Check consistency of validation, reporting and backs-up values.

Source code in measurement/models.py
250
251
252
253
254
255
256
257
258
259
260
def clean(self) -> None:
    """Check consistency of validation, reporting and backs-up values."""
    # Check consistency of validation
    if not self.is_validated and not self.is_active:
        raise ValidationError("Only validated entries can be declared as inactive.")

    # Backup values to raws, if needed
    for r in self.raws:
        value = getattr(self, r.removeprefix("raw_"))
        if value and not getattr(self, r):
            setattr(self, r, value)

MeasurementAdmin ¤

Bases: MeasurementBaseAdmin

Admin class for the Measurement model.

MeasurementBaseAdmin ¤

Bases: GuardedModelAdmin

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

Limit the queryset for foreign key fields.

Source code in measurement/admin.py
43
44
45
46
47
48
49
50
51
def formfield_for_foreignkey(self, db_field, request, **kwargs):
    """Limit the queryset for foreign key fields."""
    if db_field.name == "station":
        kwargs["queryset"] = get_objects_for_user(
            request.user, "station.change_station"
        )
    elif db_field.name == "variable":
        kwargs["queryset"] = _get_queryset(db_field, request.user)
    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 measurement/admin.py
37
38
39
40
41
def get_queryset(self, request):
    """Return a queryset of the objects that the user has view permissions for."""
    qs = super().get_queryset(request)
    stations = get_objects_for_user(request.user, "station.view_measurements")
    return qs.filter(station__in=stations)
has_add_permission(request) ¤

Check if the user has the correct permission to add objects.

Source code in measurement/admin.py
13
14
15
16
17
def has_add_permission(self, request):
    """Check if the user has the correct permission to add objects."""
    return request.user.has_perm(
        f"{self.opts.app_label}.add_{self.opts.model_name}"
    )
has_change_permission(request, obj=None) ¤

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

Source code in measurement/admin.py
19
20
21
22
23
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("change_station", obj.station)
    return True
has_delete_permission(request, obj=None) ¤

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

Source code in measurement/admin.py
25
26
27
28
29
def has_delete_permission(self, request, obj=None):
    """Check if the user has the correct permission to delete the object."""
    if obj is not None:
        return request.user.has_perm("delete_station", obj.station)
    return True
has_view_permission(request, obj=None) ¤

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

Source code in measurement/admin.py
31
32
33
34
35
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("view_measurements", obj.station)
    return True

Report ¤

Bases: MeasurementBase

Holds the different reporting data.

It also keeps track of which data has already been used when creating the reports.

Attributes:

Name Type Description
report_type str

Type of report. It can be hourly, daily or monthly.

completeness Decimal

Completeness of the report. Eg. a daily report with 24 hourly measurements would have a completeness of 100%.

Functions¤
clean() ¤

Validate that the report type and use of the data is consistent.

Source code in measurement/models.py
138
139
140
141
142
143
144
145
146
147
def clean(self) -> None:
    """Validate that the report type and use of the data is consistent."""
    if self.report_type == ReportType.HOURLY:
        self.time = self.time.replace(minute=0, second=0, microsecond=0)
    elif self.report_type == ReportType.DAILY:
        self.time = self.time.replace(hour=0, minute=0, second=0, microsecond=0)
    elif self.report_type == ReportType.MONTLY:
        self.time = self.time.replace(
            day=1, hour=0, minute=0, second=0, microsecond=0
        )

ReportAdmin ¤

Bases: MeasurementBaseAdmin

Admin class for the Report model.

Functions¤

_get_queryset(db_field, user) ¤

Return a queryset based on the permissions of the user.

Returns queryset of public objects and objects that the user has change permisions for. For the case of Station objects, having the change permission is necessary to include the object in the queryset - being Public is not enough.

Source code in management/admin.py
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
def _get_queryset(db_field, user):
    """Return a queryset based on the permissions of the user.

    Returns queryset of public objects and objects that the user has change permisions
    for. For the case of `Station` objects, having the `change` permission is
    necessary to include the object in the queryset - being `Public` is not enough.

    """
    app_name = db_field.related_model._meta.app_label
    model_name = db_field.related_model._meta.model_name
    user_objects = get_objects_for_user(user, f"{app_name}.change_{model_name}")
    public_objects = (
        db_field.related_model.objects.none()
        if model_name == "station"
        else db_field.related_model.objects.filter(visibility="public")
    )
    return user_objects | public_objects