Skip to content

stations_map

djangomain.dash_apps.stations_map ¤

Attributes¤

app = DjangoDash('StationsMap', external_stylesheets=[dbc.themes.BOOTSTRAP]) module-attribute ¤

Classes¤

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)

Functions¤

update_map(stations) ¤

Source code in djangomain/dash_apps/stations_map.py
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
@app.callback(
    [
        Output("map_graph", "figure"),
        Output("map_graph", "style"),
    ],
    Input("stations_list", "children"),
)
def update_map(stations) -> px.line:
    station_objs = [
        model_to_dict(Station.objects.get(station_code=code)) for code in stations
    ]

    keys = [
        "station_id",
        "station_code",
        "station_name",
        "station_latitude",
        "station_longitude",
    ]

    stations_filtered = [{key: obj[key] for key in keys} for obj in station_objs]

    df = pd.DataFrame(stations_filtered, columns=keys)
    plot = px.scatter_mapbox(
        df,
        lat="station_latitude",
        lon="station_longitude",
        hover_name="station_code",
        zoom=3.6,
    )
    plot.update_layout(mapbox_style="open-street-map")
    plot.update_layout(margin={"r": 0, "t": 0, "l": 0, "b": 0})

    return plot, {"display": "block"}