aurweb.db: add query, create, delete helpers

Takes sqlalchemy kwargs or stanzas:

query(Model, Model.Column == value)
query(Model, and_(Model.Column == value, Model.Column != "BAD!"))

Updated tests to reflect the new utility and a comment about upcoming
function deprecation is added to get_account_type().

From here on, phase out the use of get_account_type().

+ aurweb.db: Added create utility function
+ aurweb.db: Added delete utility function

The `delete` function can be used to delete a record by search
kwargs directly.

Example:
    delete(User, User.ID == 6)

All three functions added in this commit are typically useful to
perform these operations without having to import aurweb.db.session.
Removes a bit of redundancy overall.

Signed-off-by: Kevin Morris <kevr@0cost.org>
This commit is contained in:
Kevin Morris 2021-01-14 21:06:41 -08:00
parent 5185df629e
commit a836892cde
5 changed files with 84 additions and 9 deletions

View file

@ -11,6 +11,24 @@ Session = None
session = None
def query(model, *args, **kwargs):
return session.query(model).filter(*args, **kwargs)
def create(model, *args, **kwargs):
instance = model(*args, **kwargs)
session.add(instance)
session.commit()
return instance
def delete(model, *args, **kwargs):
instance = session.query(model).filter(*args, **kwargs)
for record in instance:
session.delete(record)
session.commit()
def get_sqlalchemy_url():
"""
Build an SQLAlchemy for use with create_engine based on the aurweb configuration.