Skip to content

views

formatting.views ¤

Classes¤

Classification ¤

Bases: PermissionsBase

Contains instructions on how to classify the data into a specific variable.

In particular, it links a format to a variable, and provides the column indices for the value, maximum, and minimum columns, as well as the validator columns. It also contains information on whether the data is accumulated, incremental, and the resolution of the data.

Attributes:

Name Type Description
cls_id AutoField

Primary key.

format ForeignKey

The format of the data file.

variable ForeignKey

The variable to which the data belongs.

value PositiveSmallIntegerField

Index of the value column, starting in 0.

maximum PositiveSmallIntegerField

Index of the maximum value column, starting in 0.

minimum PositiveSmallIntegerField

Index of the minimum value column, starting in 0.

value_validator_column PositiveSmallIntegerField

Index of the value validator column, starting in 0.

value_validator_text CharField

Value validator text.

maximum_validator_column PositiveSmallIntegerField

Index of the maximum value validator column, starting in 0.

maximum_validator_text CharField

Maximum value validator text.

minimum_validator_column PositiveSmallIntegerField

Index of the minimum value validator column, starting in 0.

minimum_validator_text CharField

Minimum value validator text.

accumulate PositiveSmallIntegerField

If set to a number of minutes, the data will be accumulated over that period.

resolution DecimalField

Resolution of the data. Only used if it is to be accumulated.

incremental BooleanField

Whether the data is an incremental counter. If it is, any value below the previous one will be removed.

decimal_comma BooleanField

Whether the data uses a comma as a decimal separator.

Functions¤
__str__() ¤

Return the string representation of the object.

Source code in formatting\models.py
419
420
421
def __str__(self) -> str:
    """Return the string representation of the object."""
    return str(self.cls_id)
clean() ¤

Validate the model instance.

It checks that the column indices are different, and that the accumulation period is greater than zero if it is set. It also checks that the resolution is set if the data is accumulated.

Source code in formatting\models.py
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
def clean(self) -> None:
    """Validate the model instance.

    It checks that the column indices are different, and that the accumulation
    period is greater than zero if it is set. It also checks that the resolution is
    set if the data is accumulated.
    """
    if self.accumulate and self.resolution is None:
        raise ValidationError(
            {
                "resolution": "The resolution must be set if the data is "
                "accumulated."
            }
        )

    col_names = [
        "value",
        "maximum",
        "minimum",
        "value_validator_column",
        "maximum_validator_column",
        "minimum_validator_column",
    ]
    unique = defaultdict(list)
    for name in col_names:
        if getattr(self, name) is not None:
            unique[getattr(self, name)].append(name)
    for _, names in unique.items():
        if len(names) != 1:
            msg = "The columns must be different."
            raise ValidationError({field: msg for field in names})
get_absolute_url() ¤

Get the absolute URL of the object.

Source code in formatting\models.py
423
424
425
def get_absolute_url(self) -> str:
    """Get the absolute URL of the object."""
    return reverse("formatting:classification_detail", kwargs={"pk": self.pk})

ClassificationCreateView ¤

Bases: CustomCreateView

View to create a classification.

ClassificationDeleteView ¤

Bases: CustomDeleteView

View to delete a classification.

ClassificationDetailView ¤

Bases: CustomDetailView

View to view a classification.

ClassificationEditView ¤

Bases: CustomEditView

View to edit a classification.

ClassificationFilter ¤

Bases: FilterSet

ClassificationListView ¤

Bases: CustomTableView

View to list all classifications.

ClassificationTable ¤

Bases: Table

Date ¤

Bases: PermissionsBase

Date format.

Format string for the date column. It is used to parse the date column in the data file. The format string must be compatible with the datetime module in Python. See the datetime documentation for more information on valid format codes.

Attributes:

Name Type Description
date_id AutoField

Primary key.

date_format CharField

The format string for the date column in human readable form, eg. DD-MM-YYYY.

code CharField

The code used to parse the date column, eg. %d-%m-%Y.

Functions¤
__str__() ¤

Return the string representation of the object.

Source code in formatting\models.py
115
116
117
def __str__(self) -> str:
    """Return the string representation of the object."""
    return str(self.date_format)
get_absolute_url() ¤

Get the absolute URL of the object.

Source code in formatting\models.py
119
120
121
def get_absolute_url(self) -> str:
    """Get the absolute URL of the object."""
    return reverse("formatting:date_detail", kwargs={"pk": self.pk})

