diff --git a/idhub/models.py b/idhub/models.py index d6f6f7a..14229e5 100644 --- a/idhub/models.py +++ b/idhub/models.py @@ -483,13 +483,12 @@ class Schemas(models.Model): return {} return json.loads(self.data) -#<<<<<<< HEAD - def _update_and_get_field(self, field_attr, schema_key): + def _update_and_get_field(self, field_attr, schema_key, is_json=False): field_value = getattr(self, field_attr) if not field_value: - field_value = self.get_schema.get(schema_key, '') + field_value = self.get_schema.get(schema_key, [] if is_json else '') self._update_model_field(field_attr, field_value) - return field_value + return json.loads(field_value) if is_json else field_value def _update_model_field(self, field_attr, field_value): if field_value: @@ -497,29 +496,38 @@ class Schemas(models.Model): self.save(update_fields=[field_attr]) @property - def name(self): - return self._update_and_get_field('_name', 'name') + def name(self, request=None): + names = self._update_and_get_field('_name', 'name', + is_json=True) + language_code = self._get_language_code(request) + name = self._get_name_by_language(names, language_code) + + return name + + def _get_language_code(self, request=None): + language_code = settings.LANGUAGE_CODE + if request: + language_code = request.LANGUAGE_CODE + if self._is_catalan_code(language_code): + language_code = 'ca' + + return language_code + + def _get_name_by_language(self, names, lang_code): + for name in names: + if name.get('lang') == lang_code: + return name.get('value') + + return None + + def _is_catalan_code(self, language_code): + return language_code == 'ca_ES' @name.setter def name(self, value): - self._name = value + self._name = json.dumps(value) @property -#======= - def name(self, request=None): - names = {} - for name in self.get_schema.get('name', []): - lang = name.get('lang') - if 'ca' in lang: - lang = 'ca' - names[lang]= name.get('value') - - if request and request.LANGUAGE_CODE in names.keys(): - return names[request.LANGUAGE_CODE] - - return names[settings.LANGUAGE_CODE] - -#>>>>>>> main def description(self): return self._update_and_get_field('_description', 'description')