Browse Source

Bugfixes in webui update handling

Yann Weber 7 years ago
parent
commit
4d361423ff
1 changed files with 39 additions and 32 deletions
  1. 39
    32
      plugins/webui/interface/controllers/admin.py

+ 39
- 32
plugins/webui/interface/controllers/admin.py View File

@@ -8,6 +8,9 @@ from lodel import logger
8 8
 from ...client import WebUiClient
9 9
 import leapi_dyncode as dyncode
10 10
 import warnings
11
+from lodel.leapi.datahandlers.base_classes import MultipleRef
12
+
13
+LIST_SEPARATOR = ','
11 14
 
12 15
 ##@brief These functions are called by the rules defined in ../urls.py
13 16
 ## To administrate the instance of the editorial model
@@ -44,40 +47,44 @@ def admin_update(request):
44 47
         except LeApiError:
45 48
             classname = None
46 49
         if classname is None or target_leo.is_abstract():
47
-            raise HttpException(400)
48
-
49
-        uid_field = target_leo.uid_fieldname()[0]
50
-        fields = dict()
50
+            raise HttpException(400, custom = "Bad classname given")
51 51
 
52
-        for in_put, in_value in request.form.items():
53
-            # The classname is handled by the datasource, we are not allowed to modify it
54
-            # uid is not a fieldname
55
-            # both are hidden in the form, to identify the object here
56
-            if in_put != 'classname' and  in_put != 'uid':
57
-                dhl = target_leo.data_handler(in_put[12:])
58
-                # Here, in case of a Reference we transform the str 
59
-                # given by the form in a iterable (a list) 
60
-                if dhl.is_reference() and in_value != '' and not dhl.is_singlereference():
61
-                    in_value.replace(" ","")
62
-                    in_value=in_value.split(',')
63
-                    in_value=list(in_value)
64
-                if in_value == '':
65
-                    fields[in_put[12:]] = None
66
-                else:
67
-                    fields[in_put[12:]] = in_value
68
-                    
69
-        # We retrieve the object to update
70
-        filter_q = '%s = %s' % (uid_field, uid)
71
-        obj = (target_leo.get((filter_q)))[0]
52
+        leo_to_update = target_leo.get_from_uid(uid)
72 53
         
73
-        # and update it
74
-        inserted = obj.update(fields)
75
-        
76
-        if inserted==1:
77
-            msg = 'Successfully updated';
78
-        else:
79
-            msg = 'Oops something wrong happened...object not saved'
80
-        return get_response('admin/admin_edit.html', target=target_leo, uidfield = uid_field, lodel_id = uid, msg = msg)
54
+        errors = dict()
55
+        for fieldname, value in request.form.items():
56
+            #We want to drop 2 input named 'classname' and 'uid'
57
+            if len(fieldname) > 12:
58
+                #Other input names are : field_input_FIELDNAME
59
+                #Extract the fieldname
60
+                fieldname = fieldname[12:]
61
+                try:
62
+                    dh = leo_to_update.data_handler(fieldname)
63
+                except NameError as e:
64
+                    errors[fieldname] = e
65
+                    continue
66
+                #Multiple ref list preparation
67
+                if issubclass(dh.__class__, MultipleRef):
68
+                    value=[spl for spl in [
69
+                           v.strip() for v in value.split(LIST_SEPARATOR)]
70
+                        if len(spl) > 0]
71
+                try:
72
+                    leo_to_update.set_data(fieldname, value)
73
+                except Exception as e:
74
+                    errors[fieldname] = e
75
+                    continue
76
+        if len(errors) > 0:
77
+            custom_msg = '<h1>Errors in datas</h1><ul>'
78
+            for fname, error in errors.items():
79
+                custom_msg += '<li>%s : %s</li>' % (
80
+                    fname, error)
81
+            custom_msg += '</ul>'
82
+            raise HttpException(400, custom = custom_msg)
83
+        try:
84
+            leo_to_update.update()
85
+        except Exception as e:
86
+            raise HttpException(
87
+                500, custom = "Something goes wrong during update : %s" % e)
81 88
 
82 89
     # Display of the form with the object's values to be updated
83 90
     if 'classname' in request.GET:

Loading…
Cancel
Save