diff --git a/boards/migrations/0005_auto.py b/boards/migrations/0005_auto.py --- a/boards/migrations/0005_auto.py +++ b/boards/migrations/0005_auto.py @@ -1,9 +1,7 @@ # -*- coding: utf-8 -*- -import datetime from south.db import db from south.v2 import SchemaMigration from django.db import models -from boards.models.post import Post, NO_PARENT class Migration(SchemaMigration): diff --git a/boards/models/post.py b/boards/models/post.py --- a/boards/models/post.py +++ b/boards/models/post.py @@ -36,23 +36,12 @@ TITLE_MAX_LENGTH = 200 DEFAULT_MARKUP_TYPE = 'markdown' -# TODO This should be removed when no code relies on it because thread id field -# was removed a long time ago -NO_PARENT = -1 - # TODO This should be removed NO_IP = '0.0.0.0' # TODO Real user agent should be saved instead of this UNKNOWN_UA = '' -# TODO This should be checked for usage and removed because a nativa -# paginator is used now -ALL_PAGES = -1 - -IMAGES_DIRECTORY = 'images/' -FILE_EXTENSION_DELIMITER = '.' - SETTING_MODERATE = "moderate" REGEX_REPLY = re.compile('>>(\d+)') @@ -107,7 +96,7 @@ class PostManager(models.Manager): map(thread.add_tag, tags) if new_thread: - self._delete_old_threads() + Thread.objects.archive_oldest_threads() self.connect_replies(post) logger.info('Created post #%d' % post.id) @@ -141,30 +130,6 @@ class PostManager(models.Manager): posts = self.filter(poster_ip=ip) map(self.delete_post, posts) - # TODO Move this method to thread manager - # TODO Rename it, because the threads are archived instead of plain - # removal. Split the delete and archive methods and make a setting to - # enable or disable archiving. - def _delete_old_threads(self): - """ - Preserves maximum thread count. If there are too many threads, - archive the old ones. - """ - - threads = Thread.objects.filter(archived=False).order_by('-bump_time') - thread_count = threads.count() - - if thread_count > settings.MAX_THREAD_COUNT: - num_threads_to_delete = thread_count - settings.MAX_THREAD_COUNT - old_threads = threads[thread_count - num_threads_to_delete:] - - for thread in old_threads: - thread.archived = True - thread.last_edit_time = timezone.now() - thread.save() - - logger.info('Archived %d old threads' % num_threads_to_delete) - def connect_replies(self, post): """ Connects replies to a post to show them as a reflink map @@ -178,11 +143,11 @@ class PostManager(models.Manager): referenced_post.referenced_posts.add(post) referenced_post.last_edit_time = post.pub_time referenced_post.build_refmap() - referenced_post.save() + 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() + referenced_thread.save(update_fields=['last_edit_time']) def get_posts_per_day(self): """ @@ -235,7 +200,7 @@ class Post(models.Model, Viewable): poster_user_agent = models.TextField() thread_new = models.ForeignKey('Thread', null=True, default=None, - db_index=True) + db_index=True) last_edit_time = models.DateTimeField() user = models.ForeignKey('User', null=True, default=None, db_index=True) @@ -379,3 +344,11 @@ class Post(models.Model, Viewable): def get_first_image(self): return self.images.earliest('id') + def delete(self, using=None): + """ + Delete all post images and the post itself. + """ + + self.images.all().delete() + + super(Post, self).delete(using) \ No newline at end of file diff --git a/boards/models/tag.py b/boards/models/tag.py --- a/boards/models/tag.py +++ b/boards/models/tag.py @@ -7,14 +7,6 @@ from boards.models.base import Viewable __author__ = 'neko259' -# TODO Tag popularity ratings are not used any more, remove all of this -MAX_TAG_FONT = 1 -MIN_TAG_FONT = 0.2 - -TAG_POPULARITY_MULTIPLIER = 20 - -ARCHIVE_POPULARITY_MODIFIER = 0.5 - class TagManager(models.Manager): @@ -59,25 +51,6 @@ class Tag(models.Model, Viewable): def get_thread_count(self): return self.threads.count() - # TODO Remove, not used any more - def get_popularity(self): - """ - Gets tag's popularity value as a percentage of overall board post - count. - """ - - all_post_count = Post.objects.count() - - tag_reply_count = 0.0 - - tag_reply_count += self.get_post_count() - tag_reply_count +=\ - self.get_post_count(archived=True) * ARCHIVE_POPULARITY_MODIFIER - - popularity = tag_reply_count / all_post_count - - return popularity - def get_linked_tags(self): """ Gets tags linked to the current one. @@ -101,20 +74,6 @@ class Tag(models.Model, Viewable): linked_tag.get_linked_tags_list(tag_list) - # TODO Remove - def get_font_value(self): - """ - Gets tag font value to differ most popular tags in the list - """ - - popularity = self.get_popularity() - - font_value = popularity * Tag.objects.get_not_empty_tags().count() - font_value = max(font_value, MIN_TAG_FONT) - font_value = min(font_value, MAX_TAG_FONT) - - return str(font_value) - def get_post_count(self, archived=False): """ Gets posts count for the tag's threads. @@ -124,8 +83,8 @@ class Tag(models.Model, Viewable): threads = self.threads.filter(archived=archived) if threads.exists(): - posts_count = threads.annotate(posts_count=Count('replies')).aggregate( - posts_sum=Sum('posts_count'))['posts_sum'] + posts_count = threads.annotate(posts_count=Count('replies')) \ + .aggregate(posts_sum=Sum('posts_count'))['posts_sum'] if not posts_count: posts_count = 0 diff --git a/boards/models/thread.py b/boards/models/thread.py --- a/boards/models/thread.py +++ b/boards/models/thread.py @@ -14,7 +14,30 @@ logger = logging.getLogger(__name__) CACHE_KEY_OPENING_POST = 'opening_post_id' +class ThreadManager(models.Manager): + def archive_oldest_threads(self): + """ + Preserves maximum thread count. If there are too many threads, + archive the old ones. + """ + + threads = Thread.objects.filter(archived=False).order_by('-bump_time') + thread_count = threads.count() + + if thread_count > settings.MAX_THREAD_COUNT: + num_threads_to_delete = thread_count - settings.MAX_THREAD_COUNT + old_threads = threads[thread_count - num_threads_to_delete:] + + for thread in old_threads: + thread.archived = True + thread.last_edit_time = timezone.now() + thread.save(update_fields=['archived', 'last_edit_time']) + + logger.info('Archived %d old threads' % num_threads_to_delete) + + class Thread(models.Model): + objects = ThreadManager() class Meta: app_label = 'boards' diff --git a/boards/urls.py b/boards/urls.py --- a/boards/urls.py +++ b/boards/urls.py @@ -75,8 +75,6 @@ urlpatterns = patterns('', name='get_thread'), url(r'^api/add_post/(?P\w+)/$', api.api_add_post, name='add_post'), - url(r'^api/get_tag_popularity/(?P\w+)$', api.get_tag_popularity, - name='get_tag_popularity'), # Search url(r'^search/', include('haystack.urls')), diff --git a/boards/views/api.py b/boards/views/api.py --- a/boards/views/api.py +++ b/boards/views/api.py @@ -215,15 +215,6 @@ def api_get_post(request, post_id): return HttpResponse(content=json) -def get_tag_popularity(request, tag_name): - tag = get_object_or_404(Tag, name=tag_name) - - json_data = [] - json_data['popularity'] = tag.get_popularity() - - return HttpResponse(content=json.dumps(json_data)) - - # TODO Add pub time and replies def _get_post_data(post_id, format_type=DIFF_TYPE_JSON, request=None, include_last_update=False):