# -*- coding: utf-8 -*- '''Database module, including the SQLAlchemy database object and DB-related mixins. ''' from .extensions import db class CRUDMixin(object): """Mixin that adds convenience methods for CRUD (create, read, update, delete) 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 def create(cls, **kwargs): '''Create a new record and save it the database.''' instance = cls(**kwargs) return instance.save() def update(self, commit=True, **kwargs): '''Update specific fields of a record.''' for attr, value in kwargs.iteritems(): setattr(self, attr, value) return commit and self.save() or self def save(self, commit=True): '''Save the record.''' db.session.add(self) if commit: db.session.commit() return self def delete(self, commit=True): '''Remove the record from the database.''' db.session.delete(self) return commit and db.session.commit()