DateCreateView ¤

Bases: CustomCreateView

View to create a date.

DateDeleteView ¤

Bases: CustomDeleteView

View to delete a date.

DateDetailView ¤

Bases: CustomDetailView

View to view a date.

DateEditView ¤

Bases: CustomEditView

View to edit a date.

DateListView ¤

Bases: CustomTableView

View to list all dates.

DateTable ¤

Bases: Table

Delimiter ¤

Bases: PermissionsBase

Delimiter between columns in the data file.

One or more characters that separate columns in a text file. The most common values are ,, ;, and \t (tab).

Attributes:

Name Type Description
delimiter_id AutoField

Primary key.

name CharField

The name of the delimiter. eg. comma, semicolon, tab.

character CharField

The character used as a delimiter. eg. ,, ;, \t.

Functions¤
__str__() ¤

Return the string representation of the object.

Source code in formatting\models.py
78
79
80
def __str__(self) -> str:
    """Return the string representation of the object."""
    return str(self.name)
get_absolute_url() ¤

Get the absolute URL of the object.

Source code in formatting\models.py
82
83
84
def get_absolute_url(self) -> str:
    """Get the absolute URL of the object."""
    return reverse("formatting:delimiter_detail", kwargs={"pk": self.pk})

DelimiterCreateView ¤

Bases: CustomCreateView

View to create a delimiter.

DelimiterDeleteView ¤

Bases: CustomDeleteView

View to delete a delimiter.

DelimiterDetailView ¤

Bases: CustomDetailView

View to view a delimiter.

DelimiterEditView ¤

Bases: CustomEditView

View to edit a delimiter.

DelimiterListView ¤

Bases: CustomTableView

View to list all delimiters.

DelimiterTable ¤

Bases: Table

Extension ¤

Bases: PermissionsBase

Extension of the data file.

It is mostly used to choose the tool to be employed to ingest the data. While it can take any value, there is currently explicit support only for xlsx and xlx. Anything else will be interpreted as a text file and loaded using pandas.read_csv.

Attributes:

Name Type Description
extension_id AutoField

Primary key.

value CharField

The extension value. eg. xlsx, xlx, txt.

Functions¤
__str__() ¤

Return the string representation of the object.

Source code in formatting\models.py
44
45
46
def __str__(self) -> str:
    """Return the string representation of the object."""
    return str(self.value)
get_absolute_url() ¤

Get the absolute URL of the object.

Source code in formatting\models.py
48
49
50
def get_absolute_url(self) -> str:
    """Get the absolute URL of the object."""
    return reverse("formatting:extension_detail", kwargs={"pk": self.pk})

ExtensionCreateView ¤

Bases: CustomCreateView

View to create a extension.

ExtensionDeleteView ¤

Bases: CustomDeleteView

View to delete a extension.

ExtensionDetailView ¤

Bases: CustomDetailView

View to view a extension.

ExtensionEditView ¤

Bases: CustomEditView

View to edit a extension.

ExtensionListView ¤

Bases: CustomTableView

View to list all extensions.

ExtensionTable ¤

Bases: Table

Format ¤

Bases: PermissionsBase

Details of the data file format, describing how to read the file.

It combines several properties, such as the file extension, the delimiter, the date and time formats, and the column indices for the date and time columns, instructing how to read the data file and parse the dates. It is mostly used to ingest data from text files, like CSV.

Attributes:

Name Type Description
format_id AutoField

Primary key.

name CharField

Short name of the format entry.

description TextField

Description of the format.

extension ForeignKey

The extension of the data file.

delimiter ForeignKey

The delimiter between columns in the data file. Only required for text files.

first_row PositiveSmallIntegerField

Index of the first row with data, starting in 0.

footer_rows PositiveSmallIntegerField

Number of footer rows to be ignored at the end.

date ForeignKey

Format for the date column. Only required for text files.

date_column PositiveSmallIntegerField

Index of the date column, starting in 0.

time ForeignKey

Format for the time column. Only required for text files.

time_column PositiveSmallIntegerField

Index of the time column, starting in 0.

Attributes¤
datetime_format: str property ¤

Obtain the datetime format string.

Functions¤
__str__() ¤

Return the string representation of the object.

Source code in formatting\models.py
253
254
255
def __str__(self) -> str:
    """Return the string representation of the object."""
    return str(self.name)
datetime_columns(delimiter) ¤

