diff --git a/boards/views/__init__.py b/boards/views/__init__.py --- a/boards/views/__init__.py +++ b/boards/views/__init__.py @@ -1,3 +1,8 @@ +from datetime import datetime, timedelta +from django.db.models import Count + +OLD_USER_AGE_DAYS = 90 + __author__ = 'neko259' import hashlib @@ -521,6 +526,8 @@ def _get_user(request): user = User.objects.create(user_id=new_id, rank=RANK_USER, registration_time=time_now) + _delete_old_users() + session['user_id'] = user.id else: user = User.objects.get(id=session['user_id']) @@ -588,3 +595,17 @@ def _get_template_thread(thread_to_show) 'last_replies': last_replies, 'skipped_replies': skipped_replies_count, } + + +def _delete_old_users(): + """ + 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() diff --git a/changelog.markdown b/changelog.markdown --- a/changelog.markdown +++ b/changelog.markdown @@ -7,4 +7,5 @@ thread. * Added new gallery with search links and image metadata # 1.6 Amon # -* Deleted threads are moved to archive instead of permanent delete \ No newline at end of file +* Deleted threads are moved to archive instead of permanent delete +* User management fixes and optimizations \ No newline at end of file