Skip to content

views

variable.views ¤

Classes¤

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

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

SensorInstallationCreateView ¤

Bases: CustomCreateView

View to create a sensor installation.

SensorInstallationDeleteView ¤

Bases: CustomDeleteView

View to delete a sensor installation.

SensorInstallationDetailView ¤

Bases: CustomDetailView

View to view a sensor installation.

SensorInstallationEditView ¤

Bases: CustomEditView

View to edit a sensor installation.

SensorInstallationFilter ¤

Bases: FilterSet

SensorInstallationListView ¤

Bases: CustomTableView

View to display a table of sensor installations.

SensorInstallationTable ¤

Bases: Table

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

UnitCreateView ¤

Bases: CustomCreateView

View to create a unit.

UnitDeleteView ¤

Bases: CustomDeleteView

View to delete a unit.

UnitDetailView ¤

Bases: CustomDetailView

View to view a unit.

UnitEditView ¤

Bases: CustomEditView

View to edit a unit.

UnitListView ¤

Bases: CustomTableView

View to display a table of units.

UnitTable ¤

Bases: Table

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

VariableCreateView ¤

Bases: CustomCreateView

View to create a variable.

VariableDeleteView ¤

Bases: CustomDeleteView

View to delete a variable.

VariableDetailView ¤

Bases: CustomDetailView

View to view a variable.

VariableEditView ¤

Bases: CustomEditView

View to edit a variable.

VariableFilter ¤

Bases: FilterSet

VariableListView ¤

Bases: CustomTableView

View to display a table of variables.

VariableTable ¤

Bases: Table