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.
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_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]))
@ -132,7 +132,7 @@ class CmdHelp(Command):
caller.msg(format_help_entry(match[0].key, match[0].__doc__, aliases=match[0].aliases, suggested=suggestions))
return
# try a database help entry match
# try an exact database help entry match
match = list(HelpEntry.objects.find_topicmatch(query, exact=True))
if len(match) == 1:
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."
try:
idnum = int(idnum)
return self.get(id=id)
return self.get(id=idnum)
except Exception:
return None

View file

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

View file

@ -2,14 +2,14 @@
Custom manager for Objects.
"""
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 src.typeclasses.managers import TypedObjectManager
from src.typeclasses.managers import returns_typeclass, returns_typeclass_list
from src.utils import utils
from src.utils.utils import to_unicode, make_iter
ObjAttribute = None
_ObjAttribute = None
__all__ = ("ObjectManager",)
@ -27,7 +27,7 @@ class ObjectManager(TypedObjectManager):
Querysets or database objects).
dbref (converter)
dbref_search
get_id (alias: dbref_search)
get_dbref_range
object_totals
typeclass_search
@ -60,19 +60,19 @@ class ObjectManager(TypedObjectManager):
user - may be a user object or user id.
"""
try:
uid = int(user)
except TypeError:
dbref = self.dbref(user)
if dbref:
try:
uid = user.id
except:
return None
return self.get(db_player__user__id=dbref)
except self.model.DoesNotExist:
pass
try:
return self.get(db_player__user__id=uid)
except Exception:
return self.get(db_player__user=user)
except self.model.DoesNotExist:
return None
# This returns typeclass since get_object_with_user and get_dbref does.
@returns_typeclass
def get_object_with_player(self, search_string):
"""
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).
search_string: (string) The name or dbref to search for.
"""
search_string = to_unicode(search_string).lstrip('*')
dbref = self.dbref(search_string)
if not dbref:
# not a dbref. Search by name.
player_matches = User.objects.filter(username__iexact=search_string)
if player_matches:
dbref = player_matches[0].id
# use the id to find the player
return self.get_object_with_user(dbref)
search_string = to_unicode(search_string).lstrip('*')
return self.filter(db_player__user__username__iexact=search_string)
return self.get_id(dbref)
@returns_typeclass_list
def get_objs_with_key_and_typeclass(self, oname, otypeclass_path):
"""
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
@returns_typeclass_list
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
if not ObjAttribute:
if not _ObjAttribute:
from src.objects.models import ObjAttribute as _ObjAttribute
lstring = ""
if location:
lstring = ", db_obj__db_location=location"
attrs = eval("_ObjAttribute.objects.filter(db_key=attribute_name%s)" % lstring)
return [attr.obj for attr in attrs]
attrs = _ObjAttribute.objects.select_related("db_obj").filter(db_key=attribute_name, db_obj__db_location=location)
else:
attrs = _ObjAttribute.objects.select_related("db_obj").filter(db_key=attribute_name)
return [attr.db_obj for attr in attrs]
@returns_typeclass_list
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.
"""
global _ObjAttribute
if not ObjAttribute:
if not _ObjAttribute:
from src.objects.models import ObjAttribute as _ObjAttribute
lstring = ""
if location:
lstring = ", db_obj__db_location=location"
attrs = eval("ObjAttribute.objects.filter(db_key=attribute_name%s)" % lstring)
# since attribute values are pickled in database, we cannot search directly, but
# must loop through the results. .
attrs = _ObjAttribute.objects.select_related("db_value").filter(db_key=attribute_name, db_obj__db_location=location)
else:
attrs = _ObjAttribute.objects.select_related("db_value").filter(db_key=attribute_name)
if exact:
return [attr.obj for attr in attrs if attribute_value == attr.value]
else:
@ -169,7 +165,7 @@ class ObjectManager(TypedObjectManager):
return []
@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
"""
@ -197,7 +193,7 @@ class ObjectManager(TypedObjectManager):
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):
query = query.exclude(db_key=objkey)
return query

View file

@ -137,32 +137,18 @@ class PlayerManager(TypedObjectManager):
"""
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
def get_player_from_name(self, uname):
"Get player object based on name"
players = self.filter(user__username=uname)
if players:
return players[0]
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)]
try:
return self.get(user__username=uname)
except self.model.DoesNotExist:
return None
@returns_typeclass_list
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 returns_typeclass_list
from src.utils.utils import make_iter
__all__ = ("ScriptManager",)
VALIDATE_ITERATION = 0
@ -18,7 +19,7 @@ class ScriptManager(TypedObjectManager):
Querysets or database objects).
dbref (converter)
dbref_search
get_id (or dbref_search)
get_dbref_range
object_totals
typeclass_search
@ -34,33 +35,37 @@ class ScriptManager(TypedObjectManager):
@returns_typeclass_list
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:
return []
scripts = self.filter(db_obj=obj)
if key:
return scripts.filter(db_key=key)
return scripts
script = []
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
def get_all_scripts(self, key=None):
"""
Return all scripts, alternative only
scripts with a certain key/dbref or path.
scripts with a certain key/dbref
"""
if key:
script = []
dbref = self.dbref(key)
if dbref:
# try to see if this is infact a dbref
script = self.dbref_search(dbref)
if script:
return script
# not a dbref. Normal key search
scripts = self.filter(db_key=key)
else:
scripts = list(self.all())
return scripts
if not script:
scripts = self.filter(db_key=key)
return scripts
return self.all()
def delete_script(self, dbref):
"""
@ -70,7 +75,7 @@ class ScriptManager(TypedObjectManager):
a specific game object.
"""
scripts = self.get_id(dbref)
for script in scripts:
for script in make_iter(scripts):
script.stop()
def remove_non_persistent(self, obj=None):
@ -80,13 +85,15 @@ class ScriptManager(TypedObjectManager):
and
"""
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:
to_stop = self.filter(db_persistent=False)
nr_deleted = to_stop.count()
for script in to_stop.filter(db_is_active=True):
to_stop = self.filter(db_persistent=False, db_is_active=True)
to_delete = self.filter(db_persistent=False, db_is_active=False)
nr_deleted = to_stop.count() + to_delete.count()
for script in to_stop:
script.stop()
for script in to_stop.filter(db_is_active=False):
for script in to_delete:
script.delete()
return nr_deleted

View file

@ -94,18 +94,23 @@ class TypedObjectManager(idmapper.manager.SharedMemoryManager):
@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)
if dbref :
try:
return self.get(id=dbref)
except self.model.DoesNotExist:
return None
try:
return self.get(id=dbref)
except self.model.DoesNotExist:
pass
return None
def dbref_search(self, dbref):
"""
Alias to get_id
"""
return self.get_id(dbref)
@returns_typeclass_list
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).filter(id__lte=min_dbref)
def get_id(self, idnum):
"""
Alias to dbref_search
"""
return self.dbref_search(idnum)
def object_totals(self):
"""
Returns a dictionary with all the typeclasses active in-game