class Views: def __init__(self, app_renderer, *args, **kwargs): self.app_renderer = app_renderer self.app_db = DBClient() def on_ad_list_view(self, request): if request.method == 'GET': ads = self.app_db.get_ordered_all() else: pass return self.app_renderer('advertisement_list.html', ads=ads, title='List of ads') def on_ad_create_view(self, request): if request.method == 'GET': pass elif request.method == 'POST': db_object = { 'title': request.form['title'], 'description': request.form['description'], 'created_on': datetime.datetime.now() } self.app_db.insert(new_object=db_object) return self.app_renderer('create_ad.html', title='Create an ad') def on_ad_detail_view(self, request, ad_id): if request.method == 'GET': title = 'Empty Ad' ad = self.app_db.get(value=ad_id) if ad is not None: ad['created_on'] = ad['created_on'].strftime( "%I:%M%p on %B %d, %Y") title = ad.get('title') return self.app_renderer('detail_ad.html', title=title, ad=ad)
def make_app(io_loop=None): """ Create and return tornado.web.Application object so it can be used in tests too :param io_loop: already existing io_loop (used for testing) :returns: application instance """ app = tornado.web.Application( [ (r"/", handlers.HomeHandler), (r"/login/?", handlers.LoginHandler), (r"/logout/?", handlers.LogoutHandler), (r"/sensors/?", handlers.SensorsHandler), (r"/switches/?", handlers.SwitchesHandler), (r"/sounds/?", handlers.SoundsHandler), (r"/cameras/?", handlers.CamerasHandler), (r"/http_video/?", handlers.VideoHTTPHandler), (r"/ws_video/?", handlers.VideoWSHandler), (r"/subscribe/?", handlers.SubscribeHandler), (r"/(manifest\.json)", tornado.web.StaticFileHandler, {"path": "static"}), (r"/(service\-worker\.js)", tornado.web.StaticFileHandler, {"path": "static"}), ], template_path=settings.TEMPLATE_PATH, static_path=settings.STATIC_PATH, cookie_secret=settings.COOKIE_SECRET, login_url="/login/", xsrf_cookies=True ) app.config = settings app.database = DBClient(settings.DSN, io_loop) app.cache = {} app.io_loop = io_loop return app
def __init__(self, config): self.db_client = DBClient(host=config['db_host'], port=config['db_port']) self.db_client.register_table('ads') self.db_client.select_table('ads') template_path = os.path.join(os.path.dirname(__file__), 'templates') self.jinja_env = Environment(loader=FileSystemLoader(template_path), autoescape=True) self.views = Views(app_renderer=self.render_template) self.url_map = Map([ Rule('/', endpoint='ad_list_view'), Rule('/ad/<ad_id>/', endpoint='ad_detail_view'), Rule('/ad/create/', endpoint='ad_create_view'), ])
class Ads(object): def __init__(self, config): self.db_client = DBClient(host=config['db_host'], port=config['db_port']) self.db_client.register_table('ads') self.db_client.select_table('ads') template_path = os.path.join(os.path.dirname(__file__), 'templates') self.jinja_env = Environment(loader=FileSystemLoader(template_path), autoescape=True) self.views = Views(app_renderer=self.render_template) self.url_map = Map([ Rule('/', endpoint='ad_list_view'), Rule('/ad/<ad_id>/', endpoint='ad_detail_view'), Rule('/ad/create/', endpoint='ad_create_view'), ]) def render_template(self, template_name, **context): t = self.jinja_env.get_template(template_name) return Response(t.render(context), mimetype='text/html') def wsgi_app(self, environ, start_response): request = Request(environ) response = self.dispatch_request(request) return response(environ, start_response) def __call__(self, environ, start_response): return self.wsgi_app(environ, start_response) def dispatch_request(self, request): adapter = self.url_map.bind_to_environ(request.environ) try: endpoint, values = adapter.match() return getattr(self.views, 'on_' + endpoint)(request, **values) except HTTPException as e: return e
def update(self): logging.info("\n\nUpdating") self.info: DBClient = DBClient("daily") self.guilds: dict = self.info.find("guilds") logging.info(self.guilds)
def __init__(self, bot: commands.Bot): self.daily_message.start() self.bot = bot self.info: DBClient = DBClient("daily") self.guilds: dict = self.info.find("guilds") self.tz = pytz.timezone("America/Chicago")
def __init__(self, bot: commands.Bot): self.bot = bot self.starboard_info: DBClient = DBClient("starboard") self.guilds = self.starboard_info.find("guilds")
def __init__(self, bot): self.bot = bot self.suggestionDB: DBClient = DBClient("gameIdeas")
def __init__(self, app_renderer, *args, **kwargs): self.app_renderer = app_renderer self.app_db = DBClient()
def __init__(self, bot: commands.Bot): self.bot = bot self.starboard_info: DBClient = DBClient("starboard") self.daily_info: DBClient = DBClient("daily") self.starboard_guilds: dict = self.starboard_info.find("guilds") self.daily_guilds: dict = self.daily_info.find("guilds")