2010-08-29 18:46:58 +00:00
|
|
|
"""
|
|
|
|
|
This implements the common managers that are used by the
|
|
|
|
|
abstract models in dbobjects.py (and which are thus shared by
|
2012-03-30 23:47:22 +02:00
|
|
|
all Attributes and TypedObjects).
|
2010-08-29 18:46:58 +00:00
|
|
|
"""
|
2012-03-27 09:59:11 +02:00
|
|
|
from functools import update_wrapper
|
2010-08-29 18:46:58 +00:00
|
|
|
from django.db import models
|
2013-07-12 11:12:21 +02:00
|
|
|
from django.db.models import Q
|
2010-10-31 08:10:02 +00:00
|
|
|
from src.utils import idmapper
|
2012-02-26 16:04:19 +01:00
|
|
|
from src.utils.utils import make_iter
|
2013-04-14 16:36:44 +02:00
|
|
|
from src.utils.dbserialize import to_pickle
|
|
|
|
|
|
2012-03-31 15:09:22 +02:00
|
|
|
__all__ = ("AttributeManager", "TypedObjectManager")
|
2013-07-08 18:13:21 +02:00
|
|
|
_GA = object.__getattribute__
|
2010-08-29 18:46:58 +00:00
|
|
|
|
2012-03-30 23:47:22 +02:00
|
|
|
# Managers
|
2010-08-29 18:46:58 +00:00
|
|
|
|
2013-04-14 16:36:44 +02:00
|
|
|
def _attr_pickled(method):
|
|
|
|
|
"""
|
|
|
|
|
decorator for safely handling attribute searches
|
|
|
|
|
- db_value is a pickled field and this is required
|
|
|
|
|
in order to be able for pickled django objects directly.
|
|
|
|
|
"""
|
|
|
|
|
def wrapper(self, *args, **kwargs):
|
|
|
|
|
"wrap all queries searching the db_value field in some way"
|
|
|
|
|
self.__doc__ = method.__doc__
|
|
|
|
|
for key in (key for key in kwargs if key.startswith('db_value')):
|
|
|
|
|
kwargs[key] = to_pickle(kwargs[key])
|
|
|
|
|
return method(self, *args, **kwargs)
|
|
|
|
|
return update_wrapper(wrapper, method)
|
|
|
|
|
|
2010-08-29 18:46:58 +00:00
|
|
|
class AttributeManager(models.Manager):
|
|
|
|
|
"Manager for handling Attributes."
|
2013-04-14 16:36:44 +02:00
|
|
|
@_attr_pickled
|
|
|
|
|
def get(self, *args, **kwargs):
|
|
|
|
|
return super(AttributeManager, self).get(*args, **kwargs)
|
|
|
|
|
@_attr_pickled
|
|
|
|
|
def filter(self,*args, **kwargs):
|
|
|
|
|
return super(AttributeManager, self).filter(*args, **kwargs)
|
|
|
|
|
@_attr_pickled
|
|
|
|
|
def exclude(self,*args, **kwargs):
|
|
|
|
|
return super(AttributeManager, self).exclude(*args, **kwargs)
|
|
|
|
|
@_attr_pickled
|
|
|
|
|
def values(self,*args, **kwargs):
|
|
|
|
|
return super(AttributeManager, self).values(*args, **kwargs)
|
|
|
|
|
@_attr_pickled
|
|
|
|
|
def values_list(self,*args, **kwargs):
|
|
|
|
|
return super(AttributeManager, self).values_list(*args, **kwargs)
|
|
|
|
|
@_attr_pickled
|
|
|
|
|
def exists(self,*args, **kwargs):
|
|
|
|
|
return super(AttributeManager, self).exists(*args, **kwargs)
|
2010-08-29 18:46:58 +00:00
|
|
|
|
2013-08-24 21:23:43 +02:00
|
|
|
def get_attrs_on_obj(self, searchstr, obj, category=None, exact_match=True):
|
2010-08-29 18:46:58 +00:00
|
|
|
"""
|
2013-07-08 18:13:21 +02:00
|
|
|
Searches the object's attributes for attribute key matches.
|
2012-03-30 23:47:22 +02:00
|
|
|
|
2010-08-29 18:46:58 +00:00
|
|
|
searchstr: (str) A string to search for.
|
|
|
|
|
"""
|
|
|
|
|
# Retrieve the list of attributes for this object.
|
2013-07-08 18:13:21 +02:00
|
|
|
|
2013-08-24 21:23:43 +02:00
|
|
|
category_cond = Q(db_category__iexact=category) if category else Q()
|
2010-08-29 18:46:58 +00:00
|
|
|
if exact_match:
|
2013-08-24 21:23:43 +02:00
|
|
|
return _GA("obj", "db_attributes").filter(db_key__iexact=searchstr & category_cond)
|
2010-08-29 18:46:58 +00:00
|
|
|
else:
|
2013-08-24 21:23:43 +02:00
|
|
|
return _GA("obj", "db_attributes").filter(db_key__icontains=searchstr & category_cond)
|
2010-08-29 18:46:58 +00:00
|
|
|
|
2013-07-12 11:12:21 +02:00
|
|
|
def attr_namesearch(self, *args, **kwargs):
|
|
|
|
|
"alias wrapper for backwards compatability"
|
|
|
|
|
return self.get_attrs_on_obj(*args, **kwargs)
|
2010-08-29 18:46:58 +00:00
|
|
|
|
2013-07-12 11:12:21 +02:00
|
|
|
def get_attr_by_value(self, searchstr, obj=None):
|
2013-04-14 16:36:44 +02:00
|
|
|
"""
|
2013-07-08 18:13:21 +02:00
|
|
|
Searches obj for Attributes with a given value.
|
|
|
|
|
searchstr - value to search for. This may be any suitable object.
|
|
|
|
|
obj - limit to a given object instance
|
|
|
|
|
|
|
|
|
|
If no restraint is given, all Attributes on all types of objects
|
|
|
|
|
will be searched. It's highly recommended to at least
|
|
|
|
|
supply the objclass argument (DBObject, DBScript or DBPlayer)
|
|
|
|
|
to restrict this lookup.
|
2013-04-14 16:36:44 +02:00
|
|
|
"""
|
|
|
|
|
if obj:
|
2013-07-08 18:13:21 +02:00
|
|
|
return _GA(obj, "db_attributes").filter(db_value=searchstr)
|
2013-04-14 16:36:44 +02:00
|
|
|
return self.filter(db_value=searchstr)
|
|
|
|
|
|
2013-07-12 11:12:21 +02:00
|
|
|
def attr_valuesearch(self, *args, **kwargs):
|
|
|
|
|
"alias wrapper for backwards compatability"
|
|
|
|
|
return self.get_attr_by_value(self, *args, **kwargs)
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
|
# TagManager
|
|
|
|
|
#
|
|
|
|
|
|
|
|
|
|
class TagManager(models.Manager):
|
|
|
|
|
"""
|
|
|
|
|
Extra manager methods for Tags
|
|
|
|
|
"""
|
2013-07-12 20:21:52 +02:00
|
|
|
def get_tags_on_obj(self, obj, key=None, category=None):
|
2013-07-12 11:12:21 +02:00
|
|
|
"""
|
|
|
|
|
Get all tags on obj, optionally limited by key and/or category
|
|
|
|
|
"""
|
2013-08-04 12:47:00 -05:00
|
|
|
tags = _GA(obj, "db_tags").all()
|
|
|
|
|
if key:
|
|
|
|
|
tags = tags.filter(db_key__iexact=key.lower().strip())
|
|
|
|
|
if category:
|
|
|
|
|
tags = tags.filter(db_category__iexact=category.lower().strip())
|
|
|
|
|
return list(tags)
|
2013-07-12 11:12:21 +02:00
|
|
|
|
2013-07-12 20:21:52 +02:00
|
|
|
def get_tag(self, key=None, category=None):
|
2013-07-12 11:12:21 +02:00
|
|
|
"""
|
|
|
|
|
Search and return all tags matching any combination of
|
|
|
|
|
the search criteria.
|
|
|
|
|
search_key (string) - the tag identifier
|
|
|
|
|
category (string) - the tag category
|
2013-07-12 14:44:49 +02:00
|
|
|
|
|
|
|
|
Returns a single Tag (or None) if both key and category is given, otherwise
|
|
|
|
|
it will return a list.
|
2013-07-12 11:12:21 +02:00
|
|
|
"""
|
2013-07-12 20:21:52 +02:00
|
|
|
key_cands = Q(db_key__iexact=key.lower().strip()) if key!=None else Q()
|
|
|
|
|
cat_cands = Q(db_category__iexact=category.lower().strip()) if category!=None else Q()
|
2013-07-12 14:44:49 +02:00
|
|
|
tags = self.filter(key_cands & cat_cands)
|
2013-07-12 20:21:52 +02:00
|
|
|
if key and category:
|
2013-07-12 14:44:49 +02:00
|
|
|
return tags[0] if tags else None
|
|
|
|
|
else:
|
|
|
|
|
return list(tags)
|
2013-07-12 11:12:21 +02:00
|
|
|
|
2013-07-12 20:21:52 +02:00
|
|
|
def get_objs_with_tag(self, objclass, key=None, category=None):
|
2013-07-12 11:12:21 +02:00
|
|
|
"""
|
|
|
|
|
Search and return all objects of objclass that has tags matching
|
|
|
|
|
the given search criteria.
|
|
|
|
|
objclass (dbmodel) - the object class to search
|
2013-07-12 20:21:52 +02:00
|
|
|
key (string) - the tag identifier
|
2013-07-12 11:12:21 +02:00
|
|
|
category (string) - the tag category
|
|
|
|
|
"""
|
2013-07-12 20:21:52 +02:00
|
|
|
key_cands = Q(db_tags__db_key__iexact=key.lower().strip()) if search_key!=None else Q()
|
2013-07-12 11:12:21 +02:00
|
|
|
cat_cands = Q(db_tags__db_category__iexact=category.lower().strip()) if category!=None else Q()
|
|
|
|
|
return objclass.objects.filter(key_cands & cat_cands)
|
|
|
|
|
|
|
|
|
|
def create_tag(self, key=None, category=None, data=None):
|
|
|
|
|
"""
|
|
|
|
|
Create a tag. This makes sure the create case-insensitive tags.
|
|
|
|
|
Note that if the exact same tag configuration (key+category)
|
|
|
|
|
exists, it will be re-used. A data keyword will overwrite existing
|
|
|
|
|
data on a tag (it is not part of what makes the tag unique).
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
data = str(data) if data!=None else None
|
|
|
|
|
|
2013-07-12 20:21:52 +02:00
|
|
|
tag = self.get_tag(key=key, category=category)
|
2013-07-12 11:12:21 +02:00
|
|
|
if tag and data != None:
|
|
|
|
|
tag.db_data = data
|
|
|
|
|
tag.save()
|
|
|
|
|
elif not tag:
|
2013-07-12 20:21:52 +02:00
|
|
|
tag = self.create(db_key=key.lower().strip() if key!=None else None,
|
2013-08-24 23:57:44 +02:00
|
|
|
db_category=category.lower().strip() if category and key!=None else None,
|
2013-07-12 20:21:52 +02:00
|
|
|
db_data=str(data) if data!=None else None)
|
2013-07-12 11:12:21 +02:00
|
|
|
tag.save()
|
2013-08-24 23:57:44 +02:00
|
|
|
return make_iter(tag)[0]
|
2013-07-08 18:13:21 +02:00
|
|
|
|
2010-08-29 18:46:58 +00:00
|
|
|
#
|
2012-03-30 23:47:22 +02:00
|
|
|
# helper functions for the TypedObjectManager.
|
|
|
|
|
#
|
2010-08-29 18:46:58 +00:00
|
|
|
|
|
|
|
|
def returns_typeclass_list(method):
|
|
|
|
|
"""
|
2012-09-17 15:31:50 +02:00
|
|
|
Decorator: Changes return of the decorated method (which are
|
2012-02-26 16:04:19 +01:00
|
|
|
TypeClassed objects) into object_classes(s) instead. Will always
|
|
|
|
|
return a list (may be empty).
|
2012-03-30 23:47:22 +02:00
|
|
|
"""
|
2010-08-29 18:46:58 +00:00
|
|
|
def func(self, *args, **kwargs):
|
2012-03-30 23:47:22 +02:00
|
|
|
"decorator. Returns a list."
|
2012-03-27 09:59:11 +02:00
|
|
|
self.__doc__ = method.__doc__
|
2013-09-28 22:23:30 -05:00
|
|
|
matches = make_iter(method(self, *args, **kwargs))
|
2012-02-26 19:04:57 +01:00
|
|
|
return [(hasattr(dbobj, "typeclass") and dbobj.typeclass) or dbobj for dbobj in make_iter(matches)]
|
2012-03-30 23:47:22 +02:00
|
|
|
return update_wrapper(func, method)
|
2010-08-29 18:46:58 +00:00
|
|
|
|
|
|
|
|
def returns_typeclass(method):
|
|
|
|
|
"""
|
2012-02-26 16:04:19 +01:00
|
|
|
Decorator: Will always return a single typeclassed result or None.
|
2010-08-29 18:46:58 +00:00
|
|
|
"""
|
|
|
|
|
def func(self, *args, **kwargs):
|
2012-02-26 16:04:19 +01:00
|
|
|
"decorator. Returns result or None."
|
2012-03-27 09:59:11 +02:00
|
|
|
self.__doc__ = method.__doc__
|
2012-09-17 15:31:50 +02:00
|
|
|
matches = method(self, *args, **kwargs)
|
|
|
|
|
dbobj = matches and make_iter(matches)[0] or None
|
|
|
|
|
if dbobj:
|
|
|
|
|
return (hasattr(dbobj, "typeclass") and dbobj.typeclass) or dbobj
|
|
|
|
|
return None
|
2012-03-27 09:59:11 +02:00
|
|
|
return update_wrapper(func, method)
|
2010-08-29 18:46:58 +00:00
|
|
|
|
2010-10-31 08:10:02 +00:00
|
|
|
|
|
|
|
|
#class TypedObjectManager(idmap.CachingManager):
|
|
|
|
|
#class TypedObjectManager(models.Manager):
|
|
|
|
|
class TypedObjectManager(idmapper.manager.SharedMemoryManager):
|
2010-08-29 18:46:58 +00:00
|
|
|
"""
|
2012-03-30 23:47:22 +02:00
|
|
|
Common ObjectManager for all dbobjects.
|
2010-08-29 18:46:58 +00:00
|
|
|
"""
|
|
|
|
|
|
2012-10-14 15:45:21 +02:00
|
|
|
def dbref(self, dbref, reqhash=True):
|
2010-08-29 18:46:58 +00:00
|
|
|
"""
|
|
|
|
|
Valid forms of dbref (database reference number)
|
|
|
|
|
are either a string '#N' or an integer N.
|
2012-03-30 23:47:22 +02:00
|
|
|
Output is the integer part.
|
2012-10-14 15:45:21 +02:00
|
|
|
reqhash - require input to be on form "#N" to be
|
|
|
|
|
identified as a dbref
|
2010-08-29 18:46:58 +00:00
|
|
|
"""
|
2012-10-14 15:45:21 +02:00
|
|
|
if reqhash and not (isinstance(dbref, basestring) and dbref.startswith("#")):
|
|
|
|
|
return None
|
2010-10-30 18:42:37 +00:00
|
|
|
if isinstance(dbref, basestring):
|
2010-08-29 18:46:58 +00:00
|
|
|
dbref = dbref.lstrip('#')
|
|
|
|
|
try:
|
2012-08-18 23:35:11 +02:00
|
|
|
if int(dbref) < 0:
|
2012-03-30 23:47:22 +02:00
|
|
|
return None
|
2010-08-29 18:46:58 +00:00
|
|
|
except Exception:
|
|
|
|
|
return None
|
|
|
|
|
return dbref
|
2012-03-29 20:30:35 +02:00
|
|
|
|
2010-08-29 18:46:58 +00:00
|
|
|
@returns_typeclass
|
2012-08-22 16:15:52 +02:00
|
|
|
def get_id(self, dbref):
|
2010-08-29 18:46:58 +00:00
|
|
|
"""
|
2012-08-22 16:15:52 +02:00
|
|
|
Find object with given dbref
|
2010-08-29 18:46:58 +00:00
|
|
|
"""
|
2012-10-14 15:45:21 +02:00
|
|
|
dbref = self.dbref(dbref, reqhash=False)
|
2012-08-22 16:15:52 +02:00
|
|
|
try:
|
|
|
|
|
return self.get(id=dbref)
|
|
|
|
|
except self.model.DoesNotExist:
|
|
|
|
|
pass
|
2010-08-29 18:46:58 +00:00
|
|
|
return None
|
|
|
|
|
|
2012-08-22 16:15:52 +02:00
|
|
|
def dbref_search(self, dbref):
|
|
|
|
|
"""
|
|
|
|
|
Alias to get_id
|
|
|
|
|
"""
|
|
|
|
|
return self.get_id(dbref)
|
|
|
|
|
|
2010-08-29 18:46:58 +00:00
|
|
|
@returns_typeclass_list
|
|
|
|
|
def get_dbref_range(self, min_dbref=None, max_dbref=None):
|
|
|
|
|
"""
|
|
|
|
|
Return all objects inside and including the
|
|
|
|
|
given boundaries.
|
|
|
|
|
"""
|
2013-07-27 04:25:01 -04:00
|
|
|
retval = super(TypedObjectManager, self).all()
|
|
|
|
|
if min_dbref != None:
|
|
|
|
|
retval = retval.filter(id__gte=self.dbref(min_dbref, reqhash=False))
|
|
|
|
|
if max_dbref != None:
|
|
|
|
|
retval = retval.filter(id__lte=self.dbref(max_dbref, reqhash=False))
|
|
|
|
|
return retval
|
2010-08-29 18:46:58 +00:00
|
|
|
|
|
|
|
|
def object_totals(self):
|
|
|
|
|
"""
|
|
|
|
|
Returns a dictionary with all the typeclasses active in-game
|
|
|
|
|
as well as the number of such objects defined (i.e. the number
|
2012-03-30 23:47:22 +02:00
|
|
|
of database object having that typeclass set on themselves).
|
2010-08-29 18:46:58 +00:00
|
|
|
"""
|
|
|
|
|
dbtotals = {}
|
2011-04-08 23:10:04 +00:00
|
|
|
typeclass_paths = set(self.values_list('db_typeclass_path', flat=True))
|
2012-03-30 23:47:22 +02:00
|
|
|
for typeclass_path in typeclass_paths:
|
2010-08-29 18:46:58 +00:00
|
|
|
dbtotals[typeclass_path] = \
|
|
|
|
|
self.filter(db_typeclass_path=typeclass_path).count()
|
2012-03-30 23:47:22 +02:00
|
|
|
return dbtotals
|
2010-08-29 18:46:58 +00:00
|
|
|
|
|
|
|
|
@returns_typeclass_list
|
|
|
|
|
def typeclass_search(self, typeclass):
|
|
|
|
|
"""
|
|
|
|
|
Searches through all objects returning those which has a certain
|
|
|
|
|
typeclass. If location is set, limit search to objects in
|
2012-03-30 23:47:22 +02:00
|
|
|
that location.
|
2010-08-29 18:46:58 +00:00
|
|
|
"""
|
|
|
|
|
if callable(typeclass):
|
|
|
|
|
cls = typeclass.__class__
|
|
|
|
|
typeclass = "%s.%s" % (cls.__module__, cls.__name__)
|
2012-08-22 13:28:38 +02:00
|
|
|
return self.filter(db_typeclass_path__exact=typeclass)
|