2018-10-15 01:43:11 +03:00
|
|
|
from pyramid.view import (
|
|
|
|
view_config,
|
|
|
|
render_view_to_response,
|
2019-04-15 15:53:17 +03:00
|
|
|
)
|
2018-10-15 01:43:11 +03:00
|
|
|
from pyramid.renderers import render_to_response
|
2019-03-04 02:17:40 +03:00
|
|
|
from pyramid.httpexceptions import HTTPNotFound
|
2018-10-15 01:43:11 +03:00
|
|
|
from pyramid.response import FileResponse
|
2019-02-24 00:17:06 +03:00
|
|
|
from pyramid.path import (DottedNameResolver, AssetResolver)
|
2018-10-31 20:23:24 +03:00
|
|
|
|
2019-05-21 20:31:11 +03:00
|
|
|
from flatfilecms.models import (Folder, Document, IMarkdown, Jinja2)
|
2018-10-15 01:43:11 +03:00
|
|
|
|
|
|
|
|
|
|
|
class PagesView:
|
|
|
|
def __init__(self, context, request):
|
|
|
|
self.context = context
|
|
|
|
self.request = request
|
|
|
|
|
|
|
|
@view_config(context=Folder)
|
|
|
|
def folder(self):
|
|
|
|
if 'index' not in self.context:
|
|
|
|
raise HTTPNotFound
|
|
|
|
return render_view_to_response(self.context['index'], self.request)
|
|
|
|
|
|
|
|
@view_config(context=Document)
|
|
|
|
def document(self):
|
|
|
|
return FileResponse(
|
2019-04-15 15:53:17 +03:00
|
|
|
AssetResolver().resolve(self.context.path).abspath(),
|
|
|
|
request=self.request)
|
2018-10-15 01:43:11 +03:00
|
|
|
|
2019-02-16 20:05:09 +03:00
|
|
|
@view_config(context=IMarkdown)
|
2018-10-15 01:43:11 +03:00
|
|
|
def process_yaml(self):
|
2019-02-24 00:17:06 +03:00
|
|
|
if 'view' in self.context.page:
|
|
|
|
r = DottedNameResolver()
|
|
|
|
if isinstance(self.context.page['view'], str):
|
2019-02-24 03:16:38 +03:00
|
|
|
response = r.resolve(self.context.page['view'])(self)
|
|
|
|
if response:
|
|
|
|
return response
|
|
|
|
elif isinstance(self.context.page['view'], dict):
|
|
|
|
for name, options in self.context.page['view'].items():
|
|
|
|
response = r.resolve(name)(self, options)
|
|
|
|
if response:
|
|
|
|
return response
|
2019-02-24 00:17:06 +03:00
|
|
|
else:
|
|
|
|
for view in self.context.page['view']:
|
|
|
|
if isinstance(view, str):
|
2019-02-24 03:16:38 +03:00
|
|
|
response = r.resolve(view)(self)
|
|
|
|
if response:
|
|
|
|
return response
|
2019-02-24 00:17:06 +03:00
|
|
|
else:
|
2019-02-24 03:16:38 +03:00
|
|
|
for name, options in view.items():
|
|
|
|
response = r.resolve(name)(self, options)
|
|
|
|
if response:
|
|
|
|
return response
|
2019-02-16 20:05:09 +03:00
|
|
|
post = self.context.page
|
2019-03-04 02:17:40 +03:00
|
|
|
return render_to_response(
|
2019-04-15 15:53:17 +03:00
|
|
|
'{0}.jinja2'.format(post.get('template', 'default')),
|
|
|
|
post,
|
|
|
|
request=self.request,
|
|
|
|
response=self.request.response)
|
2018-10-15 01:43:11 +03:00
|
|
|
|
|
|
|
@view_config(context=Jinja2)
|
|
|
|
def jinja2(self):
|
2019-04-15 15:53:17 +03:00
|
|
|
return render_to_response(self.context.path, {}, request=self.request)
|