def get_context(user: Profile) -> JSONDict: menus = get_menu(user) types_demandes = list(get_demande_types_for_user(user)) is_responsable = user.has_role(Role.RESPONSABLE, "*") is_porteur = user.has_role(Role.PORTEUR, "*") is_gestionnaire = user.has_role(Role.GESTIONNAIRE, "*") message_dgrtt = get_constant("message_dgrtt", "OK") return { "menu": [menu.asdict() for menu in menus], "user": UserSchema().dump(user).data, "types_demandes": types_demandes, "home_boxes": get_boxes(), "archives_boxes": get_boxes(archives=True), "is_admin": user.has_role(Role.ADMIN_CENTRAL), "is_membre_dri": is_membre_dri(user), "is_membre_drv": is_membre_drv(user), "is_responsable": is_responsable, "is_porteur": is_porteur, "is_gestionnaire": is_gestionnaire, "message_dgrtt": message_dgrtt, "constants": get_constants(), }
def is_responsable_ou_gestionnaire(user: Profile, demande: Demande) -> bool: if demande.structure: structures = [demande.structure] + demande.structure.ancestors for structure in structures: if user.has_role(Role.RESPONSABLE, structure): return True if user.has_role(Role.GESTIONNAIRE, structure): return True return False
def test_contacts(structure_repo, profile_repo, db_session: scoped_session): universite = Structure(nom="SU", type_name=UN.name) structure_repo.put(universite) result = get_contacts(universite.id) assert len(result) == 8 user = Profile(uid="toto") profile_repo.put(user) contacts = {ct.name: "" for ct in ContactType} update_contacts(universite.id, contacts) result = get_contacts(universite.id) assert len(result) == 8 assert glom(result, ["uid"]) == [None] * 8 contacts["CONTACT_ENTREPRISES"] = user.uid update_contacts(universite.id, contacts) result = get_contacts(universite.id) assert len(result) == 8 result1 = glom(result, ["uid"]) assert result1[0] == "toto" assert result1[1:8] == [None] * 7 contacts = {ct.name: user.uid for ct in ContactType} update_contacts(universite.id, contacts) result = get_contacts(universite.id) assert len(result) == 8 assert glom(result, ["uid"]) == ["toto"] * 8
def test_contact_service(db_session: scoped_session): # # TODO: remove # profile_repo.clear() # structure_repo.clear() # contact_service.clear() assert profile_repo.is_empty() assert structure_repo.is_empty() assert contact_service.is_empty() user = Profile() profile_repo.put(user) universite = Structure(nom="SU", type_name=UN.name) structure_repo.put(universite) # Actual test contact_service.set_contact(universite, ContactType.CDP, user) assert contact_service.get_contact(universite, ContactType.CDP) == user map = contact_service.get_mapping() map1 = map[universite] assert map1[ContactType.CDP] == user contact_service.delete_contact(universite, ContactType.CDP) assert contact_service.get_contact(universite, ContactType.CDP) == None map = contact_service.get_mapping() assert universite not in map # Cleanup profile_repo.clear() structure_repo.clear() contact_service.clear()
def test_with_no_context(db_session: scoped_session): user = Profile() profile_repo.put(user) role_service.grant_role(user, Role.ADMIN_CENTRAL) assert role_service.has_role(user, Role.ADMIN_CENTRAL) assert role_service.get_users_with_role(Role.ADMIN_CENTRAL) == {user} role_service.clear() profile_repo.clear()
def is_responsable_structure_concernee(user: Profile, demande: Demande) -> bool: for d in demande.data.get("structures_concernees", []): structure_id = d["value"] structure = structure_repo.get_by_id(structure_id) if not structure: continue if user.has_role(Role.RESPONSABLE, structure): return True return False
def get_drv_membership(user: Profile) -> Structure | None: for drv_dn in DRV_DNS.values(): drv = structure_repo.get_by_dn(drv_dn) # For tests if not drv: continue for role in [Role.MEMBRE, Role.MEMBRE_RATTACHE, Role.MEMBRE_AFFECTE]: if user.has_role(role, drv): return drv return None
class NS: # Create structures labo = Structure(dn="labo", nom="Labo", type_name=LA.name) dri = Structure(dn=DRI_DN, nom="DR&I", type_name=DU.name) # Create profiles joe_gdl = Profile(uid="joe_gdl", email="1", prenom="Joe", nom="GDL") jim_porteur = Profile(uid="jim_porteur", email="2", prenom="Jim", nom="Porteur") jake_directeur = Profile(uid="jake_directeur", email="3", prenom="Jake", nom="Directeur") jules_dri = Profile(uid="jules_dgrtt", email="4", prenom="Jules", nom="Contact DR&I") jules2_dri = Profile(uid="jules2_dgrtt", email="5", prenom="Jules2", nom="Contact DR&I")
def test_roles(structure_repo, profile_repo): universite = Structure(nom="SU", type_name=UN.name) structure_repo.put(universite) user = Profile(uid="toto") profile_repo.put(user) result = get_roles(universite.id) assert glom(result, (["users"], [["id"]])) == [[], [], [], [], []] add_roles(universite.id, [user.id], Role.PORTEUR.name) result = get_roles(universite.id) assert glom(result, (["users"], [["id"]])) == [[], [], [], [], [user.id]] delete_role(universite.id, user.id, Role.PORTEUR.name) result = get_roles(universite.id) assert glom(result, (["users"], [["id"]])) == [[], [], [], [], []] result = get_role_selectors(universite.id) # First select is not multiple assert glom(result, ["value"]) == [None, [], [], [], []]
def update_users_from_records(records: list[tuple[str, dict[str, list[str]]]]): profiles = profile_repo.get_all() old_profile_uids = { profile.uid for profile in profiles if profile.uid and profile.active } count0 = len(old_profile_uids) print(f"old total: {count0:d}") logger.info(f"old total: {count0:d}") new_profile_uids = set() for _dn, r in records: if "uid" not in r: continue uid = r["uid"][0] new_profile_uids.add(uid) deleted_uids = old_profile_uids.difference(new_profile_uids) deactivate_users(deleted_uids) uids_to_profiles = {p.uid: p for p in profiles} print("Updating profiles from LDIF dump") for _dn, r in tqdm(records, disable=None): record = LdifRecord(r) if not record.uid: continue uid = record.uid if not uid: continue if uid in uids_to_profiles: profile = uids_to_profiles[uid] else: profile = Profile(uid=uid) profile_repo.put(profile) update_profile_from_record(profile, record)
def test_with_context(db_session: scoped_session): user = Profile() profile_repo.put(user) structure = Structure() structure_repo.put(structure) role_service.grant_role(user, Role.ADMIN_LOCAL, structure) assert role_service.has_role(user, Role.ADMIN_LOCAL, structure) assert role_service.get_users_with_role(Role.ADMIN_LOCAL, structure) == {user} role_to_users = role_service.get_users_with_role_on(structure) assert len(role_to_users) == 1 user1 = role_to_users[Role.ADMIN_LOCAL].pop() assert user1 == user roles_for_user = role_service.get_roles_for_user(user) assert len(roles_for_user) == 1 assert roles_for_user[Role.ADMIN_LOCAL] == {structure} role_service.clear() profile_repo.clear()
def put(self, profile: Profile): if not profile.id: profile.id = ProfileId.new() self.session.add(profile)
def get_is_admin(self, user: Profile): return user.has_role(Role.ADMIN_CENTRAL)
def is_membre_dri(user: Profile) -> bool: dri = structure_repo.get_by_dn(DRI_DN) return user.has_role(Role.MEMBRE, dri)
def is_visible_for(self, user: Profile): return user.has_role(Role.GESTIONNAIRE, "*") or user.has_role( Role.RESPONSABLE, "*")
def is_visible_for(self, user: Profile): return user.has_role(Role.PORTEUR, "*")
def update_profile_from_record(profile: Profile, record: LdifRecord): assert profile profile.nom = record.sn profile.prenom = record.givenName profile.uid = record.uid profile.email = record.mail profile.telephone = record.telephoneNumber profile.adresse = record.address profile.login = record.supannAliasLogin profile.adresse = record.adresse affectation = record.affectation if not affectation: if profile.active: profile.affectation = "" profile.deactivate() return if not profile.active: profile.activate() if profile.affectation != affectation: profile.affectation = affectation fonctions = list(record.fonctions) if set(profile.fonctions) != set(fonctions): profile.fonctions = fonctions
def get_stats2( user: Profile, periode_debut=None, periode_fin=None, type_demande=None, type_recrutement=None, financeur=None, structure_id=None, porteur_id=None, ): query = StatsLine.query if periode_debut: query = query.filter(StatsLine.date_soumission >= periode_debut) if periode_fin: query = query.filter(StatsLine.date_soumission <= periode_fin) if type_demande: query = query.filter(StatsLine.type_demande.in_(type_demande)) if type_recrutement: query = query.filter(StatsLine.type_recrutement.in_(type_recrutement)) if financeur: query = query.filter(StatsLine.financeur.in_(financeur)) if porteur_id not in ("", "None", None): query = query.filter(StatsLine.porteur_id == porteur_id) if structure_id not in ("", "None", None): if not user.has_role(Role.ADMIN_CENTRAL): structures = mes_structures(user) if structure_id not in {s.id for s in structures}: raise Forbidden() structure = db.session.query(Structure).get(structure_id) descendant_ids = [s.id for s in structure.descendants] query = query.filter( StatsLine.structure_id.in_([structure_id] + descendant_ids)) elif user.has_role(Role.RESPONSABLE, "*"): structures = mes_structures(user) query = query.filter( StatsLine.structure_id.in_([s.id for s in structures])) # Stats totals = {} for state in ALL_STATES: state_id = state.id.lower() totals["nb_" + state_id] = query.filter( StatsLine.wf_state == state.id).count() totals["nb_en_cours"] = sum(totals[id] for id in [ "nb_en_edition", "nb_en_validation", "nb_en_verification", "nb_en_instruction", ]) totals["nb_archivee"] = sum( totals[id] for id in ["nb_traitee", "nb_rejetee", "nb_abandonnee"]) totals["nb_total"] = totals["nb_en_cours"] + totals["nb_archivee"] assert totals["nb_total"] == query.count() totals = {k: int(v) for k, v in totals.items()} stats = { "conventions": make_conventions_stats(query), "rh": make_rh_stats(query), "avenants": make_avenants_stats(query), "pi_logiciel": make_pi_logiciel_stats(query), "pi_invention": make_pi_invention_stats(query), "duree_traitement": make_duree_traitement_stats(query), } ctx = { "totals": totals, "stats": stats, } return ctx
def test_2(): profile = Profile() assert profile.prenom == "" assert profile.nom == ""
def test_1(): profile = Profile(nom="NOM", prenom="Prenom") assert profile.full_name == "Prenom NOM"