|
|
@ -6,6 +6,7 @@ from sqlalchemy.orm import relationship |
|
|
|
|
|
|
|
|
|
|
|
from .extensions import db |
|
|
|
from .extensions import db |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Alias common SQLAlchemy names |
|
|
|
Column = db.Column |
|
|
|
Column = db.Column |
|
|
|
relationship = relationship |
|
|
|
relationship = relationship |
|
|
|
|
|
|
|
|
|
|
@ -13,18 +14,6 @@ class CRUDMixin(object): |
|
|
|
"""Mixin that adds convenience methods for CRUD (create, read, update, delete) |
|
|
|
"""Mixin that adds convenience methods for CRUD (create, read, update, delete) |
|
|
|
operations. |
|
|
|
operations. |
|
|
|
""" |
|
|
|
""" |
|
|
|
__table_args__ = {'extend_existing': True} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
id = db.Column(db.Integer, primary_key=True) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@classmethod |
|
|
|
|
|
|
|
def get_by_id(cls, id): |
|
|
|
|
|
|
|
if any( |
|
|
|
|
|
|
|
(isinstance(id, basestring) and id.isdigit(), |
|
|
|
|
|
|
|
isinstance(id, (int, float))), |
|
|
|
|
|
|
|
): |
|
|
|
|
|
|
|
return cls.query.get(int(id)) |
|
|
|
|
|
|
|
return None |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@classmethod |
|
|
|
@classmethod |
|
|
|
def create(cls, **kwargs): |
|
|
|
def create(cls, **kwargs): |
|
|
@ -50,10 +39,38 @@ class CRUDMixin(object): |
|
|
|
db.session.delete(self) |
|
|
|
db.session.delete(self) |
|
|
|
return commit and db.session.commit() |
|
|
|
return commit and db.session.commit() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Model(CRUDMixin, db.Model): |
|
|
|
|
|
|
|
"""Base model class that includes CRUD convenience methods.""" |
|
|
|
|
|
|
|
__abstract__ = True |
|
|
|
|
|
|
|
|
|
|
|
# From Mike Bayer's "Building the app" talk |
|
|
|
# From Mike Bayer's "Building the app" talk |
|
|
|
# https://speakerdeck.com/zzzeek/building-the-app |
|
|
|
# https://speakerdeck.com/zzzeek/building-the-app |
|
|
|
def ReferenceCol(tablename, nullable=False, **kwargs): |
|
|
|
class SurrogatePK(object): |
|
|
|
"""Column that adds primary key foreign key reference.""" |
|
|
|
"""A mixin that adds a surrogate integer 'primary key' column named |
|
|
|
|
|
|
|
``id`` to any declarative-mapped class. |
|
|
|
|
|
|
|
""" |
|
|
|
|
|
|
|
__table_args__ = {'extend_existing': True} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
id = db.Column(db.Integer, primary_key=True) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@classmethod |
|
|
|
|
|
|
|
def get_by_id(cls, id): |
|
|
|
|
|
|
|
if any( |
|
|
|
|
|
|
|
(isinstance(id, basestring) and id.isdigit(), |
|
|
|
|
|
|
|
isinstance(id, (int, float))), |
|
|
|
|
|
|
|
): |
|
|
|
|
|
|
|
return cls.query.get(int(id)) |
|
|
|
|
|
|
|
return None |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def ReferenceCol(tablename, nullable=False, pk_name='id', **kwargs): |
|
|
|
|
|
|
|
"""Column that adds primary key foreign key reference. |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Usage: :: |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
category_id = ReferenceCol('category') |
|
|
|
|
|
|
|
category = relationship('Category', backref='categories') |
|
|
|
|
|
|
|
""" |
|
|
|
return db.Column( |
|
|
|
return db.Column( |
|
|
|
db.ForeignKey("{0}.id".format(tablename)), |
|
|
|
db.ForeignKey("{0}.{1}".format(tablename, pk_name)), |
|
|
|
nullable=nullable, **kwargs) |
|
|
|
nullable=nullable, **kwargs) |
|
|
|