def new_message(self, message): MESSAGE = message.upper() # Привет if MESSAGE == self._COMMANDS[0]: return f"Здравсвуйте, {self._USERNAME}. Чтобы посмотреть список фильмов, напиши 'расписание'," \ f" чтобы обормить бронь - 'бронь'!" # Погода elif MESSAGE == self._COMMANDS[1]: return self._get_weather() elif MESSAGE == self._COMMANDS[4]: return 'Бронь можно оформить тут:\nhttps://booking123qaw.herokuapp.com/' # Время elif MESSAGE == self._COMMANDS[2]: return self._get_time() elif MESSAGE == self._COMMANDS[5]: res = ['Расписание фильмов:\n', 'Название, Дата, Время, Цена, Зал\n', '\n'] Handler.get_films() with open("films.json", "rt", encoding="utf8") as f: films_list = json.loads(f.read()) films = films_list['films'] for i in films: res.append(i['info'][:-2] + '\n') return ''.join(res) elif MESSAGE == self._COMMANDS[3]: return f"Пока-пока, {self._USERNAME}!" elif MESSAGE == self._COMMANDS[6]: return 'Список моих команд:\n' + ' Бронь\n' + ' Расписание\n' else: return "Не понимаю о чем вы..."
def login(): form = LoginForm() if form.validate_on_submit(): ans = Handler.log_in(form.username.data, form.password.data) if ans == "0": return redirect('/success') return render_template('login.html', title='Авторизация', form=form)
def main(): backend = Backend() handler = Handler(backend) chain_path = '/home/jbones/fullchain.pem' privkey_path = '/home/jbones/privkey.pem' ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLSv1) ssl_context.load_cert_chain(chain_path, privkey_path) app = web.Application() app.add_routes([ web.get('/favicon.ico', handler.handle_favicon), web.get('/', handler.handle_index_get), web.post('/', handler.handle_index_post), web.get('/menu/{menu_id}', handler.handle_menu), web.post('/order', handler.handle_order), web.post('/echo', handler.handle_echo), web.get('/rest', handler.handle_websocket), web.get('/rest_page', handler.handle_rest_page_get), web.post('/rest_page', handler.handle_rest_page_post), web.static('/resources', '../adminwebpage/resources'), make_static_js('/qrloader.js'), make_static_js('/qr-scanner.min.js'), make_static_js('/qr-scanner-worker.min.js'), web.static('/static', '../static_stuff/static') ]) web.run_app(app, port=443, ssl_context=ssl_context)
def reqister(): form = RegisterForm() if form.validate_on_submit(): if form.password.data != form.password_again.data: return render_template('register.html', title='Регистрация', form=form, message="Пароли не совпадают") db_sess = db_session.create_session() if db_sess.query(User).filter(User.email == form.email.data).first(): return render_template('register.html', title='Регистрация', form=form, message="Такой пользователь уже есть") ans = Handler.register(form.name.data, form.email.data, form.password.data) if ans == "0": return redirect('/login') return render_template('register.html', title='Регистрация', form=form)
class TestHandler: def setup(self): self.s = Handler() def test_presence_response(self): # Нет ключа action assert self.s.presence_response({ 'one': 'two', 'time': time.time() })['response'] == 400 # Нет ключа time assert self.s.presence_response({'action': 'presence'})['response'] == 400 # Ключ не presence assert self.s.presence_response({ 'action': 'test_action', 'time': 1000.10 })['response'] == 400 # Кривое время assert self.s.presence_response({ 'action': 'presence', 'time': 'test_time' })['response'] == 400 # Всё ок assert self.s.presence_response({ 'action': 'presence', 'time': 1000.10, 'account_name': 'leo' }) == { 'response': 200 } def test_read_requests(self, monkeypatch): monkeypatch.setattr("socket.socket", ClientSocket) r_clients = [socket.socket(), socket.socket()] all_clients = [socket.socket(), socket.socket(), socket.socket()] result = [{'test': 'test_message'}, {'test': 'test_message'}] print(self.s.read_requests(r_clients, all_clients)) print(result) assert self.s.read_requests(r_clients, all_clients) == result
def setup(self): self.s = Handler()
import requests import bs4 import vk_api from vk_bot.settings import TOKEN from server import Handler from server.data import db_session import json vk = vk_api.VkApi(token=TOKEN) db_session.global_init("../server/data/db/cinema.db") Handler.get_films() with open("films.json", "rt", encoding="utf8") as f: films_list = json.loads(f.read()) class VkBot: def __init__(self, user_id): global films_list self._USER_ID = user_id self._USERNAME = self._get_user_name_from_vk_id(user_id) self._COMMANDS = ["ПОМОЩЬ", "ПОГОДА", "ВРЕМЯ", "ПОКА", "БРОНЬ", "РАСПИСАНИЕ", "КОМАНДЫ"] def _get_user_name_from_vk_id(self, user_id): request = requests.get("https://vk.com/id" + str(user_id)) bs = bs4.BeautifulSoup(request.text, "html.parser") user_name = self._clean_all_tag_from_str(bs.findAll("title")[0]) return user_name.split()[0]
def index(): Handler.get_films() with open("films.json", "rt", encoding="utf8") as f: films_list = json.loads(f.read()) return render_template('site.html', films=films_list)
def setup(self): Handler.setup(self) self.request.sendall("Please input username:"******""