Skip to content

admin

station.admin ¤

Classes¤

Basin ¤

Bases: PermissionsBase

River(s) basin where the station is located e.g. El Carmen.

Attributes:

Name Type Description
id int

Primary key.

name str

Name of the basin, e.g. El Carmen.

image ImageField

Photography/Map of the basin.

file FileField

PDF file with details of the basin.

Functions¤
__str__() ¤

Return the basin name.

Source code in station/models.py
228
229
230
def __str__(self) -> str:
    """Return the basin name."""
    return str(self.name)
get_absolute_url() ¤

Return the absolute url of the basin.

Source code in station/models.py
232
233
234
def get_absolute_url(self) -> str:
    """Return the absolute url of the basin."""
    return reverse("station:basin_detail", kwargs={"pk": self.pk})

BasinAdmin ¤

Bases: PermissionsBaseAdmin

Admin class for the Basin model.

Country ¤

Bases: PermissionsBase

The country where a station or region is in.

Attributes:

Name Type Description
id int

Primary key.

name str

Country name.

Functions¤
__str__() ¤

Return the country name.

Source code in station/models.py
47
48
49
def __str__(self) -> str:
    """Return the country name."""
    return str(self.name)
get_absolute_url() ¤

Return the absolute url of the country.

Source code in station/models.py
51
52
53
def get_absolute_url(self) -> str:
    """Return the absolute url of the country."""
    return reverse("station:country_detail", kwargs={"pk": self.pk})

CountryAdmin ¤

Bases: PermissionsBaseAdmin

Admin class for the Country model.

DeltaT ¤

Bases: PermissionsBase

Delta T: Interval of data adquisition (In minutes)

Functions¤
__str__() ¤

Return the delta_t value, as a string.

Source code in station/models.py
298
299
300
def __str__(self) -> str:
    """Return the delta_t value, as a string."""
    return f"{self.delta_t} min"
get_absolute_url() ¤

Return the absolute url of the delta_t.

Source code in station/models.py
302
303
304
def get_absolute_url(self) -> str:
    """Return the absolute url of the delta_t."""
    return reverse("station:delta_t_detail", kwargs={"pk": self.pk})
get_default() classmethod ¤

Return the default delta_t id value.

Source code in station/models.py
306
307
308
309
@classmethod
def get_default(cls) -> "DeltaT":
    """Return the default delta_t id value."""
    return cls.objects.get_or_create(delta_t=5)[0]

DeltaTAdmin ¤

Bases: PermissionsBaseAdmin

Admin class for the DeltaT model.

Ecosystem ¤

Bases: PermissionsBase

The ecosystem associated with a station.

Attributes:

Name Type Description
id int

Primary key.

name str

Name of the ecosystem, e.g. rain forest.

Functions¤
__str__() ¤

Return the ecosystem name.

Source code in station/models.py
107
108
109
def __str__(self) -> str:
    """Return the ecosystem name."""
    return str(self.name)
get_absolute_url() ¤

Return the absolute url of the ecosystem.

Source code in station/models.py
111
112
113
def get_absolute_url(self) -> str:
    """Return the absolute url of the ecosystem."""
    return reverse("station:ecosystem_detail", kwargs={"pk": self.pk})

EcosystemAdmin ¤

Bases: PermissionsBaseAdmin

Admin class for the Ecosystem model.

Institution ¤

Bases: PermissionsBase

Institutional partner responsible for a station.

Attributes:

Name Type Description
id int

Primary key.

name str

Name of the institution.

Functions¤
__str__() ¤

Return the institution name.

Source code in station/models.py
130
131
132
def __str__(self) -> str:
    """Return the institution name."""
    return str(self.name)
get_absolute_url() ¤

Return the absolute url of the institution.

Source code in station/models.py
134
135
136
def get_absolute_url(self) -> str:
    """Return the absolute url of the institution."""
    return reverse("station:institution_detail", kwargs={"pk": self.pk})

