コード例 #1
0
ファイル: Users.py プロジェクト: grololo06/ecotaxa_back
 def set_preferences_per_project(self, user_id: UserIDT,
                                 project_id: ProjectIDT, key: str,
                                 value: Any):
     """
         Set preference for a key, for given project and user. The key disappears if set to empty string.
     """
     UserBO.set_preferences_per_project(self.session, user_id, project_id,
                                        key, value)
コード例 #2
0
 def update_classif_mru(self, user_id: UserIDT, project_id: ProjectIDT, last_used: ClassifIDListT):
     """
         Update recently used list for the user+project.
         :param user_id:
         :param project_id:
         :param last_used: The used classif_id, in time order, i.e. recents are in last. No guarantee
                 of uniqueness inside the list.
     """
     mru = UserBO.get_mru(self.session, user_id, project_id)
     mru = UserBO.merge_mru(mru, last_used)
     UserBO.set_mru(self.session, user_id, project_id, mru)
コード例 #3
0
ファイル: Users.py プロジェクト: grololo06/ecotaxa_back
 def get_preferences_per_project(self, user_id: UserIDT,
                                 project_id: ProjectIDT, key: str) -> Any:
     """
         Get a preference, for given project and user. Keys are not standardized (for now).
     """
     return UserBO.get_preferences_per_project(self.session, user_id,
                                               project_id, key)
コード例 #4
0
ファイル: test_mru.py プロジェクト: ecotaxa/ecotaxa_back
def test_mru():
    # Basic addition
    before = [2, 4, 6, 7]
    new = [8, 9, 9]
    after = UserBO.merge_mru(before, new)
    assert after == [9, 8, 2, 4, 6, 7]
    # Already there
    before = [8, 9, 2, 4, 6, 7]
    new = [8, 9, 9]
    after = UserBO.merge_mru(before, new)
    assert after == [9, 8, 2, 4, 6, 7]
    # Already there further
    before = [8, 9, 2, 4, 6, 7]
    new = [8, 9, 9, 7]
    after = UserBO.merge_mru(before, new)
    assert after == [7, 9, 8, 2, 4, 6]
    # Over the limit
    before = [8, 9, 2, 4, 6, 7]
    new = [10, 11, 12, 13, 14, 17, 19]
    after = UserBO.merge_mru(before, new)
    assert after == [19, 17, 14, 13, 12, 11, 10, 8, 9, 2]
コード例 #5
0
    def search(self, current_user_id: Optional[UserIDT], prj_id: Optional[int],
               query: str) -> List[TaxaSearchRsp]:
        """
            See caller doctext for specifications.
        """
        query_len = len(query)
        # Arrange query
        query = query.lower()
        # " " and "*" mean "any chars"
        query = query.replace("*", "%").replace(" ", "%")
        # It's possible to ask for both child & parent at the same time, using "<"
        # So "<" is kind of operator "descending of"
        terms = [
            sub + r"%" if
            (not sub or sub[-1] != '%') else sub  # Semantic is 'start with'
            for sub in query.split("<")
        ]
        # Conventionally, the first term is a filter on display_name
        display_name_term = terms[0]
        name_terms = terms[1:]

        # Compose the query from different case
        limit_ids_to = None
        include_ids = []
        return_order = {}
        # Get preset list, to favor in result order
        preset = set()
        if prj_id is not None:
            the_prj = ProjectBOSet.get_one(self.session, prj_id)
            if the_prj is not None:
                include_ids = the_prj.get_preset()
                preset = set(include_ids)
        if current_user_id is None:
            # Unauthenticated call
            if query_len < 3:
                limit_ids_to = []  # No MRU, no output whatever filters
        else:
            # Authenticated call
            if query_len < 3 and prj_id is not None:
                # The query will limit to mru list, if 0 length then it's % i.e. all
                limit_ids_to = UserBO.get_mru(self.session, current_user_id,
                                              prj_id)
                # And arrange they are in first
                return_order = {
                    cl_id: num
                    for num, cl_id in enumerate(limit_ids_to)
                }
        # Do the query
        res = TaxonomyBO.query(self.session, limit_ids_to, include_ids,
                               display_name_term, name_terms)
        mru_ret = []
        preset_ret = []
        others_ret = []
        # Carefully order the result
        for a_rec in res:
            classif_id = a_rec['id']
            is_preset = 1 if classif_id in preset else 0
            to_add = TaxaSearchRsp(id=classif_id,
                                   text=a_rec['display_name'],
                                   pr=is_preset)
            if classif_id in return_order:
                mru_ret.append(to_add)
            elif is_preset:
                preset_ret.append(to_add)
            else:
                others_ret.append(to_add)
        mru_ret.sort(key=lambda r: return_order[r.id])
        return mru_ret + preset_ret + others_ret