Commit 0c148d35 by benjaoming

make possible for moderators to replace attachments

parent 8f65dd2e
......@@ -18,6 +18,12 @@ class AttachmentForm(forms.ModelForm):
required=False
)
def __init__(self, *args, **kwargs):
self.article = kwargs.pop('article', None)
self.request = kwargs.pop('request', None)
self.attachment = kwargs.pop('attachment', None)
super(AttachmentForm, self).__init__(*args, **kwargs)
def clean_file(self):
uploaded_file = self.cleaned_data.get('file', None)
if uploaded_file:
......@@ -27,32 +33,43 @@ class AttachmentForm(forms.ModelForm):
raise forms.ValidationError(e)
return uploaded_file
def __init__(self, *args, **kwargs):
self.article = kwargs.pop('article', None)
self.request = kwargs.pop('request', None)
super(AttachmentForm, self).__init__(*args, **kwargs)
def save(self, *args, **kwargs):
commit = kwargs.get('commit', True)
attachment_revision = super(AttachmentForm, self).save(commit=False)
# Added because of AttachmentArchiveForm removing file from fields
# should be more elegant
attachment_revision.file = self.cleaned_data['file']
attachment = models.Attachment()
attachment.article = self.article
attachment.original_filename = attachment_revision.get_filename()
attachment.save()
attachment.articles.add(self.article)
if not self.attachment:
attachment = models.Attachment()
attachment.article = self.article
attachment.original_filename = attachment_revision.get_filename()
if commit:
attachment.save()
attachment.articles.add(self.article)
else:
attachment = self.attachment
attachment_revision.attachment = attachment
attachment_revision.set_from_request(self.request)
attachment_revision.save()
if commit:
attachment_revision.save()
return attachment_revision
class Meta:
model = models.AttachmentRevision
fields = ('file', 'description',)
class AttachmentReplaceForm(AttachmentForm):
replace = forms.BooleanField(
label=_(u'Remove previous'),
help_text=_(u'Remove previous attachment revisions and their files (to '
'save space)?'),
required=False,
)
class AttachmentArchiveForm(AttachmentForm):
file = forms.FileField( #@ReservedAssignment
......
......@@ -19,11 +19,12 @@ class Attachment(ReusablePlugin):
objects = managers.ArticleFkManager()
current_revision = models.OneToOneField('AttachmentRevision',
verbose_name=_(u'current revision'),
blank=True, null=True, related_name='current_set',
help_text=_(u'The revision of this attachment currently in use (on all articles using the attachment)'),
)
current_revision = models.OneToOneField(
'AttachmentRevision',
verbose_name=_(u'current revision'),
blank=True, null=True, related_name='current_set',
help_text=_(u'The revision of this attachment currently in use (on all articles using the attachment)'),
)
original_filename = models.CharField(max_length=256, verbose_name=_(u'original filename'), blank=True, null=True)
......@@ -147,6 +148,7 @@ class AttachmentRevision(BaseRevisionMixin, models.Model):
self.attachment.original_filename,
self.revision_number)
def on_revision_delete(instance, *args, **kwargs):
if not instance.file:
return
......
......@@ -95,16 +95,25 @@ class AttachmentReplaceView(ArticleMixin, FormView):
if request.user.is_anonymous() and not settings.ANONYMOUS:
return response_forbidden(request, article, kwargs.get('urlpath', None))
if article.can_moderate(request.user):
self.attachment = get_object_or_404(models.Attachment, id=attachment_id, articles=article)
self.attachment = get_object_or_404(models.Attachment,
id=attachment_id, articles=article)
self.can_moderate = True
else:
self.attachment = get_object_or_404(models.Attachment.objects.active(), id=attachment_id, articles=article)
self.attachment = get_object_or_404(models.Attachment.objects.active(),
id=attachment_id, articles=article)
self.can_moderate = False
return super(AttachmentReplaceView, self).dispatch(request, article, *args, **kwargs)
def get_form_class(self):
if self.can_moderate:
return forms.AttachmentReplaceForm
else:
return forms.AttachmentForm
def form_valid(self, form):
try:
attachment_revision = form.save(commit=False)
attachment_revision.attachment = self.attachment
attachment_revision = form.save(commit=True)
attachment_revision.set_from_request(self.request)
attachment_revision.previous_revision = self.attachment.current_revision
attachment_revision.save()
......@@ -115,10 +124,15 @@ class AttachmentReplaceView(ArticleMixin, FormView):
messages.error(self.request, _(u'Your file could not be saved: %s') % e)
return redirect("wiki:attachments_replace", attachment_id=self.attachment.id,
path=self.urlpath.path, article_id=self.article.id)
except Exception:
messages.error(self.request, _(u'Your file could not be saved, probably because of a permission error on the web server.'))
return redirect("wiki:attachments_replace", attachment_id=self.attachment.id,
path=self.urlpath.path, article_id=self.article.id)
if self.can_moderate:
older_revisions = self.attachment.attachmentrevision_set.exclude(
id=attachment_revision.id,
created__lte=attachment_revision.created,
)
# Because of signalling, the files are automatically removed...
older_revisions.delete()
return redirect("wiki:attachments_index", path=self.urlpath.path, article_id=self.article.id)
......@@ -127,6 +141,13 @@ class AttachmentReplaceView(ArticleMixin, FormView):
form.fields['file'].help_text = _(u'Your new file will automatically be renamed to match the file already present. Files with different extensions are not allowed.')
return form
def get_form_kwargs(self):
kwargs = super(AttachmentReplaceView, self).get_form_kwargs()
kwargs['article'] = self.article
kwargs['request'] = self.request
kwargs['attachment'] = self.attachment
return kwargs
def get_initial(self, **kwargs):
return {'description': self.attachment.current_revision.description}
......@@ -135,6 +156,7 @@ class AttachmentReplaceView(ArticleMixin, FormView):
kwargs['selected_tab'] = 'attachments'
return super(AttachmentReplaceView, self).get_context_data(**kwargs)
class AttachmentDownloadView(ArticleMixin, View):
@method_decorator(get_article(can_read=True))
......
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