def client(): test_db, app.config['DATABASE'] = tempfile.mkstemp() app.config['TESTING'] = True client = app.test_client() with app.app_context(): db.init_app(app) db.create_all() yield client with app.app_context(): db.drop_all() os.close(test_db) os.unlink(app.config['DATABASE'])
def upgrade(): ### commands auto generated by Alembic - please adjust! ### from application.app import db, app from restore import restore with app.app_context(): db.create_all() restore()
def testDowloadServices(self): start = datetime(2013, 01, 01) end = datetime(2013, 02, 01) worker = DownloadWorker() with app.app_context(): file_url = worker.do_download(self.template_id, start, end, self.lpu_infis_code) self.assertIsNotNone(file_url)
def setUp(self): with app.app_context(): self.client = TFOMSClient(_config('core_service_url')) self.lpu_infis_code = _config('lpu_infis_code') self.app = app self.template_id = ( db.session.query(Template).join(TemplateType).filter( TemplateType.code == 'services').first())
def GetUsers(self, request, context): with app.app_context(): if request and request.name: for name in request.name: for user in UserController().getUsersByName(name = name): yield user_messages.UserResponse( \ id = user.id, \ name = user.name, \ birth = self.convertIntoTimestamp(user.birth), \ gender = user.gender.upper()) else: for user in UserController().getUsers(): yield user_messages.UserResponse( \ id = user.id, \ name = user.name, \ birth = self.convertIntoTimestamp(user.birth), \ gender = user.gender.upper())
def send_mail(recipients, file_path, chunk_number, total_chunks): msg = Message( f'AdsScreenshot {datetime.now()} yandex ({chunk_number}/{total_chunks})', sender=DefaultConfig.MAIL_DEFAULT_SENDER, recipients=recipients, body= "Скриншоты по вашим запросам во вложении. Это автоматическая рассылка, не отвечайте на письмо. При возникновении вопросов обращайтесь к вашему менеджеру." ) with open(file_path, 'rb') as fp: msg.attach(file_path, "application/zip", fp.read()) try: from application.app import app with app.app_context(): mail = Mail(app) mail.send(msg) except Exception as e: print(e)
def GetUser(self, request, context): if request.id: with app.app_context(): user = UserController().getUserByID(id = request.id) logging.debug(user) if user: return user_messages.UserResponse( \ id = user.id, \ name = user.name, \ birth = self.convertIntoTimestamp(user.birth), \ gender = user.gender.upper()) msg = 'User ID not found' context.set_details(msg) context.set_code(grpc.StatusCode.NOT_FOUND) return user_messages.UserResponse() msg = 'Must pass ID' context.set_details(msg) context.set_code(grpc.StatusCode.INVALID_ARGUMENT) return user_messages.UserResponse()
backups_dir = os.path.abspath( os.path.join(os.path.dirname(__file__), 'alembic', 'backups')) sys.path.insert(0, os.path.abspath(os.path.dirname(__file__))) from application.app import app, db def backup(model): serialized_data = dumps(db.session.query(model).all()) f = open(os.path.join(backups_dir, model.name), 'wb') f.write(serialized_data) f.close() parser = argparse.ArgumentParser(description='Backup table data') parser.add_argument('-t', dest='table', help='Name of DB table to backup (например, tfoms_tag)') args = parser.parse_args() table_name = args.table with app.app_context(): if table_name and table_name in db.metadata.tables.keys(): try: backup(db.metadata.tables[table_name]) except exceptions.Exception, e: print e else: print u'Не найдена таблица с указанным именем "%s"' % table_name
db.session.commit() except Exception, e: print e db.session.rollback() def _migrate_actual_orgs(): data = OldExcel.query.filter(OldExcel.fmarkfordel == 0).order_by(OldExcel.fnumorg).all() for old in data: __migrate_organisation(old) __link_posrednik(old) # TMP def _migrate_deleted_orgs(): stmt = db.session.query(db.distinct(OldExcel.fnumorg)).filter(OldExcel.fmarkfordel == 0).subquery() data = OldExcel.query.filter(db.not_(OldExcel.fnumorg.in_(stmt))).order_by(OldExcel.fnumorg).all() for old in data: __migrate_organisation(old) __link_posrednik(old) # TMP def migrate_org_data(): _migrate_actual_orgs() _migrate_deleted_orgs() if __name__ == '__main__': with app.app_context(): migrate_dicts() migrate_org_data()