# HG changeset patch # User neko259 # Date 2014-11-10 16:05:04 # Node ID 0dc734ff65f6a2bd20c06561b53edabcaa869a28 # Parent 14606f355025e7562a70cd35c441210a377a5c99 Added 'bumpable' field. Disable thread bump once and for all, not counting it every time by the post count diff --git a/boards/migrations/0031_auto__add_field_thread_bumpable.py b/boards/migrations/0031_auto__add_field_thread_bumpable.py new file mode 100644 --- /dev/null +++ b/boards/migrations/0031_auto__add_field_thread_bumpable.py @@ -0,0 +1,74 @@ +# -*- coding: utf-8 -*- +from south.utils import datetime_utils as 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 'Thread.bumpable' + db.add_column('boards_thread', 'bumpable', + self.gf('django.db.models.fields.BooleanField')(default=True), + keep_default=False) + + + def backwards(self, orm): + # Deleting field 'Thread.bumpable' + db.delete_column('boards_thread', 'bumpable') + + + models = { + 'boards.ban': { + 'Meta': {'object_name': 'Ban'}, + 'can_read': ('django.db.models.fields.BooleanField', [], {'default': 'True'}), + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'ip': ('django.db.models.fields.GenericIPAddressField', [], {'max_length': '39'}), + 'reason': ('django.db.models.fields.CharField', [], {'max_length': '200', 'default': "'Auto'"}) + }, + 'boards.post': { + 'Meta': {'object_name': 'Post', 'ordering': "('id',)"}, + '_text_rendered': ('django.db.models.fields.TextField', [], {}), + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'images': ('django.db.models.fields.related.ManyToManyField', [], {'symmetrical': 'False', 'null': 'True', 'db_index': 'True', 'blank': 'True', 'related_name': "'ip+'", 'to': "orm['boards.PostImage']"}), + 'last_edit_time': ('django.db.models.fields.DateTimeField', [], {}), + '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', [], {}), + 'referenced_posts': ('django.db.models.fields.related.ManyToManyField', [], {'symmetrical': 'False', 'null': 'True', 'db_index': 'True', 'blank': 'True', 'related_name': "'rfp+'", 'to': "orm['boards.Post']"}), + 'refmap': ('django.db.models.fields.TextField', [], {'null': 'True', 'blank': 'True'}), + 'text': ('markupfield.fields.MarkupField', [], {'rendered_field': 'True'}), + 'text_markup_type': ('django.db.models.fields.CharField', [], {'max_length': '30', 'default': "'bbcode'"}), + 'thread_new': ('django.db.models.fields.related.ForeignKey', [], {'default': 'None', 'null': 'True', 'to': "orm['boards.Thread']"}), + 'title': ('django.db.models.fields.CharField', [], {'max_length': '200'}) + }, + 'boards.postimage': { + 'Meta': {'object_name': 'PostImage', 'ordering': "('id',)"}, + 'hash': ('django.db.models.fields.CharField', [], {'max_length': '36'}), + 'height': ('django.db.models.fields.IntegerField', [], {'default': '0'}), + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'image': ('boards.thumbs.ImageWithThumbsField', [], {'max_length': '100', 'blank': 'True'}), + 'pre_height': ('django.db.models.fields.IntegerField', [], {'default': '0'}), + 'pre_width': ('django.db.models.fields.IntegerField', [], {'default': '0'}), + 'width': ('django.db.models.fields.IntegerField', [], {'default': '0'}) + }, + 'boards.tag': { + 'Meta': {'object_name': 'Tag', 'ordering': "('name',)"}, + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'name': ('django.db.models.fields.CharField', [], {'db_index': 'True', 'max_length': '100'}), + 'threads': ('django.db.models.fields.related.ManyToManyField', [], {'null': 'True', 'symmetrical': 'False', 'blank': 'True', 'related_name': "'tag+'", 'to': "orm['boards.Thread']"}) + }, + 'boards.thread': { + 'Meta': {'object_name': 'Thread'}, + 'archived': ('django.db.models.fields.BooleanField', [], {'default': 'False'}), + 'bump_time': ('django.db.models.fields.DateTimeField', [], {}), + 'bumpable': ('django.db.models.fields.BooleanField', [], {'default': 'True'}), + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'last_edit_time': ('django.db.models.fields.DateTimeField', [], {}), + 'replies': ('django.db.models.fields.related.ManyToManyField', [], {'null': 'True', 'symmetrical': 'False', 'blank': 'True', 'related_name': "'tre+'", 'to': "orm['boards.Post']"}), + 'tags': ('django.db.models.fields.related.ManyToManyField', [], {'symmetrical': 'False', 'to': "orm['boards.Tag']"}) + } + } + + complete_apps = ['boards'] diff --git a/boards/migrations/0032_make_not_bumpable.py b/boards/migrations/0032_make_not_bumpable.py new file mode 100644 --- /dev/null +++ b/boards/migrations/0032_make_not_bumpable.py @@ -0,0 +1,81 @@ +# -*- coding: utf-8 -*- +from south.utils import datetime_utils as datetime +from south.db import db +from south.v2 import DataMigration +from django.db import models +import boards + +class Migration(DataMigration): + + def forwards(self, orm): + changed_count = 0 + + for thread in orm['boards.Thread'].objects.all(): + if thread.replies.count() >= boards.settings.MAX_POSTS_PER_THREAD: + thread.bumpable = False + print('Disabled bump on thread {}'.format(thread.id)) + changed_count += 1 + else: + thread.bumpable = True + thread.save(update_fields=['bumpable']) + + + print('Changed {} threads.'.format(changed_count)) + + def backwards(self, orm): + "Write your backwards methods here." + + models = { + 'boards.ban': { + 'Meta': {'object_name': 'Ban'}, + 'can_read': ('django.db.models.fields.BooleanField', [], {'default': 'True'}), + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'ip': ('django.db.models.fields.GenericIPAddressField', [], {'max_length': '39'}), + 'reason': ('django.db.models.fields.CharField', [], {'default': "'Auto'", 'max_length': '200'}) + }, + 'boards.post': { + 'Meta': {'object_name': 'Post', 'ordering': "('id',)"}, + '_text_rendered': ('django.db.models.fields.TextField', [], {}), + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'images': ('django.db.models.fields.related.ManyToManyField', [], {'symmetrical': 'False', 'related_name': "'ip+'", 'null': 'True', 'to': "orm['boards.PostImage']", 'db_index': 'True', 'blank': 'True'}), + 'last_edit_time': ('django.db.models.fields.DateTimeField', [], {}), + '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', [], {}), + 'referenced_posts': ('django.db.models.fields.related.ManyToManyField', [], {'symmetrical': 'False', 'related_name': "'rfp+'", 'null': 'True', 'to': "orm['boards.Post']", 'db_index': 'True', 'blank': 'True'}), + 'refmap': ('django.db.models.fields.TextField', [], {'blank': 'True', 'null': 'True'}), + 'text': ('markupfield.fields.MarkupField', [], {'rendered_field': 'True'}), + 'text_markup_type': ('django.db.models.fields.CharField', [], {'default': "'bbcode'", 'max_length': '30'}), + 'thread_new': ('django.db.models.fields.related.ForeignKey', [], {'default': 'None', 'to': "orm['boards.Thread']", 'null': 'True'}), + 'title': ('django.db.models.fields.CharField', [], {'max_length': '200'}) + }, + 'boards.postimage': { + 'Meta': {'object_name': 'PostImage', 'ordering': "('id',)"}, + 'hash': ('django.db.models.fields.CharField', [], {'max_length': '36'}), + 'height': ('django.db.models.fields.IntegerField', [], {'default': '0'}), + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'image': ('boards.thumbs.ImageWithThumbsField', [], {'blank': 'True', 'max_length': '100'}), + 'pre_height': ('django.db.models.fields.IntegerField', [], {'default': '0'}), + 'pre_width': ('django.db.models.fields.IntegerField', [], {'default': '0'}), + 'width': ('django.db.models.fields.IntegerField', [], {'default': '0'}) + }, + 'boards.tag': { + 'Meta': {'object_name': 'Tag', 'ordering': "('name',)"}, + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'name': ('django.db.models.fields.CharField', [], {'db_index': 'True', 'max_length': '100'}), + 'threads': ('django.db.models.fields.related.ManyToManyField', [], {'symmetrical': 'False', 'null': 'True', 'to': "orm['boards.Thread']", 'related_name': "'tag+'", 'blank': 'True'}) + }, + 'boards.thread': { + 'Meta': {'object_name': 'Thread'}, + 'archived': ('django.db.models.fields.BooleanField', [], {'default': 'False'}), + 'bump_time': ('django.db.models.fields.DateTimeField', [], {}), + 'bumpable': ('django.db.models.fields.BooleanField', [], {'default': 'True'}), + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'last_edit_time': ('django.db.models.fields.DateTimeField', [], {}), + 'replies': ('django.db.models.fields.related.ManyToManyField', [], {'symmetrical': 'False', 'null': 'True', 'to': "orm['boards.Post']", 'related_name': "'tre+'", 'blank': 'True'}), + 'tags': ('django.db.models.fields.related.ManyToManyField', [], {'symmetrical': 'False', 'to': "orm['boards.Tag']"}) + } + } + + complete_apps = ['boards'] + symmetrical = True diff --git a/boards/models/post.py b/boards/models/post.py --- a/boards/models/post.py +++ b/boards/models/post.py @@ -73,7 +73,10 @@ class PostManager(models.Manager): else: thread.bump() thread.last_edit_time = posting_time - thread.save() + if thread.can_bump() and ( + thread.get_reply_count() >= settings.MAX_POSTS_PER_THREAD): + thread.bumpable = False + thread.save(update_fields=['last_edit_time', 'bumpable']) new_thread = False post = self.create(title=title, @@ -101,6 +104,7 @@ class PostManager(models.Manager): Thread.objects.process_oldest_threads() self.connect_replies(post) + return post def delete_post(self, post): @@ -414,4 +418,4 @@ class Post(models.Model, Viewable): post_id = reply_number.group(1) ref_post = Post.objects.filter(id=post_id)[0] - ref_post.send_to_websocket(request, recursive=False) \ No newline at end of file + ref_post.send_to_websocket(request, recursive=False) diff --git a/boards/models/thread.py b/boards/models/thread.py --- a/boards/models/thread.py +++ b/boards/models/thread.py @@ -38,8 +38,9 @@ class ThreadManager(models.Manager): def _archive_thread(self, thread): thread.archived = True + thread.bumpable = False thread.last_edit_time = timezone.now() - thread.save(update_fields=['archived', 'last_edit_time']) + thread.save(update_fields=['archived', 'last_edit_time', 'bumpable']) class Thread(models.Model): @@ -54,6 +55,7 @@ class Thread(models.Model): replies = models.ManyToManyField('Post', symmetrical=False, null=True, blank=True, related_name='tre+') archived = models.BooleanField(default=False) + bumpable = models.BooleanField(default=True) def get_tags(self): """ @@ -88,12 +90,7 @@ class Thread(models.Model): Checks if the thread can be bumped by replying to it. """ - if self.archived: - return False - - post_count = self.get_reply_count() - - return post_count < settings.MAX_POSTS_PER_THREAD + return self.bumpable def get_last_replies(self): """ @@ -185,4 +182,4 @@ class Thread(models.Model): if self.replies.exists(): self.replies.all().delete() - super(Thread, self).delete(using) \ No newline at end of file + super(Thread, self).delete(using) diff --git a/boards/static/js/thread_update.js b/boards/static/js/thread_update.js --- a/boards/static/js/thread_update.js +++ b/boards/static/js/thread_update.js @@ -50,7 +50,8 @@ function connectWebsocket() { }); centrifuge.on('error', function(error_message) { - alert("Error connecting to websocket server."); + console.log("Error connecting to websocket server."); + return false; }); centrifuge.on('connect', function() { @@ -68,6 +69,8 @@ function connectWebsocket() { }); centrifuge.connect(); + + return true; } function updatePost(postHtml, isAdded) { @@ -260,23 +263,23 @@ function processNewPost(post) { $(document).ready(function(){ if ('WebSocket' in window) { - initAutoupdate(); + if (initAutoupdate()) { + // Post form data over AJAX + var threadId = $('div.thread').children('.post').first().attr('id'); - // Post form data over AJAX - var threadId = $('div.thread').children('.post').first().attr('id'); - - var form = $('#form'); + var form = $('#form'); - var options = { - beforeSubmit: function(arr, $form, options) { - showAsErrors($('form'), gettext('Sending message...')); - }, - success: updateOnPost, - url: '/api/add_post/' + threadId + '/' - }; + var options = { + beforeSubmit: function(arr, $form, options) { + showAsErrors($('form'), gettext('Sending message...')); + }, + success: updateOnPost, + url: '/api/add_post/' + threadId + '/' + }; - form.ajaxForm(options); + form.ajaxForm(options); - resetForm(form); + resetForm(form); + } } });