52 lines
1.5 KiB
Python
52 lines
1.5 KiB
Python
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!'
|