added server-side endpoint for CO2Data
This commit is contained in:
parent
76ed60aaac
commit
ee989e17fe
1 changed files with 35 additions and 1 deletions
|
|
@ -26,7 +26,7 @@ from tornado.httpclient import AsyncHTTPClient, HTTPRequest
|
|||
import tornado.log
|
||||
|
||||
from . import markdown_tools
|
||||
from . import model_generator
|
||||
from . import model_generator, co2_model_generator
|
||||
from .report_generator import ReportGenerator, calculate_report_data
|
||||
from .user import AuthenticatedUser, AnonymousUser
|
||||
|
||||
|
|
@ -340,7 +340,40 @@ class GenericExtraPage(BaseRequestHandler):
|
|||
active_page=self.active_page,
|
||||
text_blocks=template_environment.globals["common_text"]
|
||||
))
|
||||
|
||||
|
||||
class CO2Data(BaseRequestHandler):
|
||||
def check_xsrf_cookie(self):
|
||||
"""
|
||||
This request handler implements a stateless API that returns report data in JSON format.
|
||||
Thus, XSRF cookies are disabled by overriding base class implementation of this method with a pass statement.
|
||||
"""
|
||||
pass
|
||||
|
||||
async def post(self) -> None:
|
||||
requested_model_config = tornado.escape.json_decode(self.request.body)
|
||||
|
||||
try:
|
||||
form = co2_model_generator.CO2FormData.from_dict(requested_model_config)
|
||||
except Exception as err:
|
||||
if self.settings.get("debug", False):
|
||||
import traceback
|
||||
print(traceback.format_exc())
|
||||
response_json = {'code': 400, 'error': f'Your request was invalid {html.escape(str(err))}'}
|
||||
self.set_status(400)
|
||||
self.finish(json.dumps(response_json))
|
||||
return
|
||||
|
||||
executor = loky.get_reusable_executor(
|
||||
max_workers=self.settings['handler_worker_pool_size'],
|
||||
timeout=300,
|
||||
)
|
||||
report_task = executor.submit(
|
||||
co2_model_generator.CO2FormData.build_model, form,
|
||||
)
|
||||
report: str = await asyncio.wrap_future(report_task)
|
||||
self.finish(dict(report.CO2_fit_params()))
|
||||
|
||||
|
||||
def get_url(app_root: str, relative_path: str = '/'):
|
||||
return app_root.rstrip('/') + relative_path.rstrip('/')
|
||||
|
|
@ -363,6 +396,7 @@ def make_app(
|
|||
base_urls: typing.List = [
|
||||
(get_root_url(r'/?'), LandingPage),
|
||||
(get_root_calculator_url(r'/?'), CalculatorForm),
|
||||
(get_root_calculator_url(r'/co2-fit/'), CO2Data),
|
||||
(get_root_calculator_url(r'/report'), ConcentrationModel),
|
||||
(get_root_url(r'/static/(.*)'), StaticFileHandler, {'path': static_dir}),
|
||||
(get_root_calculator_url(r'/static/(.*)'), StaticFileHandler, {'path': calculator_static_dir}),
|
||||
|
|
|
|||
Loading…
Reference in a new issue