~netlandish/django-wiki

fb40d08fb130cc0a62afab1b2efce8a6bfcf6450 — benjaoming 11 years ago b9e95ef
Avoid losing user data when a sidebar form is called and article contents have been modified #33
M wiki/core/plugins/base.py => wiki/core/plugins/base.py +8 -2
@@ 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


M wiki/plugins/images/forms.py => wiki/plugins/images/forms.py +1 -1
@@ 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

M wiki/plugins/images/templates/wiki/plugins/images/sidebar.html => wiki/plugins/images/templates/wiki/plugins/images/sidebar.html +31 -16
@@ 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();
    });
  });
});

</script>
{% endaddtoblock %}



@@ 72,22 82,27 @@ function insert_image(image_id) {
    {% endfor %}
  {% endif %}

  {% for field in form %}
  <fieldset id="div_{{ field.auto_id }}" class="control-group fields {% if field.errors %} error{% endif %}">
    {% if field.label %}
      <!--<label for="{{ field.id_for_label }}" class="{% if field.field.required %}requiredField{% endif %}">
      {{ field.label|safe }}
      </label>-->
    {% endif %}
    {{ field }}
    {% if field.errors %}
    <div id="error_{{ forloop.counter }}_{{ field.auto_id }}" class="help-block">
      {% for error in field.errors %}
      <div>{{ error }}</div>
      {% endfor %}
    </div>
    {% endif %}
  </fieldset>
  {# Include the hidden fields #}
  {% for hidden in form.hidden_fields %}
    {{ hidden }}
  {% endfor %}

  {% for field in form.visible_fields %}
    <fieldset id="div_{{ field.auto_id }}" class="control-group fields {% if field.errors %} error{% endif %}">
      {% if field.label %}
        <!--<label for="{{ field.id_for_label }}" class="{% if field.field.required %}requiredField{% endif %}">
        {{ field.label|safe }}
        </label>-->
      {% endif %}
      {{ field }}
      {% if field.errors %}
      <div id="error_{{ forloop.counter }}_{{ field.auto_id }}" class="help-block">
        {% for error in field.errors %}
        <div>{{ error }}</div>
        {% endfor %}
      </div>
      {% endif %}
    </fieldset>
    {% if field.help_text %}
      <p id="hint_{{ field.auto_id }}" class="help-block">{{ field.help_text|safe }}</p>
    {% endif %}

M wiki/templates/wiki/includes/editor_sidebar.html => wiki/templates/wiki/includes/editor_sidebar.html +1 -1
@@ 14,7 14,7 @@
      <div class="accordion-inner form-vertical">
        {% if plugin.sidebar.template %}
          {% with plugin_form as form and plugin as plugin %}
            <form method="POST" class="form-horizontal" action="?f={{ plugin_form.form_id }}" enctype="multipart/form-data">
            <form method="POST" class="form-horizontal sidebar-form" action="?f={{ plugin_form.form_id }}" enctype="multipart/form-data">
              {% csrf_token %}
              {% include plugin.sidebar.template %}
            </form>

M wiki/views/article.py => wiki/views/article.py +18 -0
@@ 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)