Browse Source

Merge branch 'master' of git.labocleo.org:lodel2

Driky 10 years ago
parent
commit
14a0d18707

+ 3
- 4
Database/sqlsetup.py View File

@@ -27,9 +27,8 @@ class SQLSetup(object):
27 27
         uids = {
28 28
                 "name":"uids",
29 29
                 "columns":[
30
-                    {"name":"uid",          "type":"VARCHAR(50)", "extra":{"nullable":False, "primarykey":True}},
31
-                    {"name":"class",        "type":"VARCHAR(50)"},
32
-                    {"name":"type",         "type":"VARCHAR(50)"}
30
+                    {"name":"uid",          "type":"INTEGER", "extra":{"nullable":False, "primarykey":True, 'autoincrement':True}},
31
+                    {"name":"table",        "type":"VARCHAR(50)"}
33 32
                 ]
34 33
             }
35 34
         tables.append(uids)
@@ -38,7 +37,7 @@ class SQLSetup(object):
38 37
         # Table listing the classes
39 38
         em_class = {"name":"em_class"}
40 39
         em_class['columns'] = default_columns + [
41
-            {"name":"classtype",    "type":"INTEGER"},
40
+            {"name":"classtype",    "type":"VARCHAR(50)"},
42 41
             {"name":"sortcolumn",   "type":"VARCHAR(50)", "extra":{"default":"rank"}},
43 42
             {"name":"icon",         "type":"INTEGER"},
44 43
         ]

+ 61
- 17
EditorialModel/classes.py View File

@@ -6,12 +6,15 @@
6 6
 """
7 7
 
8 8
 from EditorialModel.components import EmComponent, EmComponentNotExistError
9
-import EditorialModel.classtypes
10 9
 from Database.sqlwrapper import SqlWrapper
10
+from Database.sqlobject import SqlObject
11
+import EditorialModel
11 12
 
12 13
 class EmClass(EmComponent):
14
+    table = 'em_class'
15
+
13 16
     def __init__(self, id_or_name):
14
-        self.table = 'em_class'
17
+        self.table = EmClass.table
15 18
         super(EmClass, self).__init__(id_or_name)
16 19
 
17 20
     """ create a new class
@@ -20,41 +23,82 @@ class EmClass(EmComponent):
20 23
     """
21 24
     @staticmethod
22 25
     def create(name, class_type):
23
-        #try:
24
-        exists = EmClass(name)
25
-        #except EmComponentNotExistError:
26
-            #print ("bin")
27
-            #pass
28
-        print (name, class_type)
29
-        pass
26
+        try:
27
+            exists = EmClass(name)
28
+        except EmComponentNotExistError:
29
+            uids = SqlObject('uids')
30
+            res = uids.wexec(uids.table.insert().values(table=EmClass.table))
31
+            uid = res.inserted_primary_key
32
+
33
+            emclass = SqlObject(EmClass.table)
34
+            res = emclass.wexec(emclass.table.insert().values(uid=uid, name=name, classtype=class_type['name']))
35
+            SqlWrapper.wc().execute("CREATE TABLE %s (uid VARCHAR(50))" % name)
36
+            return EmClass(name)
37
+
38
+        return exists
39
+
40
+    def populate(self):
41
+        row = super(EmClass, self).populate()
42
+        self.classtype = row.classtype
43
+        self.icon = row.icon
44
+        self.sortcolumn = row.sortcolumn
45
+
46
+    def save(self):
47
+        # should not be here, but cannot see how to do this
48
+        if self.name is None:
49
+            self.populate()
50
+
51
+        values = {
52
+            'classtype' : self.classtype,
53
+            'icon' : self.icon,
54
+            'sortcolumn' : self.sortcolumn,
55
+        }
56
+
57
+        return super(EmClass, self).save(values)
30 58
 
31 59
     """ retrieve list of the field_groups of this class
32 60
         @return field_groups [EmFieldGroup]:
