From 0cbec006b469ff4bc514c61d87f24dc2fcdec661 Mon Sep 17 00:00:00 2001 From: Brenton Durkee Date: Mon, 16 Oct 2017 11:29:57 -0400 Subject: [PATCH 1/2] Adds duplicate button to Template edit form. Adds duplicate model function for Template that gets a Template by id and creates a new Template with the same data. --- lib/models/templates.js | 22 +++++++++++++++++----- routes/templates.js | 13 +++++++++++++ views/templates/edit.hbs | 6 ++++++ 3 files changed, 36 insertions(+), 5 deletions(-) diff --git a/lib/models/templates.js b/lib/models/templates.js index a6723eef..9bebb5b2 100644 --- a/lib/models/templates.js +++ b/lib/models/templates.js @@ -61,15 +61,17 @@ module.exports.create = (template, callback) => { let values = [name]; Object.keys(template).forEach(key => { - let value = template[key].trim(); + let value = template[key]; key = tools.toDbKey(key); + if (!allowedKeys.includes(key)) { + return; + } + value = value.trim(); if (key === 'description') { value = tools.purifyHTML(value); } - if (allowedKeys.indexOf(key) >= 0) { - keys.push(key); - values.push(value); - } + keys.push(key); + values.push(value); }); db.getConnection((err, connection) => { @@ -137,6 +139,16 @@ module.exports.update = (id, updates, callback) => { }); }; +module.exports.duplicate = (id, callback) => { + return this.get(id, (err, template) => { + if (!template) { + return callback(new Error(_('Template does not exist'))); + } + template.name = template.name + ' Copy'; + return this.create(template, callback); + }); +} + module.exports.delete = (id, callback) => { id = Number(id) || 0; diff --git a/routes/templates.js b/routes/templates.js index 3272df52..d91bdb43 100644 --- a/routes/templates.js +++ b/routes/templates.js @@ -136,6 +136,19 @@ router.post('/edit', passport.parseForm, passport.csrfProtection, (req, res) => }); }); +router.post('/duplicate', passport.parseForm, passport.csrfProtection, (req, res) => { + templates.duplicate(req.body.id, (err, duplicated) => { + if (err) { + req.flash('danger', err && err.message || err); + } else if (duplicated) { + req.flash('success', _('Template duplicated')); + } else { + req.flash('info', _('Could not duplicate specified template')); + } + return res.redirect('/templates/edit/' + duplicated); + }); +}); + router.post('/delete', passport.parseForm, passport.csrfProtection, (req, res) => { templates.delete(req.body.id, (err, deleted) => { if (err) { diff --git a/views/templates/edit.hbs b/views/templates/edit.hbs index 704b3a82..59e94d38 100644 --- a/views/templates/edit.hbs +++ b/views/templates/edit.hbs @@ -13,6 +13,11 @@ +
+ + +
+
@@ -45,6 +50,7 @@
+
From 56fedc6ee9fa7af6a77497d614a446170b17eedb Mon Sep 17 00:00:00 2001 From: Brenton Durkee Date: Tue, 17 Oct 2017 19:16:19 -0400 Subject: [PATCH 2/2] Fix lint errors --- lib/models/templates.js | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/lib/models/templates.js b/lib/models/templates.js index 9bebb5b2..b0f0099e 100644 --- a/lib/models/templates.js +++ b/lib/models/templates.js @@ -139,15 +139,16 @@ module.exports.update = (id, updates, callback) => { }); }; -module.exports.duplicate = (id, callback) => { - return this.get(id, (err, template) => { - if (!template) { - return callback(new Error(_('Template does not exist'))); - } - template.name = template.name + ' Copy'; - return this.create(template, callback); - }); -} +module.exports.duplicate = (id, callback) => module.exports.get(id, (err, template) => { + if (err) { + return callback(err); + } + if (!template) { + return callback(new Error(_('Template does not exist'))); + } + template.name = template.name + ' Copy'; + return module.exports.create(template, callback); +}); module.exports.delete = (id, callback) => { id = Number(id) || 0;