brahmlower / cssef

Cyber Security Scoring Engine Framework
7 stars 3 forks source link

Automatically create @properties for values in ModelWrapper.fields #50

Closed brahmlower closed 8 years ago

brahmlower commented 8 years ago

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']
brahmlower commented 8 years ago

this was resolved by commit 53735b687e929cbd24c20a37e714c78bdc2b8d77 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