Add a tool for purging users

This commit is contained in:
Jake Moshenko 2016-02-05 16:01:59 -05:00
parent d5920319de
commit 95c7c2090a

52
tools/deleteaccount.py Normal file
View file

@ -0,0 +1,52 @@
import logging
import sys
from app import app
from data import model
from data.database import Repository, User
logger = logging.getLogger(__name__)
def delete_summary(username):
found = User.get(User.username == username)
dependencies = found.dependencies(search_nullable=False)
counts = {}
for expression, field in dependencies:
use_count = field.model_class.select().where(expression).count()
if use_count > 0:
counts[field.model_class.__name__] = use_count
return counts
def delete_username(username):
# First delete the repositories owned by this user
found = User.get(User.username == username)
for repository in list(Repository.select().where(Repository.namespace_user == found)):
print 'Deleting repository: {0}/{1}'.format(found.username, repository.name)
model.repository.purge_repository(found.username, repository.name)
# Now recursively delete user, which will clean up all non-nullable referencing fields
print 'Deleting user: {0}'.format(username)
found.delete_instance(recursive=True)
if __name__ == '__main__':
logging.basicConfig(level=logging.DEBUG)
summary = delete_summary(sys.argv[1]).items()
if summary:
print 'Will delete:'
for model_name, count in summary:
print '{0}: {1}'.format(model_name, count)
else:
print 'Nothing references user.'
print 'Delete? [y/n]'
delete_yn = raw_input().lower().strip()
if delete_yn == 'y':
delete_username(sys.argv[1])
else:
print 'Aborted!'