diff roundup/rest.py @ 5661:b08a308c273b

Better display for Link/Multilink and content Link/Multilink are now displayed as a dictionary by default. The format is controlled by the setting of the @verbose option. With @verbose=0 we get the old behavior displaying only the id. With the default @verbose=1 we get a dictionary with the id and a link inside. With @verbose=2 or larger we get the label property in the dictionary in addition (e.g. the name of a status or the name of a file). The content property is also handled differently now. For @verbose < 2 we get a dictionary with a link property in it. The property points to the standard download link for the content (or message). For @verbose >= 2 we get the previous behavior, the content property as a possibly very large json string.
author Ralf Schlatterbeck <rsc@runtux.com>
date Fri, 22 Mar 2019 14:03:37 +0100
parents d8d2b7724292
children 5ceed592706e
line wrap: on
line diff
--- a/roundup/rest.py	Fri Mar 22 11:23:02 2019 +0100
+++ b/roundup/rest.py	Fri Mar 22 14:03:37 2019 +0100
@@ -601,6 +601,7 @@
         etag = calculate_etag(node, class_name, item_id)
         props = None
         protected=False
+        verbose=1
         for form_field in input.value:
             key = form_field.name
             value = form_field.value
@@ -610,18 +611,54 @@
                 # allow client to request read only
                 # properties like creator, activity etc.
                 protected = value.lower() == "true"
+            if key == "@verbose":
+                verbose = int (value)
 
+        result = {}
+        uid = self.db.getuid()
         if props is None:
-            props = list(sorted(class_obj.getprops(protected=protected)))
+            props = class_obj.getprops(protected=protected)
 
         try:
-            result = [
-                (prop_name, node.__getattr__(prop_name))
-                for prop_name in props
-                if self.db.security.hasPermission(
-                    'View', self.db.getuid(), class_name, prop_name,
-                        item_id )
-            ]
+            for pn in sorted(props):
+                prop = props[pn]
+                if not self.db.security.hasPermission(
+                    'View', uid, class_name, pn, item_id
+                ):
+                    continue
+                v = getattr(node, pn)
+                if isinstance (prop, (hyperdb.Link, hyperdb.Multilink)):
+                    linkcls = self.db.getclass (prop.classname)
+                    cp = '%s/%s/' % (self.data_path, prop.classname)
+                    if verbose and v:
+                        if isinstance(v, type([])):
+                            r = []
+                            for id in v:
+                                d = dict(id = id, link = cp + id)
+                                if verbose > 1:
+                                    label = linkcls.labelprop()
+                                    d [label] = linkcls.get(id, label)
+                                r.append(d)
+                            result[pn] = r
+                        else:
+                            result[pn] = dict(id = v, link = cp + v)
+                            if verbose > 1:
+                                label = linkcls.labelprop()
+                                result[pn][label] = linkcls.get(v, label)
+                    else:
+                        result[pn] = v
+                elif isinstance (prop, hyperdb.String) and pn == 'content':
+                    # Do not show the (possibly HUGE) content prop
+                    # unless very verbose, we display the standard
+                    # download link instead
+                    if verbose < 2:
+                        u = self.db.config.TRACKER_WEB
+                        p = u + '%s%s/' % (class_name, node.id)
+                        result[pn] = dict(link = p)
+                    else:
+                        result[pn] = v
+                else:
+                    result[pn] = v
         except KeyError as msg:
             raise UsageError("%s field not valid" % msg)
         result = {

Roundup Issue Tracker: http://roundup-tracker.org/