def app(): # Define your database metadata # ----------------------------- db = Gino() # Define tables as you would normally do # -------------------------------------- class User(db.Model): __tablename__ = 'users' id = db.Column(db.Integer(), primary_key=True, autoincrement=True) nickname = db.Column(db.Unicode(), nullable=False) # Now just use your tables # ------------------------ class AllUsers(GinoRequestHandler): async def get(self): users = await User.query.gino.all() for user in users: self.write('<a href="{url}">{nickname}</a><br/>'.format( url=self.application.reverse_url('user', user.id), nickname=tornado.escape.xhtml_escape(user.nickname))) class GetUser(GinoRequestHandler): async def get(self, uid): user = await User.get_or_404(int(uid)) self.write('Hi, {}!'.format(user.nickname)) class AddUser(GinoRequestHandler): async def post(self): user = await User.create(nickname=self.get_argument('name')) self.write('Hi, {}!'.format(user.nickname)) options = { 'db_host': DB_ARGS['host'], 'db_port': DB_ARGS['port'], 'db_user': DB_ARGS['user'], 'db_password': DB_ARGS['password'], 'db_database': DB_ARGS['database'], } for option, value in options.items(): setattr(tornado.options.options, option, value) app = Application([ tornado.web.URLSpec(r'/', AllUsers, name='index'), tornado.web.URLSpec(r'/user/(?P<uid>[0-9]+)', GetUser, name='user'), tornado.web.URLSpec(r'/user', AddUser, name='user_add'), ], debug=True) loop = tornado.ioloop.IOLoop.current().asyncio_loop loop.run_until_complete(app.late_init(db)) loop.run_until_complete(db.gino.create_all()) try: yield app finally: loop.run_until_complete(db.gino.drop_all())
def app(ssl_ctx): # Define your database metadata # ----------------------------- db = Gino() # Define tables as you would normally do # -------------------------------------- class User(db.Model): __tablename__ = 'users' id = db.Column(db.Integer(), primary_key=True, autoincrement=True) nickname = db.Column(db.Unicode(), nullable=False) # Now just use your tables # ------------------------ class AllUsers(tornado.web.RequestHandler): async def get(self): users = await User.query.gino.all() for user in users: self.write('<a href="{url}">{nickname}</a><br/>'.format( url=self.application.reverse_url('user', user.id), nickname=tornado.escape.xhtml_escape(user.nickname))) class GetUser(tornado.web.RequestHandler, RequestHandlerMixin): async def get(self, uid): async with self.db.acquire() as conn: user = await User.get_or_404(int(uid), bind=conn) self.write('Hi, {}!'.format(user.nickname)) class AddUser(tornado.web.RequestHandler): async def post(self): user = await User.create(nickname=self.get_argument('name')) self.write('Hi, {}!'.format(user.nickname)) class Application(tornado.web.Application, DBMixin): pass app = Application([ tornado.web.URLSpec(r'/', AllUsers, name='index'), tornado.web.URLSpec(r'/user/(?P<uid>[0-9]+)', GetUser, name='user'), tornado.web.URLSpec(r'/user', AddUser, name='user_add'), ], debug=True) IOLoop.current().run_sync( lambda: app.init_db(db, host=DB_ARGS['host'], port=DB_ARGS['port'], user=DB_ARGS['user'], password=DB_ARGS['password'], database=DB_ARGS['database'], ssl=ssl_ctx)) loop = tornado.ioloop.IOLoop.current().asyncio_loop loop.run_until_complete(db.gino.create_all()) try: yield app finally: loop.run_until_complete(db.gino.drop_all())
""" Database GINO models """ from gino.ext.tornado import Gino DB = Gino() class User(DB.Model): """ User for authorization """ __tablename__ = 'users' id = DB.Column(DB.Integer(), primary_key=True, autoincrement=True) # pylint: disable=maybe-no-member email = DB.Column(DB.String(), nullable=False) # pylint: disable=maybe-no-member password = DB.Column(DB.String(), nullable=False) # pylint: disable=maybe-no-member __table_args__ = (DB.UniqueConstraint('id'), DB.UniqueConstraint('email')) # pylint: disable=maybe-no-member class Candidate(DB.Model): """ Candidate for vacancy model """ __tablename__ = 'candidates' id = DB.Column(DB.Integer(), primary_key=True) # pylint: disable=maybe-no-member first_name = DB.Column(DB.String()) # pylint: disable=maybe-no-member last_name = DB.Column(DB.String()) # pylint: disable=maybe-no-member first_working_day = DB.Column(DB.Date()) # pylint: disable=maybe-no-member
def app(ssl_ctx): # Define your database metadata # ----------------------------- db = Gino() # Define tables as you would normally do # -------------------------------------- class User(db.Model): __tablename__ = "users" id = db.Column(db.Integer(), primary_key=True, autoincrement=True) nickname = db.Column(db.Unicode(), nullable=False) # Now just use your tables # ------------------------ class AllUsers(tornado.web.RequestHandler): async def get(self): users = await User.query.gino.all() for user in users: self.write( '<a href="{url}">{nickname}</a><br/>'.format( url=self.application.reverse_url("user", user.id), nickname=tornado.escape.xhtml_escape(user.nickname), ) ) class GetUser(tornado.web.RequestHandler, RequestHandlerMixin): async def get(self, uid): async with self.db.acquire() as conn: user = await User.get_or_404(int(uid), bind=conn) self.write("Hi, {}!".format(user.nickname)) class AddUser(tornado.web.RequestHandler): async def post(self): user = await User.create(nickname=self.get_argument("name")) self.write("Hi, {}!".format(user.nickname)) class Application(tornado.web.Application, DBMixin): pass app = Application( [ tornado.web.URLSpec(r"/", AllUsers, name="index"), tornado.web.URLSpec(r"/user/(?P<uid>[0-9]+)", GetUser, name="user"), tornado.web.URLSpec(r"/user", AddUser, name="user_add"), ], debug=True, ) IOLoop.current().run_sync( lambda: app.init_db( db, host=DB_ARGS["host"], port=DB_ARGS["port"], user=DB_ARGS["user"], password=DB_ARGS["password"], database=DB_ARGS["database"], max_inactive_connection_lifetime=59.0, ssl=ssl_ctx, ) ) loop = tornado.ioloop.IOLoop.current().asyncio_loop loop.run_until_complete(db.gino.create_all()) try: # noinspection PyProtectedMember assert app.db.bind._pool._pool._holders[0]._max_inactive_time == 59.0 yield app finally: loop.run_until_complete(db.gino.drop_all())
import fast_arrow as fa import datetime import configparser config_file = "config.dev.ini" config = configparser.ConfigParser() config.read(config_file) username = config['account']['username'] password = config['account']['password'] fa_client = fa.Client(username=username, password=password) result = fa_client.authenticate() env = Environment(loader=FileSystemLoader('templates')) db = Gino() class AbstractModel(object): @classmethod def gen_ads(cls, array): result = [] for e in array: result.append(e.to_dictionary()) return result @classmethod def find_existing(cls, id): return cls.query.where(cls.id == id).gino.all()