From fb40d08fb130cc0a62afab1b2efce8a6bfcf6450 Mon Sep 17 00:00:00 2001 From: benjaoming Date: Wed, 9 Jan 2013 01:26:12 +0100 Subject: [PATCH] Avoid losing user data when a sidebar form is called and article contents have been modified #33 --- wiki/core/plugins/base.py | 10 +++- wiki/plugins/images/forms.py | 2 +- .../wiki/plugins/images/sidebar.html | 47 ++++++++++++------- .../wiki/includes/editor_sidebar.html | 2 +- wiki/views/article.py | 18 +++++++ 5 files changed, 59 insertions(+), 20 deletions(-) diff --git a/wiki/core/plugins/base.py b/wiki/core/plugins/base.py index 62290b3d..afcab803 100644 --- a/wiki/core/plugins/base.py +++ b/wiki/core/plugins/base.py @@ -9,6 +9,7 @@ from django.utils.translation import ugettext as _ Please have a look in wiki.models.pluginbase to see where to inherit your plugin's models. """ +from django import forms class BasePlugin(object): """Plugins should inherit from this""" @@ -35,8 +36,13 @@ class BasePlugin(object): js = [] css = {} -class PluginSidebarFormMixin(object): - +class PluginSidebarFormMixin(forms.ModelForm): + + unsaved_article_title = forms.CharField(widget=forms.HiddenInput(), + required=True) + unsaved_article_content = forms.CharField(widget=forms.HiddenInput(), + required=False) + def get_usermessage(self): pass diff --git a/wiki/plugins/images/forms.py b/wiki/plugins/images/forms.py index 84c5be6d..dc5f8be7 100644 --- a/wiki/plugins/images/forms.py +++ b/wiki/plugins/images/forms.py @@ -5,7 +5,7 @@ from wiki.core.plugins.base import PluginSidebarFormMixin from wiki.plugins.images import models -class SidebarForm(forms.ModelForm, PluginSidebarFormMixin): +class SidebarForm(PluginSidebarFormMixin): def __init__(self, article, request, *args, **kwargs): self.article = article diff --git a/wiki/plugins/images/templates/wiki/plugins/images/sidebar.html b/wiki/plugins/images/templates/wiki/plugins/images/sidebar.html index 45671989..ef61b76e 100644 --- a/wiki/plugins/images/templates/wiki/plugins/images/sidebar.html +++ b/wiki/plugins/images/templates/wiki/plugins/images/sidebar.html @@ -13,6 +13,16 @@ function insert_image(image_id) { $('#id_content').insertAtCaret(imagetag+'\n\n'); } } + +$(document).ready( function() { + $('.sidebar-form').each(function () { + $(this).submit( function() { + this.unsaved_article_title.value = $('#id_title').val(); + this.unsaved_article_content.value = $('#id_content').val(); + }); + }); +}); + {% endaddtoblock %} @@ -72,22 +82,27 @@ function insert_image(image_id) { {% endfor %} {% endif %} - {% for field in form %} -
- {% if field.label %} - - {% endif %} - {{ field }} - {% if field.errors %} -
- {% for error in field.errors %} -
{{ error }}
- {% endfor %} -
- {% endif %} -
+ {# Include the hidden fields #} + {% for hidden in form.hidden_fields %} + {{ hidden }} + {% endfor %} + + {% for field in form.visible_fields %} +
+ {% if field.label %} + + {% endif %} + {{ field }} + {% if field.errors %} +
+ {% for error in field.errors %} +
{{ error }}
+ {% endfor %} +
+ {% endif %} +
{% if field.help_text %}

{{ field.help_text|safe }}

{% endif %} diff --git a/wiki/templates/wiki/includes/editor_sidebar.html b/wiki/templates/wiki/includes/editor_sidebar.html index 8b55bfc2..10cac65c 100644 --- a/wiki/templates/wiki/includes/editor_sidebar.html +++ b/wiki/templates/wiki/includes/editor_sidebar.html @@ -14,7 +14,7 @@
{% if plugin.sidebar.template %} {% with plugin_form as form and plugin as plugin %} -
+ {% csrf_token %} {% include plugin.sidebar.template %}
diff --git a/wiki/views/article.py b/wiki/views/article.py index 142e9fb9..79ffd59c 100644 --- a/wiki/views/article.py +++ b/wiki/views/article.py @@ -219,6 +219,17 @@ class Edit(FormView, ArticleMixin): self.sidebar = [] return super(Edit, self).dispatch(request, article, *args, **kwargs) + def get_initial(self): + initial = FormView.get_initial(self) + + for field_name in ['title', 'content']: + session_key = 'unsaved_article_%s_%d' % (field_name, self.article.id) + if session_key in self.request.session.keys(): + content = self.request.session[session_key] + initial[field_name] = content + del self.request.session[session_key] + return initial + def get_form(self, form_class): """ Checks from querystring data that the edit form is actually being saved, @@ -266,9 +277,16 @@ class Edit(FormView, ArticleMixin): messages.success(self.request, usermessage) else: messages.success(self.request, _(u'Your changes were saved.')) + + request.session['unsaved_article_title_%d' % self.article.id] = form.cleaned_data['unsaved_article_title'] + request.session['unsaved_article_content_%d' % self.article.id] = form.cleaned_data['unsaved_article_content'] + + messages.warning(request, _('Please note that your article text has not yet been saved!')) + if self.urlpath: return redirect('wiki:edit', path=self.urlpath.path) return redirect('wiki:edit', article_id=self.article.id) + else: form = Form(self.article, self.request) setattr(form, 'form_id', form_id) -- 2.45.2