Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 7 additions & 9 deletions enhydris/admin/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
from django.conf import settings
from django.contrib import admin
from django.utils.translation import gettext_lazy as _

from parler.admin import TranslatableAdmin

from enhydris import models

from .garea import GareaAdmin # NOQA
Expand Down Expand Up @@ -32,14 +29,15 @@ class EventTypeAdmin(admin.ModelAdmin):
list_display = ("id", "descr")


class VariableTranslationInline(admin.TabularInline):
model = models.VariableTranslation
extra = 1


@admin.register(models.Variable)
class VariableAdmin(TranslatableAdmin):
class VariableAdmin(admin.ModelAdmin):
list_display = ("id", "descr", "last_modified")

def get_queryset(self, request):
return models.Variable.objects.translated(settings.LANGUAGE_CODE).order_by(
"translations__descr"
)
inlines = [VariableTranslationInline]


@admin.register(models.UnitOfMeasurement)
Expand Down
10 changes: 5 additions & 5 deletions enhydris/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
from django.utils import translation
from rest_framework import serializers

from parler_rest.fields import TranslatedFieldsField
from parler_rest.serializers import TranslatableModelSerializer

from enhydris import models


Expand Down Expand Up @@ -104,13 +101,16 @@ class Meta:
fields = "__all__"


class VariableSerializer(TranslatableModelSerializer):
translations = TranslatedFieldsField(shared_model=models.Variable, required=False)
class VariableSerializer(serializers.ModelSerializer):
translations = serializers.SerializerMethodField()

class Meta:
model = models.Variable
fields = "__all__"

def get_translations(self, obj: models.Variable) -> dict[str, dict[str, str]]:
return {t.language_code: {"descr": t.descr} for t in obj.translations.all()}