33 61
     """
34
-    def field_groups():
35
-        pass
62
+    def fieldgroups(self):
63
+        fieldgroups_req = SqlObject(EditorialModel.fieldgroups.EmFieldGroup.table)
64
+        select = fieldgroups_req.sel
65
+        select.where(fieldgroups_req.col.class_id == self.id)
66
+
67
+        sqlresult = fieldgroups_req.rexec(select)
68
+        records = sqlresult.fetchall()
69
+        fieldgroups = [ EditorialModel.fieldgroups.EmFieldGroup(int(record.uid)) for record in records ]
70
+
71
+        return fieldgroups
36 72
 
37 73
     """ retrieve list of fields
38 74
         @return fields [EmField]:
39 75
     """
40
-    def fields():
76
+    def fields(self):
41 77
         pass
42 78
 
43 79
     """ retrieve list of type of this class
44 80
         @return types [EmType]:
45 81
     """
46
-    def types():
47
-        pass
82
+    def types(self):
83
+        emtype = SqlObject(EditorialModel.types.EmType.table)
84
+        select = emtype.sel
85
+        select.where(emtype.col.class_id == self.id)
86
+
87
+        sqlresult = emtype.rexec(select)
88
+        records = sqlresult.fetchall()
89
+        types = [ EditorialModel.types.EmType(int(record.uid)) for record in records ]
90
+
91
+        return types
48 92
 
49 93
     """ add a new EmType that can ben linked to this class
50 94
         @param  t EmType: type to link
51 95
         @return success bool: done or not
52 96
     """
53
-    def link_type(t):
97
+    def link_type(self, t):
54 98
         pass
55 99
 
56 100
     """ retrieve list of EmType that are linked to this class
57 101
         @return types [EmType]:
58 102
     """
59
-    def linked_types():
60
-        pass
103
+    def linked_types(self):
104
+        pass

+ 35
- 42
EditorialModel/components.py View File

@@ -23,9 +23,10 @@ class EmComponent(object):
23 23
         SqlWrapper.start()
24 24
         if self is EmComponent:
25 25
             raise EnvironmentError('Abstract class')
26
-        if type(id_or_name) is int:
26
+        if isinstance(id_or_name, int):
27 27
             self.id = id_or_name
28
-        elif type(id_or_name) is str:
28
+            self.name = None
29
+        elif isinstance(id_or_name, str):
29 30
             self.id = None
30 31
             self.name = id_or_name
31 32
             self.populate()
@@ -35,43 +36,50 @@ class EmComponent(object):
35 36
     """ Lookup in the database properties of the object to populate the properties
36 37
     """
37 38
     def populate(self):
38
-        dbo = SqlObject(self.table)
39
-        
40
-        req = dbo.sel
41
-        
39
+        table = SqlObject(self.table)
40
+        select = table.sel
41
+
42 42
         if self.id is None:
43
-            req.where(dbo.col.name == self.name)
43
+            select.where(table.col.name == self.name)
44 44
         else:
45
-            req.where(dbo.col.id == self.id)
46
-
47
-        sqlresult = dbo.rexec(req)
45
+            select.where(table.col.uid == self.id)
48 46
 
49
-        # Transformation du résultat en une liste de dictionnaires
47
+        sqlresult = table.rexec(select)
50 48
         records = sqlresult.fetchall()
51
-        print (records)
52 49
 
53
-        for record in records:
54
-            selected_lines.append(dict(zip(record.keys(), record)))
55
-
56
-        if not row:
50
+        if not records:
57 51
             # could have two possible Error message for id and for name
58 52
             raise EmComponentNotExistError("Bad id_or_name: could not find the component")
59 53
 
54
+        for record in records:
55
+            row = type('row', (object,), {})()
56
+            for k in record.keys():
57
+                setattr(row, k, record[k])
58
+
59
+        self.id = int(row.uid)
60 60
         self.name = row.name
61
-        self.rank = int(row.rank)
61
+        self.rank = 0 if row.rank is None else int(row.rank)
62 62
         self.date_update = row.date_update
63 63
         self.date_create = row.date_create
64
-        self.string = MlString.from_json(row.string)
65
-        self.help = MlString.from_json(row.help)
66
-        self.icon = row.icon
64
+        self.string = MlString.load(row.string)
65
+        self.help = MlString.load(row.help)
67 66
 
68 67
         return row
69 68
 
70 69
     """ write the representation of the component in the database
