Minor fixes and reducing some sql calls in various managers.

This commit is contained in:
Griatch 2012-08-22 16:15:52 +02:00
parent 4edde61be4
commit 5117bd2a0a
7 changed files with 87 additions and 102 deletions

View file

@ -100,7 +100,7 @@ class CmdHelp(Command):
# having to allow doublet commands to manage exits etc. # having to allow doublet commands to manage exits etc.
cmdset.make_unique(caller) cmdset.make_unique(caller)
# retrieve all available commands and topics # retrieve all available commands and database topics
all_cmds = [cmd for cmd in cmdset if cmd.auto_help and cmd.access(caller)] all_cmds = [cmd for cmd in cmdset if cmd.auto_help and cmd.access(caller)]
all_topics = [topic for topic in HelpEntry.objects.all() if topic.access(caller, 'view', default=True)] all_topics = [topic for topic in HelpEntry.objects.all() if topic.access(caller, 'view', default=True)]
all_categories = list(set([cmd.help_category.lower() for cmd in all_cmds] + [topic.help_category.lower() for topic in all_topics])) all_categories = list(set([cmd.help_category.lower() for cmd in all_cmds] + [topic.help_category.lower() for topic in all_topics]))
@ -132,7 +132,7 @@ class CmdHelp(Command):
caller.msg(format_help_entry(match[0].key, match[0].__doc__, aliases=match[0].aliases, suggested=suggestions)) caller.msg(format_help_entry(match[0].key, match[0].__doc__, aliases=match[0].aliases, suggested=suggestions))
return return
# try a database help entry match # try an exact database help entry match
match = list(HelpEntry.objects.find_topicmatch(query, exact=True)) match = list(HelpEntry.objects.find_topicmatch(query, exact=True))
if len(match) == 1: if len(match) == 1:
caller.msg(format_help_entry(match[0].key, match[0].entrytext, suggested=suggestions)) caller.msg(format_help_entry(match[0].key, match[0].entrytext, suggested=suggestions))

View file

@ -80,7 +80,7 @@ class MsgManager(models.Manager):
"Retrieve message by its id." "Retrieve message by its id."
try: try:
idnum = int(idnum) idnum = int(idnum)
return self.get(id=id) return self.get(id=idnum)
except Exception: except Exception:
return None return None

View file

@ -26,8 +26,9 @@ class HelpEntryManager(models.Manager):
""" """
Searches for matching topics based on player's input. Searches for matching topics based on player's input.
""" """
if utils.dbref(topicstr): dbref = utils.dbref(topicstr)
return self.filter(id=utils.dbref(topicstr)) if dbref:
return self.filter(id=dbref)
topics = self.filter(db_key__iexact=topicstr) topics = self.filter(db_key__iexact=topicstr)
if not topics and not exact: if not topics and not exact:
topics = self.filter(db_key__istartswith=topicstr) topics = self.filter(db_key__istartswith=topicstr)
@ -47,17 +48,13 @@ class HelpEntryManager(models.Manager):
Do a fuzzy match, preferably within the category of the Do a fuzzy match, preferably within the category of the
current topic. current topic.
""" """
topics = self.find_apropos(topicstr) return self.filter(db_key__icontains=topicstring).exclude(db_key__iexact=topicstring)
# we need to clean away the given help entry.
return [topic for topic in topics
if topic.key.lower() != topicstr.lower()]
def find_topics_with_category(self, help_category): def find_topics_with_category(self, help_category):
""" """
Search topics having a particular category Search topics having a particular category
""" """
topics = self.filter(db_help_category__iexact=help_category) return self.filter(db_help_category__iexact=help_category)
return topics
def get_all_topics(self): def get_all_topics(self):
""" """
@ -94,7 +91,7 @@ class HelpEntryManager(models.Manager):
category - limit the search to a particular help topic category - limit the search to a particular help topic
""" """
ostring = ostring.strip().lower() ostring = ostring.strip().lower()
help_entries = self.filter(db_topicstr=ostring) if help_categories:
if help_category: return self.filter(db_topicstr__iexact=ostring, db_help_category__iexact=help_category)
help_entries.filter(db_help_category=help_category) else:
return help_entries return self.filter(db_topicstr__iexact=ostring)

View file

