diff --git a/boards/admin.py b/boards/admin.py --- a/boards/admin.py +++ b/boards/admin.py @@ -13,7 +13,7 @@ class PostAdmin(admin.ModelAdmin): list_filter = ('pub_time',) search_fields = ('id', 'title', 'text', 'poster_ip') exclude = ('referenced_posts', 'refmap', 'images', 'global_id') - readonly_fields = ('poster_ip', 'threads', 'thread', 'linked_images', + readonly_fields = ('poster_ip', 'thread', 'linked_images', 'attachments', 'uid', 'url', 'pub_time', 'opening', 'linked_global_id', 'version', 'foreign', 'tags') diff --git a/boards/forms.py b/boards/forms.py --- a/boards/forms.py +++ b/boards/forms.py @@ -179,9 +179,6 @@ class PostForm(NeboardForm): email = forms.CharField(max_length=100, required=False, label=_('e-mail'), widget=forms.TextInput(attrs={ 'class': 'form-email'})) - threads = forms.CharField(required=False, label=_('Additional threads'), - widget=forms.TextInput(attrs={ATTRIBUTE_PLACEHOLDER: - '123 456 789'})) subscribe = forms.BooleanField(required=False, label=_('Subscribe to thread')) guess = forms.CharField(widget=forms.HiddenInput(), required=False) @@ -263,25 +260,6 @@ class PostForm(NeboardForm): return file - def clean_threads(self): - threads_str = self.cleaned_data['threads'] - - if len(threads_str) > 0: - threads_id_list = threads_str.split(' ') - - threads = list() - - for thread_id in threads_id_list: - try: - thread = Post.objects.get(id=int(thread_id)) - if not thread.is_opening() or thread.get_thread().is_archived(): - raise ObjectDoesNotExist() - threads.append(thread) - except (ObjectDoesNotExist, ValueError): - raise forms.ValidationError(_('Invalid additional thread list')) - - return threads - def clean(self): cleaned_data = super(PostForm, self).clean() diff --git a/boards/migrations/0053_auto_20161127_1541.py b/boards/migrations/0053_auto_20161127_1541.py new file mode 100644 --- /dev/null +++ b/boards/migrations/0053_auto_20161127_1541.py @@ -0,0 +1,25 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.9.5 on 2016-11-27 13:41 +from __future__ import unicode_literals + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('boards', '0052_auto_20161120_1344'), + ] + + operations = [ + migrations.RemoveField( + model_name='post', + name='threads', + ), + migrations.AlterField( + model_name='post', + name='thread', + field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='replies', to='boards.Thread'), + ), + ] diff --git a/boards/models/post/__init__.py b/boards/models/post/__init__.py --- a/boards/models/post/__init__.py +++ b/boards/models/post/__init__.py @@ -87,9 +87,7 @@ class Post(models.Model, Viewable): blank=True, related_name='refposts', db_index=True) refmap = models.TextField(null=True, blank=True) - threads = models.ManyToManyField('Thread', db_index=True, - related_name='multi_replies') - thread = models.ForeignKey('Thread', db_index=True, related_name='pt+') + thread = models.ForeignKey('Thread', db_index=True, related_name='replies') url = models.TextField() uid = models.TextField(db_index=True) @@ -279,10 +277,7 @@ class Post(models.Model, Viewable): return thread_ids = list() - for thread in self.get_threads().all(): - thread_ids.append(thread.id) - - thread.notify_clients() + self.get_thread().notify_clients() if recursive: for reply_number in re.finditer(REGEX_REPLY, self.get_raw_text()): @@ -291,7 +286,7 @@ class Post(models.Model, Viewable): try: ref_post = Post.objects.get(id=post_id) - if ref_post.get_threads().exclude(id__in=thread_ids).exists(): + if ref_post.get_thread().id not in thread_ids: # If post is in this thread, its thread was already notified. # Otherwise, notify its thread separately. ref_post.notify_clients(recursive=False) @@ -316,9 +311,9 @@ class Post(models.Model, Viewable): update_fields += ['uid'] if not new_post: - for thread in self.get_threads().all(): + thread = self.get_thread() + if thread: thread.last_edit_time = self.last_edit_time - thread.save(update_fields=['last_edit_time', 'status']) super().save(force_insert, force_update, using, update_fields) @@ -354,17 +349,6 @@ class Post(models.Model, Viewable): return text - def connect_threads(self, opening_posts): - for opening_post in opening_posts: - threads = opening_post.get_threads().all() - for thread in threads: - if thread.can_bump(): - thread.update_bump_status() - - thread.last_edit_time = self.last_edit_time - thread.save(update_fields=['last_edit_time', 'status']) - self.threads.add(opening_post.get_thread()) - def get_tripcode(self): if self.tripcode: return Tripcode(self.tripcode) 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 @@ -27,7 +27,7 @@ post_import_deps = Signal() class PostManager(models.Manager): @transaction.atomic def create_post(self, title: str, text: str, file=None, thread=None, - ip=NO_IP, tags: list=None, opening_posts: list=None, + ip=NO_IP, tags: list=None, tripcode='', monochrome=False, images=[], file_url=None): """ @@ -47,15 +47,13 @@ class PostManager(models.Manager): if not tags: tags = [] - if not opening_posts: - opening_posts = [] posting_time = timezone.now() new_thread = False if not thread: thread = boards.models.thread.Thread.objects.create( bump_time=posting_time, last_edit_time=posting_time, - monochrome=monochrome) + monochrome=monochrome) list(map(thread.tags.add, tags)) new_thread = True @@ -69,7 +67,6 @@ class PostManager(models.Manager): last_edit_time=posting_time, tripcode=tripcode, opening=new_thread) - post.threads.add(thread) logger = logging.getLogger('boards.post.create') @@ -83,7 +80,6 @@ class PostManager(models.Manager): if file_url: post.attachments.add(Attachment.objects.create_from_url(file_url)) - post.connect_threads(opening_posts) post.set_global_id() # Thread needs to be bumped only when the post is already created @@ -165,8 +161,6 @@ class PostManager(models.Manager): for file in files: self._add_file_to_post(file, post) - post.threads.add(thread) - url_to_post = '[post]{}[/post]'.format(str(global_id)) replies = self.filter(text__contains=url_to_post) for reply in replies: diff --git a/boards/models/tag.py b/boards/models/tag.py --- a/boards/models/tag.py +++ b/boards/models/tag.py @@ -102,7 +102,7 @@ class Tag(models.Model, Viewable): @cached_result() def get_post_count(self): - return self.get_threads().aggregate(num_posts=Count('multi_replies'))['num_posts'] + return self.get_threads().aggregate(num_posts=Count('replies'))['num_posts'] def get_description(self): return self.description @@ -110,7 +110,7 @@ class Tag(models.Model, Viewable): def get_random_image_post(self, status=[STATUS_ACTIVE, STATUS_BUMPLIMIT]): posts = boards.models.Post.objects.filter(attachments__mimetype__in=FILE_TYPES_IMAGE)\ .annotate(images_count=Count( - 'attachments')).filter(images_count__gt=0, threads__tags__in=[self]) + 'attachments')).filter(images_count__gt=0, thread__tags__in=[self]) if status is not None: posts = posts.filter(thread__status__in=status) return posts.order_by('?').first() diff --git a/boards/models/thread.py b/boards/models/thread.py --- a/boards/models/thread.py +++ b/boards/models/thread.py @@ -181,8 +181,8 @@ class Thread(models.Model): """ Gets sorted thread posts """ - query = self.multi_replies.order_by('pub_time').prefetch_related( - 'thread', 'attachments') + query = self.replies.order_by('pub_time').prefetch_related( + 'attachments') return query def get_viewable_replies(self) -> QuerySet: diff --git a/boards/templates/boards/thread_normal.html b/boards/templates/boards/thread_normal.html --- a/boards/templates/boards/thread_normal.html +++ b/boards/templates/boards/thread_normal.html @@ -37,7 +37,7 @@
{% for post in thread.get_viewable_replies %} - {% post_view post reply_link=True %} + {% post_view post reply_link=True thread=thread %} {% endfor %}
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 @@ -62,7 +62,8 @@ class AllThreadsView(PostMixin, FileUplo if order == 'bump': threads = threads.order_by('-bump_time') else: - threads = threads.filter(multi_replies__opening=True).order_by('-multi_replies__pub_time') + threads = threads.filter(replies__opening=True)\ + .order_by('-replies__pub_time') filter = request.GET.get('filter') threads = threads.distinct() @@ -138,7 +139,6 @@ class AllThreadsView(PostMixin, FileUplo text = data[FORM_TEXT] file = form.get_file() file_url = form.get_file_url() - threads = data[FORM_THREADS] images = form.get_images() text = self._remove_invalid_links(text) @@ -147,7 +147,7 @@ class AllThreadsView(PostMixin, FileUplo monochrome = form.is_monochrome() post = Post.objects.create_post(title=title, text=text, file=file, - ip=ip, tags=tags, opening_posts=threads, + ip=ip, tags=tags, tripcode=form.get_tripcode(), monochrome=monochrome, images=images, file_url = file_url) diff --git a/boards/views/api.py b/boards/views/api.py --- a/boards/views/api.py +++ b/boards/views/api.py @@ -60,7 +60,7 @@ def api_get_threaddiff(request): PARAMETER_UPDATED: [], PARAMETER_LAST_UPDATE: None, # TODO Maybe this can be removed already? } - posts = Post.objects.filter(threads__in=[thread]).exclude(uid__in=uids) + posts = Post.objects.filter(thread=thread).exclude(uid__in=uids) diff_type = request.GET.get(PARAMETER_DIFF_TYPE, DIFF_TYPE_HTML) diff --git a/boards/views/feed.py b/boards/views/feed.py --- a/boards/views/feed.py +++ b/boards/views/feed.py @@ -34,8 +34,8 @@ class FeedView(PostMixin, BaseBoardView) settings_manager = get_settings_manager(request) posts = Post.objects.exclude( - threads__tags__in=settings_manager.get_hidden_tags()).order_by( - '-pub_time').prefetch_related('attachments', 'thread', 'threads') + thread__tags__in=settings_manager.get_hidden_tags()).order_by( + '-pub_time').prefetch_related('attachments', 'thread') if tripcode: posts = posts.filter(tripcode=tripcode) if favorites: 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 @@ -129,7 +129,6 @@ class ThreadView(BaseBoardView, PostMixi text = data[FORM_TEXT] file = form.get_file() file_url = form.get_file_url() - threads = data[FORM_THREADS] images = form.get_images() text = self._remove_invalid_links(text) @@ -138,7 +137,6 @@ class ThreadView(BaseBoardView, PostMixi post = Post.objects.create_post(title=title, text=text, file=file, thread=post_thread, ip=ip, - opening_posts=threads, tripcode=form.get_tripcode(), images=images, file_url=file_url) post.notify_clients()