def closeSession(req, resp): # if req.session.new or req.session.dirty: req.session.commit() req.session.close() Session.remove() '''
def _unique(cls, hashfunc, queryfunc, constructor, arg, kw): from flib.models.db import Session session = Session() cache = getattr(session, '_unique_cache', None) if cache is None: session._unique_cache = cache = {} key = (cls, hashfunc(*arg, **kw)) if key in cache: return cache[key] else: with session.no_autoflush: q = session.query(cls) q = queryfunc(q, *arg, **kw) obj = q.first() if not obj: obj = constructor(*arg, **kw) session.add(obj) cache[key] = obj return obj
Just remember: Each comment is like an appology! Clean code is much better than Cleaner comments! ''' from sqlalchemy import Column, Integer, String, DateTime, Text, ForeignKey, Table, \ Float, Boolean, event from sqlalchemy.orm import relationship, backref # for relationships from sqlalchemy.orm import validates, deferred from flib.models.mixin import IDMixin, Base, getUUID, UniqueMixin from sqlalchemy_utils import PasswordType, aggregated from sqlalchemy.ext.associationproxy import association_proxy from flib.models.helpers import tag_maker from flib.models.db import Session session = Session() accounts_tags = Table( "accounts_tags", Base.metadata, Column('id', Integer, primary_key=True), Column("account_id", Integer, ForeignKey("account.id"), primary_key=True), Column("tag_id", Integer, ForeignKey("tag.id"), primary_key=True)) class Account(IDMixin, UniqueMixin, Base): '''Rules for permissions and authorizations ''' # over-ride mixin version. because of remote_side id = Column(Integer, primary_key=True) active = Column(Boolean, default=True) description = Column(Text())
user31 = User(email='*****@*****.**', password='******', active=True, firstname='Ali', lastname='Bitaraf') user32 = User(email='*****@*****.**', password='******', active=True, firstname='Majid', lastname='Hagighighatjoo') user33 = User(firstname="Amir", lastname="Mohammad", email="*****@*****.**", password="******", active=True) user34 = User(firstname="Sadegh", lastname="Hosseini", email="*****@*****.**", password="******", active=True) user35 = User(firstname="Saeed", lastname="Narimaani", email="*****@*****.**", password="******", active=True) user36 = User(firstname="Mehrdad", lastname="Taladar", email="*****@*****.**", password="******", active=True) from flib.models.db import Session session = Session() session.add_all([user1, user2, user3, user4, user5, user6, user7, user8, user9, user10, user11, user12, user13, user14, user15, user16, user17, user18, user19, user20, user21, user22, user23, user24, user25, user26, user27, user28, user29, user30, user31, user32, user33, user34, user35, user36]) user1.groups.append('admin') session.commit() #import shutil # print animate.get_tree(session, json=True) # proj.plan
def getSession(req, resp, params): from flib.utils import helpers # Session.remove() req.session = Session() # imported from models.db
class ORMClass(object): """The base of the Base class """ query = Session.query_property()