Update/refactor search_channel with aliases and proper query. Resolves #1534.

This commit is contained in:
Griatch 2018-01-06 20:12:51 +01:00
parent e3de3fb1dc
commit d2b89b7613
2 changed files with 31 additions and 32 deletions

View file

@ -355,15 +355,16 @@ class ChannelDBManager(TypedObjectManager):
channel (Channel or None): A channel match. channel (Channel or None): A channel match.
""" """
# first check the channel key dbref = self.dbref(channelkey)
channels = self.filter(db_key__iexact=channelkey) if dbref:
if not channels: try:
# also check aliases return self.get(id=dbref)
channels = [channel for channel in self.all() except self.model.DoesNotExist:
if channelkey in channel.aliases.all()] pass
if channels: results = self.filter(Q(db_key__iexact=channelkey) |
return channels[0] Q(db_tags__db_tagtype__iexact="alias",
return None db_tags__db_key__iexact=channelkey)).distinct()
return results[0] if results else None
def get_subscriptions(self, subscriber): def get_subscriptions(self, subscriber):
""" """
@ -393,26 +394,20 @@ class ChannelDBManager(TypedObjectManager):
case sensitive) match. case sensitive) match.
""" """
channels = [] dbref = self.dbref(ostring)
if not ostring: if dbref:
return channels
try: try:
# try an id match first return self.get(id=dbref)
dbref = int(ostring.strip('#')) except self.model.DoesNotExist:
channels = self.filter(id=dbref)
except Exception:
# Usually because we couldn't convert to int - not a dbref
pass pass
if not channels:
# no id match. Search on the key.
if exact: if exact:
channels = self.filter(db_key__iexact=ostring) channels = self.filter(Q(db_key__iexact=ostring) |
Q(db_tags__db_tagtype__iexact="alias",
db_tags__db_key__iexact=ostring)).distinct()
else: else:
channels = self.filter(db_key__icontains=ostring) channels = self.filter(Q(db_key__icontains=ostring) |
if not channels: Q(db_tags__db_tagtype__iexact="alias",
# still no match. Search by alias. db_tags__db_key__icontains=ostring)).distinct()
channels = [channel for channel in self.all()
if ostring.lower() in [a.lower for a in channel.aliases.all()]]
return channels return channels
# back-compatibility alias # back-compatibility alias
channel_search = search_channel channel_search = search_channel

View file

@ -76,10 +76,14 @@ class ObjectDBManager(TypedObjectManager):
# simplest case - search by dbref # simplest case - search by dbref
dbref = self.dbref(ostring) dbref = self.dbref(ostring)
if dbref: if dbref:
return dbref try:
return self.get(id=dbref)
except self.model.DoesNotExist:
pass
# not a dbref. Search by name. # not a dbref. Search by name.
cand_restriction = candidates is not None and Q(pk__in=[_GA(obj, "id") for obj in make_iter(candidates) cand_restriction = candidates is not None and Q(
if obj]) or Q() pk__in=[_GA(obj, "id") for obj in make_iter(candidates) if obj]) or Q()
if exact: if exact:
return self.filter(cand_restriction & Q(db_account__username__iexact=ostring)) return self.filter(cand_restriction & Q(db_account__username__iexact=ostring))
else: # fuzzy matching else: # fuzzy matching