InstitutionAdmin ¤

Bases: PermissionsBaseAdmin

Admin class for the Institution model.

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)

Place ¤

Bases: PermissionsBase

Specific place that a station is situated.

Attributes:

Name Type Description
id int

Primary key.

name str

Name of the place, e.g. Huaraz.

image ImageField

Photography/Map of the location.

Functions¤
__str__() ¤

Return the place name.

Source code in station/models.py
187
188
189
def __str__(self) -> str:
    """Return the place name."""
    return str(self.name)
get_absolute_url() ¤

Return the absolute url of the place.

Source code in station/models.py
191
192
193
def get_absolute_url(self) -> str:
    """Return the absolute url of the place."""
    return reverse("station:place_detail", kwargs={"pk": self.pk})

PlaceAdmin ¤

Bases: PermissionsBaseAdmin

Admin class for the Place model.

PlaceBasin ¤

Bases: PermissionsBase

Associates a Basin with a Place and an image.

Attributes:

Name Type Description
id int

Primary key.

place Place

Place of the association.

basin Basin

Basin of the association.

image ImageField

Photography/Map of the place within the basin.

Functions¤
__str__() ¤

Return the place-basin association.

Source code in station/models.py
273
274
275
def __str__(self) -> str:
    """Return the place-basin association."""
    return str(self.place) + " - " + str(self.basin)
get_absolute_url() ¤

Return the absolute url of the place-basin association.

Source code in station/models.py
277
278
279
def get_absolute_url(self) -> str:
    """Return the absolute url of the place-basin association."""
    return reverse("station:place_basin_detail", kwargs={"pk": self.pk})

PlaceBasinAdmin ¤

Bases: PermissionsBaseAdmin

Admin class for the PlaceBasin model.

Region ¤

Bases: PermissionsBase

A region within a country.

Attributes:

Name Type Description
id int

Primary key.

name str

Name of the region.

country Country

Country where the region is located.

Functions¤
__str__() ¤

Return the region name.

Source code in station/models.py
82
83
84
def __str__(self) -> str:
    """Return the region name."""
    return str(self.name)
get_absolute_url() ¤

Return the absolute url of the region.

Source code in station/models.py
86
87
88
def get_absolute_url(self) -> str:
    """Return the absolute url of the region."""
    return reverse("station:region_detail", kwargs={"pk": self.pk})

RegionAdmin ¤

Bases: PermissionsBaseAdmin

Admin class for the Region model.

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.

delta_t DeltaT

Interval of data adquisition (in minutes).

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?

Functions¤
__str__() ¤

Return the station code.

Source code in station/models.py
474
475
476
def __str__(self) -> str:
    """Return the station code."""
    return str(self.station_code)
clean() ¤

Set the default delta_t value if not provided.

Source code in station/models.py
482
483
484
485
486
def clean(self) -> None:
    """Set the default delta_t value if not provided."""
    super().clean()
    if not self.delta_t:
        self.delta_t = DeltaT.get_default()
get_absolute_url() ¤

Return the absolute url of the station.

Source code in station/models.py
478
479
480
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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
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)

StationAdmin ¤

Bases: PermissionsBaseAdmin

Admin class for the Station model.

StationType ¤

Bases: PermissionsBase

Type of the station, indicating what it measures.

Attributes:

Name Type Description
id int

Primary key.

name str

Name of the station type, e.g. pluvometric, hydrological.

Functions¤
__str__() ¤

Return the station type name.

Source code in station/models.py
156
157
158
def __str__(self) -> str:
    """Return the station type name."""
    return str(self.name)
get_absolute_url() ¤

Return the absolute url of the station type.

Source code in station/models.py
160
161
162
def get_absolute_url(self) -> str:
    """Return the absolute url of the station type."""
    return reverse("station:station_type_detail", kwargs={"pk": self.pk})

StationTypeAdmin ¤

Bases: PermissionsBaseAdmin

Admin class for the StationType model.