Permalink
Browse files

endpoint.template_render()

  • Loading branch information...
erichanson committed Jan 28, 2019
1 parent 45c6b4f commit 897db101462814cc1ca3513f89036143683426f8

This file was deleted.

Oops, something went wrong.
@@ -138,8 +138,8 @@ create table endpoint.template (
create table endpoint.template_route (
id uuid default public.uuid_generate_v4() primary key,
template_id uuid not null references endpoint.template(id),
url_pattern text not null default '',
args text not null default '{}' -- json?
url_pattern text not null default '', -- matching paths may contain arguments from the url to be passed into the template
args text not null default '{}' -- this route's static arguments to be passed into the template
);


@@ -2140,63 +2140,34 @@ create or replace function endpoint.template_render(
url_args json default '{}'
) returns text as $$
// fetch the template
var template;
try {
var template_rows = plv8.execute('select * from endpoint.template where id=$1', [ template_id ]);
var template = template_rows[0];
template = template_rows[0];
plv8.elog(NOTICE, ' template is ' + template.id);
}
catch( e ) {
plv8.elog( ERROR, e, e)
return false;
}
plv8.elog(NOTICE, 'template '+template.name+' called with args '+JSON.stringify(args));
plv8.elog(NOTICE, 'template '+template.name+' called with route_args '+JSON.stringify(route_args)+', url_args'+JSON.stringify(url_args));

// setup javascript scope
var context = {};
for (var key in args) {
context[key] = args[key];
}

// generate a unique id for this template
var id_rows = plv8.execute('select public.uuid_generate_v4() as id');
context.id = id_rows[0].id;
context.name = template.name;
for (var key in route_args) { context[key] = route_args[key]; }
for (var key in url_args) { context[key] = url_args[key]; }

// eval datum-plv8
var datum_rows = plv8.execute("select * from template.dependency_js where name='datum-plv8'");
var datum_rows = plv8.execute("select * from widget.dependency_js where name='datum-plv8'");
eval( datum_rows[0].content )

// run server_js
// vars args and context should be in scope
eval(template.server_js);

// run common_js
eval(template.common_js);

// eval doT.js
var doT_rows = plv8.execute("select * from endpoint.resource where path='/doT.min.js'");
eval(doT_rows[0].content);

// render the template
var htmlTemplate = doT.template(template.html);
var htmlTemplate = doT.template(template.content);
var html = htmlTemplate(context);

/*
// add post_js
html += '<script type="application/javascript">var id=' + context.id + '; var name = '+context.name+';\n'+template.post_js+'</script>';
// add css
var cssTemplate = doT.template(template.css);
var css = cssTemplate(context);
html += '<link rel="stylesheet">'+css+'</link>';
*/

return html;

$$ language plv8;

create table template.template_route (
id uuid not null default public.uuid_generate_v4() primary key,
template_id uuid references template.template(id),
path text not null default '',
args text not null default '{}'
);
@@ -40,9 +40,7 @@ def application(env, start_response):

try:
with map_errors_to_http(), cursor_for_request(request) as cursor:

rowcount = 0

# Text resource
cursor.execute('''
select r.content, m.mimetype
@@ -65,14 +63,29 @@ def application(env, start_response):
binary_resources = cursor.fetchall()
rowcount += cursor.rowcount

# template route
# Template resource
# check for matching route
cursor.execute('''
select
id, array_to_json(regexp_matches(%s, r.url_pattern)), endpoint.render_template(t.id, r.args::json) as content
from endpoint.template_route r
join endpoint.mimetype m on r.mimetype_id = m.id
''', (request.path,))
select array_to_json(regexp_matches(%s, r.url_pattern)) as match from endpoint.template_route r
''', (request.path,))
routes = cursor.fetchall()
logging.info('HEYYYYYY routes: %s' % (routes))
rowcount += cursor.rowcount

# render template only if we don't have other rows
template_resources = None
if routes != None:
cursor.execute('''
select
array_to_json(regexp_matches(%s, r.url_pattern)), endpoint.template_render(t.id, r.args::json, '{}'::json) as content, m.mimetype
from endpoint.template_route r
join endpoint.template t on r.template_id = t.id
join endpoint.mimetype m on t.mimetype_id = m.id
''', (request.path,))
template_resources = cursor.fetchall()
logging.info('HEEEEYYYYYYYY we got a template row. %s' % (template_resources))
else:
logging.info('HEEEEYYYYYYYY NO WE DID NOT GET a row')
# cursor.execute('''
# select
# id, regex_matches(%s, r.url_pattern),
@@ -93,8 +106,8 @@ def application(env, start_response):
row = text_resources[0]
if len(binary_resources) == 1:
row = binary_resources[0]
# if len(widget_resources) == 1:
# row = widget_resources[0]
if len(template_resources) == 1:
row = template_resources[0]


### Commenting out until security can be audited...

0 comments on commit 897db10

Please sign in to comment.