@ -2,14 +2,14 @@
Custom manager for Objects. Custom manager for Objects.
""" """
from django.conf import settings from django.conf import settings
from django.contrib.auth.models import User #from django.contrib.auth.models import User
from django.db.models.fields import exceptions from django.db.models.fields import exceptions
from src.typeclasses.managers import TypedObjectManager from src.typeclasses.managers import TypedObjectManager
from src.typeclasses.managers import returns_typeclass, returns_typeclass_list from src.typeclasses.managers import returns_typeclass, returns_typeclass_list
from src.utils import utils from src.utils import utils
from src.utils.utils import to_unicode, make_iter from src.utils.utils import to_unicode, make_iter
ObjAttribute = None _ObjAttribute = None
__all__ = ("ObjectManager",) __all__ = ("ObjectManager",)
@ -27,7 +27,7 @@ class ObjectManager(TypedObjectManager):
Querysets or database objects). Querysets or database objects).
dbref (converter) dbref (converter)
dbref_search get_id (alias: dbref_search)
get_dbref_range get_dbref_range
object_totals object_totals
typeclass_search typeclass_search
@ -60,19 +60,19 @@ class ObjectManager(TypedObjectManager):
user - may be a user object or user id. user - may be a user object or user id.
""" """
try: dbref = self.dbref(user)
uid = int(user) if dbref:
except TypeError:
try: try:
uid = user.id return self.get(db_player__user__id=dbref)
except: except self.model.DoesNotExist:
return None pass
try: try:
return self.get(db_player__user__id=uid) return self.get(db_player__user=user)
except Exception: except self.model.DoesNotExist:
return None return None
# This returns typeclass since get_object_with_user and get_dbref does. # This returns typeclass since get_object_with_user and get_dbref does.
@returns_typeclass
def get_object_with_player(self, search_string): def get_object_with_player(self, search_string):
""" """
Search for an object based on its player's name or dbref. Search for an object based on its player's name or dbref.
@ -81,38 +81,36 @@ class ObjectManager(TypedObjectManager):
the search criterion (e.g. in local_and_global_search). the search criterion (e.g. in local_and_global_search).
search_string: (string) The name or dbref to search for. search_string: (string) The name or dbref to search for.
""" """
search_string = to_unicode(search_string).lstrip('*')
dbref = self.dbref(search_string) dbref = self.dbref(search_string)
if not dbref: if not dbref:
# not a dbref. Search by name. # not a dbref. Search by name.
player_matches = User.objects.filter(username__iexact=search_string) search_string = to_unicode(search_string).lstrip('*')
if player_matches: return self.filter(db_player__user__username__iexact=search_string)
dbref = player_matches[0].id return self.get_id(dbref)
# use the id to find the player
return self.get_object_with_user(dbref)
@returns_typeclass_list @returns_typeclass_list
def get_objs_with_key_and_typeclass(self, oname, otypeclass_path): def get_objs_with_key_and_typeclass(self, oname, otypeclass_path):
""" """
Returns objects based on simultaneous key and typeclass match. Returns objects based on simultaneous key and typeclass match.
""" """
return self.filter(db_key__iexact=oname).filter(db_typeclass_path__exact=otypeclass_path) return self.filter(db_key__iexact=oname, db_typeclass_path__exact=otypeclass_path)
# attr/property related # attr/property related
@returns_typeclass_list @returns_typeclass_list
def get_objs_with_attr(self, attribute_name, location=None): def get_objs_with_attr(self, attribute_name, location=None):
""" """
Returns all objects having the given attribute_name defined at all. Returns all objects having the given attribute_name defined at all. Location
should be a valid location object.
""" """
global _ObjAttribute global _ObjAttribute
if not ObjAttribute: if not _ObjAttribute:
from src.objects.models import ObjAttribute as _ObjAttribute from src.objects.models import ObjAttribute as _ObjAttribute
lstring = ""
if location: if location:
lstring = ", db_obj__db_location=location" attrs = _ObjAttribute.objects.select_related("db_obj").filter(db_key=attribute_name, db_obj__db_location=location)
attrs = eval("_ObjAttribute.objects.filter(db_key=attribute_name%s)" % lstring) else:
return [attr.obj for attr in attrs] attrs = _ObjAttribute.objects.select_related("db_obj").filter(db_key=attribute_name)
return [attr.db_obj for attr in attrs]
@returns_typeclass_list @returns_typeclass_list
def get_objs_with_attr_match(self, attribute_name, attribute_value, location=None, exact=False): def get_objs_with_attr_match(self, attribute_name, attribute_value, location=None, exact=False):
@ -122,14 +120,12 @@ class ObjectManager(TypedObjectManager):
to attribute_value, and so it can accept also non-strings. to attribute_value, and so it can accept also non-strings.
""" """
global _ObjAttribute global _ObjAttribute
if not ObjAttribute: if not _ObjAttribute:
from src.objects.models import ObjAttribute as _ObjAttribute from src.objects.models import ObjAttribute as _ObjAttribute
lstring = ""
if location: if location:
lstring = ", db_obj__db_location=location" attrs = _ObjAttribute.objects.select_related("db_value").filter(db_key=attribute_name, db_obj__db_location=location)
attrs = eval("ObjAttribute.objects.filter(db_key=attribute_name%s)" % lstring) else:
# since attribute values are pickled in database, we cannot search directly, but attrs = _ObjAttribute.objects.select_related("db_value").filter(db_key=attribute_name)
# must loop through the results. .
if exact: if exact:
return [attr.obj for attr in attrs if attribute_value == attr.value] return [attr.obj for attr in attrs if attribute_value == attr.value]
else: else:
@ -169,7 +165,7 @@ class ObjectManager(TypedObjectManager):
return [] return []
@returns_typeclass_list @returns_typeclass_list
def get_objs_with_key_or_alias(self, ostring, location, exact=False): def get_objs_with_key_or_alias(self, ostring, location=None, exact=False):
""" """
Returns objects based on key or alias match Returns objects based on key or alias match
""" """
@ -197,7 +193,7 @@ class ObjectManager(TypedObjectManager):
excludeobjs - one or more object keys to exclude from the match excludeobjs - one or more object keys to exclude from the match
""" """
query = self.filter(db_location__id=location.id, ) query = self.filter(db_location__id=location.id)
for objkey in make_iter(excludeobj): for objkey in make_iter(excludeobj):
query = query.exclude(db_key=objkey) query = query.exclude(db_key=objkey)
return query return query

