Commit 354c6b23 by benjaoming

Customizable storage backend for attachments. Proper error handling for illegal…

Customizable storage backend for attachments. Proper error handling for illegal file types on replace view.
parent 72165849
...@@ -73,7 +73,8 @@ class AttachmentRevision(BaseRevisionMixin, models.Model): ...@@ -73,7 +73,8 @@ class AttachmentRevision(BaseRevisionMixin, models.Model):
attachment = models.ForeignKey('Attachment') attachment = models.ForeignKey('Attachment')
file = models.FileField(upload_to=upload_path, #@ReservedAssignment file = models.FileField(upload_to=upload_path, #@ReservedAssignment
verbose_name=_(u'file')) verbose_name=_(u'file'),
storage=settings.STORAGE_BACKEND)
description = models.TextField(blank=True) description = models.TextField(blank=True)
......
...@@ -29,3 +29,5 @@ UPLOAD_PATH_OBSCURIFY = getattr(django_settings, 'WIKI_UPLOAD_PATH_OBSCURIFY', T ...@@ -29,3 +29,5 @@ UPLOAD_PATH_OBSCURIFY = getattr(django_settings, 'WIKI_UPLOAD_PATH_OBSCURIFY', T
# to allow. For your own safety. # to allow. For your own safety.
FILE_EXTENSIONS = getattr(django_settings, 'WIKI_FILE_EXTENSIONS', ['pdf', 'doc', 'odt', 'docx', 'txt']) FILE_EXTENSIONS = getattr(django_settings, 'WIKI_FILE_EXTENSIONS', ['pdf', 'doc', 'odt', 'docx', 'txt'])
from django.core.files.storage import default_storage
STORAGE_BACKEND = getattr(django_settings, 'WIKI_STORAGE_BACKEND', default_storage)
\ No newline at end of file
...@@ -34,7 +34,7 @@ class AttachmentView(ArticleMixin, FormView): ...@@ -34,7 +34,7 @@ class AttachmentView(ArticleMixin, FormView):
return super(AttachmentView, self).dispatch(request, article, *args, **kwargs) return super(AttachmentView, self).dispatch(request, article, *args, **kwargs)
# WARNING! The below decorator silences other exceptions that may occur! # WARNING! The below decorator silences other exceptions that may occur!
#@transaction.commit_manually @transaction.commit_manually
def form_valid(self, form): def form_valid(self, form):
if self.request.user.is_anonymous() and not settings.ANONYMOUS: if self.request.user.is_anonymous() and not settings.ANONYMOUS:
return redirect(django_settings.LOGIN_URL) return redirect(django_settings.LOGIN_URL)
...@@ -53,11 +53,11 @@ class AttachmentView(ArticleMixin, FormView): ...@@ -53,11 +53,11 @@ class AttachmentView(ArticleMixin, FormView):
except models.IllegalFileExtension, e: except models.IllegalFileExtension, e:
transaction.rollback() transaction.rollback()
messages.error(self.request, _(u'Your file could not be saved: %s') % e) messages.error(self.request, _(u'Your file could not be saved: %s') % e)
#except Exception: except Exception:
# transaction.rollback() transaction.rollback()
# messages.error(self.request, _(u'Your file could not be saved, probably because of a permission error on the web server.')) messages.error(self.request, _(u'Your file could not be saved, probably because of a permission error on the web server.'))
#transaction.commit() transaction.commit()
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)
def get_context_data(self, **kwargs): def get_context_data(self, **kwargs):
...@@ -99,17 +99,32 @@ class AttachmentReplaceView(ArticleMixin, FormView): ...@@ -99,17 +99,32 @@ class AttachmentReplaceView(ArticleMixin, FormView):
return redirect(wiki_settings.LOGIN_URL) return redirect(wiki_settings.LOGIN_URL)
return super(AttachmentReplaceView, self).dispatch(request, article, *args, **kwargs) return super(AttachmentReplaceView, self).dispatch(request, article, *args, **kwargs)
@transaction.commit_manually
def form_valid(self, form): def form_valid(self, form):
attachment_revision = form.save(commit=False) try:
attachment_revision.attachment = self.attachment attachment_revision = form.save(commit=False)
attachment_revision.set_from_request(self.request) attachment_revision.attachment = self.attachment
attachment_revision.previous_revision = self.attachment.current_revision attachment_revision.set_from_request(self.request)
attachment_revision.save() attachment_revision.previous_revision = self.attachment.current_revision
self.attachment.current_revision = attachment_revision attachment_revision.save()
self.attachment.save() self.attachment.current_revision = attachment_revision
self.attachment.save()
messages.success(self.request, _(u'%s uploaded and replaces old attachment.') % attachment_revision.get_filename())
except models.IllegalFileExtension, e:
transaction.rollback()
messages.error(self.request, _(u'Your file could not be saved: %s') % e)
transaction.commit()
return redirect("wiki:attachments_replace", attachment_id=self.attachment.id,
path=self.urlpath.path, article_id=self.article.id)
except Exception:
transaction.rollback()
messages.error(self.request, _(u'Your file could not be saved, probably because of a permission error on the web server.'))
transaction.commit()
return redirect("wiki:attachments_replace", attachment_id=self.attachment.id,
path=self.urlpath.path, article_id=self.article.id)
messages.success(self.request, _(u'%s uploaded and replaces old attachment.') % attachment_revision.get_filename()) transaction.commit()
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)
def get_form(self, form_class): def get_form(self, form_class):
......
...@@ -14,9 +14,9 @@ def upload_path(instance, filename): ...@@ -14,9 +14,9 @@ def upload_path(instance, filename):
from os import path from os import path
# Has to match original extension filename # Has to match original extension filename
upload_path = settings.UPLOAD_PATH upload_path = settings.IMAGE_PATH
upload_path = upload_path.replace('%aid', str(instance.plugin.image.article.id)) upload_path = upload_path.replace('%aid', str(instance.plugin.image.article.id))
if settings.UPLOAD_PATH_OBSCURIFY: if settings.IMAGE_PATH_OBSCURIFY:
import random, hashlib import random, hashlib
m=hashlib.md5(str(random.randint(0,100000000000000))) m=hashlib.md5(str(random.randint(0,100000000000000)))
upload_path = path.join(upload_path, m.hexdigest()) upload_path = path.join(upload_path, m.hexdigest())
......
from django.conf import settings as django_settings from django.conf import settings as django_settings
# Where to store images # Where to store images
UPLOAD_PATH = getattr(django_settings, 'WIKI_IMAGE_PATH', "wiki/images/%aid/") IMAGE_PATH = getattr(django_settings, 'WIKI_IMAGE_PATH', "wiki/images/%aid/")
# Should the upload path be obscurified? If so, a random hash will be added to the path # Should the upload path be obscurified? If so, a random hash will be added to the path
# such that someone can not guess the location of files (if you have # such that someone can not guess the location of files (if you have
# restricted permissions and the files are still located within the web server's # restricted permissions and the files are still located within the web server's
UPLOAD_PATH_OBSCURIFY = getattr(django_settings, 'WIKI_UPLOAD_PATH_OBSCURIFY', True) IMAGE_PATH_OBSCURIFY = getattr(django_settings, 'WIKI_IMAGE_PATH_OBSCURIFY', True)
# Allow anonymous users to upload (not nice on an open network) # Allow anonymous users to upload (not nice on an open network)
ANONYMOUS = getattr(django_settings, 'WIKI_ATTACHMENTS_ANONYMOUS', False) ANONYMOUS = getattr(django_settings, 'WIKI_ATTACHMENTS_ANONYMOUS', False)
......
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