init
This commit is contained in:
46
app/user/management/commands/import_old_users.py
Normal file
46
app/user/management/commands/import_old_users.py
Normal file
@@ -0,0 +1,46 @@
|
||||
from django.core.management.base import BaseCommand
|
||||
|
||||
import json
|
||||
import base64
|
||||
import sys
|
||||
|
||||
from user.models import User
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
def handle(self, *args, **options):
|
||||
# comment utiliser :
|
||||
# se connect en bash à l'ancien environnement, ensuite taper : python manage.py dumpdata member.User | base64 -w 0
|
||||
# se connecter en bash au nouvel environnement et ensuite taper : echo -e "le_précédent_output..." | python manage.py import_old_users
|
||||
|
||||
inp = ""
|
||||
for i in sys.stdin:
|
||||
inp += i
|
||||
|
||||
old_users = json.loads(base64.b64decode(inp.encode("utf-8")))
|
||||
|
||||
old_new_users_maps = {}
|
||||
old_friends = {}
|
||||
|
||||
for data in old_users:
|
||||
user_data = data["fields"]
|
||||
user_pk = data["pk"]
|
||||
if User.objects.filter(username__iexact=user_data["username"]).exists():
|
||||
old_new_users_maps[user_pk] = User.objects.filter(username__iexact=user_data["username"]).get()
|
||||
else:
|
||||
old_new_users_maps[user_pk] = User.objects.create(
|
||||
email=user_data["email"],
|
||||
is_active=user_data["is_active"],
|
||||
is_staff=user_data["is_staff"],
|
||||
is_superuser=user_data["is_superuser"],
|
||||
username=user_data["username"],
|
||||
password=user_data["password"],
|
||||
max_size=user_data["limit_size"]
|
||||
)
|
||||
old_friends[user_pk] = user_data["friends"]
|
||||
|
||||
for old_user, friends in old_friends.items():
|
||||
current_user = old_new_users_maps[old_user]
|
||||
for friend in friends:
|
||||
if not current_user.friends.filter(id=old_new_users_maps[friend].id).exists():
|
||||
current_user.friends.add(old_new_users_maps[friend])
|
||||
Reference in New Issue
Block a user