Refactor for item display pass1

"John P. Rouillard" <[email protected]>
Newsgroups gmane.comp.bug-tracking.roundup.devel
Message-ID <[email protected]>
In a prior email Ralf said:
>>John said
>> So the code re-arrangement would be:
>[...]
>Yes!
>
>> This would unify the representation of the objects regardless of which
>> way they were accessed (class (+ optional embed), class/id (+ optional
>> embed)) for a given verbose level.
>Yes!
>
>> If the property is requested/shown (either by an explicit @embed, or
>> implicitly by requesting the class/id) it always has the exact same
>> form based on the @verbose level.
>Yes!

I took a first pass at it. It passes all the rest tests using anydbm.
It still uses verbose=1 as the default. So we will have to update the
code to use Ralf's new verbose values.

This change adds a new method to the RestfulInstance class:

   def format_item(self, node, item_id, props=None, verbose=1):

The properties included in the output are defined by props. It is a dict
{ propname: proptype } as though returned by

    class_obj.getprops()

If @verbose is set to 2, the labelprop is also included. item_id is a
string and node is the result of a class_obj.getnode(item_id) since
the caller already has one around anyway. This function is basically
the formatting/dict generation part of the /class_name/item_id method.

It turns out that the /class_name/item_id GET handler already supported
@fields to specify returned fields. It was a comma separated list of
field names/properties/attributes. So I made the code support

    @fields=prop1,prop2

and

    @attrs=prop1,prop2

Note ":" can be used in place of ","s, but they can't be mixed. Get
for both /class_name and /class_name/item_id supports @fields/@attrs.
We should probably choose one key and one format (, or :) and remove
the other one. I think @fields makes more sense (and is shorter than
@attributes).

When accessing /class_name/item_id, the results of format_item are
assigned to the "attributes" key of returned dict. When accessing
/class_name the results of format_item are updated/merged into the
dict that is a member of the list "collection".

So:

  rest/data/user?@verbose=2&@attrs=queries:organisation

now returns:

collection": [
   {
       "organisation": null,
       "username": "admin",
       "id": "1",
       "link": ".../rest/data/user/1",
       "queries": [
           {
               "name": "Show Unassigned",
               "id": "4",
               "link": ".../rest/data/query/4"
           }
       ]
   }, ...

while:

  rest/data/user/1?@verbose=2&@fields=queries,organisation returns:

{   
  "data": {
    "attributes": {
      "organisation": null,
      "queries": [
        {  
          "link": ".../rest/data/query/4",
          "name": "Show Unassigned",
          "id": "4"
        }
      ],
      "username": "admin"
    },
    "@etag": "\"a3d16094da6c6de1c1e9fe56a1ff763d\"",
    "link": ".../rest/data/user/1",
    "type": "user",
    "id": "1"
  }
}

Same queries but with @verbose=0 for /class_name returns:

  "collection": [
      {
          "organisation": null,
          "link": "https://rouilj.dynamic-dns.net/demo/rest/data/user/1",
          "queries": [
              "4"
          ],
          "id": "1"
      }, ...

and for /class_name/item_id:

{   
    "data": {
        "attributes": {
            "organisation": null,
            "queries": [
                "4"
            ]
        },
        "@etag": "\"a3d16094da6c6de1c1e9fe56a1ff763d\"",
        "link": "https://rouilj.dynamic-dns.net/demo/rest/data/user/1",
        "type": "user",
        "id": "1"
    }
}

Also this code doesn't display password fields. Instead it displays:

   "password": "[password hidden scheme PBKDF2]",

if the scheme is PBKDF2 or the account it locked without a password.
 
This can probably be optimized and cleaned up but I think it's ok for
a first pass.

I'll try to get a test or two in for @fields, @attrs and different
@verbose levels this weekend.

Have a great weekend all.

--
				-- rouilj
John Rouillard
===========================================================================
My employers don't acknowledge my existence much less my opinions.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.