Add a script to bulk delete users

This commit is contained in:
Wil Clouser 2010-08-04 13:25:09 -07:00
Родитель df6b6575b8
Коммит 1758532dfd
5 изменённых файлов: 76 добавлений и 1 удалений

Просмотреть файл

Просмотреть файл

Просмотреть файл

@ -0,0 +1,55 @@
import os
import sys
from django.conf import settings
from django.core.management.base import BaseCommand
from django.db import connection, transaction
from celery.messaging import establish_connection
class Command(BaseCommand):
args = "<file>"
help = """This command accepts a new-line separated file of user id's to
delete from the database. See [1] for the policy.
[1]
http://blog.mozilla.com/addons/2010/07/26/upcoming-changes-to-amo-accounts/
"""
def handle(self, *args, **options):
# Avoiding loops
from amo.utils import chunked, slugify
from users.models import UserProfile
from users.tasks import _delete_users
if not args:
print "Usage: manage.py delete_users <file>"
sys.exit(1)
if not os.path.exists(args[0]):
print "File not found: %s" % args[0]
sys.exit(1)
f = open(args[0], 'r')
data = True
print "Reading %s" % args[0]
while data:
data = f.readlines(100000) # 100000 is about 13500 user ids
data = [x.strip() for x in data] # has newlines on it
print "Sending %s users to celery" % len(data)
with establish_connection() as conn:
for chunk in chunked(data, 100):
_delete_users.apply_async(args=[chunk], connection=conn)
f.close()
print "All done."

Просмотреть файл

@ -1 +1,20 @@
import commonware.log
from celery.decorators import task
from . import cron
from django.contrib.auth.models import User as DjangoUser
from users.models import UserProfile
task_log = commonware.log.getLogger('z.task')
@task(rate_limit='10/m')
def _delete_users(data, **kw):
"""Feed me a list of user ids you want to delete from the database. This
isn't a flag, it actually deletes rows."""
task_log.info("[%s@%s] Bulk deleting users" %
(len(data), _delete_users.rate_limit))
UserProfile.objects.filter(pk__in=data).delete()
DjangoUser.objects.filter(pk__in=data).delete()

Просмотреть файл

@ -154,7 +154,8 @@ class VersionComment(amo.models.ModelBase):
"""Editor comments for version discussion threads."""
version = models.ForeignKey(Version)
user = models.ForeignKey(UserProfile)
reply_to = models.ForeignKey(Version, related_name="reply_to", null=True)
reply_to = models.ForeignKey(Version, related_name="reply_to",
db_column='reply_to', null=True)
subject = models.CharField(max_length=1000)
comment = models.TextField()