* Added stop button to export/import features, cancelling a previously started job

* Added some messages to the export/import features
 * Added routine to remove the corresponding file after successful import
This commit is contained in:
Tobi Schäfer 2010-04-24 12:28:29 +00:00
parent 081d601cea
commit 01b15b36f0
3 changed files with 52 additions and 35 deletions

View file

@ -30,9 +30,9 @@ var Importer = {}
Importer.run = function(site, user) { Importer.run = function(site, user) {
try { try {
var file = File.getById(site.import_id); var xml = File.getById(site.import_id);
if (file) { if (xml) {
file = new java.io.File(file.getFile()); var file = new java.io.File(xml.getFile());
var reader = new rome.XmlReader(file); var reader = new rome.XmlReader(file);
var input = new rome.SyndFeedInput(true); var input = new rome.SyndFeedInput(true);
var feed = input.build(reader); var feed = input.build(reader);
@ -55,6 +55,7 @@ Importer.run = function(site, user) {
}); });
site.stories.add(story); site.stories.add(story);
} }
File.remove.call(xml);
} }
} catch (ex) { } catch (ex) {
app.log(ex); app.log(ex);

View file

@ -120,11 +120,13 @@ referrers.push(new Antville.Referrer("<% param.referrer %>",
<% #export %> <% #export %>
<p class="storyTitle"><% gettext "Export Site Data" %></p> <p class="storyTitle"><% gettext "Export Site Data" %></p>
<div class="ample"> <div class="ample">
<p><% if <% file.self %> is null then '' else <% gettext "Download file {0}." <p><% if <% param.status %> is null then <% if <% file.self %> is null then '' else <% gettext "Download file {0}."
<% file.skin File#main %> '<small>' <% file.skin File#main %> '<small>'
<% file.created | format short %> '</small>' %> %></p> <% file.created | format short %> '</small>' %> %> else <% param.status %> %></p>
<form action="" method="post"> <form action="" method="post">
<button type="submit" name="submit" value="export"><% gettext Export %></button> <button type="submit" name="submit" value="<% if <% param.status %> is null then start else stop %>">
<% if <% param.status %> is null then <% gettext Start %> else <% gettext Stop %> %>
</button>
<a href="" class="cancel"><% gettext Cancel %></a> <a href="" class="cancel"><% gettext Cancel %></a>
</form> </form>
</div> </div>
@ -132,12 +134,14 @@ referrers.push(new Antville.Referrer("<% param.referrer %>",
<% #import %> <% #import %>
<p class="storyTitle"><% gettext 'Import Site Data' %></p> <p class="storyTitle"><% gettext 'Import Site Data' %></p>
<div class="ample"> <div class="ample">
<p><% if <% file.self %> is null then '' else <% gettext "Queued file {0} for import." <p><% if <% file.self %> is null then '' else <% gettext "The site is scheduled for importing the file {0}. The imported site data will be available within 24 hours."
<% file.skin File#main %> '</a>' '<small>' <% file.skin File#main %> '</a>' '<small>'
<% file.created | format short %> '</small>' %> %></p> <% file.created | format short %> '</small>' %> %></p>
<form method="post" enctype="multipart/form-data"> <form method="post" enctype="multipart/form-data">
<p><input type="file" name="file" /></p><br /> <% if <% file.self %> is null then '<p><input type="file" name="file" /></p><br />' %>
<button type="submit" name="submit" value="import"><% gettext Import %></button> <button type="submit" name="submit" value="<% if <% file.self %> is null then start else stop %>">
<% if <% file.self %> is null then <% gettext Start %> else <% gettext Stop %> %>
</button>
<a href="" class="cancel"><% gettext Cancel %></a> <a href="" class="cancel"><% gettext Cancel %></a>
</form> </form>
</div> </div>

View file

@ -643,17 +643,18 @@ Site.prototype.unsubscribe_action = function() {
} }
Site.prototype.export_action = function() { Site.prototype.export_action = function() {
if (req.postParams.submit === "export") { var job = this.job && new Admin.Job(this.job);
var data = req.postParams;
if (data.submit === "start") {
try { try {
if (!this.job) { if (!job) {
this.job = Admin.queue(this, "export"); this.job = Admin.queue(this, "export");
res.message = gettext("Site is queued for export."); res.message = gettext("Site is scheduled for export.");
} else { } else {
var job = new Admin.Job(this.job); if (job.method !== "export") {
if (job.method === "export") { throw Error(gettext("There is already another job queued for this site: {0}",
throw Error(gettext("Site is already being exported.")); job.method));
} else if (job.method) {
throw Error(gettext("There is already another job queued for this site: {0}", job.method));
} }
} }
} catch (ex) { } catch (ex) {
@ -661,33 +662,31 @@ Site.prototype.export_action = function() {
app.log(res.message); app.log(res.message);
} }
res.redirect(this.href(req.action)); res.redirect(this.href(req.action));
} else if (data.submit === "stop") {
job && job.remove();
this.job = null;
res.redirect(this.href(req.action));
} }
var param = {
status: (job && job.method === "export") ?
gettext("The site is scheduled for export. Exported site data will be available for download from here within 24 hours.") :
null
}
res.handlers.file = File.getById(this.export_id) || {}; res.handlers.file = File.getById(this.export_id) || {};
res.data.body = this.renderSkinAsString("$Site#export"); res.data.body = this.renderSkinAsString("$Site#export", param);
this.renderSkin("Site#page"); this.renderSkin("Site#page");
return; return;
} }
Site.prototype.import_action = function() { Site.prototype.import_action = function() {
var job = this.job && new Admin.Job(this.job);
var file = this.import_id && File.getById(this.import_id);
var data = req.postParams; var data = req.postParams;
if (data.submit === "import") { if (data.submit === "start") {
try { try {
if (!data.file) { if (job) {
throw Error(gettext("Please choose a ZIP file to import."));
}
var file;
if (this.import_id && (file = File.getById(this.import_id))) {
File.remove.call(file);
}
data.file_origin = data.file.name;
file = new File;
file.site = this;
file.update(data);
this.files.add(file);
file.creator = session.user;
if (this.job) {
var job = new Admin.Job(this.job);
if (job.method === "import") { if (job.method === "import") {
job.remove(); job.remove();
this.job = null; this.job = null;
@ -696,14 +695,27 @@ Site.prototype.import_action = function() {
job.method)); job.method));
} }
} }
file && File.remove.call(file);
data.file_origin = data.file.name;
file = new File;
file.site = this;
file.update(data);
this.files.add(file);
file.creator = session.user;
this.job = Admin.queue(this, "import"); this.job = Admin.queue(this, "import");
this.import_id = file._id; this.import_id = file._id;
res.message = gettext("Site is queued for import."); res.message = gettext("Site is scheduled for import.");
res.redirect(this.href(req.action)); res.redirect(this.href(req.action));
} catch (ex) { } catch (ex) {
res.message = ex.toString(); res.message = ex.toString();
app.log(res.message); app.log(res.message);
} }
} else if (data.submit === "stop") {
file && File.remove.call(file);
job && job.remove();
this.job = null;
this.import_id = null;
res.redirect(this.href(req.action));
} }
res.handlers.file = File.getById(this.import_id) || {}; res.handlers.file = File.getById(this.import_id) || {};