Esempio n. 1
0
 def user_has_role(session: Session, user_id: int, role: str) -> User:
     """
         Check user role. Should be temporary until a proper action is defined, e.g. refresh taxo tree.
     """
     # Load ORM entity
     user: User = session.query(User).get(user_id)
     # Check
     assert user.has_role(role), NOT_AUTHORIZED
     return user
Esempio n. 2
0
 def get_children(self, session: Session, taxa_list: List[TaxonBO]):
     # Enrich TaxonBOs with children
     bos_per_id = {a_bo.id: a_bo for a_bo in taxa_list}
     tch = Taxonomy.__table__.alias('tch')
     qry: Query = session.query(Taxonomy.id, tch.c.id)
     qry = qry.join(tch, tch.c.parent_id == Taxonomy.id)
     qry = qry.filter(Taxonomy.id == any_(list(bos_per_id.keys())))
     for an_id, a_child_id in qry.all():
         bos_per_id[an_id].children.append(a_child_id)
Esempio n. 3
0
 def recent_projects(self, session: Session):
     """
         Return display information for last used projects.
     """
     mru = self.prefs.get(self.RECENT_PROJECTS_KEY, [])
     qry: Query = session.query(Project.projid, Project.title)
     qry = qry.filter(Project.projid.in_(mru))
     qry = qry.order_by(Project.title)
     return [{"projid": projid, "title": title} for projid, title in qry.all()]
Esempio n. 4
0
 def get_cardinalities(self, session: Session):
     # Enrich TaxonBOs with number of objects. Due to ecotaxa/ecotaxa_dev#648, pick data from projects stats.
     bos_per_id = {a_bo.id: a_bo for a_bo in self.taxa}
     qry: Query = session.query(ProjectTaxoStat.id,
                                func.sum(ProjectTaxoStat.nbr_v))
     qry = qry.filter(ProjectTaxoStat.id == any_(list(bos_per_id.keys())))
     qry = qry.group_by(ProjectTaxoStat.id)
     for an_id, a_sum in qry.all():
         bos_per_id[an_id].nb_objects = a_sum
Esempio n. 5
0
 def user_wants_create_project(session: Session, user_id: int) \
         -> User:
     """
         Check rights for the user to do this specific action.
     """
     # Load ORM entity
     user: User = session.query(User).get(user_id)
     assert user is not None, NOT_AUTHORIZED
     # Check
     assert Action.CREATE_PROJECT in RightsBO.allowed_actions(
         user), NOT_AUTHORIZED
     return user
Esempio n. 6
0
 def user_wants(session: Session, user_id: int, action: Action, prj_id: int) \
         -> Tuple[User, Project]:
     """
         Check rights for the user to do this specific action onto this project.
     """
     # Load ORM entities
     user: User = session.query(User).get(user_id)
     project = session.query(Project).get(prj_id)
     assert project is not None, NOT_FOUND
     # Check
     if user.has_role(Role.APP_ADMINISTRATOR):
         # King of the world
         pass
     else:
         a_priv: ProjectPrivilege
         # Collect privileges for user on project
         # noinspection PyTypeChecker
         rights_on_proj = {
             a_priv.privilege
             for a_priv in user.privs_on_projects if a_priv.projid == prj_id
         }
         if action == Action.ADMINISTRATE:
             assert ProjectPrivilegeBO.MANAGE in rights_on_proj, NOT_AUTHORIZED
         elif action == Action.ANNOTATE:
             # TODO: Bah, not nice
             assert ProjectPrivilegeBO.ANNOTATE in rights_on_proj \
                    or ProjectPrivilegeBO.MANAGE in rights_on_proj, NOT_AUTHORIZED
         elif action == Action.READ:
             # TODO: Bah, not nice either
             assert project.visible \
                    or ProjectPrivilegeBO.VIEW in rights_on_proj \
                    or ProjectPrivilegeBO.ANNOTATE in rights_on_proj \
                    or ProjectPrivilegeBO.MANAGE in rights_on_proj, NOT_AUTHORIZED
         else:
             raise Exception("Not implemented")
     # Keep the last accessed projects
     if Preferences(user).add_recent_project(prj_id):
         session.commit()
     return user, project
Esempio n. 7
0
 def anonymous_wants(session: Session, action: Action, prj_id: int) \
         -> Project:
     """
         Check rights for an anonymous user to do this action.
     """
     # Load ORM entities
     project: Optional[Project] = session.query(Project).get(prj_id)
     # Check
     if project and action == Action.READ:
         assert project.visible, NOT_AUTHORIZED
     else:
         assert False, NOT_AUTHORIZED
     return project
Esempio n. 8
0
 def __init__(self, session: Session, object_ids: Any,
              obj_mapping: TableMapping):
     needed_cols = obj_mapping.real_cols_to_tsv.keys()
     # noinspection PyPep8Naming
     ReducedObjectFields = minimal_model_of(MetaData(), ObjectFields,
                                            set(needed_cols))
     qry: Query = session.query(ObjectHeader, ReducedObjectFields)
     qry = qry.filter(ObjectHeader.objid.in_(object_ids))
     # noinspection PyUnresolvedReferences
     qry = qry.join(
         ReducedObjectFields,
         ObjectHeader.objid == ReducedObjectFields.objfid)  # type:ignore
     qry = qry.options(joinedload(ObjectHeader.all_images))
     self.all = [
         ObjectBO(session, 0, an_obj, its_fields)
         for an_obj, its_fields in qry.all()
     ]
Esempio n. 9
0
    def strict_match(
            session: Session,
            used_taxo_ids: ClassifIDListT) -> List[Tuple[Taxonomy, WoRMS]]:
        """
            Candidate match until a better is found.
                We match Phylo types taxa on one side.
                    using name <-> scientificname, case insensitive
                And a _single_ accepted taxon on the other.
        """
        subqry = TaxonomyChangeService.strict_match_subquery(
            session, used_taxo_ids, phylo_or_morpho="P")

        qry: Query = session.query(Taxonomy, WoRMS)
        qry = qry.join(subqry, subqry.c.id == Taxonomy.id)
        qry = qry.join(WoRMS, subqry.c.aphia_id == WoRMS.aphia_id)
        logger.info("matching qry:%s", str(qry))
        res = qry.all()
        return res