Skip to content

views

station.views ¤

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

BasinCreateView ¤

Bases: CustomCreateView

View to create a basin.

BasinDeleteView ¤

Bases: CustomDeleteView

View to delete a basin.

BasinDetailView ¤

Bases: CustomDetailView

View to view a basin.

BasinEditView ¤

Bases: CustomEditView

View to edit a basin.

BasinListView ¤

Bases: CustomTableView

View to display a table of basins.

BasinTable ¤

Bases: Table

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

CountryCreateView ¤

Bases: CustomCreateView

View to create a country.

CountryDeleteView ¤

Bases: CustomDeleteView

View to delete a country.

CountryDetailView ¤

Bases: CustomDetailView

View to view a country.

CountryEditView ¤

Bases: CustomEditView

View to edit a country.

CountryListView ¤

Bases: CustomTableView

View to display a table of countries.

CountryTable ¤

Bases: Table

CustomCreateView ¤

Bases: URLMixin, LoginRequiredMixin, CreateView

Generic create view.

This view is used to create a new model object. The user must have the permission to create the object, otherwise a 403 error is returned.

The view includes a form with the object data, and the context includes the title of the view and the URL to the list view.

If provided, the foreign_key_fields attribute is used to limit the queryset for foreign key fields.

If successful, the view redirects to the detail view of the created object.

Users need to be logged in to access this view.

Attributes:

Name Type Description
template_name str

Template to be used.

Functions¤
form_valid(form) ¤

Set the owner of the object to the current user.

This is done before saving the object to the database.

Parameters:

Name Type Description Default
form ModelForm

Form with the object data.

required

Returns:

Name Type Description
HttpResponse HttpResponse

Redirect to the detail view of the created object.

Source code in management\views.py
346
347
348
349
350
351
352
353
354
355
356
357
358
def form_valid(self, form: forms.ModelForm) -> HttpResponse:
    """Set the owner of the object to the current user.

    This is done before saving the object to the database.

    Args:
        form (forms.ModelForm): Form with the object data.

    Returns:
        HttpResponse: Redirect to the detail view of the created object.
    """
    form.instance.owner = self.request.user
    return super().form_valid(form)
get_form_kwargs() ¤

Add the user to the form kwargs, so we can filter the options.

Source code in management\views.py
384
385
386
387
388
def get_form_kwargs(self):
    """Add the user to the form kwargs, so we can filter the options."""
    kwargs = super().get_form_kwargs()
    kwargs["user"] = self.request.user
    return kwargs

CustomDeleteView ¤

Bases: URLMixin, LoginRequiredMixin, DeleteView

Generic delete view.

This view is used to delete a model object. The user must have the permission to delete the object, otherwise a 403 error is returned. A confirmation page is shown with the related objects that will be deleted.

The permissions required to delete the object are app_label.delete_model_name. For example, the permission required to delete a DataImport object would be importing.delete_dataimport.

If successful, the view redirects to the list view.

Users need to be logged in to access this view.

Attributes:

Name Type Description
template_name str

Template to be used.

CustomDetailView ¤

Bases: URLMixin, LoginRequiredMixin, DetailView

Generic detail view.

This view is used to show the details of a model object. The user must have the permission to view the object, otherwise a 403 error is returned.

The view includes a form with the object data, and the context includes the URLs for the list, delete, and edit views.

The permissions required to view the object are app_label.view_model_name. For example, the permission required to view a DataImport object would be importing.view_dataimport.

Users need to be logged in to access this view.

Attributes:

Name Type Description
template_name str

Template to be used.

fields str

Fields to be shown in the form.

Functions¤
get_inline() ¤

Return the inline data for the format.

If provided, this method should return a dictionary with the inline data to be shown in the detail view. The dictionary should have the following keys:

  • title: Title of the inline data.
  • header: List with the header of the table.
  • objects: List with the objects to be shown in the table. Each object should be a list with the same length as the header.

Returns:

Type Description
dict | None

dict | None: Inline data for the format.

Source code in management\views.py
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
def get_inline(self) -> dict | None:
    """Return the inline data for the format.

    If provided, this method should return a dictionary with the inline data to be
    shown in the detail view. The dictionary should have the following keys:

    - title: Title of the inline data.
    - header: List with the header of the table.
    - objects: List with the objects to be shown in the table. Each object should be
        a list with the same length as the header.

    Returns:
        dict | None: Inline data for the format.
    """
    return None

CustomEditView ¤

Bases: URLMixin, LoginRequiredMixin, UpdateView

Generic edit view.

This view is used to edit a model object. The user must have the permission to edit the object, otherwise a 403 error is returned.

The view includes a form with the object data, and the context includes the title of the view and the URL to the list view.

The permissions required to edit the object are app_label.change_model_name. For example, the permission required to edit a DataImport object would be importing.change_dataimport.

If successful or cancelled, the view redirects to the detail view of the created object.

Users need to be logged in to access this view.

Attributes:

Name Type Description
template_name str

Template to be used.

Functions¤
get_form_kwargs() ¤

Add the user to the form kwargs, so we can filter the options.

Source code in management\views.py
284
285
286
287
288
def get_form_kwargs(self):
    """Add the user to the form kwargs, so we can filter the options."""
    kwargs = super().get_form_kwargs()
    kwargs["user"] = self.request.user
    return kwargs

CustomTableView ¤

Bases: URLMixin, LoginRequiredMixin, SingleTableMixin, FilterView

This view is used to show a list of model objects.

The view includes a table with the objects, and the context includes the title of the view, the refresh URL, and the URL to create a new object.

The permissions required to view the objects are app_label.view_model_name. For example, the permission required to view a DataImport object would be importing.view_dataimport.

If provided, the filter_class attribute is used to create a filter form on top of the table.

Users need to be logged in to access this view.

Attributes:

Name Type Description
model Model

Model to be used.

table_class Table

Table class to be used.

filterset_class FilterSet

Filter class to be used. If not provided, the model's default filter is used.

template_name str

Template to be used.

paginate_by int

Number of objects per page.

show_refresh_btn bool

If True, a refresh url is included in the context.

show_new_btn bool

If True, a create url is included in the context.

Functions¤
get_filterset_class() ¤

Return the filter class for the view.

If no filter class is provided in the view, the default filter for the model is used. The default filter is created by the FilterSet class, and includes only the 'visibility'.

Source code in management\views.py
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
def get_filterset_class(self):
    """Return the filter class for the view.

    If no filter class is provided in the view, the default filter for the model is
    used. The default filter is created by the `FilterSet` class, and includes only
    the 'visibility'.
    """
    if not self.filterset_class:

        class VisbilityFilter(FilterSet):
            class Meta:
                model = self.model
                fields = ["visibility"]

        return VisbilityFilter

    return super().get_filterset_class()

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

EcosystemCreateView ¤

Bases: CustomCreateView

View to create an ecosystem.

EcosystemDeleteView ¤

Bases: CustomDeleteView

View to delete an ecosystem.

EcosystemDetailView ¤

Bases: CustomDetailView

View to view an ecosystem.

EcosystemEditView ¤

Bases: CustomEditView

View to edit an ecosystem.

EcosystemListView ¤

Bases: CustomTableView

View to display a table of ecosystems.

EcosystemTable ¤

Bases: Table

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

InstitutionCreateView ¤

Bases: CustomCreateView

View to create an institution.

InstitutionDeleteView ¤

Bases: CustomDeleteView

View to delete an institution.

InstitutionDetailView ¤

Bases: CustomDetailView

View to view an institution.

InstitutionEditView ¤

Bases: CustomEditView

View to edit an institution.

InstitutionListView ¤

Bases: CustomTableView

View to display a table of institutions.

InstitutionTable ¤

Bases: Table

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

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:placebasin_detail", kwargs={"pk": self.pk})

PlaceBasinCreateView ¤

Bases: CustomCreateView

View to create a place basin.

PlaceBasinDeleteView ¤

Bases: CustomDeleteView

View to delete a place basin.

PlaceBasinDetailView ¤

Bases: CustomDetailView

View to view a place basin.

PlaceBasinEditView ¤

Bases: CustomEditView

View to edit a place basin.

PlaceBasinFilter ¤

Bases: FilterSet

PlaceBasinListView ¤

Bases: CustomTableView

View to display a table of place basins.

PlaceBasinTable ¤

Bases: Table

PlaceCreateView ¤

Bases: CustomCreateView

View to create a place.

PlaceDeleteView ¤

Bases: CustomDeleteView

View to delete a place.

PlaceDetailView ¤

Bases: CustomDetailView

View to view a place.

PlaceEditView ¤

Bases: CustomEditView

View to edit a place.

PlaceListView ¤

Bases: CustomTableView

View to display a table of places.

PlaceTable ¤

Bases: Table

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

RegionCreateView ¤

Bases: CustomCreateView

View to create a region.

RegionDeleteView ¤

Bases: CustomDeleteView

View to delete a region.

RegionDetailView ¤

Bases: CustomDetailView

View to view a region.

RegionEditView ¤

Bases: CustomEditView

View to edit a region.

RegionFilter ¤

Bases: FilterSet

RegionListView ¤

Bases: CustomTableView

View to display a table of regions.

RegionTable ¤

Bases: Table

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)

StationCreateView ¤

Bases: CustomCreateView

View to create a station.

StationDeleteView ¤

Bases: CustomDeleteView

View to delete a station.

StationDetailView ¤

Bases: CustomDetailView

View to view a station.

StationEditView ¤

Bases: CustomEditView

View to edit a station.

StationFilter ¤

Bases: FilterSet

StationListView ¤

Bases: CustomTableView

View to display a table of stations.

StationTable ¤

Bases: Table

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:stationtype_detail", kwargs={"pk": self.pk})

StationTypeCreateView ¤

Bases: CustomCreateView

View to create a station type.

StationTypeDeleteView ¤

Bases: CustomDeleteView

View to delete a station type.

StationTypeDetailView ¤

Bases: CustomDetailView

View to view a station type.

StationTypeEditView ¤

Bases: CustomEditView

View to edit a station type.

StationTypeListView ¤

Bases: CustomTableView

View to display a table of station types.

StationTypeTable ¤

Bases: Table