71 70
         @return bool
72 71
     """
73
-    def save(self):
74
-        pass
72
+    def save(self, values):
73
+        values['name'] = self.name
74
+        values['rank'] = self.rank
75
+        values['date_update'] = self.date_update
76
+        values['date_create'] = self.date_create
77
+        values['string'] = str(self.string)
78
+        values['help']= str(self.help)
79
+
80
+        emclass = SqlObject(self.table)
81
+        update = emclass.table.update(values=values)
82
+        res = emclass.wexec(update)
75 83
 
76 84
     """ delete this component data in the database
77 85
         @return bool
@@ -85,27 +93,12 @@ class EmComponent(object):
85 93
     def modify_rank(self, new_rank):
86 94
         pass
87 95
 
88
-    """ set a string representation of the component for a given language
89
-        @param  lang str: iso 639-2 code of the language http://en.wikipedia.org/wiki/List_of_ISO_639-2_codes
90
-        @param  text str: text to set
91
-        @return bool
92
-    """
93
-    def set_string(self, lang, text):
94
-        pass
95
-
96
-    """ set the string representation of the component
97
-        @param  ml_string  MlString: strings for all language
98
-        @return bool
99
-    """
100
-    def set_strings(self, ml_string):
101
-        pass
96
+    def __repr__(self):
97
+        if self.name is None:
98
+            return "<%s #%s, 'non populated'>" % (type(self).__name__, self.id)
99
+        else:
100
+            return "<%s #%s, '%s'>" % (type(self).__name__, self.id, self.name)
102 101
 
103
-    """ get the string representation of the component for the given language
104
-        @param  lang   str: iso 639-2 code of the language
105
-        @return text   str: 
106
-    """
107
-    def get_string(self, lang):
108
-        pass
109 102
 
110 103
 class EmComponentNotExistError(Exception):
111 104
     pass

+ 45
- 29
EditorialModel/fieldgroups.py View File

@@ -1,51 +1,67 @@
1 1
 #-*- coding: utf-8 -*-
2 2
 
3
-from EditorialModel.components import EmComponent
3
+from EditorialModel.components import EmComponent, EmComponentNotExistError
4
+from Database.sqlobject import SqlObject
5
+
6
+import EditorialModel
4 7
 
5 8
 class EmFieldGroup(EmComponent):
6 9
     """ Represents groups of EmField
7
-            
10
+
8 11
         EmClass fields representation is organised with EmFieldGroup
9 12
         @see EmField
10 13
     """
11
-    
12 14
 
13
-    def __init__(id_or_name):
14
-        """ Instanciate an EmFieldGroupe with data fetched from db
15
+    table = 'em_fieldgroup'
16
+
17
+    def __init__(self, id_or_name):
18
+        """ Instanciate an EmFieldGroup with data fetched from db
15 19
             @param id_or_name str|int: Identify the EmFieldGroup by name or by global_id
16 20
             @throw TypeError
17 21
             @see component::EmComponent::__init__()
