Commit e9495a8f by benjaoming

Merge pull request #345 from cXhristian/attachment-fixes

Fix multiple issues in the attachments plugin, especially for reusing attachments in other articles
parents c7f8ff0e b46ced1c
...@@ -34,7 +34,8 @@ class AttachmentPreprocessor(markdown.preprocessors.Preprocessor): ...@@ -34,7 +34,8 @@ class AttachmentPreprocessor(markdown.preprocessors.Preprocessor):
try: try:
attachment = models.Attachment.objects.get( attachment = models.Attachment.objects.get(
articles__current_revision__deleted=False, articles__current_revision__deleted=False,
id=attachment_id, current_revision__deleted=False id=attachment_id, current_revision__deleted=False,
articles=self.markdown.article
) )
url = reverse('wiki:attachments_download', kwargs={'article_id': self.markdown.article.id, url = reverse('wiki:attachments_download', kwargs={'article_id': self.markdown.article.id,
'attachment_id':attachment.id,}) 'attachment_id':attachment.id,})
...@@ -59,7 +60,8 @@ class AttachmentPreprocessor(markdown.preprocessors.Preprocessor): ...@@ -59,7 +60,8 @@ class AttachmentPreprocessor(markdown.preprocessors.Preprocessor):
})) }))
line = self.markdown.htmlStash.store(html, safe=True) line = self.markdown.htmlStash.store(html, safe=True)
except models.Attachment.DoesNotExist: except models.Attachment.DoesNotExist:
line = line.replace(m.group(1), """<span class="attachment attachment-deleted">Attachment with ID #%s is deleted.</span>""" % attachment_id) html = """<span class="attachment attachment-deleted">Attachment with ID #%s is deleted.</span>""" % attachment_id
line = line.replace(m.group(2), self.markdown.htmlStash.store(html, safe=True))
line = before + line + after line = before + line + after
new_text.append(line) new_text.append(line)
return new_text return new_text
......
...@@ -50,6 +50,7 @@ class AttachmentView(ArticleMixin, FormView): ...@@ -50,6 +50,7 @@ class AttachmentView(ArticleMixin, FormView):
messages.success(self.request, _('Successfully added: %s') % (", ".join([ar.get_filename() for ar in attachment_revision]))) messages.success(self.request, _('Successfully added: %s') % (", ".join([ar.get_filename() for ar in attachment_revision])))
else: else:
messages.success(self.request, _('%s was successfully added.') % attachment_revision.get_filename()) messages.success(self.request, _('%s was successfully added.') % attachment_revision.get_filename())
self.article.clear_cache()
return redirect("wiki:attachments_index", path=self.urlpath.path, article_id=self.article.id) return redirect("wiki:attachments_index", path=self.urlpath.path, article_id=self.article.id)
...@@ -122,6 +123,7 @@ class AttachmentReplaceView(ArticleMixin, FormView): ...@@ -122,6 +123,7 @@ class AttachmentReplaceView(ArticleMixin, FormView):
self.attachment.current_revision = attachment_revision self.attachment.current_revision = attachment_revision
self.attachment.save() self.attachment.save()
messages.success(self.request, _('%s uploaded and replaces old attachment.') % attachment_revision.get_filename()) messages.success(self.request, _('%s uploaded and replaces old attachment.') % attachment_revision.get_filename())
self.article.clear_cache()
except models.IllegalFileExtension as e: except models.IllegalFileExtension as e:
messages.error(self.request, _('Your file could not be saved: %s') % e) messages.error(self.request, _('Your file could not be saved: %s') % e)
return redirect("wiki:attachments_replace", attachment_id=self.attachment.id, return redirect("wiki:attachments_replace", attachment_id=self.attachment.id,
...@@ -203,6 +205,7 @@ class AttachmentChangeRevisionView(ArticleMixin, View): ...@@ -203,6 +205,7 @@ class AttachmentChangeRevisionView(ArticleMixin, View):
def post(self, request, *args, **kwargs): def post(self, request, *args, **kwargs):
self.attachment.current_revision = self.revision self.attachment.current_revision = self.revision
self.attachment.save() self.attachment.save()
self.article.clear_cache()
messages.success(self.request, _('Current revision changed for %s.') % self.attachment.original_filename) messages.success(self.request, _('Current revision changed for %s.') % self.attachment.original_filename)
return redirect("wiki:attachments_index", path=self.urlpath.path, article_id=self.article.id) return redirect("wiki:attachments_index", path=self.urlpath.path, article_id=self.article.id)
...@@ -219,12 +222,15 @@ class AttachmentAddView(ArticleMixin, View): ...@@ -219,12 +222,15 @@ class AttachmentAddView(ArticleMixin, View):
return super(AttachmentAddView, self).dispatch(request, article, *args, **kwargs) return super(AttachmentAddView, self).dispatch(request, article, *args, **kwargs)
def post(self, request, *args, **kwargs): def post(self, request, *args, **kwargs):
if self.attachment.articles.filter(id=self.article.id): if not self.attachment.articles.filter(id=self.article.id):
self.attachment.articles.add(self.article) self.attachment.articles.add(self.article)
self.attachment.save() self.attachment.save()
messages.success(self.request, _('Added a reference to "%(att)s" from "%(art)s".') % self.article.clear_cache()
{'att': self.attachment.original_filename, messages.success(self.request, _('Added a reference to "%(att)s" from "%(art)s".') %
'art': self.article.current_revision.title}) {'att': self.attachment.original_filename,
'art': self.article.current_revision.title})
else:
messages.error(self.request, _('"%(att)s" is already referenced.') % {'att': self.attachment.original_filename})
return redirect("wiki:attachments_index", path=self.urlpath.path, article_id=self.article.id) return redirect("wiki:attachments_index", path=self.urlpath.path, article_id=self.article.id)
...@@ -252,11 +258,12 @@ class AttachmentDeleteView(ArticleMixin, FormView): ...@@ -252,11 +258,12 @@ class AttachmentDeleteView(ArticleMixin, FormView):
revision.save() revision.save()
self.attachment.current_revision = revision self.attachment.current_revision = revision
self.attachment.save() self.attachment.save()
self.article.clear_cache()
messages.info(self.request, _('The file %s was deleted.') % self.attachment.original_filename) messages.info(self.request, _('The file %s was deleted.') % self.attachment.original_filename)
else: else:
self.attachment.articles.remove(self.article) self.attachment.articles.remove(self.article)
messages.info(self.request, _('This article is no longer related to the file %s.') % self.attachment.original_filename) messages.info(self.request, _('This article is no longer related to the file %s.') % self.attachment.original_filename)
self.article.clear_cache()
return redirect("wiki:get", path=self.urlpath.path, article_id=self.article.id) return redirect("wiki:get", path=self.urlpath.path, article_id=self.article.id)
def get_context_data(self, **kwargs): def get_context_data(self, **kwargs):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment