class Pagination_handler(Handler): __arguments_schema__ = good.Schema({ 'per_page': good.Any( good.All( [good.All(good.Coerce(int), good.Range(min=1))], good.Length(max=1), ), good.Default([constants.PER_PAGE]) ), 'page': good.Any( good.All( [good.All(good.Coerce(int), good.Range(min=1))], good.Length(max=1), ), good.Default([1]) ), }, default_keys=good.Required, extra_keys=good.Allow,) def get(self, *args, **kwargs): args = self.validate_arguments(Pagination_handler.__arguments_schema__) self.per_page = args.pop('per_page', [0])[0] self.page = args.pop('page', [0])[0] self.request.arguments.pop('per_page', None) self.request.arguments.pop('page', None)
class Handler(Api_handler): __schema__ = good.Schema({ 'message': good.All(str, good.Length(min=1, max=500)), 'webhook_url': good.Any(good.Url('https'), good.All(str, good.Length(max=0))), }) @Level(3) async def get(self, channel_id): r = await self.db.fetchone( 'SELECT webhook_url, message FROM twitch_discord_live_notification WHERE channel_id=%s', (channel_id, )) if not r: self.set_status(204) else: self.write_object({ 'webhook_url': r['webhook_url'], 'message': r['message'], }) @Level(3) async def put(self, channel_id): data = self.validate() r = await self.db.fetchone( 'SELECT webhook_url, message FROM twitch_discord_live_notification WHERE channel_id=%s', (channel_id, )) if not r: await self.db.execute( ''' INSERT INTO twitch_discord_live_notification (channel_id, webhook_url, message) VALUES (%s, %s, %s); ''', ( channel_id, data['webhook_url'], data['message'], )) else: await self.db.execute( ''' UPDATE twitch_discord_live_notification SET webhook_url=%s, message=%s WHERE channel_id=%s ''', ( data['webhook_url'], data['message'], channel_id, ))
def validate(inputs, state, outputs): """ Validate the data structures for this vertex. """ inputs_schema = good.Schema({ 'cfg': { 'enable_ui': bool }, 'ctrl': {}, 'ctk': good.Any(), 'vid': good.Any(), 'diag': good.Any(), 'slog': good.Any(), 'elog': good.Any() }) state_schema = good.Schema({}) outputs_schema = good.Schema({}) inputs_schema(inputs) state_schema(state) outputs_schema(outputs)
def validate(inputs, state, outputs): """ Validate the data structures for this vertex. """ inputs_schema = good.Schema({}) state_schema = good.Schema({'reader': good.Any(), 'msg_num': good.Any()}) outputs_schema = good.Schema({ 'ctrl': good.Any(), 'vid': good.Any(), 'diag': good.Any(), 'slog': good.Any(), 'elog': good.Any() }) inputs_schema(inputs) state_schema(state) outputs_schema(outputs)
def stream_append(params): if params.stream_id is None: params.stream_id = create_stream() message_id = create_message(stream_id=params.stream_id, text=params.text) return {"message_id": message_id, "stream_id": params.stream_id} @endpoint("stream/query", G.Schema({ "stream_id": int, "updated_since": G.Maybe(int), "page": G.Any(int, G.Default(0), G.Range(0, 999)), "page_size": G.Any(int, G.Default(20), G.Range(1, 200)), "order_dir": G.Any("DESC", "ASC", G.Default("DESC")), "order_by": G.Any("created", "updated", G.Default("updated")) })) def stream_query(params): return query_messages(**params) @endpoint("stream/ping", G.Schema({"stream_id": G.Maybe(int)})) def stream_ping(params): return {"success": ping_stream(params.stream_id)}
return f Description_schema = good.Schema({ 'text': good.Maybe(str), 'title': good.Maybe(str), 'url': good.Maybe(str), }, default_keys=good.Optional) _Episode_schema = { 'title': good.Maybe(str), good.Required('number'): good.All(good.Coerce(int), good.Range(min=1)), good.Optional('season'): good.Maybe(good.All(good.Coerce(int), good.Range(min=1))), good.Optional('episode'): good.Maybe(good.All(good.Coerce(int), good.Range(min=1))), 'air_date': good.Maybe(date_()), 'air_time': good.Maybe(time_()), 'air_datetime': good.Maybe(iso8601_to_utc()), 'description': good.Any(None, Description_schema), 'runtime': good.Maybe(good.Coerce(int)), } Episode_schema = good.Schema(_Episode_schema, default_keys=good.Optional) External_schema = good.Schema({ good.All(good.Length(min=1, max=45)):good.Any(None, good.All(good.Coerce(str), good.Length(min=1, max=45))) }, default_keys=good.Optional) Importer_schema = good.Schema( {key: good.Maybe(good.All(str, good.Length(min=1, max=45))) \ for key in constants.IMPORTER_TYPE_NAMES}, default_keys=good.Optional, ) _Show_schema = { 'title': good.Maybe(str), 'description': good.Maybe(Description_schema), 'premiered': good.Maybe(date_()),
""" SELECT * FROM bot %s ORDER BY bot_id ASC LIMIT %%s OFFSET %%s """ % where, *params) def get_bot(bot_id): with db.connect() as c: return c.get( """ SELECT * FROM bot WHERE bot_id = %s """, bot_id) @endpoint("bot/query", G.Schema({ "name": G.Maybe(str), "page": G.Any(int, G.Default(0), G.Range(0, 999)), "page_size": G.Any(int, G.Default(100), G.Range(1, 200)) })) def bot_query(params): return query_bots(**params) @endpoint("bot/get", G.Schema({"bot_id": int})) def bot_get(params): return get_bot(params.bot_id)
def Any(*validators, **kwargs): msg = kwargs.pop('msg', None) assert not kwargs, 'Sorry, Any() does not support Schema keyword arguments anymore' return _wrapMsg(good.Any(*validators), msg)
class Handler(base.Handler): __arguments_schema__ = good.Schema( { 'days_back': good.Any( good.All(good.Coerce(int), good.Range(min=0, max=7)), good.Default(2), ), 'days_ahead': good.Any(good.All(good.Coerce(int), good.Range(min=0, max=14)), good.Default(7)), }, default_keys=good.Required) async def get(self, user_id): user_id = self.user_id_or_current(user_id) shows_episodes = await self.get_shows_episodes(user_id) if not shows_episodes: self.write_object([]) return ids = [] shows = {se[0]['id']: se[0] for se in shows_episodes} airdates = OrderedDict() airdate_shows = OrderedDict() prev = None for se in shows_episodes: if prev == None: prev = se[1]['air_datetime'].date() if prev != se[1]['air_datetime'].date(): airdates[prev] = list(airdate_shows.values()) prev = se[1]['air_datetime'].date() airdate_shows = {} if se[1]['show_id'] not in airdate_shows: airdate_shows[se[1]['show_id']] = copy.copy( shows[se[1]['show_id']]) show = airdate_shows[se[1]['show_id']] show.setdefault('episodes', []) show['episodes'].append(self.episode_wrapper(se[1])) if shows_episodes: airdates[prev] = list(airdate_shows.values()) self.write_object([{'air_date': ad, 'shows': airdates[ad]}\ for ad in airdates]) @run_on_executor def get_shows_episodes(self, user_id): args = self.validate_arguments() now = datetime.utcnow() from_ = (now - timedelta(days=args['days_back'])).date() to_ = (now + timedelta(days=args['days_ahead'])).date() with new_session() as session: rows = session.query(models.Episode, models.Show).filter( models.Show_fan.user_id == user_id, models.Show_fan.show_id == models.Show.id, models.Episode.show_id == models.Show.id, sa.func.date(models.Episode.air_datetime) >= from_, sa.func.date(models.Episode.air_datetime) <= to_, ).order_by( models.Episode.air_datetime, models.Show.id, ).all() return [(r.Show.serialize(), r.Episode.serialize()) for r in rows]