View file

@ -137,32 +137,18 @@ class PlayerManager(TypedObjectManager):
""" """
Returns a player object based on User id. Returns a player object based on User id.
""" """
return User.objects.get(id=uid) try:
return User.objects.get(id=uid)
except User.model.DoesNotExist:
return None
@returns_typeclass @returns_typeclass
def get_player_from_name(self, uname): def get_player_from_name(self, uname):
"Get player object based on name" "Get player object based on name"
players = self.filter(user__username=uname) try:
if players: return self.get(user__username=uname)
return players[0] except self.model.DoesNotExist:
return None return None
# @returns_typeclass_list
# def get_players_with_perm(self, permstring):
# """
# Returns all players having access according to the given
# permission string.
# """
# return [player for player in self.all()
# if player.has_perm(permstring)]
# @returns_typeclass_list
# def get_players_with_group(self, groupstring):
# """
# Returns all players belonging to the given group.
# """
# return [player.user for player in self.all()
# if player.has_group(groupstring)]
@returns_typeclass_list @returns_typeclass_list
def player_search(self, ostring): def player_search(self, ostring):

View file

@ -4,6 +4,7 @@ The custom manager for Scripts.
from src.typeclasses.managers import TypedObjectManager from src.typeclasses.managers import TypedObjectManager
from src.typeclasses.managers import returns_typeclass_list from src.typeclasses.managers import returns_typeclass_list
from src.utils.utils import make_iter
__all__ = ("ScriptManager",) __all__ = ("ScriptManager",)
VALIDATE_ITERATION = 0 VALIDATE_ITERATION = 0
@ -18,7 +19,7 @@ class ScriptManager(TypedObjectManager):
Querysets or database objects). Querysets or database objects).
dbref (converter) dbref (converter)
dbref_search get_id (or dbref_search)
get_dbref_range get_dbref_range
object_totals object_totals
typeclass_search typeclass_search
@ -34,33 +35,37 @@ class ScriptManager(TypedObjectManager):
@returns_typeclass_list @returns_typeclass_list
def get_all_scripts_on_obj(self, obj, key=None): def get_all_scripts_on_obj(self, obj, key=None):
""" """
Returns as result all the Scripts related to a particular object Returns as result all the Scripts related to a particular object.
key can be given as a dbref or name string. If given, only scripts
matching the key on the object will be returned.
""" """
if not obj: if not obj:
return [] return []
scripts = self.filter(db_obj=obj)
if key: if key:
return scripts.filter(db_key=key) script = []
return scripts dbref = self.dbref(key)
if dbref:
script = self.filter(db_obj=obj, id=dbref)
if not script:
script = self.filter(db_obj=obj, db_key=key)
return script
return self.filter(db_obj=obj)
@returns_typeclass_list @returns_typeclass_list
def get_all_scripts(self, key=None): def get_all_scripts(self, key=None):
""" """
Return all scripts, alternative only Return all scripts, alternative only
scripts with a certain key/dbref or path. scripts with a certain key/dbref
""" """
if key: if key:
script = []
dbref = self.dbref(key) dbref = self.dbref(key)
if dbref: if dbref:
# try to see if this is infact a dbref
script = self.dbref_search(dbref) script = self.dbref_search(dbref)
if script: if not script:
return script scripts = self.filter(db_key=key)
# not a dbref. Normal key search return scripts
scripts = self.filter(db_key=key) return self.all()
else:
scripts = list(self.all())
return scripts
def delete_script(self, dbref): def delete_script(self, dbref):
""" """
@ -70,7 +75,7 @@ class ScriptManager(TypedObjectManager):
a specific game object. a specific game object.
""" """
scripts = self.get_id(dbref) scripts = self.get_id(dbref)
for script in scripts: for script in make_iter(scripts):
script.stop() script.stop()
def remove_non_persistent(self, obj=None): def remove_non_persistent(self, obj=None):
@ -80,13 +85,15 @@ class ScriptManager(TypedObjectManager):
and and
""" """
if obj: if obj:
to_stop = self.filter(db_persistent=False, db_obj=obj) to_stop = self.filter(db_obj=obj, db_persistent=False, db_is_active=True)
to_delete = self.filter(db_obj=obj, db_persistent=False, db_is_active=False)
else: else:
to_stop = self.filter(db_persistent=False) to_stop = self.filter(db_persistent=False, db_is_active=True)
nr_deleted = to_stop.count() to_delete = self.filter(db_persistent=False, db_is_active=False)
for script in to_stop.filter(db_is_active=True): nr_deleted = to_stop.count() + to_delete.count()
for script in to_stop:
script.stop() script.stop()
for script in to_stop.filter(db_is_active=False): for script in to_delete:
script.delete() script.delete()
return nr_deleted return nr_deleted

