|
|
@ -0,0 +1,48 @@ |
|
|
|
|
|
import json |
|
|
|
|
|
from django.core.management.base import BaseCommand |
|
|
|
|
|
from reymotausers.models import ReyMotaUser |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Command(BaseCommand): |
|
|
|
|
|
help = "Importa usuarios desde un archivo JSON" |
|
|
|
|
|
|
|
|
|
|
|
def add_arguments(self, parser): |
|
|
|
|
|
parser.add_argument('archivo_json', type=str, help="Ruta del archivo JSON") |
|
|
|
|
|
|
|
|
|
|
|
def handle(self, *args, **kwargs): |
|
|
|
|
|
archivo_json = kwargs['archivo_json'] |
|
|
|
|
|
|
|
|
|
|
|
try: |
|
|
|
|
|
with open(archivo_json, 'r', encoding='utf-8') as file: |
|
|
|
|
|
datos = json.load(file) |
|
|
|
|
|
|
|
|
|
|
|
self.stdout.write(self.style.WARNING(f"\nSe encontraron {len(datos)} usuarios en el archivo '{archivo_json}'.")) |
|
|
|
|
|
confirmar = input("¿Deseas continuar con la importación? (s/n): ").strip().lower() |
|
|
|
|
|
|
|
|
|
|
|
if confirmar != 's': |
|
|
|
|
|
self.stdout.write(self.style.ERROR("Importación cancelada.")) |
|
|
|
|
|
return |
|
|
|
|
|
|
|
|
|
|
|
usuarios_creados = 0 |
|
|
|
|
|
for usuario_data in datos: |
|
|
|
|
|
creado = ReyMotaUser.objects.create( |
|
|
|
|
|
foto=usuario_data['foto'], |
|
|
|
|
|
password=usuario_data['password'], |
|
|
|
|
|
is_superuser=usuario_data['is_superuser'], |
|
|
|
|
|
is_staff=usuario_data['is_staff'], |
|
|
|
|
|
is_active=usuario_data['is_active'], |
|
|
|
|
|
nombre=usuario_data['nombre'], |
|
|
|
|
|
email=usuario_data['email'], |
|
|
|
|
|
groups=usuario_data['groups'], |
|
|
|
|
|
user_permissions=usuario_data['user_permissions'], |
|
|
|
|
|
last_login=usuario_data['last_login'] |
|
|
|
|
|
) |
|
|
|
|
|
if creado: |
|
|
|
|
|
usuarios_creados += 1 |
|
|
|
|
|
|
|
|
|
|
|
self.stdout.write(self.style.SUCCESS(f'Se importaron {usuarios_creados} usuarios correctamente.')) |
|
|
|
|
|
|
|
|
|
|
|
except FileNotFoundError: |
|
|
|
|
|
self.stderr.write(self.style.ERROR(f"El archivo {archivo_json} no se encontró.")) |
|
|
|
|
|
except json.JSONDecodeError: |
|
|
|
|
|
self.stderr.write(self.style.ERROR("Error al leer el archivo JSON. Asegúrate de que el formato sea correcto.")) |