Column indices that correspond to the date and time columns in the dataset.

Parameters:

Name Type Description Default
delimiter str

The delimiter used to split the date and time codes.

required

Returns:

Type Description
list[int]

list[int]: A list of column indices.

Source code in formatting\models.py
266
267
268
269
270
271
272
273
274
275
276
277
278
279
def datetime_columns(self, delimiter: str) -> list[int]:
    """Column indices that correspond to the date and time columns in the dataset.

    Args:
        delimiter (str): The delimiter used to split the date and time codes.

    Returns:
        list[int]: A list of column indices.
    """
    date_items = self.date.code.split(delimiter)
    date_cols = list(range(self.date_column, self.date_column + len(date_items)))
    time_items = self.time.code.split(delimiter)
    time_cols = list(range(self.time_column, self.time_column + len(time_items)))
    return date_cols + time_cols
get_absolute_url() ¤

Get the absolute URL of the object.

Source code in formatting\models.py
257
258
259
def get_absolute_url(self) -> str:
    """Get the absolute URL of the object."""
    return reverse("formatting:format_detail", kwargs={"pk": self.pk})

FormatCreateView ¤

Bases: CustomCreateView

View to create a format.

FormatDeleteView ¤

Bases: CustomDeleteView

View to delete a format.

FormatDetailView ¤

Bases: CustomDetailView

View to view a format.

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 formatting\views.py
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
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.
    """
    objects = [
        [
            linkify(obj.pk, "formatting:classification_detail", obj.pk),
            obj.value,
            obj.variable.name,
            linkify(
                obj.variable.pk,
                "variable:variable_detail",
                obj.variable.variable_code,
            ),
            linkify(
                obj.variable.unit.pk, "variable:unit_detail", obj.variable.unit
            ),
        ]
        for obj in self.object.classification_set.all()
    ]
    return {
        "title": "Classifications",
        "header": ["Id", "Column", "Variable", "Code", "Unit"],
        "objects": objects,
    }

FormatEditView ¤

Bases: CustomEditView

View to edit a format.

FormatFilter ¤

Bases: FilterSet

FormatListView ¤

Bases: CustomTableView

View to list all formats.

FormatTable ¤

Bases: Table

Time ¤

Bases: PermissionsBase

Time format.

Format string for the time column. It is used to parse the time column in the data file. The format string must be compatible with the datetime module in Python. See the datetime documentation for more information on valid format codes.

Attributes:

Name Type Description
date_id AutoField

Primary key.

date_format CharField

The format string for the date column in human readable form, eg. HH:MM:SS 24H.

code CharField

The code used to parse the date column, eg. %H:%M:%S.

Functions¤
__str__() ¤

Return the string representation of the object.

Source code in formatting\models.py
155
156
157
def __str__(self) -> str:
    """Return the string representation of the object."""
    return str(self.time_format)
get_absolute_url() ¤

Get the absolute URL of the object.

Source code in formatting\models.py
159
160
161
def get_absolute_url(self) -> str:
    """Get the absolute URL of the object."""
    return reverse("formatting:time_detail", kwargs={"pk": self.pk})

TimeCreateView ¤

Bases: CustomCreateView

View to create a time.

TimeDeleteView ¤

Bases: CustomDeleteView

View to delete a time.

TimeDetailView ¤

Bases: CustomDetailView

View to view a time.

TimeEditView ¤

Bases: CustomEditView

View to edit a time.

TimeListView ¤

Bases: CustomTableView

View to list all times.

TimeTable ¤

Bases: Table

Functions¤

linkify(pk, address, label) ¤

Return a link to the address with the label.

Parameters:

Name Type Description Default
pk int

Primary key of the object.

required
address str

URL address to link to. It must be a named URL, eg 'app_name:model_detail'.

required
label str

Label to display on the link.

required

Returns:

Name Type Description
str str

HTML link to the address.

Source code in formatting\views.py
24
25
26
27
28
29
30
31
32
33
34
35
36
37
def linkify(pk: int, address: str, label: str) -> str:
    """Return a link to the address with the label.

    Args:
        pk (int): Primary key of the object.
        address (str): URL address to link to. It must be a named URL, eg
            'app_name:model_detail'.
        label (str): Label to display on the link.

    Returns:
        str: HTML link to the address.
    """
    url = reverse(address, kwargs={"pk": pk})
    return mark_safe(f"<a href='{url}' class='btn btn-link'>{label}</a>")