Skip to content

Commit

Permalink
Merge branch 'multi-langs_merge_translation-model-migration' into fea…
Browse files Browse the repository at this point in the history
…t/translation-model-migration
  • Loading branch information
20cents authored Oct 25, 2024
2 parents 482cfd5 + b701c38 commit 8662fa2
Show file tree
Hide file tree
Showing 17 changed files with 732 additions and 199 deletions.
12 changes: 8 additions & 4 deletions backend/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from pypnusershub.login_manager import login_manager
from routes import main as main_blueprint
from flask import Flask
from flask_babel import Babel, get_locale
from flask_babel import Babel
from flask_cors import CORS
from api import api
import config
Expand Down Expand Up @@ -56,6 +56,10 @@ def __call__(self, environ, start_response):
# app.wsgi_app = ReverseProxied(app.wsgi_app)
CORS(app, supports_credentials=True)

@babel.localeselector
def determine_locale():
return utils.getLocale()

app.register_blueprint(main_blueprint)
app.register_blueprint(api)
app.register_blueprint(custom_app.custom)
Expand All @@ -71,9 +75,9 @@ def __call__(self, environ, start_response):
def inject_to_tpl():
custom = custom_app.custom_inject_to_tpl()
data = dict(
dbconf=utils.getDbConf(),
debug=app.debug,
locale=get_locale(),
dbconf=utils.getDbConf(),
debug=app.debug,
locale=utils.getLocale(),
isMultiObservatories=utils.isMultiObservatories,
getThumborUrl=utils.getThumborUrl,
getCustomTpl=utils.getCustomTpl,
Expand Down
2 changes: 1 addition & 1 deletion backend/migrations/versions/d7fd422e1054_translations.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,4 +339,4 @@ def downgrade():
op.drop_table("dico_stheme_translation", schema="geopaysages")
op.drop_table("communes_translation", schema="geopaysages")
op.drop_table("lang", schema="geopaysages")
# ### end Alembic commands ###
# ### end Alembic commands ###
107 changes: 97 additions & 10 deletions backend/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from geoalchemy2.types import Geometry
import geoalchemy2.functions as geo_funcs
from geoalchemy2.shape import to_shape
from marshmallow import fields
from marshmallow import fields, post_dump
from marshmallow_enum import EnumField
from shapely.geometry import mapping

Expand Down Expand Up @@ -85,7 +85,6 @@ class ObservatoryTranslation(db.Model):
"Lang", primaryjoin="ObservatoryTranslation.lang_id == Lang.id"
)


class TSite(db.Model):
__tablename__ = "t_site"
__table_args__ = {"schema": "geopaysages"}
Expand Down Expand Up @@ -381,6 +380,16 @@ def _deserialize(self, value, attr, data):
# schemas#


def get_translated_data(self, data):
if not self.lang_id:
return data
for field in self.translatable_fields:
for translation in data["translations"]:
if translation["lang_id"] == self.lang_id:
data[field] = translation[field]
return data


class LangSchema(ma.SQLAlchemyAutoSchema):
class Meta:
model = Lang
Expand Down Expand Up @@ -430,6 +439,16 @@ class Meta:
class DicoThemeSchema(ma.SQLAlchemyAutoSchema):
translations = ma.Nested(DicoThemeTranslationSchema, many=True)

translatable_fields = DicoThemeTranslationSchema.Meta.fields

def __init__(self, *args, **kwargs):
self.lang_id = kwargs.pop("locale", None)
super().__init__(*args, **kwargs)

@post_dump
def translate_fields(self, data, **kwargs):
return get_translated_data(self, data)

class Meta:
model = DicoTheme
fields = ("id_theme", "icon", "translations")
Expand All @@ -438,6 +457,16 @@ class Meta:
class DicoSthemeSchema(ma.SQLAlchemyAutoSchema):
translations = ma.Nested(DicoSthemeTranslationSchema, many=True)

translatable_fields = DicoSthemeTranslationSchema.Meta.fields

def __init__(self, *args, **kwargs):
self.lang_id = kwargs.pop("locale", None)
super().__init__(*args, **kwargs)

@post_dump
def translate_fields(self, data, **kwargs):
return get_translated_data(self, data)

class Meta:
model = DicoStheme
include_relationships = True
Expand Down Expand Up @@ -468,8 +497,8 @@ class Meta:


