2024-01-08 21:02:27 +00:00
|
|
|
from sanic import response, redirect, Blueprint, exceptions
|
2024-01-18 17:03:32 +00:00
|
|
|
from room import unconfirm_room_by_order
|
2024-01-08 21:02:27 +00:00
|
|
|
from config import *
|
|
|
|
from utils import *
|
|
|
|
from ext import *
|
2024-01-21 23:35:44 +00:00
|
|
|
from sanic.log import logger
|
2024-01-08 21:02:27 +00:00
|
|
|
|
|
|
|
bp = Blueprint("admin", url_prefix="/manage/admin")
|
|
|
|
|
2024-01-21 23:35:44 +00:00
|
|
|
@bp.middleware
|
|
|
|
async def credentials_check(request: Request):
|
|
|
|
order = await get_order(request)
|
2024-01-08 21:02:27 +00:00
|
|
|
if not order:
|
|
|
|
raise exceptions.Forbidden("You have been logged out. Please access the link in your E-Mail to login again!")
|
2024-01-13 15:59:24 +00:00
|
|
|
if EXTRA_PRINTS:
|
2024-01-21 23:35:44 +00:00
|
|
|
logger.info(f"Checking admin credentials of {order.code} with secret {order.secret}")
|
2024-01-08 21:02:27 +00:00
|
|
|
if not order.isAdmin() : raise exceptions.Forbidden("Birichino :)")
|
|
|
|
|
2024-01-13 15:59:24 +00:00
|
|
|
|
|
|
|
|
2024-01-08 21:02:27 +00:00
|
|
|
@bp.get('/cache/clear')
|
2024-01-18 17:03:32 +00:00
|
|
|
async def clear_cache(request, order:Order):
|
2024-02-22 18:15:33 +00:00
|
|
|
success = await request.app.ctx.om.fill_cache()
|
|
|
|
if not success: raise exceptions.ServerError("An error occurred while loading the cache")
|
2024-01-08 21:02:27 +00:00
|
|
|
return redirect(f'/manage/admin')
|
|
|
|
|
2024-01-13 15:59:24 +00:00
|
|
|
@bp.get('/loginas/<code>')
|
2024-01-18 17:03:32 +00:00
|
|
|
async def login_as(request, code, order:Order):
|
|
|
|
dOrder = await get_order_by_code(request, code, throwException=True)
|
2024-01-13 15:59:24 +00:00
|
|
|
if(dOrder.isAdmin()):
|
|
|
|
raise exceptions.Forbidden("You can't login as another admin!")
|
|
|
|
|
|
|
|
if EXTRA_PRINTS:
|
2024-01-21 23:35:44 +00:00
|
|
|
logger.info(f"Swapping login: {order.secret} {order.code} -> {dOrder.secret} {code}")
|
2024-01-13 15:59:24 +00:00
|
|
|
r = redirect(f'/manage/welcome')
|
|
|
|
r.cookies['foxo_code_ORG'] = order.code
|
|
|
|
r.cookies['foxo_secret_ORG'] = order.secret
|
|
|
|
r.cookies['foxo_code'] = code
|
|
|
|
r.cookies['foxo_secret'] = dOrder.secret
|
|
|
|
return r
|
|
|
|
|
2024-01-21 23:35:44 +00:00
|
|
|
@bp.get('/room/verify')
|
|
|
|
async def verify_rooms(request, order:Order):
|
2024-02-22 18:15:33 +00:00
|
|
|
already_checked, success = await request.app.ctx.om.update_cache()
|
|
|
|
if not already_checked and success:
|
2024-01-21 23:35:44 +00:00
|
|
|
orders = filter(lambda x: x.status not in ['c', 'e'] and x.room_id == x.code, request.app.ctx.om.cache.values())
|
|
|
|
await validate_rooms(request, orders, None)
|
|
|
|
return redirect(f'/manage/admin')
|
|
|
|
|
2024-01-08 21:02:27 +00:00
|
|
|
@bp.get('/room/unconfirm/<code>')
|
2024-01-18 17:03:32 +00:00
|
|
|
async def unconfirm_room(request, code, order:Order):
|
|
|
|
dOrder = await get_order_by_code(request, code, throwException=True)
|
2024-01-21 23:35:44 +00:00
|
|
|
await unconfirm_room_by_order(order=dOrder, throw=True, request=request)
|
2024-01-08 21:02:27 +00:00
|
|
|
return redirect(f'/manage/nosecount')
|
|
|
|
|
|
|
|
@bp.get('/room/delete/<code>')
|
2024-01-18 17:03:32 +00:00
|
|
|
async def delete_room(request, code, order:Order):
|
|
|
|
dOrder = await get_order_by_code(request, code, throwException=True)
|
2024-01-08 21:02:27 +00:00
|
|
|
|
2024-01-21 23:35:44 +00:00
|
|
|
ppl = await get_people_in_room_by_code(request, code)
|
2024-01-08 21:02:27 +00:00
|
|
|
for p in ppl:
|
|
|
|
await p.edit_answer('room_id', None)
|
|
|
|
await p.edit_answer('room_confirmed', "False")
|
|
|
|
await p.edit_answer('room_name', None)
|
|
|
|
await p.edit_answer('pending_room', None)
|
|
|
|
await p.edit_answer('pending_roommates', None)
|
|
|
|
await p.edit_answer('room_members', None)
|
|
|
|
await p.edit_answer('room_owner', None)
|
|
|
|
await p.edit_answer('room_secret', None)
|
|
|
|
await p.send_answers()
|
|
|
|
|
|
|
|
await dOrder.send_answers()
|
|
|
|
return redirect(f'/manage/nosecount')
|
|
|
|
|
|
|
|
@bp.post('/room/rename/<code>')
|
2024-01-18 17:03:32 +00:00
|
|
|
async def rename_room(request, code, order:Order):
|
|
|
|
dOrder = await get_order_by_code(request, code, throwException=True)
|
2024-01-08 21:02:27 +00:00
|
|
|
|
|
|
|
name = request.form.get('name')
|
|
|
|
if len(name) > 64 or len(name) < 4:
|
|
|
|
raise exceptions.BadRequest("Your room name is invalid. Please try another one.")
|
|
|
|
|
|
|
|
await dOrder.edit_answer("room_name", name)
|
|
|
|
await dOrder.send_answers()
|
|
|
|
return redirect(f'/manage/nosecount')
|