18 22
         """
19
-        super(EmFieldGroup, self).__init__()
20
-        pass
23
+        self.table = EmFieldGroup.table
24
+        super(EmFieldGroup, self).__init__(id_or_name)
21 25
 
22 26
     @staticmethod
23
-    def create(name, em_class, ml_repr = None, ml_help = None, icon = None):
24
-        """ Create a new EmType and instanciate it
25
-            
26
-            @todo Change the icon param type
27
-            @todo em_class == None => Error ?
28
-            @todo change staticmethod to classmethod ?
29
-            
30
-            @param name str: The name of the new Type
27
+    def create(name, em_class):
28
+        """ Create a new EmFieldGroup, save it and instanciate it
29
+
30
+            @param name str: The name of the new fielgroup
31 31
             @param em_class EmClass: The new EmFieldGroup will belong to this class
32
-            @param ml_repr MlString|None: Multilingual representation of the type
33
-            @param ml_help MlString|None: Multilingual help for the type
34
-            @param The string|None: filename of the icon
35
-            @return An EmFieldGroup instance
36
-            @see EmComponent::__init__()
37 32
         """
38
-        pass
33
+        try:
34
+            exists = EmFieldGroup(name)
35
+        except EmComponentNotExistError:
36
+            uids = SqlObject('uids')
37
+            res = uids.wexec(uids.table.insert().values(table=EmFieldGroup.table))
38
+            uid = res.inserted_primary_key
39
+
40
+            emfieldgroup = SqlObject(EmFieldGroup.table)
41
+            res = emfieldgroup.wexec(emfieldgroup.table.insert().values(uid=uid, name=name, class_id=em_class.id))
42
+            return EmFieldGroup(name)
39 43
 
40
-    def fields():
44
+        return exists
45
+
46
+    """ Use dictionary (from database) to populate the object
47
+    """
48
+    def populate(self):
49
+        row = super(EmFieldGroup, self).populate()
50
+        self.em_class = EditorialModel.classes.EmClass(int(row.class_id))
51
+
52
+    def save(self):
53
+        # should not be here, but cannot see how to do this
54
+        if self.name is None:
55
+            self.populate()
56
+
57
+        values = {
58
+            'class_id' : self.em_class.id,
59
+        }
60
+
61
+        return super(EmFieldGroup, self).save(values)
62
+
63
+    def fields(self):
41 64
         """ Get the list of associated fields
42 65
             @return A list of EmField
43 66
         """
44 67
         pass
45
-
46
-    def field():
47
-        """ ???
48
-            @todo : find what this function is for
49
-        """
50
-        pass
51
-

+ 65
- 54
EditorialModel/fields.py View File

@@ -1,78 +1,89 @@
1 1
 #-*- coding: utf-8 -*-
2 2
 
3
-from EditorialModel.components import EmComponent
3
+from EditorialModel.components import EmComponent, EmComponentNotExistError
4
+from Database.sqlobject import SqlObject
5
+
6
+import EditorialModel
4 7
 
5 8
 """Represent one data for a lodel2 document"""
6 9
 class EmField(EmComponent):
7 10
 
8
-    
9
-    def __init__(id_or_name):
10
-        """ Instanciate an EmType with data fetched from db
11
+    table = 'em_field'
12
+
13
+    def __init__(self, id_or_name):
14
+        """ Instanciate an EmField with data fetched from db
11 15
             @param id_or_name str|int: Identify the EmType by name or by global_id
12 16
             @throw TypeError
13 17
             @see EmComponent::__init__()
