2020-11-13 11:45:32 +01:00
|
|
|
from datetime import date
|
|
|
|
|
|
|
|
from django.contrib.auth.models import User
|
2020-11-21 19:19:35 +01:00
|
|
|
from django.core.management.base import BaseCommand, CommandError
|
2020-11-13 11:45:32 +01:00
|
|
|
|
|
|
|
from fiches.models import Department, Profile
|
|
|
|
|
2020-11-13 17:23:17 +01:00
|
|
|
from ._ldap import ClipperLDAP
|
2020-11-13 11:45:32 +01:00
|
|
|
|
|
|
|
|
|
|
|
class Command(BaseCommand):
|
2020-11-13 17:23:17 +01:00
|
|
|
help = "Crée les fiches annuaire des conscrit·e·s automatiquement"
|
2020-11-13 11:45:32 +01:00
|
|
|
|
|
|
|
def add_arguments(self, parser):
|
|
|
|
group = parser.add_mutually_exclusive_group()
|
|
|
|
group.add_argument(
|
|
|
|
"--all", action="store_true", help="Importe l'intégralité des promotions"
|
|
|
|
)
|
2020-11-21 17:45:26 +01:00
|
|
|
group.add_argument("--promo", type=int, help="Spécifie la promotion à importer")
|
2020-11-13 11:45:32 +01:00
|
|
|
|
|
|
|
def get_current_promo(self):
|
|
|
|
today = date.today()
|
|
|
|
year = today.year
|
|
|
|
if today.month < 9:
|
|
|
|
year -= 1
|
|
|
|
|
2020-11-21 17:45:26 +01:00
|
|
|
return year
|
2020-11-13 11:45:32 +01:00
|
|
|
|
|
|
|
def handle(self, *args, **options):
|
|
|
|
if options["all"]:
|
|
|
|
promo = None
|
|
|
|
elif options["promo"] is not None:
|
|
|
|
promo = options["promo"]
|
2020-11-21 17:45:26 +01:00
|
|
|
if promo < 100:
|
|
|
|
promo = 2000 + promo
|
2020-11-13 11:45:32 +01:00
|
|
|
else:
|
|
|
|
promo = self.get_current_promo()
|
|
|
|
|
2020-11-21 19:21:43 +01:00
|
|
|
if promo < 2000 or promo > 2100:
|
2020-11-21 19:19:35 +01:00
|
|
|
raise CommandError("Promotion invalide : {}".format(promo))
|
|
|
|
|
2020-11-21 17:45:55 +01:00
|
|
|
verbosity = options["verbosity"]
|
|
|
|
|
2020-11-13 11:45:32 +01:00
|
|
|
# On récupère la liste des élèves à créer
|
|
|
|
ldap = ClipperLDAP()
|
2020-11-21 17:45:55 +01:00
|
|
|
clipper_list = ldap.get_clipper_list(
|
|
|
|
promo_filter=promo, verbosity=verbosity, stdout=self.stdout
|
|
|
|
)
|
2020-11-13 11:45:32 +01:00
|
|
|
|
|
|
|
# On vire les élèves déjà existants
|
|
|
|
existing_users = set(User.objects.values_list("username", flat=True))
|
|
|
|
clippers = [
|
|
|
|
clipper for clipper in clipper_list if clipper.uid not in existing_users
|
|
|
|
]
|
|
|
|
|
|
|
|
# Les départements sont créés à la main ; il y en a peu.
|
|
|
|
depts = {
|
|
|
|
dept: Department.objects.get_or_create(name=dept)[0].id
|
|
|
|
for dept in ldap.verbose_depts.values()
|
|
|
|
}
|
|
|
|
|
|
|
|
users_to_create = []
|
|
|
|
profiles_to_create = []
|
|
|
|
dept_m2m_to_create = []
|
|
|
|
|
|
|
|
for clipper in clippers:
|
|
|
|
user = User(username=clipper.uid, email=clipper.email)
|
|
|
|
profile = Profile(user=user, full_name=clipper.name, promotion=clipper.year)
|
|
|
|
users_to_create.append(user)
|
|
|
|
profiles_to_create.append(profile)
|
|
|
|
dept_m2m_to_create.append(
|
|
|
|
Profile.department.through(
|
|
|
|
profile=profile, department_id=depts[clipper.dept]
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2020-11-21 17:45:55 +01:00
|
|
|
# À décommenter pour utilisation locale (avec SQLite)
|
|
|
|
|
2020-11-21 19:17:45 +01:00
|
|
|
# def _manual_ids(cls, to_create):
|
|
|
|
# pid = getattr(cls.objects.order_by("-id").first(), "id", 1)
|
|
|
|
# for p in to_create:
|
|
|
|
# pid += 1
|
|
|
|
# p.id = pid
|
|
|
|
|
|
|
|
# _manual_ids(User, users_to_create)
|
|
|
|
# _manual_ids(Profile, profiles_to_create)
|
|
|
|
# _manual_ids(Profile.department.through, dept_m2m_to_create)
|
2020-11-21 17:45:55 +01:00
|
|
|
|
2020-11-13 11:45:32 +01:00
|
|
|
User.objects.bulk_create(users_to_create)
|
|
|
|
for profile in profiles_to_create:
|
|
|
|
profile.user_id = profile.user.id
|
2020-11-13 17:23:17 +01:00
|
|
|
Profile.objects.bulk_create(profiles_to_create)
|
2020-11-13 11:45:32 +01:00
|
|
|
for dept_m2m in dept_m2m_to_create:
|
|
|
|
dept_m2m.profile_id = dept_m2m.profile.id
|
|
|
|
Profile.department.through.objects.bulk_create(dept_m2m_to_create)
|
|
|
|
|
2020-11-21 17:45:55 +01:00
|
|
|
if verbosity >= 1:
|
|
|
|
self.stdout.write(
|
|
|
|
(
|
|
|
|
"Création de {} utilisateur·ices et de {}"
|
|
|
|
" profils effectuée avec succès"
|
|
|
|
).format(len(users_to_create), len(profiles_to_create))
|
2020-11-13 17:23:17 +01:00
|
|
|
)
|