def refresh_from_ldap(cls, username: str, password: str) -> bool: """ Refreshes all users from ldap and the LocalStudent db""" from tqdm import tqdm # Load all the users from LDAP print('** READING DATA FROM LDAP **') from jacobsdata.parsing import user users = user.parse_all_users(username, password) # if we get no users, there was an error in authentication if users is None: return False # mark all of the current ones inactive print('** DISABLING OLD USERS **') cls.objects.all().update(active=False) print('** UPDATING USERS **') for u in tqdm(users): s = Student.from_json(u) eid = s.pop("eid") (stud, sup) = cls.objects.update_or_create(eid=eid, defaults=s) stud.localise() # and we are done return True
def handle(self, *args, **options): # check what to store. if options["students"] is not None: store_student = True student_fn = options["students"] else: store_student = False student_fn = None if options["courses"] is not None: store_course = True course_fn = options["courses"] else: store_course = False course_fn = None # if we have nothing to store, do nothing. if (not store_course) and (not store_student): self.stderr.write("No data to be stored, nothing to do. ") return # check username and password if options["username"] is None: self.stdout.write("Username: "******"username"] if options["password"] is None: pwd = getpass("Password for %s:" % usr) else: pwd = options["password"] self.stdout.write("Retrieving data from LDAP ...") # if we have to store courses, get everything if store_course: (u, c) = data.parse_all(usr, pwd) # else get only users else: u = user.parse_all_users(usr, pwd) c = None self.stdout.write("Done. \n") # Store Student file if store_student: json.dump(u, open(student_fn, 'w'), sort_keys=False, indent=4) self.stdout.write("Wrote file %r.\n" % student_fn) # Store Courses file if store_course: json.dump(c, open(course_fn, 'w'), sort_keys=False, indent=4) self.stdout.write("Wrote file %r.\n" % course_fn)
def parse_all(u: str, password: str) -> typing.Tuple[ typing.List[dict], typing.List[dict]]: """ Parses users and courses from LDAP. """ # parse all the users users = user.parse_all_users(u, password) if users is None: return (None, None) courses = course.parse_all_courses(u, password, users) if courses is None: return (None, None) return (users, courses)