From 939c18f45f7dda1d268ee640a4bbcdb8f1fc02c3 Mon Sep 17 00:00:00 2001 From: Tom Hubrecht Date: Tue, 23 Feb 2021 22:32:52 +0100 Subject: [PATCH] =?UTF-8?q?On=20utilise=20les=20fonctions=20d=C3=A9j=C3=A0?= =?UTF-8?q?=20pr=C3=A9sentes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- fiches/utils.py | 56 +++++++++---------------------------------------- 1 file changed, 10 insertions(+), 46 deletions(-) diff --git a/fiches/utils.py b/fiches/utils.py index 1d19112..e6c1cac 100644 --- a/fiches/utils.py +++ b/fiches/utils.py @@ -1,51 +1,11 @@ -import ldap - -from django.conf import settings - -verbose_depts = { - "bio": "Biologie", - "chimie": "Chimie", - "dec": "Études Cognitives", - "geol": "Géosciences", - "guests": "Invité·e", - "info": "Informatique", - "litt": "Lettres", - "maths": "Mathématiques", - "pei": "PEI", - "phy": "Physique", -} - - -def parse_dept(home_dir): - """Extrait le département d'entrée d'un·e élève à partir de son dossier. - Le dossier a le format `/users///`. - """ - users, promo, dept, login = home_dir.split("/")[1:] - - if users != "users": - raise ValueError("Invalid home directory") - - # Ça casse en 2100, mais le système de naming de sas aussi... - promo = 2000 + int(promo) - - return promo, verbose_depts.get(dept, None) - - -def extract_ldap_info(entry, field): - return entry[1].get(field, [b""])[0].decode("utf-8") +from .management.commands._ldap import ClipperLDAP def get_ldap_infos(clipper_login): - ldap_server = settings.LDAP["SPI"] - ldap_obj = ldap.initialize("{PROTOCOL}://{URL}:{PORT}".format(**ldap_server)) + ldap = ClipperLDAP() try: - res = ldap_obj.search_s( - "dc=spi,dc=ens,dc=fr", - ldap.SCOPE_SUBTREE, - "(uid={})".format(clipper_login), - ["uid", "cn", "homeDirectory"], - ) - except ldap.SERVER_DOWN: + res = ldap.search("(uid={})".format(clipper_login)) + except Exception: return None if not res: @@ -54,6 +14,10 @@ def get_ldap_infos(clipper_login): if len(res) != 1: raise RuntimeError("LDAP returned too many results: {}".format(res)) (res,) = res - promo, dept = parse_dept(extract_ldap_info(res, "homeDirectory")) + promo, dept = ldap.parse_dept(ldap.extract_ldap_info(res, "homeDirectory")) - return {"name": extract_ldap_info(res, "cn"), "promo": promo, "dept": dept} + return { + "name": ldap.extract_ldap_info(res, "cn"), + "promo": promo, + "dept": dept, + }