From 2512cf7bdd788ee0579384f76132a8ccb671f125 Mon Sep 17 00:00:00 2001 From: Pirave Eahalaivan Date: Wed, 2 Dec 2015 10:28:48 -0500 Subject: [PATCH 1/3] Adding django-multiupload app and modifying forms to use it --- djangobb_forum/forms.py | 40 +++++++++++++++++++++++++++++----------- requirements.txt | 1 + 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/djangobb_forum/forms.py b/djangobb_forum/forms.py index be22d13f..f376c5e2 100644 --- a/djangobb_forum/forms.py +++ b/djangobb_forum/forms.py @@ -1,19 +1,21 @@ # coding: utf-8 from __future__ import unicode_literals -import os.path -from datetime import timedelta +import os.path, errno +from datetime import timedelta, datetime from django import forms from django.conf import settings from django.contrib.auth import get_user_model from django.utils import timezone from django.utils.translation import ugettext_lazy as _ +from django.template.defaultfilters import slugify from djangobb_forum.models import Topic, Post, Profile, Reputation, Report, \ Attachment, Poll, PollChoice from djangobb_forum import settings as forum_settings from djangobb_forum.util import convert_text_to_html, set_language +from multiupload.fields import MultiFileField User = get_user_model() @@ -54,7 +56,7 @@ class AddPostForm(forms.ModelForm): name = forms.CharField(label=_('Subject'), max_length=255, widget=forms.TextInput(attrs={'size':'115'})) - attachment = forms.FileField(label=_('Attachment'), required=False) + attachment = MultiFileField(label=_('Attachment'), required=False, max_num = settings.DJANGOBB_ATTACHMENT_MAX, max_file_size=forum_settings.ATTACHMENT_SIZE_LIMIT) subscribe = forms.BooleanField(label=_('Subscribe'), help_text=_("Subscribe this topic."), required=False) class Meta: @@ -98,9 +100,10 @@ def clean(self): def clean_attachment(self): if self.cleaned_data['attachment']: - memfile = self.cleaned_data['attachment'] - if memfile.size > forum_settings.ATTACHMENT_SIZE_LIMIT: - raise forms.ValidationError(_('Attachment is too big')) + for attach in self.cleaned_data['attachment']: + memfile = attach + if memfile.size > forum_settings.ATTACHMENT_SIZE_LIMIT: + raise forms.ValidationError(_('Attachment is too big')) return self.cleaned_data['attachment'] def save(self): @@ -122,7 +125,8 @@ def save(self): post.save() if forum_settings.ATTACHMENT_SUPPORT: - self.save_attachment(post, self.cleaned_data['attachment']) + for attach in self.cleaned_data['attachment']: + self.save_attachment(post, attach) return post @@ -130,11 +134,25 @@ def save_attachment(self, post, memfile): if memfile: obj = Attachment(size=memfile.size, content_type=memfile.content_type, name=memfile.name, post=post) - dir = os.path.join(settings.MEDIA_ROOT, forum_settings.ATTACHMENT_UPLOAD_TO) - fname = '%d.0' % post.id - path = os.path.join(dir, fname) + dir = os.path.join( + settings.MEDIA_ROOT, + forum_settings.ATTACHMENT_UPLOAD_TO, + '%d_%s' % (post.topic.forum.id, slugify(post.topic.forum.name)), + '%d_%s' % (post.topic.id, slugify(post.topic.name)) + ) + + try: + os.makedirs(dir) + except OSError as exc: # Python >2.5 + if exc.errno == errno.EEXIST and os.path.isdir(dir): + pass + else: raise + + fname, ext = os.path.splitext(memfile.name) + date = datetime.now().strftime('%Y%m%d%H%M%S%f') + path = os.path.join(dir, '%s_%s%s' % (fname, date, ext)) open(path, 'wb').write(memfile.read()) - obj.path = fname + obj.path = path obj.save() diff --git a/requirements.txt b/requirements.txt index f5925b1f..487c19b2 100644 --- a/requirements.txt +++ b/requirements.txt @@ -5,3 +5,4 @@ postmarkup pygments pytz>=2015.4 django-pagination-py3==1.1.1 +django-multiupload From 3ad51728105e8a6be757420ba4fa0dc1e3215090 Mon Sep 17 00:00:00 2001 From: Pirave Eahalaivan Date: Wed, 2 Dec 2015 10:37:04 -0500 Subject: [PATCH 2/3] Added multiupload to INSTALLED_APPS in test cases and adding default values for maximum number attachments; currently set to 30 --- djangobb_forum/forms.py | 4 +++- djangobb_forum/settings.py | 1 + runtests.py | 1 + 3 files changed, 5 insertions(+), 1 deletion(-) diff --git a/djangobb_forum/forms.py b/djangobb_forum/forms.py index f376c5e2..65fca8b0 100644 --- a/djangobb_forum/forms.py +++ b/djangobb_forum/forms.py @@ -56,7 +56,9 @@ class AddPostForm(forms.ModelForm): name = forms.CharField(label=_('Subject'), max_length=255, widget=forms.TextInput(attrs={'size':'115'})) - attachment = MultiFileField(label=_('Attachment'), required=False, max_num = settings.DJANGOBB_ATTACHMENT_MAX, max_file_size=forum_settings.ATTACHMENT_SIZE_LIMIT) + attachment = MultiFileField(label=_('Attachment'), required=False, + max_num = forum_settings.ATTACHMENT_MAX, + max_file_size=forum_settings.ATTACHMENT_SIZE_LIMIT) subscribe = forms.BooleanField(label=_('Subscribe'), help_text=_("Subscribe this topic."), required=False) class Meta: diff --git a/djangobb_forum/settings.py b/djangobb_forum/settings.py index 6f3e271f..fc3e58f4 100644 --- a/djangobb_forum/settings.py +++ b/djangobb_forum/settings.py @@ -66,6 +66,7 @@ def get(key, default): ATTACHMENT_SUPPORT = get('DJANGOBB_ATTACHMENT_SUPPORT', True) ATTACHMENT_UPLOAD_TO = get('DJANGOBB_ATTACHMENT_UPLOAD_TO', 'djangobb_forum/attachments') ATTACHMENT_SIZE_LIMIT = get('DJANGOBB_ATTACHMENT_SIZE_LIMIT', 1024 * 1024) +ATTACHMENT_MAX = get('DJANGOBB_ATTACHMENT_MAX', 30) # SMILE Extension SMILES_SUPPORT = get('DJANGOBB_SMILES_SUPPORT', True) diff --git a/runtests.py b/runtests.py index 0e5035f5..17eae19a 100755 --- a/runtests.py +++ b/runtests.py @@ -29,6 +29,7 @@ 'haystack', 'pagination', + 'multiupload', 'djangobb_forum', ), From 8d99475e73aed26275222b69e6f5a9c221cace0a Mon Sep 17 00:00:00 2001 From: Pirave Eahalaivan Date: Wed, 6 Jan 2016 10:41:03 -0500 Subject: [PATCH 3/3] Fixing bug: check if attachment exists before looping through it --- djangobb_forum/forms.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/djangobb_forum/forms.py b/djangobb_forum/forms.py index 65fca8b0..244598a7 100644 --- a/djangobb_forum/forms.py +++ b/djangobb_forum/forms.py @@ -57,7 +57,7 @@ class AddPostForm(forms.ModelForm): name = forms.CharField(label=_('Subject'), max_length=255, widget=forms.TextInput(attrs={'size':'115'})) attachment = MultiFileField(label=_('Attachment'), required=False, - max_num = forum_settings.ATTACHMENT_MAX, + max_num=forum_settings.ATTACHMENT_MAX, max_file_size=forum_settings.ATTACHMENT_SIZE_LIMIT) subscribe = forms.BooleanField(label=_('Subscribe'), help_text=_("Subscribe this topic."), required=False) @@ -126,7 +126,7 @@ def save(self): body=self.cleaned_data['body']) post.save() - if forum_settings.ATTACHMENT_SUPPORT: + if forum_settings.ATTACHMENT_SUPPORT and self.cleaned_data['attachment']: for attach in self.cleaned_data['attachment']: self.save_attachment(post, attach) return post