diff --git a/boards/migrations/0009_post_thread.py b/boards/migrations/0009_post_thread.py new file mode 100644 --- /dev/null +++ b/boards/migrations/0009_post_thread.py @@ -0,0 +1,28 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import models, migrations + + +class Migration(migrations.Migration): + + def first_thread_to_thread(apps, schema_editor): + Post = apps.get_model('boards', 'Post') + for post in Post.objects.all(): + post.thread = post.threads.first() + post.save(update_fields=['thread']) + + + dependencies = [ + ('boards', '0008_auto_20150205_1304'), + ] + + operations = [ + migrations.AddField( + model_name='post', + name='thread', + field=models.ForeignKey(related_name='pt+', to='boards.Thread', default=None, null=True), + preserve_default=False, + ), + migrations.RunPython(first_thread_to_thread), + ] diff --git a/boards/models/post.py b/boards/models/post.py --- a/boards/models/post.py +++ b/boards/models/post.py @@ -91,6 +91,7 @@ class PostManager(models.Manager): text=pre_text, pub_time=posting_time, poster_ip=ip, + thread=thread, poster_user_agent=UNKNOWN_UA, # TODO Get UA at # last! last_edit_time=posting_time) @@ -151,9 +152,12 @@ class PostManager(models.Manager): referenced_post.build_refmap() referenced_post.save(update_fields=['refmap', 'last_edit_time']) - referenced_thread = referenced_post.get_thread() - referenced_thread.last_edit_time = post.pub_time - referenced_thread.save(update_fields=['last_edit_time']) + referenced_threads = referenced_post.get_threads().all() + for thread in referenced_threads: + thread.last_edit_time = post.pub_time + thread.save(update_fields=['last_edit_time']) + + post.threads.add(thread) @cached_result def get_posts_per_day(self): @@ -220,6 +224,7 @@ class Post(models.Model, Viewable): db_index=True) refmap = models.TextField(null=True, blank=True) threads = models.ManyToManyField('Thread', db_index=True) + thread = models.ForeignKey('Thread', db_index=True, related_name='pt+') def __str__(self): return 'P#{}/{}'.format(self.id, self.title) @@ -299,9 +304,8 @@ class Post(models.Model, Viewable): return link - # TODO This must be removed after full transition to multiple threads def get_thread(self): - return self.get_threads().first() + return self.thread def get_threads(self): """