forked from Hay1tsme/artemis
port frontend changes from different project
This commit is contained in:
@ -1,12 +1,13 @@
|
||||
import json
|
||||
from typing import List
|
||||
from starlette.routing import Route
|
||||
from starlette.responses import Response, RedirectResponse
|
||||
import yaml
|
||||
import jinja2
|
||||
from os import path
|
||||
from twisted.web.util import redirectTo
|
||||
from starlette.requests import Request
|
||||
from twisted.web.server import Session
|
||||
|
||||
from core.frontend import FE_Base, IUserSession
|
||||
from core.frontend import FE_Base, UserSession
|
||||
from core.config import CoreConfig
|
||||
from titles.idac.database import IDACData
|
||||
from titles.idac.schema.profile import *
|
||||
@ -26,7 +27,8 @@ class IDACFrontend(FE_Base):
|
||||
self.game_cfg.update(
|
||||
yaml.safe_load(open(f"{cfg_dir}/{IDACConstants.CONFIG_NAME}"))
|
||||
)
|
||||
self.nav_name = "頭文字D THE ARCADE"
|
||||
#self.nav_name = "頭文字D THE ARCADE"
|
||||
self.nav_name = "IDAC"
|
||||
# TODO: Add version list
|
||||
self.version = IDACConstants.VER_IDAC_SEASON_2
|
||||
|
||||
@ -36,6 +38,11 @@ class IDACFrontend(FE_Base):
|
||||
25: "full_tune_tickets",
|
||||
34: "full_tune_fragments",
|
||||
}
|
||||
|
||||
def get_routes(self) -> List[Route]:
|
||||
return [
|
||||
Route("/", self.render_GET)
|
||||
]
|
||||
|
||||
async def generate_all_tables_json(self, user_id: int):
|
||||
json_export = {}
|
||||
@ -87,35 +94,32 @@ class IDACFrontend(FE_Base):
|
||||
return json.dumps(json_export, indent=4, default=str, ensure_ascii=False)
|
||||
|
||||
async def render_GET(self, request: Request) -> bytes:
|
||||
uri: str = request.uri.decode()
|
||||
uri: str = request.url.path
|
||||
|
||||
template = self.environment.get_template(
|
||||
"titles/idac/frontend/idac_index.jinja"
|
||||
"titles/idac/templates/idac_index.jinja"
|
||||
)
|
||||
sesh: Session = request.getSession()
|
||||
usr_sesh = IUserSession(sesh)
|
||||
user_id = usr_sesh.userId
|
||||
usr_sesh = self.validate_session(request)
|
||||
if not usr_sesh:
|
||||
usr_sesh = UserSession()
|
||||
user_id = usr_sesh.user_id
|
||||
# user_id = usr_sesh.user_id
|
||||
|
||||
# profile export
|
||||
if uri.startswith("/game/idac/export"):
|
||||
if user_id == 0:
|
||||
return redirectTo(b"/game/idac", request)
|
||||
return RedirectResponse(b"/game/idac", request)
|
||||
|
||||
# set the file name, content type and size to download the json
|
||||
content = await self.generate_all_tables_json(user_id).encode("utf-8")
|
||||
request.responseHeaders.addRawHeader(
|
||||
b"content-type", b"application/octet-stream"
|
||||
)
|
||||
request.responseHeaders.addRawHeader(
|
||||
b"content-disposition", b"attachment; filename=idac_profile.json"
|
||||
)
|
||||
request.responseHeaders.addRawHeader(
|
||||
b"content-length", str(len(content)).encode("utf-8")
|
||||
)
|
||||
|
||||
self.logger.info(f"User {user_id} exported their IDAC data")
|
||||
return content
|
||||
return Response(
|
||||
content,
|
||||
200,
|
||||
{'content-disposition': 'attachment; filename=idac_profile.json'},
|
||||
"application/octet-stream"
|
||||
)
|
||||
|
||||
profile_data, tickets, rank = None, None, None
|
||||
if user_id > 0:
|
||||
@ -128,7 +132,7 @@ class IDACFrontend(FE_Base):
|
||||
for ticket in ticket_data
|
||||
}
|
||||
|
||||
return template.render(
|
||||
return Response(template.render(
|
||||
title=f"{self.core_config.server.name} | {self.nav_name}",
|
||||
game_list=self.environment.globals["game_list"],
|
||||
profile=profile_data,
|
||||
@ -136,4 +140,4 @@ class IDACFrontend(FE_Base):
|
||||
rank=rank,
|
||||
sesh=vars(usr_sesh),
|
||||
active_page="idac",
|
||||
).encode("utf-16")
|
||||
))
|
||||
|
Reference in New Issue
Block a user