WSME Custom Type with multiple sub types for openstack project

75 views
Skip to first unread message

Lakshmi Sampath

unread,
Apr 9, 2014, 8:21:00 PM4/9/14
to pytho...@googlegroups.com
Hi,


I am trying to model a json object for a openstack project and having trouble representing multiple sub types within a custom type.

>>>>>>>>>>>>>>>>>>>>>   Here's the json object structure i am trying to model.  Notice that properties is a dictionary of multiple sub types(prop1, prop2, prop3)
{
    "name": "openstack",
    "description": "openstack project",
    "properties": {
        "prop1": {
            "type": "type1",
            "description": "description of property1",
            "address": "some address"
        },
        "prop2": {
            "type": "type2",
            "description": "description of property2",
            "enabled": "False",
            "created_date": "2014-04019"
        },
        "prop3": {
            "type": "type3",
            "description": "description of property 3",
            "items": [
                "item1",
                "item2"
            ]
        }
    }
}

>>>>>>>>>>>>>>>>>>>>>>    Here's the class representation. 
properties  = wsme.wsattr([Properties]) is not right. How do I represent a dictionary of multiple sub types ? Let me know if there is a completely different way to represent the above model.

class CustomType(types.Base):
    name = wsme.wsattr(types.text, mandatory=True)
    description = wsme.wsattr(types.text, mandatory=False)
    properties = wsme.wsattr([Properties], mandatory=False)
class Properties(types.Base):
type = wsme.wsattr(types.text, mandatory=True)

class Type1(Properties):
    description = wsme.wsattr(types.text, mandatory=False)
address = wsme.wsattr(types.text, mandatory=True)
    
class Type2(Properties):
    description = wsme.wsattr(types.text, mandatory=False)
enabled = bool

class Type3(Properties):
    description = wsme.wsattr(types.text, mandatory=False)
items = wsme.wsattr([Item], mandatory=False)

...
...
...
class MyController(RestController):
    @wsexpose(CustomType, six.text_type)
    def get_one(self, name):

   @wsexpose(CustomType, body=CustomType) 
   def post(self, custom_type):


Thanks
lakshmi.
    

Christophe de Vienne

unread,
Apr 10, 2014, 3:52:12 AM4/10/14
to pytho...@googlegroups.com
Hi Lakshmi,

WSME does not support inheritance nor containers with a variable type.

That said, if your different property types have little variations, you can keep a single type Property with all the possible attributes :

class Property(types.Base):
type = wsme.wsattr(types.text, mandatory=True)

    description = wsme.wsattr(types.text, mandatory=False)
address = wsme.wsattr(types.text, mandatory=False)
    
enabled = bool

    items = wsme.wsattr([Item], mandatory=False)

The fields that are not set by the client will be seen as 'Unset' on the server side.

Another way, if you have a lot of possible property types, is to do the following :

class Type1(types.Base):
    description = wsme.wsattr(types.text, mandatory=False)
    address = wsme.wsattr(types.text, mandatory=True)
    
class Type2(types.Base):
    description = wsme.wsattr(types.text, mandatory=False)
    enabled = bool

class Type3(types.Base):
    description = wsme.wsattr(types.text, mandatory=False)
    items = wsme.wsattr([Item], mandatory=False)

class Property(types.Base):
    as_type1 = Type1
    as_type2 = Type2
    as_type3 = Type3

This later solution allow to add extra types from a plugin more easily, but make you change the json format you describe, where your 'properties' would look like :

{"properties": {
    "prop1": {"as_type1": {"address": ""}},
    "prop2": {"as_type2": {"enabled": ""}},
    "prop3": {"as_type3": {"items": []}}
}}

For declaring your dictionnary of properties, this is the way to go :

class CustomType(types.Base):
    name = wsme.wsattr(types.text, mandatory=True)
    description = wsme.wsattr(types.text, mandatory=False)
    properties = wsme.wsattr({types.text: Property}, mandatory=False)



Cheers,

Christophe


--
You received this message because you are subscribed to the Google Groups "python-wsme" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python-wsme...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Lakshmi Sampath

unread,
Apr 10, 2014, 3:58:38 PM4/10/14
to pytho...@googlegroups.com
Thanks christophe for the detailed explanation,

I will explore the first option with single class having all attributes.  
 

thanks
lakshmi.
Reply all
Reply to author
Forward
0 new messages