def load_teacher(sciper): logger.info("loading teacher") logger.debug(sciper) try: teacher = Person.objects.get(sciper=sciper) except ObjectDoesNotExist: teacher = Person() teacher.sciper = sciper teacher.username = get_username(sciper) teacher.first_name = get_firstname(sciper) teacher.last_name = get_lastname(sciper) teacher.email = get_email(sciper) if ( teacher.first_name is None or teacher.last_name is None or teacher.email is None ): logger.warning( "teacher not saved because he does not have a first name or a last name or an email address" ) return "teacher not saved because he does not have a first name or a last name or an email address" else: logger.debug("saving teacher") teacher.save() teachers_group = Group.objects.get(name="teachers") if teacher.groups.filter(name="teachers").exists() == False: logger.debug("adding teacher to group") teachers_group.user_set.add(teacher) teachers_group.save() teacher.save() else: logger.debug("teacher already part of the group") return ""
def add_person(sciper): """Add the person to the database Args: sciper (str): the sciper of the person to be added Returns: Person: The created entry in DB """ logger.info("adding person with sciper #" + sciper) ldapData = get_users(settings, [sciper])[0] person = Person() person.sciper = sciper person.username = ldapData["username"] person.email = ldapData["email"] person.first_name = ldapData["first_name"] person.last_name = ldapData["last_name"] person.save() return person
def get_new_phds(): logger.info("Retrieving all TAs from LDAP") phd_group = Group.objects.get(name="phds") TAs = epfl_ldap.get_STI_TA_Students(settings) for key, TA in TAs.items(): try: TA_obj = Person.objects.get(sciper=TA["sciper"]) logger.debug("found entry for sciper {}".format(TA["sciper"])) except Person.DoesNotExist: logger.info("creating user for sciper {}".format(TA["sciper"])) TA_obj = Person() TA_obj.sciper = TA["sciper"] TA_obj.username = TA["username"] TA_obj.first_name = TA["first_name"] TA_obj.last_name = TA["last_name"] TA_obj.email = TA["mail"] TA_obj.save() TA_obj.groups.add(phd_group)
def load_teachers(teachers=list()): logger.info("loading teachers") logger.debug("teachers: %s", teachers) teachers_group = Group.objects.get(name="teachers") return_value = list() if not os.path.isfile(settings.PICKLED_DATA_FROM_LDAP): load_mappings() pickle_in = open(settings.PICKLED_DATA_FROM_LDAP, 'rb') mappings = pickle.load(pickle_in) for teacher in teachers: if teacher in mappings: base_data = mappings[teacher] try: existing_user = Person.objects.get(sciper=base_data['sciper']) # Make sure that the teacher belongs to the teachers group if not existing_user.groups.filter(name="teachers").exists(): teachers_group.user_set.add(existing_user) return_value.append(existing_user) except ObjectDoesNotExist as ex: new_person = Person() new_person.sciper = base_data['sciper'] new_person.username = base_data['username'] if 'mail' in base_data: new_person.email = base_data['mail'] new_person.first_name = base_data['first_name'] new_person.last_name = base_data['last_name'] new_person.save() # Add the teacher to the teachers group for later use teachers_group.user_set.add(new_person) return_value.append(new_person) return return_value
def load_teacher(row): logger.info("loading teacher") logger.debug(row["sciper"]) sciper = row["sciper"] if pd.isnull(sciper): logger.warning("Teacher not added because the sciper is null") return try: teacher = Person.objects.get(sciper=sciper) except ObjectDoesNotExist: teacher = Person() teacher.sciper = row["sciper"] teacher.username = get_username(row["sciper"]) teacher.first_name = get_firstname(row["sciper"]) teacher.last_name = get_lastname(row["sciper"]) teacher.email = get_email(row["sciper"]) if ( pd.isnull(teacher.first_name) or pd.isnull(teacher.last_name) or pd.isnull(teacher.email) ): logger.warning( "teacher not saved because he does not have a first name or a last name or an email address" ) return else: logger.debug("saving teacher") teacher.save() teachers_group = Group.objects.get(name="teachers") if teacher.groups.filter(name="teachers").exists() == False: logger.debug("adding teacher to group") teachers_group.user_set.add(teacher) teachers_group.save() teacher.save() else: logger.debug("teacher already part of the group")