14 18
         """
15
-        super(EmField, self).__init__()
16
-        pass
19
+        self.table = EmField.table
20
+        super(EmField, self).__init__(id_or_name)
17 21
 
18 22
     @staticmethod
19
-    def create( name, em_fieldgroup, ml_repr = None, ml_help = None,
20
-                icon = None, optionnal = False, type_relation = None,
21
-                relationnal_field = None, primary_data = False,
22
-                default_value = None, params = None, value = None):
23
-        """ Create a new EmType and instanciate it
24
-            
25
-            @todo Change the icon param type
26
-            @todo simplify function aguments ?
27
-            @todo typeof default_value argument ?
28
-            @todo typeof params argument ?
29
-            @todo typeof value argument ?
30
-            
23
+    def create(name, em_fieldgroup, em_fieldtype, optional=True, internal=False):
24
+        """ Create a new EmField and instanciate it
31 25
             @static
32
-            
26
+
33 27
             @param name str: The name of the new Type
34 28
             @param em_fieldgroup EmFieldGroup: The new field will belong to this fieldgroup
35 29
             @param ml_repr MlString|None: Multilingual representation of the type
36 30
             @param ml_help MlString|None: Multilingual help for the type
37 31
             @param The string|None: filename of the icon
38
-            
39
-            @param optionnal bool: Is the field optionnal ?
40
-            @param type_relation EmType|None: If not None make a link between the class of the new EmField and this EmType
41
-            @param relationnal_field EmField|None: If not None indicates that the new field defines the relation created by this EmField argument
42
-            @param primary_data bool: Is the new field a primary data field ?
43
-            @param default_value str: The field's default value
44
-            @param params str: Params of the field
45
-            @param value str: Value of the field
46
-            
32
+
33
+            @param optional bool: Is the field optional ?
34
+            @param internal bool: Is the field internal?
35
+
47 36
             @throw TypeError
48 37
             @see EmComponent::__init__()
49 38
             @staticmethod
50 39
         """
51
-        pass
40
+        try:
41
+            exists = EmField(name)
42
+        except EmComponentNotExistError:
43
+            uids = SqlObject('uids')
44
+            res = uids.wexec(uids.table.insert().values(table=EmField.table))
45
+            uid = res.inserted_primary_key
52 46
 
53
-    def set_default(default_value):
54
-        """ Set the default value
55
-            @todo argument type ?
56
-            @todo return type ?
57
-            @param default_value anytype: The default value
58
-        """
59
-        pass
47
+            values = {
48
+                'uid' : uid,
49
+                'name' : name,
50
+                'fieldgroup_id' : em_fieldgroup.id,
51
+                'fieldtype_id' : em_fieldtype.id,
52
+                'optional' : 1 if optional else 0,
53
+                'internal' : 1 if internal else 0,
54
+            }
60 55
 
61
-    def set_param(params):
62
-        """ Set the field parameters
63
-            @todo argument type ? EmFieldParam ?
64
-            @todo return type ?
65
-            @param params anytype: The field parameters
66
-        """
67
-        pass
68
-
69
-    def set_value(v):
70
-        """ Set the field value
71
-            
72
-            @todo Better explanations
73
-            
74
-            Don't set the field value in a document, it's a special kind of value
75
-            
76
-            @param The v: value
77
-        """
78
-        pass
56
+            emfield_req = SqlObject(EmField.table)
57
+            res = emfield_req.wexec(emfield_req.table.insert(values=values))
58
+            return EmField(name)
59
+
60
+        return exists
61
+
62
+    """ Use dictionary (from database) to populate the object
63
+    """
64
+    def populate(self):
65
+        row = super(EmField, self).populate()
66
+        self.em_fieldgroup = EditorialModel.fieldgroups.EmFieldGroup(int(row.fieldgroup_id))
67
+        self.em_fieldtype = EditorialModel.fieldtypes.EmFieldType(int(row.fieldtype_id))
68
+        self.optional = True if row.optional == 1 else False;
69
+        self.internal = True if row.internal == 1 else False;
70
+        self.icon = row.icon
71
+        self.rel_to_type_id = EditorialModel.fieldtypes.EmFieldType(int(row.rel_to_type_id)) if row.rel_to_type_id else ''
72
+        self.rel_field_id = EmField(int(row.rel_field_id)) if row.rel_field_id else ''
73
+
74
+    def save(self):
75
+        # should not be here, but cannot see how to do this
76
+        if self.name is None:
77
+            self.populate()
78
+
79
+        values = {
80
+            'fieldgroup_id' : self.em_fieldgroup.id,
81
+            'fieldtype_id' : self.em_fieldtype.id,
82
+            'optional' : 1 if self.optional else 0,
83
+            'internal' : 1 if self.internal else 0,
84
+            'icon' : self.icon,
85
+            'rel_to_type_id' : self.rel_to_type_id,
86
+            'rel_field_id' : self.rel_field_id
87
+        }
88
+
89
+        return super(EmField, self).save(values)

