bl2tools/bl2tools/views.py
2022-04-21 13:27:51 -04:00

98 lines
2.2 KiB
Python

from izzylib_http_async import View
from . import tpl_path
class Home(View):
__path__ = ['/']
async def get(self, request, response):
response.set_template('home.haml')
class Style(View):
__path__ = ['/style.css']
async def get(self, request, response):
response.set_template('style.css', content_type='text/css')
class Javascript(View):
__path__ = ['/functions.js']
jspath = tpl_path.join('functions.js')
jsfile = None
last_mod = 0
async def get(self, request, response):
if not self.jsfile or self.last_mod < self.jspath.mtime:
self.last_mod = self.jspath.mtime
with self.jspath.open('r') as fd:
self.jsfile = fd.read()
response.body = self.jsfile
response.content_type = 'application/javascript'
class ApiList(View):
__path__ = ['/api/list']
async def get(self, request, response):
save_list = [save.id for save in self.app.saves]
response.set_json(sorted(save_list))
class ApiSave(View):
__path__ = ['/api/save/{saveid:int}']
async def get(self, request, response, saveid):
try:
save = self.app.get_save_by_id(saveid)
except KeyError as e:
return response.set_json({'error': str(e)}, status=404)
data = save.props.to_dict()
data['filename'] = save.path.name
data['readonly'] = save.path.permissions()['user'] == 4
return response.set_json(data)
class ApiSaveToggle(View):
__path__ = ['/api/save/{saveid:int}/toggle']
async def get(self, request, response, saveid):
try:
save = self.app.get_save_by_id(saveid)
except KeyError as e:
return response.set_json({'error': e}, status=404)
readonly = save.path.permissions()['user'] == 4
if readonly:
save.path.chmod(644)
else:
save.path.chmod(444)
return response.set_json({'readonly': not readonly})
class ApiSaveHtml(View):
__path__ = ['/api/save/{saveid:int}/html']
async def get(self, request, response, saveid):
try:
save = self.app.get_save_by_id(saveid)
except KeyError as e:
return response.set_json({'error': str(e)}, status=404)
data = save.props.to_dict()
data['filename'] = save.path.name
data['readonly'] = str(save.path.permissions()['user'] == 4).lower()
return response.set_template('save.haml', {'save': data})