예제 #1
0
파일: manager.py 프로젝트: no-space/evennia
 def get_object_with_player(self, ostring, exact=True, candidates=None):
     """
     Search for an object based on its player's name or dbref.
     This search
     is sometimes initiated by appending a * to the beginning of
     the search criterion (e.g. in local_and_global_search).
     search_string:  (string) The name or dbref to search for.
     """
     ostring = to_unicode(ostring).lstrip("*")
     # simplest case - search by dbref
     dbref = self.dbref(ostring)
     if dbref:
         return dbref
     # not a dbref. Search by name.
     cand_restriction = (
         candidates != None and Q(pk__in=[_GA(obj, "id") for obj in make_iter(candidates) if obj]) or Q()
     )
     if exact:
         return self.filter(cand_restriction & Q(db_player__username__iexact=ostring))
     else:  # fuzzy matching
         ply_cands = self.filter(cand_restriction & Q(playerdb__username__istartswith=ostring)).values_list(
             "db_key", flat=True
         )
         if candidates:
             index_matches = string_partial_matching(ply_cands, ostring, ret_index=True)
             return [obj for ind, obj in enumerate(make_iter(candidates)) if ind in index_matches]
         else:
             return string_partial_matching(ply_cands, ostring, ret_index=False)
예제 #2
0
 def get_objs_with_key_or_alias(self, ostring, exact=True, candidates=None):
     """
     Returns objects based on key or alias match. Will also do fuzzy matching based on
     the utils.string_partial_matching function.
     """
     # build query objects
     candidates_id = [_GA(obj, "id") for obj in make_iter(candidates) if obj]
     cand_restriction = candidates and Q(pk__in=candidates_id) or Q()
     if exact:
         # exact match - do direct search
         return self.filter(cand_restriction & (Q(db_key__iexact=ostring) | Q(alias__db_key__iexact=ostring))).distinct()
     elif candidates:
         # fuzzy with candidates
         key_candidates = self.filter(cand_restriction)
     else:
         # fuzzy without supplied candidates - we select our own candidates
         key_candidates = self.filter(Q(db_key__istartswith=ostring) | Q(alias__db_key__istartswith=ostring)).distinct()
         candidates_id = [_GA(obj, "id") for obj in key_candidates]
     # fuzzy matching
     key_strings = key_candidates.values_list("db_key", flat=True)
     index_matches = string_partial_matching(key_strings, ostring, ret_index=True)
     if index_matches:
         return [obj for ind, obj in enumerate(key_candidates) if ind in index_matches]
     else:
         alias_candidates = self.model.alias_set.related.model.objects.filter(db_obj__pk__in=candidates_id)
         alias_strings = alias_candidates.values_list("db_key", flat=True)
         index_matches = string_partial_matching(alias_strings, ostring, ret_index=True)
         if index_matches:
             return [alias.db_obj for ind, alias in enumerate(alias_candidates) if ind in index_matches]
         return []
예제 #3
0
파일: manager.py 프로젝트: no-space/evennia
 def get_objs_with_key_or_alias(self, ostring, exact=True, candidates=None, typeclasses=None):
     """
     Returns objects based on key or alias match. Will also do fuzzy
     matching based on the utils.string_partial_matching function.
     candidates - list of candidate objects to restrict on
     typeclasses - list of typeclass path strings to restrict on
     """
     if not isinstance(ostring, basestring):
         if hasattr(ostring, "key"):
             ostring = ostring.key
         else:
             return []
     if is_iter(candidates) and not len(candidates):
         # if candidates is an empty iterable there can be no matches
         # Exit early.
         return []
     # build query objects
     candidates_id = [_GA(obj, "id") for obj in make_iter(candidates) if obj]
     cand_restriction = candidates != None and Q(pk__in=make_iter(candidates_id)) or Q()
     type_restriction = typeclasses and Q(db_typeclass_path__in=make_iter(typeclasses)) or Q()
     if exact:
         # exact match - do direct search
         return self.filter(
             cand_restriction
             & type_restriction
             & (
                 Q(db_key__iexact=ostring)
                 | Q(db_tags__db_key__iexact=ostring) & Q(db_tags__db_category__iexact="objectalias")
             )
         ).distinct()
     elif candidates:
         # fuzzy with candidates
         key_candidates = self.filter(cand_restriction & type_restriction)
     else:
         # fuzzy without supplied candidates - we select our own candidates
         key_candidates = self.filter(
             type_restriction & (Q(db_key__istartswith=ostring) | Q(db_tags__db_key__istartswith=ostring))
         ).distinct()
         candidates_id = [_GA(obj, "id") for obj in key_candidates]
     # fuzzy matching
     key_strings = key_candidates.values_list("db_key", flat=True)
     index_matches = string_partial_matching(key_strings, ostring, ret_index=True)
     if index_matches:
         return [obj for ind, obj in enumerate(key_candidates) if ind in index_matches]
     else:
         alias_candidates = self.filter(id__in=candidates_id, db_tags__db_category__iexact="objectalias")
         # print alias_candidates
         alias_strings = alias_candidates.values_list("db_key", flat=True)
         index_matches = string_partial_matching(alias_strings, ostring, ret_index=True)
         if index_matches:
             return [alias.db_obj for ind, alias in enumerate(alias_candidates) if ind in index_matches]
         return []