View file

@ -94,18 +94,23 @@ class TypedObjectManager(idmapper.manager.SharedMemoryManager):
@returns_typeclass @returns_typeclass
def dbref_search(self, dbref): def get_id(self, dbref):
""" """
Returns an object when given a dbref. Find object with given dbref
""" """
dbref = self.dbref(dbref) dbref = self.dbref(dbref)
if dbref : try:
try: return self.get(id=dbref)
return self.get(id=dbref) except self.model.DoesNotExist:
except self.model.DoesNotExist: pass
return None
return None return None
def dbref_search(self, dbref):
"""
Alias to get_id
"""
return self.get_id(dbref)
@returns_typeclass_list @returns_typeclass_list
def get_dbref_range(self, min_dbref=None, max_dbref=None): def get_dbref_range(self, min_dbref=None, max_dbref=None):
""" """
@ -121,12 +126,6 @@ class TypedObjectManager(idmapper.manager.SharedMemoryManager):
return self.filter(id__gte=min_dbref) return self.filter(id__gte=min_dbref)
return self.filter(id__gte=min_dbref).filter(id__lte=min_dbref) return self.filter(id__gte=min_dbref).filter(id__lte=min_dbref)
def get_id(self, idnum):
"""
Alias to dbref_search
"""
return self.dbref_search(idnum)
def object_totals(self): def object_totals(self):
""" """
Returns a dictionary with all the typeclasses active in-game Returns a dictionary with all the typeclasses active in-game