async def storylet_outcome( storylet_outcome_request: StoryletBranchOutcomeRequest, ) -> OutcomeSubmitResponse: with get_session() as session: user = authorize(session, storylet_outcome_request.apiKey) if not user: return OutcomeSubmitResponse(success=False, error="Invalid API key") area_id, setting_id = get_location(user, storylet_outcome_request) outcome = record_outcome( user=user, session=session, branch_id=storylet_outcome_request.branchId, outcome_info=storylet_outcome_request.endStorylet, messages=storylet_outcome_request.messages, redirect=storylet_outcome_request.redirect, area_id=area_id, setting_id=setting_id, ) if storylet_outcome_request.isLinkingFromOutcomeObservation is not None: # TODO Test this, does it have the branch ID? observation = session.query(OutcomeObservation).get( storylet_outcome_request.isLinkingFromOutcomeObservation) if observation: observation.redirect_outcome = outcome if outcome.redirect_area_id is not None: user.current_area_id = outcome.redirect_area_id if outcome.redirect_setting_id is not None: user.current_setting_id = outcome.redirect_setting_id return OutcomeSubmitResponse( success=True, outcomeObservationId=outcome.id, newAreaId=outcome.redirect_area_id, newSettingId=outcome.redirect_setting_id, )
async def area(area_request: AreaRequest) -> SubmitResponse: with get_session() as session: user = authorize(session, area_request.apiKey) if not user: return SubmitResponse(success=False, error="Invalid API key") user.current_area = record_area(session, area_request.area, area_request.settingId) return SubmitResponse(success=True)
async def setting(setting_request: SettingRequest) -> SubmitResponse: with get_session() as session: user = authorize(session, setting_request.apiKey) if not user: return SubmitResponse(success=False, error="Invalid API key") user.current_setting = record_setting(session, setting_request.setting, setting_request.areaId) return SubmitResponse(success=True)
async def storylet_view(request: Request, area_id: int, storylet_id: int): with get_session() as session: area = session.query(Area).get(area_id) storylet = session.query(Storylet)\ .options( selectinload(Storylet.areas), selectinload(Storylet.branches).selectinload(Branch.observations) ).get(storylet_id) return {"area": area, "storylet": storylet}
async def possessions( possessions_request: PossessionsRequest) -> SubmitResponse: with get_session() as session: user = authorize(session, possessions_request.apiKey) if not user: return SubmitResponse(success=False, error="Invalid API key") update_user_possessions(session, user, (p for cpi in possessions_request.possessions for p in cpi.possessions)) return SubmitResponse(success=True)
def setup_db(): from fallen_london_chronicler import model model.Base.metadata.create_all(engine) # Create a default admin user in case one doesn't exist # The user will not be usable unless config.require_api_key is false with get_session() as session: if not config.require_api_key and not User.get_by_api_key(session, ""): user = User.create(session, "Administrator", True) user.api_key = "" logging.info( "Created default administrator user with empty API key")
async def export_html(): from fallen_london_chronicler.export.html import HTMLExporter if not config.html_export_enable: return {"success": False} with get_session() as session: exporter = HTMLExporter(export_dir=config.html_export_path, root_url=config.html_export_url) path = exporter.export_all(session) return {"success": True, "path": path}
async def branch_view(request: Request, area_id: int, branch_id: int): with get_session() as session: area = session.query(Area).get(area_id) branch = ( session.query(Branch).options( selectinload(Branch.storylet), selectinload(Branch.outcome_observations) .selectinload(OutcomeObservation.redirect_branch) ) .get(branch_id) ) return render_branch(area, branch)
async def opportunities( opportunities_request: OpportunitiesRequest) -> SubmitResponse: with get_session() as session: user = authorize(session, opportunities_request.apiKey) if not user: return SubmitResponse(success=False, error="Invalid API key") area_id, setting_id = get_location(user, opportunities_request) record_opportunities( session, opportunities_request.displayCards, area_id, setting_id, ) return SubmitResponse(success=True)
async def storylet_list( storylet_list_request: StoryletListRequest) -> SubmitResponse: with get_session() as session: user = authorize(session, storylet_list_request.apiKey) if not user: return SubmitResponse(success=False, error="Invalid API key") area_id, setting_id = get_location(user, storylet_list_request) if area_id is None or setting_id is None: return SubmitResponse( success=False, error="Current area ID or setting ID does not match submitted " "area ID or setting ID, refresh page") record_area_storylets(session, area_id, setting_id, storylet_list_request.storylets) return SubmitResponse(success=True)
async def export_google_docs(): from fallen_london_chronicler.export.google_docs import GoogleDocsExporter if not config.google_docs_export_enable: return {"success": False} with get_session() as session: exporter = GoogleDocsExporter( google_credentials_path=config.google_credentials_path, google_credentials_cache_path=config.google_credentials_cache_path, template_document_id=config.google_docs_template_id, ) url = exporter.export_all(session) return { "success": True, "url": url, }
async def area_view(request: Request, api_key: str): with get_session() as session: require_admin(session, api_key) form = await request.form() if "save_user" in form: save_user = form["save_user"] is_admin = bool(form.get("is_admin")) if save_user: user: User = session.query(User).get(int(save_user)) user.is_admin = is_admin else: user = User.create(session, name=form["name"].strip(), is_admin=is_admin) user.is_active = bool(form.get("is_active")) return render_admin(session)
async def storylet_view( storylet_view_request: StoryletViewRequest) -> SubmitResponse: with get_session() as session: user = authorize(session, storylet_view_request.apiKey) if not user: return SubmitResponse(success=False, error="Invalid API key") area_id, setting_id = get_location(user, storylet_view_request) storylet = record_storylet( session, storylet_view_request.storylet, area_id, setting_id, ) if storylet_view_request.isLinkingFromOutcomeObservation is not None: observation = session.query(OutcomeObservation).get( storylet_view_request.isLinkingFromOutcomeObservation) if observation: observation.redirect = storylet return SubmitResponse(success=True)
async def areas_view(request: Request): with get_session() as session: areas = session.query(Area).options(selectinload(Area.storylets)).all() return {"areas": areas}
async def area_view(request: Request, area_id: int): with get_session() as session: area = (session.query(Area).options(selectinload( Area.storylets)).get(area_id)) return render_area(area)
async def area_view(request: Request, api_key: str = ""): with get_session() as session: require_admin(session, api_key) return render_admin(session)