views:

201

answers:

2

I'm creating an app that I want to have an expandable set of properties (each a RatingProperty) I also want to validate that any dynamic properties are of the RatingProperty type.

In the Expando documentation it says:

Tip: If you want to validate a dynamic property value using a Property class, you can instantiate the Property class and call its validate() method on the value.

So if I want to validate a dynamic property I need to know what the class's non-dynamic properties are. How can I ask my class what it's defined properties are?

I've considered creating a class method that takes a string and returns true if that string is in a list of property names that I create and maintain, but it seems like a hack. I've searched the Google for tips, but haven't had any luck.

Thanks, Pat

A: 

After a bit more research (damn you lazyweb!) I've found a solution that I think is acceptable:

A dynamic property can't be of a db subclassed property type. Thus, there are two distinct steps that must be taken. First you need to create an instance of your property class and validate your value:

test = db.RatingProperty()
if test.validate(valueToSave):
    #do your thing

Next you need to check if the property you want to save is a declared property:

if valueToSaveKey not in myObject.properties():
    #if not save it as desired
    myObject.valueToSaveKey = valueToSave

The down side here is that the value you save isn't stored as the property type you want.

pat
The line above that reads: myObject.valueToSaveKey = valueToSaveShould read: myObject.__setattr__(valueToSaveKey, valueToSave)
pat
A: 

I may be misunderstanding your question, but if you have a list of properties you expect to find, why not just use a standard db.Model, instead of an Expando? You can add additional properties to a Model class, as long as you either provide a default or don't make them required.

Nick Johnson
I have a list of properties that I know I want to save and an undetermined amount of properties that I know will be of a certain type. When I try to add a property I want to first check to see if it's a declared property and, if not, validate that it's value is of the type required.
pat
But are the 'undetermined' properties being added at runtime, or simply in later revisions of the app? If the former, you could use an ArrayProperty. If the latter, just add them as new properties.
Nick Johnson
If you place them in an array you can't filter based on their value. I've considered simply adding properties to the model, but I'm not convinced that it would be easier that way.
pat
You can filter based on their value, but only based on the value of one at a time. I'm still not clear on your exact requirements. Do you know at runtime what the (current) set of possible properties is?
Nick Johnson
er, s/run time/design time/
Nick Johnson
Users can set the value of many different properties, though none are required, and there is no sensible default for them. I have a group of suggested properties to edit, though I envision user being able to add properties that they feel are appropriate.
pat