def save(self): course = Course.lookup_id(self.course_id) user = Course.lookup_id(self.user_id) course.users.append(user) user.courses.append(course) Base.save(self)
def return_model(tablename): """ 返回model :return: """ for model in Base.__subclasses__(): if model.__tablename__ == tablename: return model return None
def tables(engine): from app.models import Base try: import os Base().metadata.create_all(engine) except sqlalchemy.exc.OperationalError as e: if 'database "{}" does not exist'.format( TEST_DATABASE_URI.split('/')[-1:][0]) in str(e.orig): db_url = sqlalchemy.engine.url.make_url(TEST_DATABASE_URI) dbname = db_url.database if db_url.drivername == 'postgresql': subprocess.call(['/usr/bin/env', 'createdb', dbname]) Base().metadata.create_all(engine) else: raise yield Base().metadata.drop_all(engine)
def save(self, warned): credentials = User.lookup_id(self.user_id) prerequisites = Course.lookup_id(self.course_id).prerequisites conflict = None if not warned: for prerequisite in prerequisites: matched = [ c for c in credentials if c.id == prerequisite.credential_id ] if matched.length == 0: conflict = prerequisite.id break if conflict: return "Missing prerequisite: " + Credential.lookup_id( conflict).name else: Base.save(self)
def lookup_id(id): return Base.lookup_id(User, id)
def create_task_table() -> None: sql_file = ('%s/create_task_table.sql' % current_app.config.get('MOCKUP_DIRECTORY')) with open(sql_file, 'r') as fp: sql = fp.read() Base.raw(sql)