class CorSthemeThemeSchema(ma.SQLAlchemyAutoSchema):
dico_theme = ma.Nested(DicoThemeSchema, only=["id_theme", "name_theme"])
dico_stheme = ma.Nested(DicoSthemeSchema, only=["id_stheme", "name_stheme"])
dico_theme = ma.Nested(DicoThemeSchema, only=["id_theme"])
dico_stheme = ma.Nested(DicoSthemeSchema, only=["id_stheme"])

class Meta:
fields = ("dico_theme", "dico_stheme")
Expand All @@ -481,6 +510,16 @@ class ObservatorySchema(ma.SQLAlchemyAutoSchema):
comparator = EnumField(ComparatorEnum, by_value=True)
geom = fields.Method("geomSerialize")

translatable_fields = ObservatoryTranslationSchema.Meta.fields

def __init__(self, *args, **kwargs):
self.lang_id = kwargs.pop("locale", None)
super().__init__(*args, **kwargs)

@post_dump
def translate_fields(self, data, **kwargs):
return get_translated_data(self, data)

@staticmethod
def geomSerialize(obj):
if obj.geom is None:
Expand All @@ -494,7 +533,21 @@ class Meta:
include_relationships = True


class ObservatorySchemaFull(ObservatorySchema):
class ObservatorySchemaFull(ma.SQLAlchemyAutoSchema):
translations = ma.Nested(ObservatoryTranslationSchema, many=True)
comparator = EnumField(ComparatorEnum, by_value=True)
geom = fields.Method("geomSerialize")

translatable_fields = ObservatoryTranslationSchema.Meta.fields

def __init__(self, *args, **kwargs):
self.lang_id = kwargs.pop("locale", None)
super().__init__(*args, **kwargs)

@post_dump
def translate_fields(self, data, **kwargs):
return get_translated_data(self, data)

@staticmethod
def geomSerialize(obj):
if obj.geom is None:
Expand All @@ -503,8 +556,21 @@ def geomSerialize(obj):
return p.wkt


class ObservatorySchemaLite(ObservatorySchema):
class ObservatorySchemaLite(ma.SQLAlchemyAutoSchema):
translations = ma.Nested(ObservatoryTranslationSchema, many=True)
comparator = EnumField(ComparatorEnum, by_value=False)
geom = fields.Method("geomSerialize")

translatable_fields = ObservatoryTranslationSchema.Meta.fields

def __init__(self, *args, **kwargs):
self.lang_id = kwargs.pop("locale", None)
super().__init__(*args, **kwargs)

@post_dump
def translate_fields(self, data, **kwargs):
print("translate_fields", self.lang_id)
return get_translated_data(self, data)

@staticmethod
def geomSerialize(obj):
Expand All @@ -513,17 +579,28 @@ def geomSerialize(obj):
p = to_shape(obj.geom)
s = p.simplify(0.001, preserve_topology=True)
return s.wkt

class Meta:
model = Observatory
include_relationships = True


class TSiteSchema(ma.SQLAlchemyAutoSchema):
translations = ma.Nested(TSiteTranslationSchema, many=True)
geom = GeographySerializationField(attribute="geom")
observatory = ma.Nested(
ObservatorySchema,
only=["id", "translations", "ref", "color", "logo"],
)
observatory = ma.Nested(ObservatorySchema, only=["id", "ref", "color", "logo"])
main_theme = ma.Nested(DicoThemeSchema, only=["id_theme", "translations", "icon"])

translatable_fields = TSiteTranslationSchema.Meta.fields

def __init__(self, *args, **kwargs):
self.lang_id = kwargs.pop("locale", None)
super().__init__(*args, **kwargs)

@post_dump
def translate_fields(self, data, **kwargs):
return get_translated_data(self, data)

class Meta:
model = TSite
include_fk = True
Expand All @@ -533,6 +610,16 @@ class Meta:
class CommunesSchema(ma.SQLAlchemyAutoSchema):
translations = ma.Nested(CommunesTranslationSchema, many=True)

translatable_fields = CommunesTranslationSchema.Meta.fields

def __init__(self, *args, **kwargs):
self.lang_id = kwargs.pop("locale", None)
super().__init__(*args, **kwargs)

@post_dump
def translate_fields(self, data, **kwargs):
return get_translated_data(self, data)

class Meta:
model = Communes
include_relationships = True
Loading

0 comments on commit 8662fa2

Please sign in to comment.