2023-12-30 10:27:42 +00:00
|
|
|
from os.path import join
|
2024-01-08 21:02:27 +00:00
|
|
|
from sanic import exceptions
|
2023-12-30 10:27:42 +00:00
|
|
|
from config import *
|
|
|
|
import httpx
|
2024-01-19 17:03:54 +00:00
|
|
|
from messages import ROOM_ERROR_TYPES
|
2024-01-21 23:35:44 +00:00
|
|
|
from email_util import send_unconfirm_message
|
2024-02-14 19:22:56 +00:00
|
|
|
from sanic.response import text, html, redirect, raw
|
2024-01-21 23:35:44 +00:00
|
|
|
from sanic.log import logger
|
2024-02-14 19:22:56 +00:00
|
|
|
from metrics import *
|
2024-02-22 18:15:33 +00:00
|
|
|
import pretixClient
|
|
|
|
import traceback
|
2023-12-30 10:27:42 +00:00
|
|
|
|
2024-01-08 21:02:27 +00:00
|
|
|
METADATA_TAG = "meta_data"
|
|
|
|
VARIATIONS_TAG = "variations"
|
|
|
|
|
2023-12-30 10:27:42 +00:00
|
|
|
QUESTION_TYPES = { #https://docs.pretix.eu/en/latest/api/resources/questions.html
|
|
|
|
"number": "N",
|
|
|
|
"one_line_string": "S",
|
|
|
|
"multi_line_string": "T",
|
|
|
|
"boolean": "B",
|
|
|
|
"choice_from_list": "C",
|
|
|
|
"multiple_choice_from_list": "M",
|
|
|
|
"file_upload": "F",
|
|
|
|
"date": "D",
|
|
|
|
"time": "H",
|
|
|
|
"date_time": "W",
|
|
|
|
"country_code": "CC",
|
|
|
|
"telephone_number": "TEL"
|
|
|
|
}
|
|
|
|
TYPE_OF_QUESTIONS = {} # maps questionId -> type
|
|
|
|
|
|
|
|
|
2024-02-22 18:15:33 +00:00
|
|
|
async def load_questions() -> bool:
|
2023-12-30 10:27:42 +00:00
|
|
|
global TYPE_OF_QUESTIONS
|
2024-02-22 18:15:33 +00:00
|
|
|
# TYPE_OF_QUESTIONS.clear() It should not be needed
|
|
|
|
logger.info("[QUESTIONS] Loading questions...")
|
|
|
|
success = True
|
|
|
|
try:
|
2023-12-30 10:27:42 +00:00
|
|
|
p = 0
|
|
|
|
while 1:
|
|
|
|
p += 1
|
2024-02-22 18:15:33 +00:00
|
|
|
res = await pretixClient.get(f"questions/?page={p}", expectedStatusCodes=[200, 404])
|
2023-12-30 10:27:42 +00:00
|
|
|
if res.status_code == 404: break
|
|
|
|
|
|
|
|
data = res.json()
|
|
|
|
for q in data['results']:
|
2024-01-08 21:02:27 +00:00
|
|
|
TYPE_OF_QUESTIONS[q['id']] = q['type']
|
2024-02-22 18:15:33 +00:00
|
|
|
except Exception:
|
|
|
|
logger.warning(f"[QUESTIONS] Error while loading questions.\n{traceback.format_exc()}")
|
|
|
|
success = False
|
|
|
|
return success
|
2024-01-08 21:02:27 +00:00
|
|
|
|
2024-02-22 18:15:33 +00:00
|
|
|
async def load_items() -> bool:
|
2024-01-08 21:02:27 +00:00
|
|
|
global ITEMS_ID_MAP
|
|
|
|
global ITEM_VARIATIONS_MAP
|
|
|
|
global CATEGORIES_LIST_MAP
|
|
|
|
global ROOM_TYPE_NAMES
|
2024-02-22 18:15:33 +00:00
|
|
|
logger.info("[ITEMS] Loading items...")
|
|
|
|
success = True
|
|
|
|
try:
|
2024-01-08 21:02:27 +00:00
|
|
|
p = 0
|
|
|
|
while 1:
|
|
|
|
p += 1
|
2024-02-22 18:15:33 +00:00
|
|
|
res = await pretixClient.get(f"items/?page={p}", expectedStatusCodes=[200, 404])
|
2024-01-08 21:02:27 +00:00
|
|
|
if res.status_code == 404: break
|
|
|
|
|
|
|
|
data = res.json()
|
|
|
|
for q in data['results']:
|
|
|
|
# Map item id
|
2024-01-18 17:03:32 +00:00
|
|
|
itemName = check_and_get_name ('item', q)
|
2024-01-08 21:02:27 +00:00
|
|
|
if itemName and itemName in ITEMS_ID_MAP:
|
|
|
|
ITEMS_ID_MAP[itemName] = q['id']
|
|
|
|
# If item has variations, map them, too
|
|
|
|
if itemName in ITEM_VARIATIONS_MAP and VARIATIONS_TAG in q:
|
|
|
|
isBedInRoom = itemName == 'bed_in_room'
|
|
|
|
for v in q[VARIATIONS_TAG]:
|
2024-01-18 17:03:32 +00:00
|
|
|
variationName = check_and_get_name('variation', v)
|
2024-01-08 21:02:27 +00:00
|
|
|
if variationName and variationName in ITEM_VARIATIONS_MAP[itemName]:
|
|
|
|
ITEM_VARIATIONS_MAP[itemName][variationName] = v['id']
|
|
|
|
if isBedInRoom and variationName in ITEM_VARIATIONS_MAP['bed_in_room']:
|
|
|
|
roomName = v['name'] if 'name' in v and isinstance(v['name'], str) else None
|
|
|
|
if not roomName and 'value' in v:
|
|
|
|
roomName = v['value'][list(v['value'].keys())[0]]
|
|
|
|
ROOM_TYPE_NAMES[v['id']] = roomName
|
|
|
|
# Adds itself to the category list
|
2024-01-18 17:03:32 +00:00
|
|
|
categoryName = check_and_get_category ('item', q)
|
2024-01-08 21:02:27 +00:00
|
|
|
if not categoryName: continue
|
|
|
|
CATEGORIES_LIST_MAP[categoryName].append(q['id'])
|
2024-01-11 17:15:56 +00:00
|
|
|
if (EXTRA_PRINTS):
|
2024-01-21 23:35:44 +00:00
|
|
|
logger.debug(f'Mapped Items: %s', ITEMS_ID_MAP)
|
|
|
|
logger.debug(f'Mapped Variations: %s', ITEM_VARIATIONS_MAP)
|
|
|
|
logger.debug(f'Mapped categories: %s', CATEGORIES_LIST_MAP)
|
|
|
|
logger.debug(f'Mapped Rooms: %s', ROOM_TYPE_NAMES)
|
2024-02-22 18:15:33 +00:00
|
|
|
except Exception:
|
|
|
|
logger.warning(f"[ITEMS] Error while loading items.\n{traceback.format_exc()}")
|
|
|
|
success = False
|
|
|
|
return success
|
2024-01-08 21:02:27 +00:00
|
|
|
|
|
|
|
# Tries to get an item name from metadata. Prints a warning if an item has no metadata
|
2024-01-18 17:03:32 +00:00
|
|
|
def check_and_get_name(type, q):
|
|
|
|
itemName = extract_metadata_name(q)
|
2024-01-11 17:15:56 +00:00
|
|
|
if not itemName and EXTRA_PRINTS:
|
2024-01-21 23:35:44 +00:00
|
|
|
logger.warning('%s %s has not been mapped.', type, q['id'])
|
2024-01-08 21:02:27 +00:00
|
|
|
return itemName
|
|
|
|
|
2024-01-18 17:03:32 +00:00
|
|
|
def check_and_get_category (type, q):
|
|
|
|
categoryName = extract_category (q)
|
2024-01-11 17:15:56 +00:00
|
|
|
if not categoryName and EXTRA_PRINTS:
|
2024-01-21 23:35:44 +00:00
|
|
|
logger.warning('%s %s has no category set.', type, q['id'])
|
2024-01-08 21:02:27 +00:00
|
|
|
return categoryName
|
|
|
|
|
|
|
|
# Checks if the item has specified metadata name
|
2024-01-18 17:03:32 +00:00
|
|
|
def internal_name_check (toExtract, name):
|
2024-01-08 21:02:27 +00:00
|
|
|
return toExtract and name and METADATA_TAG in toExtract and toExtract[METADATA_TAG][METADATA_NAME] == str(name)
|
|
|
|
|
|
|
|
# Returns the item_name metadata from the item or None if not defined
|
2024-01-18 17:03:32 +00:00
|
|
|
def extract_metadata_name (toExtract):
|
|
|
|
return extract_data(toExtract, [METADATA_TAG, METADATA_NAME])
|
2024-01-08 21:02:27 +00:00
|
|
|
|
|
|
|
# Returns the category_name metadata from the item or None if not defined
|
2024-01-18 17:03:32 +00:00
|
|
|
def extract_category (toExtract):
|
|
|
|
return extract_data(toExtract, [METADATA_TAG, METADATA_CATEGORY])
|
2024-01-08 21:02:27 +00:00
|
|
|
|
2024-01-18 17:03:32 +00:00
|
|
|
def extract_data (dataFrom, tags):
|
2024-01-08 21:02:27 +00:00
|
|
|
data = dataFrom
|
|
|
|
for t in tags:
|
|
|
|
if t not in data: return None
|
|
|
|
data = data[t]
|
|
|
|
return data
|
|
|
|
|
2024-01-18 17:03:32 +00:00
|
|
|
def key_from_value(dict, value):
|
2024-01-08 21:02:27 +00:00
|
|
|
return [k for k,v in dict.items() if v == value]
|
|
|
|
|
|
|
|
def sizeof_fmt(num, suffix="B"):
|
|
|
|
for unit in ("", "K", "M", "G", "T", "P", "E", "Z"):
|
|
|
|
if abs(num) < 1000.0:
|
|
|
|
return f"{num:3.1f}{unit}{suffix}"
|
|
|
|
num /= 1000.0
|
|
|
|
return f"{num:.1f}Yi{suffix}"
|
|
|
|
|
2024-01-18 17:03:32 +00:00
|
|
|
async def get_order_by_code(request, code, throwException=False):
|
2024-01-08 21:02:27 +00:00
|
|
|
res = await request.app.ctx.om.get_order(code=code)
|
2024-01-08 22:16:26 +00:00
|
|
|
if not throwException:
|
|
|
|
return res
|
2024-01-08 21:02:27 +00:00
|
|
|
if res is None:
|
2024-01-08 22:16:26 +00:00
|
|
|
raise exceptions.BadRequest(f"[getOrderByCode] Code {code} not found!")
|
2024-01-08 21:02:27 +00:00
|
|
|
return res
|
|
|
|
|
2024-01-21 23:35:44 +00:00
|
|
|
async def get_people_in_room_by_code(request, code, om=None):
|
2024-01-19 00:15:20 +00:00
|
|
|
if not om: om = request.app.ctx.om
|
2024-01-21 23:35:44 +00:00
|
|
|
await om.update_cache()
|
|
|
|
return filter(lambda rm: rm.room_id == code, om.cache.values())
|
|
|
|
|
2024-05-13 10:01:47 +00:00
|
|
|
async def confirm_room_by_order(order, request):
|
|
|
|
bed_in_room = order.bed_in_room # Variation id of the ticket for that kind of room
|
|
|
|
room_members = []
|
|
|
|
for m in order.room_members:
|
|
|
|
if m == order.code:
|
|
|
|
res = order
|
|
|
|
else:
|
|
|
|
res = await request.app.ctx.om.get_order(code=m)
|
|
|
|
|
|
|
|
if res.room_id != order.code:
|
|
|
|
raise exceptions.BadRequest("Please contact support: some of the members in your room are actually somewhere else")
|
|
|
|
|
|
|
|
if res.status != 'paid':
|
|
|
|
raise exceptions.BadRequest("Somebody hasn't paid.")
|
|
|
|
|
|
|
|
if res.bed_in_room != bed_in_room:
|
|
|
|
raise exceptions.BadRequest("Somebody has a ticket for a different type of room!")
|
|
|
|
|
|
|
|
if res.daily:
|
|
|
|
raise exceptions.BadRequest("Somebody in your room has a daily ticket!")
|
|
|
|
|
|
|
|
room_members.append(res)
|
|
|
|
|
|
|
|
|
|
|
|
if len(room_members) != order.room_person_no:
|
|
|
|
raise exceptions.BadRequest("The number of people in your room mismatches your type of ticket!")
|
|
|
|
|
|
|
|
for rm in room_members:
|
|
|
|
await rm.edit_answer('room_id', order.code)
|
|
|
|
await rm.edit_answer('room_confirmed', "True")
|
|
|
|
await rm.edit_answer('pending_roommates', None)
|
|
|
|
await rm.edit_answer('pending_room', None)
|
|
|
|
|
|
|
|
for rm in room_members:
|
|
|
|
await rm.send_answers()
|
|
|
|
|
2024-02-29 10:41:49 +00:00
|
|
|
async def unconfirm_room_by_order(order, room_members=None, throw=True, request=None, om=None):
|
2024-01-19 00:15:20 +00:00
|
|
|
if not om: om = request.app.ctx.om
|
|
|
|
if not order.room_confirmed:
|
|
|
|
if throw:
|
|
|
|
raise exceptions.BadRequest("Room is not confirmed!")
|
|
|
|
else:
|
|
|
|
return
|
|
|
|
|
2024-01-21 23:35:44 +00:00
|
|
|
room_members = await get_people_in_room_by_code(request, order.code, om) if not room_members or len(room_members) == 0 else room_members
|
|
|
|
for p in room_members:
|
2024-01-19 00:15:20 +00:00
|
|
|
await p.edit_answer('room_confirmed', "False")
|
|
|
|
await p.send_answers()
|
|
|
|
|
2024-05-13 08:26:29 +00:00
|
|
|
async def remove_members_from_room(order, removeMembers):
|
|
|
|
didSomething = False
|
|
|
|
for member in removeMembers:
|
|
|
|
if (member in order.room_members):
|
|
|
|
order.room_members.remove(member)
|
|
|
|
didSomething = True
|
|
|
|
if(didSomething):
|
|
|
|
await order.edit_answer("room_members", ','.join(order.room_members))
|
|
|
|
await order.send_answers()
|
|
|
|
return didSomething
|
|
|
|
|
2024-01-21 23:35:44 +00:00
|
|
|
async def validate_rooms(request, rooms, om):
|
|
|
|
logger.info('Validating rooms...')
|
2024-01-19 00:15:20 +00:00
|
|
|
if not om: om = request.app.ctx.om
|
2024-01-21 23:35:44 +00:00
|
|
|
|
2024-02-10 17:44:20 +00:00
|
|
|
# rooms_to_unconfirm is the room that MUST be unconfirmed, room_with_errors is a less strict set containing all rooms with kind-ish errors
|
|
|
|
rooms_to_unconfirm = []
|
|
|
|
room_with_errors = []
|
2024-05-13 08:26:29 +00:00
|
|
|
remove_members = []
|
2024-01-21 23:35:44 +00:00
|
|
|
|
|
|
|
# Validate rooms
|
|
|
|
for order in rooms:
|
|
|
|
result = await check_room(request, order, om)
|
2024-02-10 17:44:20 +00:00
|
|
|
if(len(order.room_errors) > 0):
|
|
|
|
room_with_errors.append(result)
|
2024-01-21 23:35:44 +00:00
|
|
|
check = result[1]
|
|
|
|
if check != None and check == False:
|
2024-02-10 17:44:20 +00:00
|
|
|
rooms_to_unconfirm.append(result)
|
2024-01-21 23:35:44 +00:00
|
|
|
|
|
|
|
# End here if no room has failed check
|
2024-02-10 17:44:20 +00:00
|
|
|
if len(room_with_errors) == 0:
|
2024-01-21 23:35:44 +00:00
|
|
|
logger.info('[ROOM VALIDATION] Every room passed the check.')
|
|
|
|
return
|
|
|
|
|
2024-02-10 17:44:20 +00:00
|
|
|
roomErrListSrts = []
|
|
|
|
for fr in room_with_errors:
|
|
|
|
for error in fr[0].room_errors:
|
|
|
|
roomErrListSrts.append(f"[ROOM VALIDATION] [ERR] Parent room: {fr[0].code} {'C' if fr[0].room_confirmed else 'N'} | Order {error[0] if error[0] else '-----'} with code {error[1]}")
|
|
|
|
logger.warning(f'[ROOM VALIDATION] Room validation failed for orders: \n%s', "\n".join(roomErrListSrts))
|
2024-01-19 00:15:20 +00:00
|
|
|
|
2024-01-21 23:35:44 +00:00
|
|
|
# Get confirmed rooms that fail validation
|
2024-02-10 17:44:20 +00:00
|
|
|
failed_confirmed_rooms = list(filter(lambda fr: (fr[0].room_confirmed == True), rooms_to_unconfirm))
|
2024-01-21 23:35:44 +00:00
|
|
|
|
2024-05-13 08:26:29 +00:00
|
|
|
didSomething = False
|
|
|
|
|
2024-01-21 23:35:44 +00:00
|
|
|
if len(failed_confirmed_rooms) == 0:
|
|
|
|
logger.info('[ROOM VALIDATION] No rooms to unconfirm.')
|
2024-05-13 08:26:29 +00:00
|
|
|
else:
|
|
|
|
didSomething = True
|
|
|
|
logger.info(f"[ROOM VALIDATION] Trying to unconfirm {len(failed_confirmed_rooms)} rooms...")
|
2024-01-21 23:35:44 +00:00
|
|
|
|
2024-05-13 08:26:29 +00:00
|
|
|
# Try unconfirming them
|
|
|
|
for rtu in failed_confirmed_rooms:
|
|
|
|
order = rtu[0]
|
|
|
|
member_orders = rtu[2]
|
|
|
|
logger.warning(f"[ROOM VALIDATION] [UNCONFIRMING] Unconfirming room {order.code}...")
|
|
|
|
|
|
|
|
# Unconfirm and email users about the room
|
|
|
|
if UNCONFIRM_ROOMS_ENABLE:
|
|
|
|
await unconfirm_room_by_order(order, member_orders, False, None, om)
|
|
|
|
|
|
|
|
for r in rooms_to_unconfirm:
|
|
|
|
order = r[0]
|
|
|
|
removeMembers = r[3]
|
|
|
|
if len(removeMembers) > 0:
|
|
|
|
logger.warning(f"[ROOM VALIDATION] [REMOVING] Removing members '{','.join(removeMembers)}' from room {order.code}")
|
2024-01-21 23:35:44 +00:00
|
|
|
|
2024-03-20 16:28:35 +00:00
|
|
|
if UNCONFIRM_ROOMS_ENABLE:
|
2024-05-13 08:26:29 +00:00
|
|
|
didSomething |= await remove_members_from_room(order, removeMembers)
|
|
|
|
if(r not in failed_confirmed_rooms): failed_confirmed_rooms.append(r)
|
|
|
|
|
|
|
|
|
|
|
|
if(didSomething):
|
|
|
|
logger.info(f"[ROOM VALIDATION] Sending unconfirm notice to room members...")
|
|
|
|
sent_count = 0
|
|
|
|
# Send unconfirm notice via email
|
|
|
|
for rtu in failed_confirmed_rooms:
|
|
|
|
order = rtu[0]
|
|
|
|
member_orders = rtu[2]
|
|
|
|
try:
|
|
|
|
if UNCONFIRM_ROOMS_ENABLE:
|
|
|
|
await send_unconfirm_message(order, member_orders)
|
|
|
|
sent_count += len(member_orders)
|
|
|
|
except Exception as ex:
|
|
|
|
if EXTRA_PRINTS: logger.exception(str(ex))
|
|
|
|
logger.info(f"[ROOM VALIDATION] Sent {sent_count} emails")
|
2024-01-19 00:15:20 +00:00
|
|
|
|
|
|
|
|
2024-05-20 10:07:59 +00:00
|
|
|
# Returns true if the logged used is an admin OR if it's an admin logged as another user
|
|
|
|
async def isSessionAdmin(request, order):
|
|
|
|
if(order.isAdmin()): return True
|
|
|
|
|
|
|
|
orgCode = request.cookies.get("foxo_code_ORG")
|
|
|
|
orgSecret = request.cookies.get("foxo_secret_ORG")
|
|
|
|
if orgCode != None and orgSecret != None:
|
|
|
|
|
|
|
|
user = await request.app.ctx.om.get_order(code=orgCode)
|
|
|
|
if(user == None): return False
|
|
|
|
if(user.secret != orgSecret): raise exceptions.Forbidden("Birichino :)")
|
|
|
|
return user.isAdmin()
|
|
|
|
|
2024-01-19 00:15:20 +00:00
|
|
|
async def check_room(request, order, om=None):
|
|
|
|
room_errors = []
|
|
|
|
room_members = []
|
2024-05-13 08:26:29 +00:00
|
|
|
remove_members = []
|
2024-01-21 23:35:44 +00:00
|
|
|
use_cached = request == None
|
2024-01-19 00:15:20 +00:00
|
|
|
if not om: om = request.app.ctx.om
|
2024-05-13 08:26:29 +00:00
|
|
|
if not order or not order.room_id or order.room_id != order.code: return (order, False, room_members, remove_members)
|
2024-01-19 00:15:20 +00:00
|
|
|
|
|
|
|
# This is not needed anymore you buy tickets already
|
|
|
|
#if quotas.get_left(len(order.room_members)) == 0:
|
|
|
|
# raise exceptions.BadRequest("There are no more rooms of this size to reserve.")
|
2024-02-10 17:44:20 +00:00
|
|
|
allOk = True
|
2024-01-19 00:15:20 +00:00
|
|
|
|
|
|
|
bed_in_room = order.bed_in_room # Variation id of the ticket for that kind of room
|
|
|
|
for m in order.room_members:
|
|
|
|
if m == order.code:
|
|
|
|
res = order
|
|
|
|
else:
|
2024-01-21 23:35:44 +00:00
|
|
|
res = await om.get_order(code=m, cached=use_cached)
|
2024-01-19 00:15:20 +00:00
|
|
|
|
|
|
|
# Room user in another room
|
|
|
|
if res.room_id != order.code:
|
2024-02-10 17:44:20 +00:00
|
|
|
room_errors.append((res.code, 'room_id_mismatch'))
|
|
|
|
allOk = False
|
2024-05-13 08:26:29 +00:00
|
|
|
|
|
|
|
if res.status == 'canceled':
|
|
|
|
room_errors.append((res.code, 'canceled'))
|
|
|
|
remove_members.append(res.code)
|
|
|
|
allOk = False
|
|
|
|
elif res.status != 'paid':
|
2024-02-10 17:44:20 +00:00
|
|
|
room_errors.append((res.code, 'unpaid'))
|
2024-01-19 00:15:20 +00:00
|
|
|
|
|
|
|
if res.bed_in_room != bed_in_room:
|
2024-02-10 17:44:20 +00:00
|
|
|
room_errors.append((res.code, 'type_mismatch'))
|
|
|
|
if order.room_confirmed:
|
|
|
|
allOk = False
|
2024-01-19 00:15:20 +00:00
|
|
|
|
|
|
|
if res.daily:
|
2024-02-10 17:44:20 +00:00
|
|
|
room_errors.append((res.code, 'daily'))
|
|
|
|
if order.room_confirmed:
|
|
|
|
allOk = False
|
2024-01-19 00:15:20 +00:00
|
|
|
|
|
|
|
room_members.append(res)
|
|
|
|
|
2024-03-20 16:28:35 +00:00
|
|
|
if len(room_members) != order.room_person_no and order.room_person_no != None and order.room_person_no >= 0:
|
2024-02-10 17:44:20 +00:00
|
|
|
room_errors.append((None, 'capacity_mismatch'))
|
|
|
|
if order.room_confirmed:
|
|
|
|
allOk = False
|
2024-01-21 23:35:44 +00:00
|
|
|
order.set_room_errors(room_errors)
|
2024-05-13 08:26:29 +00:00
|
|
|
return (order, allOk, room_members, remove_members)
|