web/views/basecontrollers.py
branchtls-sprint
changeset 1798 cc86fe8efaaa
parent 1795 abffc24f93c5
child 1827 93840d187f26
child 1838 d4cbcc15c01c
equal deleted inserted replaced
1797:c2a80130b06d 1798:cc86fe8efaaa
   380             if not err.entity or isinstance(err.entity, (long, int)):
   380             if not err.entity or isinstance(err.entity, (long, int)):
   381                 eid = err.entity
   381                 eid = err.entity
   382             else:
   382             else:
   383                 eid = err.entity.eid
   383                 eid = err.entity.eid
   384             return (False, (eid, err.errors))
   384             return (False, (eid, err.errors))
   385         except Redirect, err:
   385         except Redirect, redir:
   386             return (True, err.location)
   386             return (True, redir.location)
   387         except Exception, err:
   387         except Exception, err:
   388             self.req.cnx.rollback()
   388             self.req.cnx.rollback()
   389             self.exception('unexpected error in js_validateform')
   389             self.exception('unexpected error in js_validateform')
   390             return (False, self.req._(str(err).decode('utf-8')))
   390             return (False, self.req._(str(err).decode('utf-8')))
   391         return (False, '???')
   391         return (False, '???')
   392 
   392 
   393     @jsonize
   393     @jsonize
   394     def js_edit_field(self, action, names, values, rtype, eid):
   394     def js_edit_field(self, action, names, values, rtype, eid, default):
   395         success, args = self.validate_form(action, names, values)
   395         success, args = self.validate_form(action, names, values)
   396         if success:
   396         if success:
   397             # Any X,N where we don't seem to use N is an optimisation
   397             # Any X,N where we don't seem to use N is an optimisation
   398             # printable_value won't need to query N again
   398             # printable_value won't need to query N again
   399             rset = self.req.execute('Any X,N WHERE X eid %%(x)s, X %s N' % rtype,
   399             rset = self.req.execute('Any X,N WHERE X eid %%(x)s, X %s N' % rtype,
   400                                     {'x': eid}, 'x')
   400                                     {'x': eid}, 'x')
   401             entity = rset.get_entity(0, 0)
   401             entity = rset.get_entity(0, 0)
   402             value = entity.printable_value(rtype)
   402             value = entity.printable_value(rtype)
   403             return (success, args, value or self.req._('not specified'))
   403             return (success, args, value or default)
   404         else:
   404         else:
   405             return (success, args, None)
   405             return (success, args, None)
   406 
   406 
   407     @jsonize
   407     @jsonize
   408     def js_edit_relation(self, action, names, values,
   408     def js_edit_relation(self, action, names, values,
   409                          rtype, eid, vid, role='subject'):
   409                          rtype, role, eid, vid, default):
   410         success, args = self.validate_form(action, names, values)
   410         success, args = self.validate_form(action, names, values)
   411         if success:
   411         if success:
   412             entity = self.req.eid_rset(eid).get_entity(0, 0)
   412             entity = self.req.eid_rset(eid).get_entity(0, 0)
   413             rset = entity.related(rtype, role)
   413             rset = entity.related(rtype, role)
   414             return (success, args, self.view(vid, rset))
   414             if rset:
       
   415                 output = self.view(vid, rset)
       
   416             else:
       
   417                 output = default
       
   418             return (success, args, output)
   415         else:
   419         else:
   416             return (success, args, None)
   420             return (success, args, None)
   417 
   421 
   418     @jsonize
   422     @jsonize
   419     def js_i18n(self, msgids):
   423     def js_i18n(self, msgids):