Skip to content
This repository has been archived by the owner on Apr 21, 2020. It is now read-only.

Add multiple attachments to a single post #48

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 32 additions & 12 deletions djangobb_forum/forms.py
Original file line number Diff line number Diff line change
@@ -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()
Expand Down Expand Up @@ -54,7 +56,9 @@ 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=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:
Expand Down Expand Up @@ -98,9 +102,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):
Expand All @@ -121,20 +126,35 @@ def save(self):
body=self.cleaned_data['body'])

post.save()
if forum_settings.ATTACHMENT_SUPPORT:
self.save_attachment(post, self.cleaned_data['attachment'])
if forum_settings.ATTACHMENT_SUPPORT and self.cleaned_data['attachment']:
for attach in self.cleaned_data['attachment']:
self.save_attachment(post, attach)
return post


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()


Expand Down
1 change: 1 addition & 0 deletions djangobb_forum/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ postmarkup
pygments
pytz>=2015.4
django-pagination-py3==1.1.1
django-multiupload
1 change: 1 addition & 0 deletions runtests.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

'haystack',
'pagination',
'multiupload',

'djangobb_forum',
),
Expand Down