cleanusers.py
28 lines
| 972 B
| text/x-python
|
PythonLexer
neko259
|
r689 | 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) |