+ 8
- 0
EditorialModel/fieldtypes.py View File

@@ -0,0 +1,8 @@
1
+#-*- coding: utf-8 -*-
2
+
3
+from EditorialModel.components import EmComponent, EmComponentNotExistError
4
+
5
+class EmFieldType():
6
+
7
+    def __init__(self, id):
8
+        self.id = id

+ 57
- 27
EditorialModel/types.py View File

@@ -1,6 +1,9 @@
1 1
 #-*- coding: utf-8 -*-
2 2
 
3
-from EditorialModel.components import EmComponent
3
+from EditorialModel.components import EmComponent, EmComponentNotExistError
4
+from Database.sqlobject import SqlObject
5
+
6
+import EditorialModel.classes
4 7
 
5 8
 class EmType(EmComponent):
6 9
     """ Represents type of documents
@@ -10,48 +13,77 @@ class EmType(EmComponent):
10 13
         EmType with special fields called relation_to_type fields
11 14
         @see EmComponent
12 15
     """
16
+    table = 'em_type'
13 17
 
14
-    def __init__(id_or_name):
18
+    def __init__(self, id_or_name):
15 19
         """  Instanciate an EmType with data fetched from db
16 20
             @param id_or_name str|int: Identify the EmType by name or by global_id
17 21
             @throw TypeError
18 22
             @see EmComponent::__init__()
19 23
         """
20
-        super(EmType, self).__init__()
21
-        pass
24
+        self.table = EmType.table
25
+        super(EmType, self).__init__(id_or_name)
22 26
 
23 27
     @staticmethod
24
-    def create(name, em_class, ml_repr = None, ml_help = None, icon = None, sort_field = None):
28
+    def create(name, em_class):
25 29
         """ Create a new EmType and instanciate it
26 30
 
27 31
             @param name str: The name of the new type
28 32
             @param em_class EmClass: The class that the new type will specialize
29
-            @param ml_repr MlString: Multilingual representation of the type
30
-            @param ml_help MlString: Multilingual help for the type
31
-            @param icon str|None: The filename of the icon
32
-            @param sort_field EmField|None: The field used to sort by default
33 33
 
34 34
             @see EmComponent::__init__()
35 35
 
36 36
             @todo Change the icon param type
37 37
             @todo change staticmethod to classmethod
38 38
         """
39
-        pass
39
+        try:
40
+            exists = EmType(name)
41
+        except EmComponentNotExistError:
42
+            uids = SqlObject('uids')
43
+            res = uids.wexec(uids.table.insert().values(table=EmType.table))
44
+            uid = res.inserted_primary_key
40 45
 
41
-    def field_groups():
46
+            emtype = SqlObject(EmType.table)
47
+            res = emtype.wexec(emtype.table.insert().values(uid=uid, name=name, class_id=em_class.id))
48
+            return EmType(name)
49
+
50
+        return exists
51
+
52
+    """ Use dictionary (from database) to populate the object
53
+    """
54
+    def populate(self):
55
+        row = super(EmType, self).populate()
56
+        self.em_class = EditorialModel.classes.EmClass(int(row.class_id))
57
+        self.icon = row.icon
58
+        self.sortcolumn = row.sortcolumn
59
+
60
+    def save(self):
61
+        # should not be here, but cannot see how to do this
62
+        if self.name is None:
63
+            self.populate()
64
+
65
+        values = {
66
+            'class_id' : self.em_class.id,
67
+            'icon' : self.icon,
68
+            'sortcolumn' : self.sortcolumn,
69
+        }
70
+
71
+        return super(EmType, self).save(values)
72
+
73
+    def field_groups(self):
42 74
         """ Get the list of associated fieldgroups
43 75
             @return A list of EmFieldGroup
44 76
         """
45 77
         pass
46 78
 
47 79
 
48
-    def fields():
80
+    def fields(self):
49 81
         """ Get the list of associated fields
50 82
             @return A list of EmField
51 83
         """
