Skip to content
This repository has been archived by the owner on Aug 26, 2022. It is now read-only.

Commit

Permalink
bug 1260197 - handle spam and midair collision error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
dchukhin committed Aug 22, 2016
1 parent 889e145 commit b4e5f64
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 18 deletions.
37 changes: 23 additions & 14 deletions kuma/static/js/wiki-edit.js
Expand Up @@ -496,7 +496,6 @@
var formURL = window.location.href; // submits to self
console.log('posting this data: ', formData)

// ajax submit
$.ajax({
url : formURL + '?async',
type: "POST",
Expand Down Expand Up @@ -528,16 +527,22 @@
// We also need to update the form's current_rev to
// avoid triggering a conflict, since we just saved in
// the background.
var responseRevision = JSON.parse(data)['new_revision_id'];
console.log('responseRevision: ', responseRevision);
// TODO: why are there multiple <input id="id_current_rev">s?
$("input[id=id_current_rev]").val(responseRevision)

// Clear the review comment
$('#id_comment').val('');

// Trigger a `mdn:save-success` event so dirtiness can be reset throughout the page
$form.trigger('mdn:save-success');
console.log('data is: ', data)
var responseData = JSON.parse(data)
if (responseData['error'] == true) {
saveNotification.error(responseData['error_message'])
} else {
var responseRevision = JSON.parse(data)['new_revision_id'];
console.log('responseRevision: ', responseRevision);
// TODO: why are there multiple <input id="id_current_rev">s?
$("input[id=id_current_rev]").val(responseRevision)

// Clear the review comment
$('#id_comment').val('');

// Trigger a `mdn:save-success` event so dirtiness can be reset throughout the page
$form.trigger('mdn:save-success');
}

// Re-enable the form; it gets disabled to prevent double-POSTs
$form.data('disabled', false).removeClass('disabled');
Expand All @@ -549,9 +554,13 @@
// console.log(jqXHR.status);
// console.log(textStatus);
// console.log(errorThrown);
var msg = "Publishing failed. Please copy and paste your changes into a safe place and try submitting the form using the 'Publish' button.";

saveNotification.error(msg);
// Try to display the error that comes back from the server
try {
var errorMessage = JSON.parse(jqXHR['responseText'])['error_message']
} catch (err) {
errorMessage = "Publishing failed. Please copy and paste your changes into a safe place and try submitting the form using the 'Publish' button.";
}
saveNotification.error(errorMessage);
// saveNotification.error(msg, {closable: true, duration: 0});

// save draft
Expand Down
19 changes: 15 additions & 4 deletions kuma/wiki/forms.py
Expand Up @@ -17,6 +17,7 @@

import kuma.wiki.content
from kuma.core.form_fields import StrippedCharField
from kuma.core.urlresolvers import reverse
from kuma.spam.akismet import AkismetError
from kuma.spam.forms import AkismetCheckFormMixin, AkismetSubmissionFormMixin

Expand Down Expand Up @@ -64,8 +65,10 @@
SLUG_COLLIDES = _(u'Another document with this slug already exists.')
OTHER_COLLIDES = _(u'Another document with this metadata already exists.')

MIDAIR_COLLISION = _(u'This document was modified while you were '
u'editing it.')
MIDAIR_COLLISION = _(u'Publishing failed. Conflicting edit attempts detected. '
u'Please copy and paste your edits to a safe place and '
u'visit the <a href="%(url)s">revision history</a> page '
u'to see what was changed before making further edits.')
MOVE_REQUIRED = _(u"Changing this document's slug requires "
u"moving it and its children.")

Expand Down Expand Up @@ -659,13 +662,21 @@ def clean_current_rev(self):
if orig_ct != curr_ct:
# Oops. Looks like the section did actually get
# changed, so yeah this is a collision.
raise forms.ValidationError(MIDAIR_COLLISION)
url = reverse(
'wiki.document_revisions',
kwargs={'document_path': self.instance.document.slug}
)
raise forms.ValidationError(MIDAIR_COLLISION % {'url': url})

return current_rev

else:
# No section edit, so this is a flat-out collision.
raise forms.ValidationError(MIDAIR_COLLISION)
url = reverse(
'wiki.document_revisions',
kwargs={'document_path': self.instance.document.slug}
)
raise forms.ValidationError(MIDAIR_COLLISION % {'url': url})

except Document.DoesNotExist:
# If there's no document yet, just bail.
Expand Down
8 changes: 8 additions & 0 deletions kuma/wiki/views/edit.py
Expand Up @@ -208,6 +208,14 @@ def edit(request, document_slug, document_locale, revision_id=None):
if not rev_form.is_valid():
# Was there a mid-air collision?
if 'current_rev' in rev_form._errors:
# If this was an Ajax POST, then return a JSONReponse
if is_async_submit:
data = {
"error": True,
"error_message": rev_form.errors['current_rev'],
"new_revision_id": curr_rev.id,
}
return JsonResponse(data=data, status=500)
# Jump out to a function to escape indentation hell
return _edit_document_collision(
request, orig_rev, curr_rev, is_async_submit,
Expand Down

0 comments on commit b4e5f64

Please sign in to comment.