Use default Nunjucks template if necessary.

If the survey configuration does not itself have a template
we will use the one in etc/defaults/templates/sequence.html.njk.

It is not very likely that the template will be changed all that
often and it avoids issues when people forget to copy it across
to a new survey, etc.
This commit is contained in:
D. Berge
2021-05-20 20:38:39 +02:00
parent e7661bfd1c
commit 39d85a692b
2 changed files with 9 additions and 4 deletions

View File

@@ -2,14 +2,17 @@ const { configuration } = require('../../../../lib/db');
const { transform, prepare } = require('../../../../lib/sse'); const { transform, prepare } = require('../../../../lib/sse');
const render = require('../../../../lib/render'); const render = require('../../../../lib/render');
// FIXME Refactor when able
const defaultTemplatePath = require('path').resolve(__dirname, "../../../../../../../etc/default/templates/sequence.html.njk");
const html = async function (req, res, next) { const html = async function (req, res, next) {
try { try {
const query = req.query; const query = req.query;
query.sequence = req.params.sequence; query.sequence = req.params.sequence;
const {events, sequences} = await prepare(req.params.project, query); const {events, sequences} = await prepare(req.params.project, query);
const seis = transform(events, sequences, {projectId: req.params.project}); const seis = transform(events, sequences, {projectId: req.params.project});
const templates = await configuration.get(req.params.project, "sse/templates"); const template = (await configuration.get(req.params.project, "sse/templates/0/template")) || defaultTemplatePath;
const template = templates[0].template; console.log("TEMPLATE", template);
const response = await render(seis, template); const response = await render(seis, template);

View File

@@ -6,6 +6,9 @@ const { transform, prepare } = require('../../../../lib/sse');
const render = require('../../../../lib/render'); const render = require('../../../../lib/render');
const { url2pdf } = require('../../../../lib/selenium'); const { url2pdf } = require('../../../../lib/selenium');
// FIXME Refactor when able
const defaultTemplatePath = require('path').resolve(__dirname, "../../../../../../../etc/default/templates/sequence.html.njk");
function tmpname (tmpdir="/dev/shm") { function tmpname (tmpdir="/dev/shm") {
return Path.join(tmpdir, crypto.randomBytes(16).toString('hex')+".tmp"); return Path.join(tmpdir, crypto.randomBytes(16).toString('hex')+".tmp");
} }
@@ -17,8 +20,7 @@ const pdf = async function (req, res, next) {
query.sequence = req.params.sequence; query.sequence = req.params.sequence;
const {events, sequences} = await prepare(req.params.project, query); const {events, sequences} = await prepare(req.params.project, query);
const seis = transform(events, sequences, {projectId: req.params.project}); const seis = transform(events, sequences, {projectId: req.params.project});
const templates = await configuration.get(req.params.project, "sse/templates"); const template = (await configuration.get(req.params.project, "sse/templates/0/template")) || defaultTemplatePath;
const template = templates[0].template;
const html = await render(seis, template); const html = await render(seis, template);