Selaa lähdekoodia

MlString update

Yann Weber 9 vuotta sitten
vanhempi
commit
02e32d887b
4 muutettua tiedostoa jossa 26 lisäystä ja 19 poistoa
  1. 1
    1
      EditorialModel/components.py
  2. 2
    0
      EditorialModel/fieldtypes/i18n.py
  3. 22
    17
      Lodel/utils/mlstring.py
  4. 1
    1
      leapi/lefactory.py

+ 1
- 1
EditorialModel/components.py Näytä tiedosto

@@ -57,7 +57,7 @@ class EmComponent(object):
57 57
             if isinstance(attributes_dump[attr_name], EmComponent):
58 58
                 attributes_dump[attr_name] = attributes_dump[attr_name].uid
59 59
             elif isinstance(attributes_dump[attr_name], MlString):
60
-                attributes_dump[attr_name] = attributes_dump[attr_name].__str__()
60
+                attributes_dump[attr_name] = attributes_dump[attr_name].json_dumps()
61 61
         attributes_dump['component'] = self.__class__.__name__
62 62
 
63 63
         return attributes_dump

+ 2
- 0
EditorialModel/fieldtypes/i18n.py Näytä tiedosto

@@ -18,6 +18,8 @@ class EmFieldType(MultiValueFieldType):
18 18
         super().__init__(**args)
19 19
 
20 20
     def _check_data_value(self, value):
21
+        if value is None:
22
+            return (None, None)
21 23
         if isinstance(value, MlString):
22 24
             return (value, None)
23 25
         if isinstance(value, dict):

+ 22
- 17
Lodel/utils/mlstring.py Näytä tiedosto

@@ -13,10 +13,15 @@ class MlString(object):
13 13
     #
14 14
     # @param translations dict: With key = lang and value the translation
15 15
     def __init__(self, translations=None, default_value = None):
16
+        if isinstance(translations, str):
17
+            try:
18
+                translations = json.loads(translations)
19
+            except ValueError:
20
+                if default_value is None:
21
+                    default_value = str(translations)
22
+                    translations = None
16 23
         if translations is None:
17 24
             translations = dict()
18
-        elif isinstance(translations, str):
19
-            translations = json.loads(translations)
20 25
 
21 26
         if not isinstance(translations, dict):
22 27
             raise ValueError('Bad value given for translations argument on MlString instanciation')
@@ -48,29 +53,35 @@ class MlString(object):
48 53
         else:
49 54
             self.translations[lang] = text
50 55
 
56
+    ## @return Default translation
51 57
     def get_default(self):
52 58
         return self.get(self.default_lang)
53
-
59
+    
60
+    ## @brief Set default value
54 61
     def set_default(self, text):
55 62
         self.set(self.default_lang, text)
56 63
 
57
-    def __repr__(self):
58
-        return self.__str__()
59
-
60 64
     ## String representation
61 65
     # @return A json dump of the MlString::translations dict
62 66
     def __str__(self):
67
+        return self.get_default()
68
+    
69
+    ## @brief Serialize the MlString in Json
70
+    def json_dumps(self):
63 71
         if self.translations:
64 72
             return json.dumps(self.translations, sort_keys=True)
65 73
         else:
66 74
             return ""
67 75
 
76
+    ## @brief Equivalent to json_dumps
77
+    def dumps(self): return self.json_dumps()
78
+
68 79
     ## Test if two MlString instance are equivalent
69 80
     # @param other MlString|str : Another MlString instance or a string (json formated)
70 81
     # @return True or False
71 82
     def __eq__(self, other):
72 83
         if isinstance(other, str):
73
-            other = MlString.load(other)
84
+            other = MlString(other)
74 85
 
75 86
         if not isinstance(other, MlString):
76 87
             return False
@@ -97,13 +108,7 @@ class MlString(object):
97 108
     def items(self): return self.translations.items()
98 109
 
99 110
     @staticmethod
100
-    ## Instanciate a MlString from json
101
-    # @param json_string str: Json datas in a string
102
-    # @return A new MlString instance
103
-    # @warning fails silently
104
-    def load(json_string):
105
-        if isinstance(json_string, str) and json_string != '':
106
-            translations = json.loads(json_string)
107
-        else:
108
-            translations = dict()
109
-        return MlString(translations)
111
+    ## Instanciate a MlString from something
112
+    # @deprecated Equivalent to MlString(translations = arg, default_value = None)
113
+    def load(arg):
114
+        return MlString(arg)

+ 1
- 1
leapi/lefactory.py Näytä tiedosto

@@ -124,7 +124,7 @@ class {classname}(LeRel2Type):
124 124
                 fti = field.fieldtype_instance()
125 125
                 if field.string.get() == '':
126 126
                     field.string.set_default(field.name)
127
-                ml_fieldnames[field.name] = field.string.__str__()
127
+                ml_fieldnames[field.name] = field.string.dumps()
128 128
 
129 129
         return """
130 130
 #Initialisation of {name} class attributes

Loading…
Peruuta
Tallenna