2022-12-18 16:40:39 +00:00
|
|
|
from sanic import Sanic, response, exceptions
|
|
|
|
from sanic.response import text, html, redirect, raw
|
|
|
|
from jinja2 import Environment, FileSystemLoader
|
|
|
|
from time import time
|
|
|
|
import httpx
|
|
|
|
import re
|
|
|
|
import json
|
2023-07-04 21:07:39 +00:00
|
|
|
import logging
|
2022-12-18 16:48:29 +00:00
|
|
|
from os.path import join
|
2022-12-18 16:40:39 +00:00
|
|
|
from ext import *
|
|
|
|
from config import *
|
2023-01-08 10:48:01 +00:00
|
|
|
from aztec_code_generator import AztecCode
|
|
|
|
from io import BytesIO
|
2023-07-04 21:07:39 +00:00
|
|
|
from asyncio import Queue
|
|
|
|
import sqlite3
|
|
|
|
|
|
|
|
log = logging.getLogger()
|
2022-12-18 16:40:39 +00:00
|
|
|
|
|
|
|
app = Sanic(__name__)
|
|
|
|
app.static("/res", "res/")
|
|
|
|
|
|
|
|
app.ext.add_dependency(Order, get_order)
|
|
|
|
app.ext.add_dependency(Quotas, get_quotas)
|
|
|
|
|
|
|
|
from room import bp as room_bp
|
2022-12-19 21:08:59 +00:00
|
|
|
from propic import bp as propic_bp
|
2023-05-23 19:02:44 +00:00
|
|
|
from karaoke import bp as karaoke_bp
|
2023-01-17 21:25:35 +00:00
|
|
|
from export import bp as export_bp
|
2023-01-19 16:02:57 +00:00
|
|
|
from stats import bp as stats_bp
|
2023-05-08 21:04:15 +00:00
|
|
|
from api import bp as api_bp
|
|
|
|
from carpooling import bp as carpooling_bp
|
2023-07-04 21:07:39 +00:00
|
|
|
from checkin import bp as checkin_bp
|
2022-12-18 16:40:39 +00:00
|
|
|
|
2023-07-29 14:05:46 +00:00
|
|
|
app.blueprint([room_bp, karaoke_bp, propic_bp, export_bp, stats_bp, api_bp, carpooling_bp, checkin_bp])
|
2022-12-18 16:40:39 +00:00
|
|
|
|
|
|
|
@app.exception(exceptions.SanicException)
|
|
|
|
async def clear_session(request, exception):
|
|
|
|
tpl = app.ctx.tpl.get_template('error.html')
|
2023-05-11 22:18:49 +00:00
|
|
|
r = html(tpl.render(exception=exception))
|
2022-12-18 16:40:39 +00:00
|
|
|
|
|
|
|
if exception.status_code == 403:
|
2023-05-11 22:18:49 +00:00
|
|
|
del r.cookies["foxo_code"]
|
|
|
|
del r.cookies["foxo_secret"]
|
|
|
|
return r
|
2022-12-18 16:40:39 +00:00
|
|
|
|
|
|
|
@app.before_server_start
|
|
|
|
async def main_start(*_):
|
|
|
|
print(">>>>>> main_start <<<<<<")
|
2023-01-17 21:25:35 +00:00
|
|
|
|
|
|
|
app.ctx.om = OrderManager()
|
2023-07-04 21:07:39 +00:00
|
|
|
if FILL_CACHE:
|
|
|
|
log.info("Filling cache!")
|
|
|
|
await app.ctx.om.fill_cache()
|
|
|
|
log.info("Cache fill done!")
|
|
|
|
|
|
|
|
app.ctx.nfc_counts = sqlite3.connect('data/nfc_counts.db')
|
|
|
|
|
2023-05-11 22:18:49 +00:00
|
|
|
app.ctx.login_codes = {}
|
2023-07-04 21:07:39 +00:00
|
|
|
|
2022-12-18 16:40:39 +00:00
|
|
|
app.ctx.tpl = Environment(loader=FileSystemLoader("tpl"), autoescape=True)
|
|
|
|
app.ctx.tpl.globals.update(time=time)
|
2023-05-08 21:04:15 +00:00
|
|
|
app.ctx.tpl.globals.update(PROPIC_DEADLINE=PROPIC_DEADLINE)
|
2022-12-18 16:40:39 +00:00
|
|
|
app.ctx.tpl.globals.update(int=int)
|
|
|
|
app.ctx.tpl.globals.update(len=len)
|
2023-01-08 10:48:01 +00:00
|
|
|
|
|
|
|
@app.route("/manage/barcode/<code>")
|
|
|
|
async def gen_barcode(request, code):
|
|
|
|
aa = AztecCode(code).image(module_size=8, border=2)
|
|
|
|
img = BytesIO()
|
|
|
|
aa.save(img, format='PNG')
|
|
|
|
|
|
|
|
return raw(img.getvalue(), content_type="image/png")
|
2023-01-17 21:25:35 +00:00
|
|
|
|
2023-07-29 14:05:46 +00:00
|
|
|
@app.route(f"/{ORGANIZER}/{EVENT_NAME}/order/<code>/<secret>/open/<secret2>")
|
2022-12-18 16:40:39 +00:00
|
|
|
async def redirect_explore(request, code, secret, order: Order, secret2=None):
|
|
|
|
|
2023-05-11 22:18:49 +00:00
|
|
|
r = redirect(app.url_for("welcome"))
|
2022-12-18 16:40:39 +00:00
|
|
|
if order and order.code != code: order = None
|
|
|
|
|
|
|
|
if not order:
|
|
|
|
async with httpx.AsyncClient() as client:
|
2022-12-18 16:48:29 +00:00
|
|
|
res = await client.get(join(base_url, f"orders/{code}/"), headers=headers)
|
2023-07-29 14:05:46 +00:00
|
|
|
print(res.json())
|
2022-12-18 16:40:39 +00:00
|
|
|
if res.status_code != 200:
|
|
|
|
raise exceptions.NotFound("This order code does not exist. Check that your order wasn't deleted, or the link is correct.")
|
|
|
|
|
|
|
|
res = res.json()
|
|
|
|
if secret != res['secret']:
|
|
|
|
raise exceptions.Forbidden("The secret part of the url is not correct. Check your E-Mail for the correct link, or contact support!")
|
2023-05-11 22:18:49 +00:00
|
|
|
r.cookies['foxo_code'] = code
|
|
|
|
r.cookies['foxo_secret'] = secret
|
|
|
|
return r
|
2022-12-18 16:40:39 +00:00
|
|
|
|
2023-02-02 21:47:33 +00:00
|
|
|
@app.route("/manage/privacy")
|
|
|
|
async def privacy(request):
|
|
|
|
tpl = app.ctx.tpl.get_template('privacy.html')
|
|
|
|
return html(tpl.render())
|
|
|
|
|
2022-12-18 16:40:39 +00:00
|
|
|
@app.route("/manage/welcome")
|
|
|
|
async def welcome(request, order: Order, quota: Quotas):
|
|
|
|
|
|
|
|
if not order:
|
|
|
|
raise exceptions.Forbidden("You have been logged out. Please access the link in your E-Mail to login again!")
|
|
|
|
|
|
|
|
pending_roommates = []
|
|
|
|
if order.pending_roommates:
|
|
|
|
for pr in order.pending_roommates:
|
|
|
|
if not pr: continue
|
2023-01-17 21:25:35 +00:00
|
|
|
pending_roommates.append(await app.ctx.om.get_order(code=pr, cached=True))
|
2022-12-18 16:40:39 +00:00
|
|
|
|
|
|
|
room_members = []
|
|
|
|
if order.room_id:
|
|
|
|
if order.room_id != order.code:
|
2023-01-17 21:25:35 +00:00
|
|
|
room_owner = await app.ctx.om.get_order(code=order.room_id, cached=True)
|
2022-12-18 16:40:39 +00:00
|
|
|
else:
|
|
|
|
room_owner = order
|
|
|
|
|
|
|
|
room_members.append(room_owner)
|
|
|
|
|
|
|
|
for member_id in room_owner.ans('room_members').split(','):
|
|
|
|
if member_id == room_owner.code: continue
|
|
|
|
if member_id == order.code:
|
|
|
|
room_members.append(order)
|
|
|
|
else:
|
2023-01-17 21:25:35 +00:00
|
|
|
room_members.append(await app.ctx.om.get_order(code=member_id, cached=True))
|
2022-12-18 16:40:39 +00:00
|
|
|
|
|
|
|
tpl = app.ctx.tpl.get_template('welcome.html')
|
|
|
|
return html(tpl.render(order=order, quota=quota, room_members=room_members, pending_roommates=pending_roommates))
|
|
|
|
|
|
|
|
|
|
|
|
@app.route("/manage/download_ticket")
|
2022-12-19 17:58:16 +00:00
|
|
|
async def download_ticket(request, order: Order):
|
2022-12-18 16:40:39 +00:00
|
|
|
|
|
|
|
if not order:
|
|
|
|
raise exceptions.Forbidden("You have been logged out. Please access the link in your E-Mail to login again!")
|
|
|
|
|
|
|
|
if not order.status != 'confirmed':
|
|
|
|
raise exceptions.Forbidden("You are not allowed to download this ticket.")
|
|
|
|
|
|
|
|
async with httpx.AsyncClient() as client:
|
2022-12-18 16:48:29 +00:00
|
|
|
res = await client.get(join(base_url, f"orders/{order.code}/download/pdf/"), headers=headers)
|
2022-12-19 17:58:16 +00:00
|
|
|
|
2023-01-08 10:48:01 +00:00
|
|
|
if res.status_code == 409:
|
2022-12-19 17:58:16 +00:00
|
|
|
raise exceptions.SanicException("Your ticket is still being generated. Please try again later!", status_code=res.status_code)
|
2023-01-08 10:48:01 +00:00
|
|
|
elif res.status_code == 403:
|
|
|
|
raise exceptions.SanicException("You can download your ticket only after the order has been confirmed and paid. Try later!", status_code=400)
|
2022-12-19 17:58:16 +00:00
|
|
|
|
|
|
|
return raw(res.content, content_type='application/pdf')
|
2023-01-08 10:48:01 +00:00
|
|
|
|
|
|
|
@app.route("/manage/logout")
|
|
|
|
async def logour(request):
|
|
|
|
raise exceptions.Forbidden("You have been logged out.", status_code=403)
|
2022-12-18 16:40:39 +00:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2023-01-08 10:48:01 +00:00
|
|
|
app.run(host="0.0.0.0", port=8188, dev=DEV_MODE)
|