예제 #4
0
 def get_object_with_player(self, ostring, exact=True, candidates=None):
     """
     Search for an object based on its player's name or dbref.
     This search
     is sometimes initiated by appending a * to the beginning of
     the search criterion (e.g. in local_and_global_search).
     search_string:  (string) The name or dbref to search for.
     """
     ostring = to_unicode(ostring).lstrip('*')
     # simplest case - search by dbref
     dbref = self.dbref(ostring)
     if dbref:
         return dbref
     # not a dbref. Search by name.
     cand_restriction = candidates != None and Q(
         pk__in=[_GA(obj, "id")
                 for obj in make_iter(candidates) if obj]) or Q()
     if exact:
         return self.filter(cand_restriction
                            & Q(db_player__username__iexact=ostring))
     else:  # fuzzy matching
         ply_cands = self.filter(cand_restriction & Q(
             playerdb__username__istartswith=ostring)).values_list(
                 "db_key", flat=True)
         if candidates:
             index_matches = string_partial_matching(ply_cands,
                                                     ostring,
                                                     ret_index=True)
             return [
                 obj for ind, obj in enumerate(make_iter(candidates))
                 if ind in index_matches
             ]
         else:
             return string_partial_matching(ply_cands,
                                            ostring,
                                            ret_index=False)
예제 #5
0
    def get_objs_with_key_or_alias(self,
                                   ostring,
                                   exact=True,
                                   candidates=None,
                                   typeclasses=None):
        """
        Returns objects based on key or alias match. Will also do fuzzy
        matching based on the utils.string_partial_matching function.
        candidates - list of candidate objects to restrict on
        typeclasses - list of typeclass path strings to restrict on
        """
        if not isinstance(ostring, basestring):
            if hasattr(ostring, "key"):
                ostring = ostring.key
            else:
                return []
        if is_iter(candidates) and not len(candidates):
            # if candidates is an empty iterable there can be no matches
            # Exit early.
            return []

        # build query objects
        candidates_id = [
            _GA(obj, "id") for obj in make_iter(candidates) if obj
        ]
        cand_restriction = candidates != None and Q(
            pk__in=make_iter(candidates_id)) or Q()
        type_restriction = typeclasses and Q(
            db_typeclass_path__in=make_iter(typeclasses)) or Q()
        if exact:
            # exact match - do direct search
            return self.filter(cand_restriction & type_restriction & (
                Q(db_key__iexact=ostring) | Q(db_tags__db_key__iexact=ostring)
                & Q(db_tags__db_tagtype__iexact="alias"))).distinct()
        elif candidates:
            # fuzzy with candidates
            key_candidates = self.filter(cand_restriction & type_restriction)
        else:
            # fuzzy without supplied candidates - we select our own candidates
            key_candidates = self.filter(type_restriction & (
                Q(db_key__istartswith=ostring)
                | Q(db_tags__db_key__istartswith=ostring))).distinct()
            candidates_id = [_GA(obj, "id") for obj in key_candidates]
        # fuzzy matching
        key_strings = key_candidates.values_list("db_key", flat=True)
        index_matches = string_partial_matching(key_strings,
                                                ostring,
                                                ret_index=True)
        if index_matches:
            return [
                obj for ind, obj in enumerate(key_candidates)
                if ind in index_matches
            ]
        else:
            alias_candidates = self.filter(id__in=candidates_id,
                                           db_tags__db_tagtype__iexact="alias")
            alias_strings = alias_candidates.values_list("db_key", flat=True)
            index_matches = string_partial_matching(alias_strings,
                                                    ostring,
                                                    ret_index=True)
            if index_matches:
                return [
                    alias.db_obj for ind, alias in enumerate(alias_candidates)
                    if ind in index_matches
                ]
            return []