From 90e20e0cfff513ddae668a0705701832a6db054f Mon Sep 17 00:00:00 2001 From: Benjamin Bach Date: Tue, 10 May 2016 14:16:47 +0200 Subject: [PATCH] Image soft deletion was out of order, fixes #533 --- wiki/plugins/images/markdown_extensions.py | 1 - wiki/plugins/images/models.py | 7 ++++++- .../images/templates/wiki/plugins/images/index.html | 4 ++-- wiki/plugins/images/views.py | 13 +++++++++++++ wiki/plugins/images/wiki_plugin.py | 2 +- 5 files changed, 22 insertions(+), 5 deletions(-) diff --git a/wiki/plugins/images/markdown_extensions.py b/wiki/plugins/images/markdown_extensions.py index d896ae89..180219ef 100644 --- a/wiki/plugins/images/markdown_extensions.py +++ b/wiki/plugins/images/markdown_extensions.py @@ -12,7 +12,6 @@ IMAGE_RE = re.compile( re.IGNORECASE) - class ImageExtension(markdown.Extension): """ Images plugin markdown extension for django-wiki. """ diff --git a/wiki/plugins/images/models.py b/wiki/plugins/images/models.py index 215a2344..d70692d2 100644 --- a/wiki/plugins/images/models.py +++ b/wiki/plugins/images/models.py @@ -91,8 +91,13 @@ class ImageRevision(RevisionPluginRevision): """ Inherit certain properties from predecessor because it's very convenient. Remember to always call this method before - setting properties :)""" + setting properties :) + + A revision may not have a predecessor if the property is unset, it may + be unset if it's the initial history entry. + """ predecessor = image.current_revision.imagerevision + super(ImageRevision, self).inherit_predecessor(image) self.plugin = predecessor.plugin self.deleted = predecessor.deleted self.locked = predecessor.locked diff --git a/wiki/plugins/images/templates/wiki/plugins/images/index.html b/wiki/plugins/images/templates/wiki/plugins/images/index.html index 319af312..191d5533 100644 --- a/wiki/plugins/images/templates/wiki/plugins/images/index.html +++ b/wiki/plugins/images/templates/wiki/plugins/images/index.html @@ -36,7 +36,7 @@ {% trans "Upload and publish new image" %}
- + {% trans "Restore image" %} {% else %} @@ -84,7 +84,7 @@ {{ old_revision.imagerevision.width }}x{{ old_revision.imagerevision.height }} {% if image|can_write:user and old_revision != image.current_revision %} - + {% trans "Revert to this version" %} diff --git a/wiki/plugins/images/views.py b/wiki/plugins/images/views.py index cfe88980..6199ef02 100644 --- a/wiki/plugins/images/views.py +++ b/wiki/plugins/images/views.py @@ -1,5 +1,7 @@ from __future__ import absolute_import, unicode_literals +import logging + from django.contrib import messages from django.core.urlresolvers import reverse from django.shortcuts import get_object_or_404, redirect @@ -10,9 +12,12 @@ from django.views.generic.edit import FormView from django.views.generic.list import ListView from wiki.conf import settings as wiki_settings from wiki.decorators import get_article +from wiki.models.pluginbase import RevisionPluginRevision from wiki.plugins.images import forms, models from wiki.views.mixins import ArticleMixin +logger = logging.getLogger(__name__) + class ImageView(ArticleMixin, ListView): @@ -60,9 +65,17 @@ class DeleteView(ArticleMixin, RedirectView): def get_redirect_url(self, **kwargs): + if not self.image.current_revision: + logger.critical('Encountered an image without current revision set, ID: {}'.format(self.image.id)) + latest_revision = RevisionPluginRevision.objects.filter( + plugin=self.image + ).latest('pk') + self.image.current_revision = latest_revision + new_revision = models.ImageRevision() new_revision.inherit_predecessor(self.image) new_revision.set_from_request(self.request) + new_revision.revision_number = RevisionPluginRevision.objects.filter(plugin=self.image).count() new_revision.deleted = not self.restore new_revision.save() self.image.current_revision = new_revision diff --git a/wiki/plugins/images/wiki_plugin.py b/wiki/plugins/images/wiki_plugin.py index 6fa5549e..e4e5c27c 100644 --- a/wiki/plugins/images/wiki_plugin.py +++ b/wiki/plugins/images/wiki_plugin.py @@ -60,7 +60,7 @@ class ImagePlugin(BasePlugin): name='images_purge'), url('^(?P\d+)/revision/change/(?P\d+)/$', views.RevisionChangeView.as_view(), - name='images_restore'), + name='images_set_revision'), url('^(?P\d+)/revision/add/$', views.RevisionAddView.as_view(), name='images_add_revision'), -- 2.45.2