def handle(self, *args, **options): connection = get_ldap_connection() base_dn = settings.AUTH_LDAP_USER_SEARCH.base_dn # Get all students. print("Retrieving students") student_synced = set() connection.search(base_dn, "(objectClass=eleve)", attributes='*') print("%s students found" % len(connection.response)) processed = 0 # Add/update students. for s in connection.response: student_dict = get_django_dict_from_ldap(s) # Check if the student already exists. try: student = StudentModel.objects.get(matricule=int(student_dict['matricule'])) except ObjectDoesNotExist: student = StudentModel(matricule=int(student_dict['matricule'])) # Check if student's teaching already exists. try: teaching = TeachingModel.objects.get(name=student_dict['teaching'][0]) except ObjectDoesNotExist: teaching = TeachingModel(name=student_dict['teaching'][0], display_name=student_dict['teaching'][0].title()) teaching.save() student.teaching = teaching # Check if student's classe already exists. try: classe = ClasseModel.objects.get(year=student_dict['year'], letter=student_dict['classe_letter'].lower(), teaching=teaching) except ObjectDoesNotExist: classe = ClasseModel(year=student_dict['year'], letter=student_dict['classe_letter'].lower(), teaching=teaching) classe.save() student.classe = classe student.first_name = student_dict['first_name'] student.last_name = student_dict['last_name'] student.save() # Additional info. try: info = student.additionalstudentinfo except ObjectDoesNotExist: info = AdditionalStudentInfo(student=student) info.gender = student_dict['gender'] info.scholar_year = student_dict['scholar_year'] info.previous_classe = student_dict['previous_classe'] info.orientation = student_dict['orientation'] info.birth_date = student_dict['birth_date'] info.street = student_dict['street'] info.postal_code = student_dict['postal_code'] info.locality = student_dict['locality'] info.student_phone = student_dict['student_phone'] info.student_mobile = student_dict['student_phone'] info.student_email = student_dict['student_email'] info.resp_last_name = student_dict['resp_last_name'] info.resp_first_name = student_dict['resp_first_name'] info.resp_phone = student_dict['resp_phone'] info.resp_mobile = student_dict['resp_mobile'] info.resp_email = student_dict['resp_email'] info.father_last_name = student_dict['father_last_name'] info.father_first_name = student_dict['father_first_name'] info.father_job = student_dict['father_job'] info.father_phone = student_dict['father_phone'] info.father_mobile = student_dict['father_mobile'] info.father_email = student_dict['father_email'] info.mother_last_name = student_dict['mother_last_name'] info.mother_first_name = student_dict['mother_first_name'] info.mother_job = student_dict['mother_job'] info.mother_phone = student_dict['mother_phone'] info.mother_mobile = student_dict['mother_mobile'] info.mother_email = student_dict['mother_email'] info.doctor = student_dict['doctor'] info.doctor_phone = student_dict['doctor_phone'] info.mutual = student_dict['mutual'] info.mutual_number = student_dict['mutual_number'] info.medical_information = student_dict['medical_information'] info.username = student_dict['username'] info.password = student_dict['password'] info.save() student_synced.add(student.matricule) processed += 1 if processed % 50 == 0: print(processed) # Remove removed students. all_students = StudentModel.objects.all() for s in all_students: if s.matricule not in student_synced: s.delete() # Get all responsibles. print("Retrieving responsibles") connection.search(base_dn, "(objectClass=responsible)", attributes='*') print("%s responsibles found" % len(connection.response)) processed = 0 resp_synced = set() for r in connection.response: resp_dict = get_django_dict_from_ldap(r) # Check if the responsible already exists. try: resp = ResponsibleModel.objects.get(matricule=int(resp_dict['matricule'])) except ObjectDoesNotExist: try: user = User.objects.get(username=resp_dict['username']) except ObjectDoesNotExist: user = User.objects.create_user(resp_dict['username']) resp = ResponsibleModel(matricule=int(resp_dict['matricule']), user=user) if 'professeur' in r['attributes']['objectClass']: resp.is_teacher = True if 'educateur' in r['attributes']['objectClass']: resp.is_educator = True resp.save() # Check if responsible's teaching already exists. for t in resp_dict['teaching']: try: teaching = TeachingModel.objects.get(name=t) except ObjectDoesNotExist: teaching = TeachingModel(name=t, display_name=t.title()) teaching.save() resp.teaching.add(teaching) # Check if responsible's classes already exists. if 'classe' in resp_dict: for c in resp_dict['classe']: try: classe = ClasseModel.objects.get(year=int(c[0]), letter=c[1].lower(), teaching=teaching) except ObjectDoesNotExist: classe = ClasseModel(year=int(c[0]), letter=c[1].lower(), teaching=teaching) classe.save() resp.classe.add(classe) # Check if responsible's tenures already exists. if 'tenure' in resp_dict: for t in resp_dict['tenure']: try: tenure = ClasseModel.objects.get(year=int(t[0]), letter=t[1].lower(), teaching=teaching) except ObjectDoesNotExist: tenure = ClasseModel(year=int(t[0]), letter=t[1].lower(), teaching=teaching) tenure.save() resp.tenure.add(tenure) resp.classe.add(tenure) if 'resp_email' in resp_dict: resp.email = resp_dict['resp_email'] if 'gen_email' in resp_dict: resp.email_alias = resp_dict['gen_email'] resp.first_name = resp_dict['first_name'] resp.last_name = resp_dict['last_name'] resp.save() resp_synced.add(resp.matricule) processed += 1 if processed % 25 == 0: print(processed) # Remove removed responsibles. all_resp = ResponsibleModel.objects.all() for r in all_resp: if r.matricule not in resp_synced: r.delete()
def _sync(self, iterable) -> None: if not self.teaching: self.print_log("teaching is missing, aborting.") return processed = 0 resp_synced = set() if self.search_login_directory: self.ldap_connection = get_ldap_connection() self.base_dn = settings.AUTH_LDAP_USER_SEARCH.base_dn self.print_log("Importing responsibles…(%s)" % self.teaching.display_name) for entry in iterable: # First check mandatory field. first_name = self.get_value(entry, "first_name") if not first_name: self.print_log("No first name found, skipping responsible.") continue last_name = self.get_value(entry, "last_name") if not last_name: self.print_log("No last name found, skipping responsible.") continue resp = self.get_responsible(entry) if not resp: self.print_log("No unique identifier found, skipping responsible.") continue username = self.get_value(entry, self.username_attribute) if self.search_login_directory: matricule = self.get_value(entry, "matricule") self.ldap_connection.search(self.base_dn, "(%s=%i)" % (self.ldap_unique_attr, matricule), attributes='*') for r in self.ldap_connection.response: ldap_info = get_django_dict_from_ldap(r) username = ldap_info['username'] if username and len(username.strip(" ")) > 0: try: user = User.objects.get(username=username) except ObjectDoesNotExist: user = User.objects.create_user(username) user.last_name = last_name user.first_name = first_name if "email" in self.username_attribute: user.email = user.username user.save() resp.user = user resp.first_name = first_name resp.last_name = last_name resp.inactive_from = None resp.save() resp.teaching.add(self.teaching) if self.has_inactivity: inactive_from = self.get_value(entry, "inactive_from") if inactive_from: resp.inactive_from = timezone.make_aware( timezone.datetime.combine(inactive_from, timezone.datetime.min.time())) else: resp.inactive_from = None educ_group = Group.objects.get(name="educateur") is_educator = self.get_value(entry, "is_educator") if is_educator: resp.is_educator = True if resp.user: educ_group.user_set.add(resp.user) teach_group = Group.objects.get(name="professeur") is_teacher = self.get_value(entry, "is_teacher") if is_teacher: resp.is_teacher = True if resp.user: teach_group.user_set.add(resp.user) # Update classes and course only for teachers if is_teacher: # Check if responsible's classes already exists. if resp.matricule not in resp_synced: resp.classe.remove(*resp.classe.filter(teaching=self.teaching)) resp.courses.remove(*resp.courses.filter(course__teaching=self.teaching)) classe = self.get_value(entry, "classe") if classe and type(classe) != list: classe = [classe] if classe: for c in classe: if len(c) < 2: continue try: classe_model = ClasseModel.objects.get(year=int(c[0]), letter=c[1:].lower(), teaching=self.teaching) except ObjectDoesNotExist: classe_model = ClasseModel(year=int(c[0]), letter=c[1:].lower(), teaching=self.teaching) classe_model.save() resp.classe.add(classe_model) courses = self.get_value(entry, "courses") if courses and type(courses) != list: courses = [courses] if courses: for c in courses: if not c["classes"]: continue try: course_model = CourseModel.objects.get(id=c["id"]) except ObjectDoesNotExist: course_model = CourseModel(id=c["id"], name=c["name"], teaching=self.teaching) course_model.save() try: given_course = GivenCourseModel.objects.get(course=course_model, group=c["group"]) except ObjectDoesNotExist: given_course = GivenCourseModel(course=course_model, group=c["group"]) given_course.save() resp.courses.add(given_course) # Check if responsible's tenures already exists. if resp.matricule not in resp_synced: resp.tenure.clear() tenure = self.get_value(entry, "tenure") if tenure: if type(tenure) == str: tenure = [tenure] for t in tenure: try: tenure_model = ClasseModel.objects.get(year=int(t[0]), letter=t[1:].lower(), teaching=self.teaching) except ObjectDoesNotExist: tenure_model = ClasseModel(year=int(t[0]), letter=t[1:].lower(), teaching=self.teaching) tenure_model.save() resp.tenure.add(tenure_model) resp.classe.add(tenure_model) email = self.get_value(entry, "email") email_school = self.get_value(entry, "email_school") if email: resp.email = email if email_school: resp.email_school = email_school birth_date = self.get_value(entry, "birth_date") if birth_date: resp.birth_date = birth_date resp.save() if not resp.matricule in resp_synced: processed += 1 if processed % 50 == 0: self.print_log(processed) if is_teacher: resp_synced.add(resp.matricule) # Set inactives teachers. if len(resp_synced) != 0: self.print_log("Set inactive teachers…") all_resp = ResponsibleModel.objects.filter(teaching=self.teaching, is_teacher=True) for r in all_resp: if r.matricule not in resp_synced: if not self.has_inactivity: r.inactive_from = timezone.make_aware(timezone.datetime.now()) r.classe.clear() r.tenure.clear() r.courses.clear() r.save() else: r.delete() self.print_log("Import done.")
def _sync(self, iterable) -> None: if not self.teaching: self.print_log("teaching is missing, aborting.") return processed = 0 student_synced = set() if self.search_login_directory: self.ldap_connection = get_ldap_connection() self.base_dn = settings.AUTH_LDAP_USER_SEARCH.base_dn self.print_log("Importing students…(%s)" % self.teaching.display_name) for entry in iterable: # First check mandatory field. matricule = int(self.get_value(entry, "matricule")) if not matricule: self.print_log("No matricule found, skipping student.") continue first_name = self.get_value(entry, "first_name") if not first_name: self.print_log("No first name found, skipping student.") continue last_name = self.get_value(entry, "last_name") if not last_name: self.print_log("No last name found, skipping student.") continue year = self.get_value(entry, "year") if not year: self.print_log("No year found, skipping student (%s %s)." % (last_name, first_name)) continue classe_letter = self.get_value(entry, "classe_letter") if not classe_letter: self.print_log("No classe letter found, skipping student (%s %s)." % (last_name, first_name)) continue try: student = StudentModel.objects.get(matricule=matricule) student.inactive_from = None except ObjectDoesNotExist: student = StudentModel(matricule=matricule) student.first_name = first_name student.last_name = last_name student.teaching = self.teaching # Check if student's classe already exists. try: classe = ClasseModel.objects.get(year=year, letter=classe_letter, teaching=self.teaching) except ObjectDoesNotExist: classe = ClasseModel(year=year, letter=classe_letter, teaching=self.teaching) classe.save() student.classe = classe courses = self.get_value(entry, "courses") if courses and type(courses) != list: courses = [courses] if courses: for c in courses: try: course_model = CourseModel.objects.get(id=c["id"]) except ObjectDoesNotExist: course_model = CourseModel(id=c["id"], name=c["name"], teaching=self.teaching) course_model.save() try: given_course = GivenCourseModel.objects.get(course=course_model, group=c["group"]) except ObjectDoesNotExist: given_course = GivenCourseModel(course=course_model, group=c["group"]) given_course.save() student.courses.add(given_course) student.save() student_synced.add(student.matricule) # Print progress. processed += 1 if processed % 50 == 0: self.print_log(processed) # Additional info. try: info = student.additionalstudentinfo except ObjectDoesNotExist: info = AdditionalStudentInfo(student=student) for c in self.additional_columns: val = self.get_value(entry, c) if val: setattr(info, c, val) else: if c == "birth_date": continue setattr(info, c, "") if self.search_login_directory: self.ldap_connection.search(self.base_dn, "(matricule=%i)" % matricule, attributes='*') for r in self.ldap_connection.response: ldap_info = get_django_dict_from_ldap(r) info.username = ldap_info['username'] info.password = ldap_info['password'] info.save() # Set inactives. self.print_log("Set inactive students…") all_students = StudentModel.objects.filter(teaching=self.teaching) for s in all_students: if s.matricule not in student_synced: s.inactive_from = timezone.make_aware(timezone.datetime.now()) s.classe = None s.courses.clear() s.save() self.print_log("Import done.")
def __init__(self, teaching: TeachingModel) -> None: super().__init__(teaching=teaching, search_login_directory=False) self.connection = get_ldap_connection() self.base_dn = settings.AUTH_LDAP_USER_SEARCH.base_dn
def __init__(self, teaching: TeachingModel) -> None: super().__init__(teaching=teaching) self.connection = get_ldap_connection() self.base_dn = settings.AUTH_LDAP_USER_SEARCH.base_dn