diff --git a/boards/management/__init__.py b/boards/management/__init__.py new file mode 100644 --- /dev/null +++ b/boards/management/__init__.py @@ -0,0 +1,1 @@ +__author__ = 'vurdalak' diff --git a/boards/management/commands/__init__.py b/boards/management/commands/__init__.py new file mode 100644 --- /dev/null +++ b/boards/management/commands/__init__.py @@ -0,0 +1,1 @@ +__author__ = 'vurdalak' diff --git a/boards/management/commands/cleanusers.py b/boards/management/commands/cleanusers.py new file mode 100644 --- /dev/null +++ b/boards/management/commands/cleanusers.py @@ -0,0 +1,29 @@ +from datetime import datetime, timedelta +from django.core.management import BaseCommand +from django.db import transaction +from django.db.models import Count +from boards.models import User, Post + +__author__ = 'neko259' + +OLD_USER_AGE_DAYS = 90 + + +class Command(BaseCommand): + help = 'Removes empty users (that don\'t have posts or tags' + + @transaction.atomic + def handle(self, *args, **options): + old_registration_date = datetime.now().date() - timedelta( + OLD_USER_AGE_DAYS) + + old_users = User.objects.annotate(tags_count=Count('fav_tags')).filter( + tags_count=0).filter(registration_time__lt=old_registration_date) + deleted_users = 0 + for user in old_users: + if not Post.objects.filter(user=user).exists(): + self.stdout.write('Deleting user %s' % user.user_id) + user.delete() + deleted_users += 1 + + self.stdout.write('Deleted %d users' % deleted_users) \ No newline at end of file diff --git a/boards/tests.py b/boards/tests.py --- a/boards/tests.py +++ b/boards/tests.py @@ -161,8 +161,7 @@ class PagesTest(TestCase): response_not_existing = client.get(THREAD_PAGE + str( existing_post_id + 1) + '/') - self.assertEqual(PAGE_404, - response_not_existing.templates[0].name, + self.assertEqual(PAGE_404, response_not_existing.templates[0].name, u'Not existing thread is opened') response_existing = client.get(TAG_PAGE + tag_name + '/') @@ -177,7 +176,7 @@ class PagesTest(TestCase): reply_id = Post.objects.create_post('', TEST_TEXT, thread=Post.objects.all()[0] - .thread) + .get_thread()) response_not_existing = client.get(THREAD_PAGE + str( reply_id) + '/') self.assertEqual(PAGE_404, diff --git a/boards/views/api.py b/boards/views/api.py --- a/boards/views/api.py +++ b/boards/views/api.py @@ -37,9 +37,6 @@ def api_get_threaddiff(request, thread_i thread = get_object_or_404(Post, id=thread_id).get_thread() - logger.info('Getting thread #%s diff since %s' % (thread_id, - last_update_time)) - filter_time = datetime.fromtimestamp(float(last_update_time) / 1000000, timezone.get_current_timezone()) diff --git a/boards/views/base.py b/boards/views/base.py --- a/boards/views/base.py +++ b/boards/views/base.py @@ -13,8 +13,6 @@ import neboard BAN_REASON_SPAM = 'Autoban: spam bot' -OLD_USER_AGE_DAYS = 90 - PARAMETER_FORM = 'form' @@ -74,9 +72,6 @@ class BaseBoardView(View): user = User.objects.create(user_id=new_id, rank=RANK_USER, registration_time=time_now) - # TODO Move this to manage.py commands - #self._delete_old_users() - session['user_id'] = user.id else: user = User.objects.select_related('fav_tags').get( @@ -97,21 +92,6 @@ class BaseBoardView(View): return theme - def _delete_old_users(self): - """ - Delete users with no favorite tags and posted messages. These can be spam - bots or just old user accounts - """ - - old_registration_date = datetime.now().date() - timedelta( - OLD_USER_AGE_DAYS) - - for user in User.objects.annotate(tags_count=Count('fav_tags')).filter( - tags_count=0).filter( - registration_time__lt=old_registration_date): - if not Post.objects.filter(user=user).exists(): - user.delete() - @transaction.atomic def _ban_current_user(self, request): """ diff --git a/boards/views/thread.py b/boards/views/thread.py --- a/boards/views/thread.py +++ b/boards/views/thread.py @@ -23,7 +23,10 @@ PARAMETER_BUMPABLE = 'bumpable' class ThreadView(BaseBoardView, PostMixin, FormMixin): def get(self, request, post_id, mode=MODE_NORMAL, form=None): - opening_post = Post.objects.filter(id=post_id).only('thread_new')[0] + try: + opening_post = Post.objects.filter(id=post_id).only('thread_new')[0] + except IndexError: + raise Http404 # If this is not OP, don't show it as it is if not opening_post or not opening_post.is_opening():