diff --git a/boards/migrations/0006_auto__add_field_post_thread.py b/boards/migrations/0006_auto__add_field_post_thread.py new file mode 100644 --- /dev/null +++ b/boards/migrations/0006_auto__add_field_post_thread.py @@ -0,0 +1,72 @@ +# -*- coding: utf-8 -*- +import datetime +from south.db import db +from south.v2 import SchemaMigration +from django.db import models + + +class Migration(SchemaMigration): + + def forwards(self, orm): + # Adding field 'Post.thread' + db.add_column(u'boards_post', 'thread', + self.gf('django.db.models.fields.related.ForeignKey')(default=None, to=orm['boards.Post'], null=True), + keep_default=False) + + + def backwards(self, orm): + # Deleting field 'Post.thread' + db.delete_column(u'boards_post', 'thread_id') + + + models = { + u'boards.ban': { + 'Meta': {'object_name': 'Ban'}, + u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'ip': ('django.db.models.fields.GenericIPAddressField', [], {'max_length': '39'}) + }, + u'boards.post': { + 'Meta': {'object_name': 'Post'}, + '_text_rendered': ('django.db.models.fields.TextField', [], {}), + u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'image': ('boards.thumbs.ImageWithThumbsField', [], {'max_length': '100', 'blank': 'True'}), + 'image_height': ('django.db.models.fields.IntegerField', [], {'default': '0'}), + 'image_width': ('django.db.models.fields.IntegerField', [], {'default': '0'}), + 'last_edit_time': ('django.db.models.fields.DateTimeField', [], {}), + 'parent': ('django.db.models.fields.BigIntegerField', [], {}), + 'poster_ip': ('django.db.models.fields.GenericIPAddressField', [], {'max_length': '39'}), + 'poster_user_agent': ('django.db.models.fields.TextField', [], {}), + 'pub_time': ('django.db.models.fields.DateTimeField', [], {}), + 'replies': ('django.db.models.fields.related.ManyToManyField', [], {'blank': 'True', 'related_name': "'re+'", 'null': 'True', 'symmetrical': 'False', 'to': u"orm['boards.Post']"}), + 'tags': ('django.db.models.fields.related.ManyToManyField', [], {'to': u"orm['boards.Tag']", 'symmetrical': 'False'}), + 'text': ('markupfield.fields.MarkupField', [], {'rendered_field': 'True'}), + 'text_markup_type': ('django.db.models.fields.CharField', [], {'default': "'markdown'", 'max_length': '30'}), + 'thread': ('django.db.models.fields.related.ForeignKey', [], {'default': 'None', 'to': u"orm['boards.Post']", 'null': 'True'}), + 'title': ('django.db.models.fields.CharField', [], {'max_length': '50'}), + 'user': ('django.db.models.fields.related.ForeignKey', [], {'default': 'None', 'to': u"orm['boards.User']", 'null': 'True'}) + }, + u'boards.setting': { + 'Meta': {'object_name': 'Setting'}, + u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'name': ('django.db.models.fields.CharField', [], {'max_length': '50'}), + 'user': ('django.db.models.fields.related.ForeignKey', [], {'to': u"orm['boards.User']"}), + 'value': ('django.db.models.fields.CharField', [], {'max_length': '50'}) + }, + u'boards.tag': { + 'Meta': {'object_name': 'Tag'}, + u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'name': ('django.db.models.fields.CharField', [], {'max_length': '100'}) + }, + u'boards.user': { + 'Meta': {'object_name': 'User'}, + 'fav_tags': ('django.db.models.fields.related.ManyToManyField', [], {'symmetrical': 'False', 'to': u"orm['boards.Tag']", 'null': 'True', 'blank': 'True'}), + 'fav_threads': ('django.db.models.fields.related.ManyToManyField', [], {'blank': 'True', 'related_name': "'+'", 'null': 'True', 'symmetrical': 'False', 'to': u"orm['boards.Post']"}), + u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'last_access_time': ('django.db.models.fields.DateTimeField', [], {}), + 'rank': ('django.db.models.fields.IntegerField', [], {}), + 'registration_time': ('django.db.models.fields.DateTimeField', [], {}), + 'user_id': ('django.db.models.fields.CharField', [], {'max_length': '50'}) + } + } + + complete_apps = ['boards'] \ No newline at end of file diff --git a/boards/models.py b/boards/models.py --- a/boards/models.py +++ b/boards/models.py @@ -33,27 +33,26 @@ RANK_USER = 100 class PostManager(models.Manager): - def create_post(self, title, text, image=None, parent_id=NO_PARENT, + def create_post(self, title, text, image=None, thread=None, ip=NO_IP, tags=None, user=None): post = self.create(title=title, text=text, pub_time=timezone.now(), - parent=parent_id, + thread=thread, image=image, poster_ip=ip, poster_user_agent=UNKNOWN_UA, last_edit_time=timezone.now(), user=user) - if parent_id != NO_PARENT: - parent = self.get(id=parent_id) - parent.replies.add(post) + if thread: + thread.replies.add(post) if tags: map(post.tags.add, tags) - if parent_id != NO_PARENT: - self._bump_thread(parent_id) + if thread: + thread.bump() else: self._delete_old_threads() @@ -61,7 +60,7 @@ class PostManager(models.Manager): def delete_post(self, post): if post.replies.count() > 0: - map(self.delete_post, post.replies) + map(self.delete_post, post.replies.all()) post.delete() def delete_posts_by_ip(self, ip): @@ -71,11 +70,15 @@ class PostManager(models.Manager): def get_threads(self, tag=None, page=ALL_PAGES, order_by='-last_edit_time'): if tag: - threads = self.filter(parent=NO_PARENT, tags=tag) + threads = self.filter(thread=None, tags=tag) + + # TODO This needs to be uncommented when 'all tags' view won't + # use this method to get threads for tag - # TODO Throw error 404 if no threads for tag found? + # if threads.count() == 0: + # raise Http404 else: - threads = self.filter(parent=NO_PARENT) + threads = self.filter(thread=None) threads = threads.order_by(order_by) @@ -92,7 +95,7 @@ class PostManager(models.Manager): def get_thread(self, opening_post_id): try: - opening_post = self.get(id=opening_post_id, parent=NO_PARENT) + opening_post = self.get(id=opening_post_id, thread=None) except Post.DoesNotExist: raise Http404 @@ -109,9 +112,9 @@ class PostManager(models.Manager): def get_thread_page_count(self, tag=None): if tag: - threads = self.filter(parent=NO_PARENT, tags=tag) + threads = self.filter(thread=None, tags=tag) else: - threads = self.filter(parent=NO_PARENT) + threads = self.filter(thread=None) return int(math.ceil(threads.count() / float( settings.THREADS_PER_PAGE))) @@ -225,8 +228,9 @@ class Post(models.Model): poster_user_agent = models.TextField() # TODO Convert this field to ForeignKey - parent = models.BigIntegerField() + parent = models.BigIntegerField(default=NO_PARENT) + thread = models.ForeignKey('Post', null=True, default=None) tags = models.ManyToManyField(Tag) last_edit_time = models.DateTimeField() user = models.ForeignKey('User', null=True, default=None) @@ -267,7 +271,12 @@ class Post(models.Model): return post_count <= settings.MAX_POSTS_PER_THREAD - def get_last_replies(self): + def bump(self): + if self.can_bump(): + self.last_edit_time = timezone.now() + self.save() + +def get_last_replies(self): if settings.LAST_REPLIES_COUNT > 0: reply_count = self.get_reply_count() diff --git a/boards/templates/boards/base.html b/boards/templates/boards/base.html --- a/boards/templates/boards/base.html +++ b/boards/templates/boards/base.html @@ -28,8 +28,10 @@