class UnitOfMeasurementSerializer(serializers.ModelSerializer):
class Meta:
Expand Down
6 changes: 3 additions & 3 deletions enhydris/api/tests/test_serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@ def test_type_deserialization_for_checked(self):
class TimeseriesSerializerUniqueTypeTestCase(APITestCase):
@classmethod
def setUpTestData(cls):
cls.timeseries_group = baker.make(
models.TimeseriesGroup,
variable__descr="Temperature",
cls.timeseries_group = baker.make(models.TimeseriesGroup)
cls.timeseries_group.variable.translations.create(
language_code="en", descr="Temperature"
)

def test_only_one_initial_timeseries_per_group(self):
Expand Down
16 changes: 15 additions & 1 deletion enhydris/api/tests/test_views/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,25 @@ def test_get_event_type(self):
@override_settings(ENHYDRIS_AUTHENTICATION_REQUIRED=False)
class VariableTestCase(APITestCase):
def setUp(self):
self.variable = baker.make(models.Variable, descr="Temperature")
self.variable = models.Variable.objects.create()
self.variable.translations.create(language_code="en", descr="Temperature")
self.variable.translations.create(language_code="el", descr="Θερμοκρασία")

def test_get_variable(self):
r = self.client.get("/api/variables/{}/".format(self.variable.id))
self.assertEqual(r.status_code, 200)
self.assertEqual(
r.json()["translations"],
{"en": {"descr": "Temperature"}, "el": {"descr": "Θερμοκρασία"}},
)

def test_list_variable(self):
r = self.client.get("/api/variables/")
self.assertEqual(r.status_code, 200)
self.assertEqual(
r.json()["results"][0]["translations"],
{"en": {"descr": "Temperature"}, "el": {"descr": "Θερμοκρασία"}},
)


@override_settings(ENHYDRIS_AUTHENTICATION_REQUIRED=False)
Expand Down
16 changes: 4 additions & 12 deletions enhydris/api/tests/test_views/test_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from rest_framework.test import APITestCase

from model_bakery import baker
from parler.utils.context import switch_language

from enhydris import models

Expand Down Expand Up @@ -101,10 +100,6 @@ class SearchByRemarksWithAccentsTestCase(SearchByRemarksTestCase):

language_settings = {
"LANGUAGE_CODE": "en",
"PARLER_LANGUAGES": {
settings.SITE_ID: ({"code": "en"}, {"code": "fr"}),
"default": {"fallbacks": ["en"], "hide_untranslated": False},
},
}


Expand All @@ -119,13 +114,10 @@ def _create_models(self):
self._create_timeseries(station1, "Rain", "Pluie")
self._create_timeseries(station2, "Humidity", "Humidité")

def _create_timeseries(self, station, var_en, var_fr):
variable = models.Variable()
with switch_language(variable, "en"):
variable.descr = var_en
with switch_language(variable, "fr"):
variable.descr = var_fr
variable.save()
def _create_timeseries(self, station: models.Station, var_en: str, var_fr: str):
variable = models.Variable.objects.create()
variable.translations.create(language_code="en", descr=var_en)
variable.translations.create(language_code="fr", descr=var_fr)
baker.make(
models.Timeseries,
timeseries_group__gentity=station,
Expand Down
4 changes: 3 additions & 1 deletion enhydris/api/tests/test_views/test_search_by_ts_has_years.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ def _make_timeseries(self, station, variable_descr, datastr):
result = baker.make(
models.Timeseries,
timeseries_group__gentity=station,
timeseries_group__variable__descr=variable_descr,
)
result.timeseries_group.variable.translations.create(
language_code="en", descr=variable_descr
)
result.set_data(StringIO(datastr), default_timezone="Etc/GMT-2")
return result
Expand Down
13 changes: 7 additions & 6 deletions enhydris/api/tests/test_views/test_station.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,9 @@ def _create_timeseries_groups(self):
self._create_timeseries_group(self.station_agios_athanasios, "Humidity")

def _create_timeseries_group(self, station, variable_descr):
baker.make(
models.TimeseriesGroup, gentity=station, variable__descr=variable_descr
timeseries_group = baker.make(models.TimeseriesGroup, gentity=station)
timeseries_group.variable.translations.create(
language_code="en", descr=variable_descr
)

def test_station_csv(self):
Expand All @@ -247,8 +248,8 @@ def test_station_with_geometry_with_no_srid_is_included(self):

def test_num_queries(self):
self._create_timeseries_groups()
# There should be seven queries: one for stations, one for timeseries_groups,
# one for timeseries. The other four are two for django_session and two
# for a savepoint.
with self.assertNumQueries(7):
# There should be eight queries: one for stations, one for timeseries_groups,
# one for timeseries, one for prefetching variable translations. The
# other four are two for django_session and two for a savepoint.
with self.assertNumQueries(8):
self.client.get("/api/stations/csv/")
13 changes: 9 additions & 4 deletions enhydris/api/tests/test_views/test_timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -469,9 +469,11 @@ def setUp(self):
self.timeseries_group = baker.make(
models.TimeseriesGroup,
gentity=self.station,
variable__descr="irrelevant",
precision=2,
)
self.timeseries_group.variable.translations.create(
language_code="en", descr="irrelevant"
)
self.timeseries = baker.make(
models.Timeseries,
timeseries_group=self.timeseries_group,
Expand Down Expand Up @@ -583,7 +585,8 @@ class TimeseriesPostTestCase(APITestCase):
def setUp(self):
self.user1 = baker.make(User, is_active=True, is_superuser=False)
self.user2 = baker.make(User, is_active=True, is_superuser=False)
self.variable = baker.make(models.Variable, descr="Temperature")
self.variable = models.Variable.objects.create()
self.variable.translations.create(language_code="en", descr="Temperature")
self.unit_of_measurement = baker.make(models.UnitOfMeasurement)
self.station = baker.make(models.Station, creator=self.user1)
self.timeseries_group = baker.make(models.TimeseriesGroup, gentity=self.station)
Expand Down Expand Up @@ -629,7 +632,8 @@ def test_returns_proper_error_when_creating_second_initial_timeseries(self):
class TimeseriesPostWithWrongStationOrTimeseriesGroupTestCase(APITestCase):
def setUp(self):
self.user = baker.make(User, is_active=True, is_superuser=False)
self.variable = baker.make(models.Variable, descr="Temperature")
self.variable = models.Variable.objects.create()
self.variable.translations.create(language_code="en", descr="Temperature")
self.unit_of_measurement = baker.make(models.UnitOfMeasurement)
self.station1 = baker.make(models.Station, creator=self.user)
self.timeseries_group_1_1 = baker.make(
Expand Down Expand Up @@ -690,7 +694,8 @@ def test_create_timeseries_with_everything_correct(self):
class TimeseriesPostWithWrongTimeseriesTypeTestCase(APITestCase):
def setUp(self):
self.user = baker.make(User, is_active=True, is_superuser=False)
self.variable = baker.make(models.Variable, descr="Temperature")
self.variable = models.Variable.objects.create()
self.variable.translations.create(language_code="en", descr="Temperature")
self.unit_of_measurement = baker.make(models.UnitOfMeasurement)
self.station = baker.make(models.Station, creator=self.user)
self.timeseries_group = baker.make(models.TimeseriesGroup, gentity=self.station)
Expand Down
3 changes: 2 additions & 1 deletion enhydris/api/tests/test_views/test_timeseries_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ class TimeseriesGroupPostTestCase(APITestCase):
def setUp(self):
self.user1 = baker.make(User, is_active=True, is_superuser=False)
self.user2 = baker.make(User, is_active=True, is_superuser=False)
self.variable = baker.make(models.Variable, descr="Temperature")
self.variable = models.Variable.objects.create()
self.variable.translations.create(language_code="en", descr="Temperature")
self.unit_of_measurement = baker.make(models.UnitOfMeasurement)
self.station = baker.make(models.Station, creator=self.user1)
self.station2 = baker.make(models.Station, creator=self.user1)
Expand Down
10 changes: 8 additions & 2 deletions enhydris/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,13 @@ def csv(self, request: Request):
queryset=models.TimeseriesGroup.objects.select_related(
"variable", "unit_of_measurement"
)
.prefetch_related("timeseries_set")
.prefetch_related(
"timeseries_set",
Prefetch(
"variable__translations",
to_attr="prefetched_translations",
),
)
.order_by("variable__id"),
)
)
Expand Down Expand Up @@ -90,7 +96,7 @@ class EventTypeViewSet(ReadOnlyModelViewSet[models.EventType]):

class VariableViewSet(ReadOnlyModelViewSet[models.Variable]):
serializer_class = serializers.VariableSerializer
queryset = models.Variable.objects.all() # type: ignore
queryset = models.Variable.objects.prefetch_related("translations") # type: ignore


class UnitOfMeasurementViewSet(ReadOnlyModelViewSet[models.UnitOfMeasurement]):
Expand Down
3 changes: 2 additions & 1 deletion enhydris/autoprocess/tests/test_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ def _create_data(cls):
cls.organization = enhydris.models.Organization.objects.create(
name="Serial killers SA"
)
cls.variable = baker.make(enhydris.models.Variable, descr="myvar")
cls.variable = enhydris.models.Variable.objects.create()
cls.variable.translations.create(language_code="en", descr="myvar")
cls.unit = baker.make(enhydris.models.UnitOfMeasurement)
cls.station = baker.make(
enhydris.models.Station, creator=cls.user, owner=cls.organization
Expand Down
36 changes: 15 additions & 21 deletions enhydris/autoprocess/tests/test_models/test_aggregation.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
class AggregationTestCase(TestCase):
def setUp(self):
self.station = baker.make(Station)
variable = baker.make(Variable, descr="Irrelevant")
variable = Variable.objects.create()
variable.translations.create(language_code="en", descr="irrelevant")
self.timeseries_group = baker.make(
TimeseriesGroup, gentity=self.station, variable=variable
)
Expand Down Expand Up @@ -59,7 +60,8 @@ def test_str(self):

def test_no_extra_queries_for_str(self):
self._baker_make_aggregation()
with self.assertNumQueries(1):
# One query for the aggregation, one for prefetching of variable translations
with self.assertNumQueries(2):
str(Aggregation.objects.first())

def test_wrong_resulting_timestamp_offset_1(self):
Expand Down Expand Up @@ -225,12 +227,14 @@ def _execute(self, max_missing):
self.aggregation = baker.make(
Aggregation,
timeseries_group__gentity=station,
timeseries_group__variable__descr="Hello",
target_time_step="1h",
method="sum",
max_missing=max_missing,
resulting_timestamp_offset="1min",
)
self.aggregation.timeseries_group.variable.translations.create(
language_code="en", descr="Hello"
)
self.aggregation._htimeseries = HTimeseries(self.source_timeseries)
self.aggregation._htimeseries.time_step = "10min"
return self.aggregation.process_timeseries().data
Expand Down Expand Up @@ -283,9 +287,8 @@ class AggregationProcessTimeseriesWhenNoTimeStepTestCase(TestCase):
@classmethod
def setUpTestData(cls):
station = baker.make(Station)
timeseries_group = baker.make(
TimeseriesGroup, gentity=station, variable__descr="hello"
)
timeseries_group = baker.make(TimeseriesGroup, gentity=station)
timeseries_group.variable.translations.create(language_code="en", descr="hello")
cls.aggregation = baker.make(
Aggregation,
timeseries_group=timeseries_group,
Expand Down Expand Up @@ -319,9 +322,8 @@ class AggregationRegularizationModeTestCase(TestCase):
@classmethod
def setUpTestData(cls):
station = baker.make(Station)
timeseries_group = baker.make(
TimeseriesGroup, gentity=station, variable__descr="hello"
)
timeseries_group = baker.make(TimeseriesGroup, gentity=station)
timeseries_group.variable.translations.create(language_code="en", descr="hello")
cls.aggregation = baker.make(
Aggregation,
timeseries_group=timeseries_group,
Expand Down Expand Up @@ -389,12 +391,8 @@ class AggregationRecalculatesLastValueIfNeededTestCase(TestCase):

def setUp(self):
station = baker.make(Station, name="Hobbiton", display_timezone="Etc/GMT-2")
timeseries_group = baker.make(
TimeseriesGroup,
gentity=station,
variable__descr="h",
precision=0,
)
timeseries_group = baker.make(TimeseriesGroup, gentity=station, precision=0)
timeseries_group.variable.translations.create(language_code="en", descr="h")
source_timeseries = baker.make(
Timeseries,
timeseries_group=timeseries_group,
Expand Down Expand Up @@ -482,12 +480,8 @@ class AggregationTooFewValuesTestCase(TestCase):

def setUp(self):
station = baker.make(Station, name="Hobbiton", display_timezone="Etc/GMT-2")
timeseries_group = baker.make(
TimeseriesGroup,
gentity=station,
variable__descr="h",
precision=0,
)
timeseries_group = baker.make(TimeseriesGroup, gentity=station, precision=0)
timeseries_group.variable.translations.create(language_code="en", descr="h")
source_timeseries = baker.make(
Timeseries,
timeseries_group=timeseries_group,
Expand Down
14 changes: 6 additions & 8 deletions enhydris/autoprocess/tests/test_models/test_autoprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,9 @@ def test_auto_process_is_not_triggered_before_commit(self):
class AutoProcessExecuteTestCase(ClearCacheMixin, TestCase):
def setUp(self):
station = baker.make(Station, display_timezone="Etc/GMT-2")
self.timeseries_group = baker.make(
TimeseriesGroup,
gentity=station,
variable__descr="irrelevant",
self.timeseries_group = baker.make(TimeseriesGroup, gentity=station)
self.timeseries_group.variable.translations.create(
language_code="en", descr="irrelevant"
)
self.checks = baker.make(Checks, timeseries_group=self.timeseries_group)
self.range_check = baker.make(RangeCheck, checks=self.checks)
Expand Down Expand Up @@ -115,10 +114,9 @@ class AutoProcessRecalculateTestCaseBase(TestCase):
def setUp(self, m: mock.MagicMock):
self.mock_process_timeseries = m
station = baker.make(Station, display_timezone="Etc/GMT-2")
self.timeseries_group = baker.make(
TimeseriesGroup,
gentity=station,
variable__descr="h",
self.timeseries_group = baker.make(TimeseriesGroup, gentity=station)
self.timeseries_group.variable.translations.create(
language_code="en", descr="h"
)
self.source_timeseries = baker.make(
Timeseries, timeseries_group=self.timeseries_group, type=Timeseries.INITIAL
Expand Down
Loading