import sqlite3 from typing import List, Optional from telegram import User from app import log from app.fs import DB_PATH from .answer import Answer logger = log.getLogger(__name__) # logger.setLevel(log.DEBUG) MAX_ANSWERS = 50 MAX_POLLS_PER_USER = 50 class Poll(object): def __init__(self, owner: User, topic: str): self.id: Optional[int] = None self.owner: User = owner self.topic: str = topic self._answers: List[Answer] = [] def answers(self) -> List[Answer]: """ list of possible answers for the poll in order they were added. """ return self._answers def add_answer(self, answer: str): a = Answer(self, answer)
import threading from app import application from app import log as logging LOG = logging.getLogger(__name__) class HyperCache(): """ A thread-safe cache with a timer to process cache data after an interval A hyper cache will call a `handler` after an interval to process the data you put inside the cache-backend. """ def __init__(self, driver, interval=application.config.get("CACHE_TIMEOUT_INTERVAL", 60), *args, **kwargs): self._buffer = driver(*args, **kwargs) self._timers = dict() self._interval = interval self._lock = threading.Lock() def set(self, key, value, handler=None, *func_args, **func_kwargs): self._lock.acquire() try: self._buffer[key] = value # TODO: may be useful while trigger the specific event # always set the key as `_event_cached_value` so that # we can distinguish it when performing the action # func_kwargs["_event_cached_value"] = value if handler: if self._timers.get(key):
from app import manager from app import validation from app import exceptions from app import version_obj from app import log from flask import abort from flask import jsonify from flask import request from flask import make_response from flask import render_template random_manager = manager.Manager() LOG = log.getLogger(__name__) @app.route('/') @app.route('/index') def index(): return render_template('hello.html') #create a random code with a timeout window @app.route('/random', methods=['POST']) def create_random(): #LOG = log.getLogger(__name__) version = request.headers.get('X-Version', '1.0') req = request.json ver = str(version.decode('utf8'))