Skip to content

Commit d6e7b6b

Browse files
committed
app: serve .htm as well as .json
1 parent 4e98f34 commit d6e7b6b

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

tanco/app.py

+16-8
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,8 @@ def htmx_fragment(f0):
121121
async def f(*a, **kw):
122122
body = (await f0(*a, **kw)) if inspect.iscoroutinefunction(f0) else f0(*a, **kw)
123123
qr = quart.request
124-
if qr.headers.get('HX-Request'):
124+
_fmt = kw.pop('_fmt') if '_fmt' in kw else ''
125+
if qr.headers.get('HX-Request') or _fmt=='htm':
125126
return body
126127
else:
127128
return await quart.render_template('index.html', body=body)
@@ -134,24 +135,31 @@ def platonic(route, template, hx=True):
134135
depending on the presence of the string '.json' in the url"""
135136
def decorator(f0):
136137
async def fp(*a, **kw):
137-
data = (await f0(*a, **kw)) if inspect.iscoroutinefunction(f0) else f0(*a, **kw)
138-
if quart.request.path.endswith('.json'):
138+
_fmt = kw.pop('_fmt') if '_fmt' in kw else ''
139+
data = (await f0(*a, **kw)) if inspect.iscoroutinefunction(f0) \
140+
else f0(*a, **kw)
141+
if _fmt == 'json':
139142
return data
140143
else:
141-
return await quart.render_template(template, data=data, url=quart.request.path)
144+
return await quart.render_template(
145+
template, data=data, url=quart.request.path)
146+
147+
async def ff(*a, **kw):
148+
match kw.get('_fmt', ''):
149+
case 'json': return await fp(*a, **kw)
150+
case 'htm': return await fhx(*a, **kw)
151+
case _: return f"bad format type: '.{_fmt}'", 400
142152

143-
async def fj(*a, **kw):
144-
return await fp(*a, **kw)
145153

146154
@htmx_fragment
147155
async def fhx(*a, **kw):
148156
return await fp(*a, **kw)
149157

150158
fp.__name__ = f0.__name__
151159
fhx.__name__ = f0.__name__
152-
fj.__name__ = f0.__name__ + '_json'
160+
ff.__name__ = f0.__name__ + '_fmt'
153161
app.route(route)(fhx if hx else fp)
154-
app.route(route + '.json')(fj)
162+
app.route(route + '.<_fmt>')(ff)
155163
return fp
156164
return decorator
157165

0 commit comments

Comments
 (0)