52 84
         pass
53 85
 
54
-    def select_field(field):
86
+    def select_field(self, field):
55 87
         """ Indicate that an optionnal field is used
56 88
 
57 89
             @param field EmField: The optional field to select
@@ -60,7 +92,7 @@ class EmType(EmComponent):
60 92
         """
61 93
         pass
62 94
 
63
-    def unselect_field(field):
95
+    def unselect_field(self, field):
64 96
         """ Indicate that an optionnal field will not be used
65 97
             @param field EmField: The optional field to unselect
66 98
             @throw ValueError, TypeError
@@ -68,18 +100,17 @@ class EmType(EmComponent):
68 100
         """
69 101
         pass
70 102
 
71
-        
72
-    def hooks():
103
+
104
+    def hooks(self):
73 105
         """Get the list of associated hooks"""
74 106
         pass
75 107
 
76
-    def add_hook(hook):
108
+    def add_hook(self, hook):
77 109
         """ Add a new hook
78 110
             @param hook EmHook: A EmHook instance
79 111
             @throw TypeError
80 112
         """
81 113
         pass
82
-    
83 114
 
84 115
     def del_hook(hook):
85 116
         """ Delete a hook
@@ -90,16 +121,16 @@ class EmType(EmComponent):
90 121
         pass
91 122
 
92 123
 
93
-    def superiors():
124
+    def superiors(self):
94 125
         """ Get the list of superiors EmType in the type hierarchy
95 126
             @return A list of EmType
96 127
         """
97 128
         pass
98 129
 
99 130
 
100
-    def add_superior(em_type, relation_nature):
131
+    def add_superior(self, em_type, relation_nature):
101 132
         """ Add a superior in the type hierarchy
102
-            
133
+
103 134
             @param em_type EmType: An EmType instance
104 135
             @param relation_nature str: The name of the relation's nature
105 136
             @throw TypeError
@@ -107,22 +138,21 @@ class EmType(EmComponent):
107 138
         """
108 139
         pass
109 140
 
110
-    def del_superior(em_type):
141
+    def del_superior(self, em_type):
111 142
         """ Delete a superior in the type hierarchy
112
-            
143
+
113 144
             @param em_type EmType: An EmType instance
114 145
             @throw TypeError
115 146
             @todo define return value and raise condition
116 147
         """
117 148
         pass
118 149
 
119
-    def linked_types():
150
+    def linked_types(self):
120 151
         """ Get the list of linked type
121
-            
152
+
122 153
             Types are linked with special fields called relation_to_type fields
123
-            
154
+
124 155
             @return a list of EmType
125 156
             @see EmFields
126 157
         """
127 158
         pass
128
-

+ 31
- 2
Lodel/utils/mlstring.py View File

@@ -1,7 +1,36 @@
1 1
 # -*- coding: utf-8 -*-
2 2
 
3
+import json
4
+
3 5
 class MlString(object):
4 6
     """ Handle string with translations """
5 7
 
6
-    def __init__(self, default_str, translations = dict()):
7
-        pass
8
+    def __init__(self, translations = dict()):
9
+        self.translations = translations
10
+
11
+    def get(self, lang):
12
+        if not lang in self.translations:
13
+            return ''
14
+
15
+        return self.translations[lang]
16
+
17
+    def set(self, lang, text):
18
+        if not text:
19
+            if lang in self.translations:
20
+                del(self.translations[lang])
21
+        else:
22
+            self.translations[lang] = text
23
+
24
+    def __str__(self):
25
+        if self.translations:
26
+            return json.dumps(self.translations)
27
+        else:
28
+            return ""
29
+
30
+    @staticmethod
31
+    def load(json_string):
32
+        if isinstance(json_string, str) and json_string != '':
33
+            translations = json.loads(json_string)
34
+        else:
35
+            translations = dict()
36
+        return MlString(translations)

Loading…
Cancel
Save