brahmlower/cssef

Automatically create @properties for values in ModelWrapper.fields

Closed this issue · 1 comments

Subclasses of ModelWrapper need to define properties for the values that should be writable through the api and tasks that extend that api. This is tedious and bloats code when there are many properties that require no additional filters. Properties listed in the ModelWrapper.fields list should automatically have basic setters/getters made for them, which can be overwritten by the subclass.

An example subclass of ModelWrapper that requires no special treatment on a value that exists in the model might look like the following:

class ExampleModel(Base):
    name = Column(String(10))

class Example(ModelWrapper):
    model_object = ExampleModel
    fields = ['name']

    @property
    def name(self):
        return self.model.name

    @name.setter
    def name(self, value)
        self.model.name = value
        self.db_conn.commit()

The author of the Example class shouldn't have to define the property for name, so the class should function the same, but look like the following:

class ExampleModel(Base):
    name = Column(String(10))

class Example(ModelWrapper):
    model_object = ExampleModel
    fields = ['name']

this was resolved by commit 53735b6
This was a pretty intense bit of code. I've got an example gist with more information about how the whole thing worked here: https://gist.github.com/bplower/bfad5148507434e5525ed181d330c655