Package: assignables
AssignablesHelper package for Flask and Flask-APIThird Party Library RequirementsinflectionIntroductionThis package is helper package for assigning values to SqlAlchemy db model objects and to get their dict representation so they can be sent as response from Flask-API endpoints.
This package can be used without using SqlAlchemy, Flask and Flask-API.Installationpip install assignablesUsagefrom assignables import Assignable

class SomeModel(db.Model, Assignable):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(80), unique=True, nullable=False)
    email = db.Column(db.String(120), unique=True, nullable=False)

    def __init__(self, username=None, email=None):
        Assignable.__init__()
        self.username = username
        self.email = email

data = {
    "username": 'user123',
    "email": 'user@email.com'
}

obj = SomeModel()
obj.assign(data) # username and email will be assigned.

obj_dict = obj.get_json_dict() # obj_dict won't contain _sa_instance_state and Assignable class atributes.Assignable will give your model two new methods:assign(data_dict)- this method will assign coresponding atributes from respective key value pairs fromdata_dict.get_json_dict()- this method will return objects dictionary.Usingassignmethod by default will not assign objectsid, but will other atributes if they exist indata_dict.
Methodget_json_dictwill not have_sa_instance_statekey inside by default and atriubets Assignable class contains.If you want to specify custom atributes for not assigning or not serializing you can do that:
from assignables import Assignablesclass SomeModel(db.Model, Assignable):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(80), unique=True, nullable=False)
    email = db.Column(db.String(120), unique=True, nullable=False)

    def __init__(self, username=None, email=None):
        unassignables = ['id', 'email']
        unserializables = ['_sa_instance_state', 'email']
        Assignable.__init__(unassignables=unassignables, unserializables=unserializables)
        self.username = username
        self.email = email

data = {
    "username": 'user123',
    "email": 'user@email.com'
}

obj = SomeModel()
obj.assign(data) # username will only be assigned.

obj_dict = obj.get_json_dict() # obj_dict will not contain _sa_instance_state and email atributes.If used like this,assignmethod will not assignidandemailatributes.
Dictinary returned by callingget_json_dictwill not have keys_sa_instance_stateandemail.You can also add you custom data validator - Validation will be executed insideassignmethod.
If validation failedValidationErrorexception is raised.from assignables import DataValidator, Assignables


class CustomValidator(DataValidator):
    def validate(self, obj):
        # return True if validated, false otherwise.


class SomeModel(db.Model, Assignable):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(80), unique=True, nullable=False)
    email = db.Column(db.String(120), unique=True, nullable=False)

    def __init__(self, username=None, email=None):
        Assignable.__init__(validator=CustomValidator())
        self.username = username
        self.email = email

data = {
        "username": 'user123',
        "email": 'user@email.com'
    }

obj = SomeModel()
obj.assign(data) # Data will be validate using you custom data validator.If there is a missmatch between your atribute naming or you wish to specify a naming convetion for
resulting dictionary there is a way.data = {
        "Username": 'user123',
        "Email": 'user@email.com'
    }

obj = SomeModel()
obj.assign(data, under_score_data=True) # This will handle create snake case keys from data dictionary keys.
obj_dict = obj.get_json_dict(naming_convetion='camel_case') # Output dict would have camel case keys.Options fornaming_conventionare:camel_caseupper_camel_casesnake_case