# HG changeset patch # User neko259 # Date 2016-12-29 14:16:00 # Node ID 22f6212496e8c05f426b564191ce39053445c32f # Parent 4ca1286ef2212e832b5e89c544bb8ddfd89ce46b Add any number of files and URLs to post, not just one diff --git a/boards/forms.py b/boards/forms.py --- a/boards/forms.py +++ b/boards/forms.py @@ -187,7 +187,7 @@ class PostForm(NeboardForm): def _update_file_extension(self, file): if file: - mimetype = get_file_mimetype(file) + mimetype =get_file_mimetype(file) extension = MIMETYPE_EXTENSIONS.get(mimetype) if extension: filename = file.name.split(FILE_EXTENSION_DELIMITER, 1)[0] @@ -292,19 +292,23 @@ class PostForm(NeboardForm): return cleaned_data - def get_file(self): + def get_files(self): """ Gets file from form or URL. """ file = self.cleaned_data['file'] if isinstance(file, UploadedFile): - return file + return [file] + else: + return [] - def get_file_url(self): + def get_file_urls(self): file = self.cleaned_data['file'] if type(file) == str: - return file + return [file] + else: + return [] def get_tripcode(self): title = self.cleaned_data['title'] @@ -333,8 +337,8 @@ class PostForm(NeboardForm): def _clean_text_file(self): text = self.cleaned_data.get('text') - file = self.get_file() - file_url = self.get_file_url() + file = self.get_files() + file_url = self.get_file_urls() images = self.get_images() if (not text) and (not file) and (not file_url) and len(images) == 0: diff --git a/boards/models/post/manager.py b/boards/models/post/manager.py --- a/boards/models/post/manager.py +++ b/boards/models/post/manager.py @@ -26,10 +26,10 @@ post_import_deps = Signal() class PostManager(models.Manager): @transaction.atomic - def create_post(self, title: str, text: str, file=None, thread=None, + def create_post(self, title: str, text: str, files=[], thread=None, ip=NO_IP, tags: list=None, tripcode='', monochrome=False, images=[], - file_url=None): + file_urls=[]): """ Creates new post """ @@ -73,11 +73,11 @@ class PostManager(models.Manager): logger.info('Created post [{}] with text [{}] by {}'.format(post, post.get_text(),post.poster_ip)) - if file: + for file in files: self._add_file_to_post(file, post) for image in images: post.attachments.add(image) - if file_url: + for file_url in file_urls: post.attachments.add(Attachment.objects.create_from_url(file_url)) post.set_global_id() diff --git a/boards/views/all_threads.py b/boards/views/all_threads.py --- a/boards/views/all_threads.py +++ b/boards/views/all_threads.py @@ -142,8 +142,8 @@ class AllThreadsView(PostMixin, FileUplo title = form.get_title() text = data[FORM_TEXT] - file = form.get_file() - file_url = form.get_file_url() + files = form.get_files() + file_urls = form.get_file_urls() images = form.get_images() text = self._remove_invalid_links(text) @@ -151,11 +151,11 @@ class AllThreadsView(PostMixin, FileUplo tags = data[FORM_TAGS] monochrome = form.is_monochrome() - post = Post.objects.create_post(title=title, text=text, file=file, + post = Post.objects.create_post(title=title, text=text, files=files, ip=ip, tags=tags, tripcode=form.get_tripcode(), monochrome=monochrome, images=images, - file_url = file_url) + file_urls = file_urls) # This is required to update the threads to which posts we have replied # when creating this one diff --git a/boards/views/thread/thread.py b/boards/views/thread/thread.py --- a/boards/views/thread/thread.py +++ b/boards/views/thread/thread.py @@ -127,18 +127,18 @@ class ThreadView(BaseBoardView, PostMixi title = form.get_title() text = data[FORM_TEXT] - file = form.get_file() - file_url = form.get_file_url() + files = form.get_files() + file_urls = form.get_file_urls() images = form.get_images() text = self._remove_invalid_links(text) post_thread = opening_post.get_thread() - post = Post.objects.create_post(title=title, text=text, file=file, + post = Post.objects.create_post(title=title, text=text, files=files, thread=post_thread, ip=ip, tripcode=form.get_tripcode(), - images=images, file_url=file_url) + images=images, file_urls=file_urls) post.notify_clients() if form.is_subscribe():