2010-08-29 18:46:58 +00:00
|
|
|
"""
|
|
|
|
|
Custom manager for Objects.
|
|
|
|
|
"""
|
2012-09-28 00:02:31 +02:00
|
|
|
try: import cPickle as pickle
|
|
|
|
|
except ImportError: import pickle
|
2012-09-17 15:31:50 +02:00
|
|
|
from django.db.models import Q
|
2010-08-29 18:46:58 +00:00
|
|
|
from django.conf import settings
|
2012-03-30 23:47:22 +02:00
|
|
|
from django.db.models.fields import exceptions
|
2010-08-29 18:46:58 +00:00
|
|
|
from src.typeclasses.managers import TypedObjectManager
|
2010-09-04 12:18:00 +00:00
|
|
|
from src.typeclasses.managers import returns_typeclass, returns_typeclass_list
|
2011-04-21 16:45:18 +00:00
|
|
|
from src.utils import utils
|
2012-09-28 00:02:31 +02:00
|
|
|
from src.utils.utils import to_unicode, make_iter, string_partial_matching, to_str
|
2010-08-29 18:46:58 +00:00
|
|
|
|
2012-03-31 15:09:22 +02:00
|
|
|
__all__ = ("ObjectManager",)
|
2012-09-17 15:31:50 +02:00
|
|
|
_GA = object.__getattribute__
|
2012-09-28 00:02:31 +02:00
|
|
|
_DUMPS = lambda inp: to_unicode(pickle.dumps(inp))
|
2012-03-31 15:09:22 +02:00
|
|
|
|
2010-08-29 18:46:58 +00:00
|
|
|
# Try to use a custom way to parse id-tagged multimatches.
|
2011-04-23 11:54:08 +00:00
|
|
|
|
2012-04-22 12:23:42 +02:00
|
|
|
_AT_MULTIMATCH_INPUT = utils.variable_from_module(*settings.SEARCH_AT_MULTIMATCH_INPUT.rsplit('.', 1))
|
2010-09-12 15:07:12 +00:00
|
|
|
|
2010-08-29 18:46:58 +00:00
|
|
|
class ObjectManager(TypedObjectManager):
|
|
|
|
|
"""
|
2012-03-29 19:42:08 +02:00
|
|
|
This ObjectManager implementes methods for searching
|
2012-03-30 23:47:22 +02:00
|
|
|
and manipulating Objects directly from the database.
|
2012-03-29 19:42:08 +02:00
|
|
|
|
|
|
|
|
Evennia-specific search methods (will return Typeclasses or
|
|
|
|
|
lists of Typeclasses, whereas Django-general methods will return
|
2012-03-30 23:47:22 +02:00
|
|
|
Querysets or database objects).
|
2012-03-29 19:42:08 +02:00
|
|
|
|
|
|
|
|
dbref (converter)
|
2012-08-22 16:15:52 +02:00
|
|
|
get_id (alias: dbref_search)
|
2012-03-29 19:42:08 +02:00
|
|
|
get_dbref_range
|
|
|
|
|
object_totals
|
|
|
|
|
typeclass_search
|
|
|
|
|
get_object_with_user
|
|
|
|
|
get_object_with_player
|
|
|
|
|
get_objs_with_key_and_typeclass
|
|
|
|
|
get_objs_with_attr
|
|
|
|
|
get_objs_with_attr_match
|
|
|
|
|
get_objs_with_db_property
|
|
|
|
|
get_objs_with_db_property_match
|
|
|
|
|
get_objs_with_key_or_alias
|
|
|
|
|
get_contents
|
|
|
|
|
object_search (interface to many of the above methods, equivalent to ev.search_object)
|
|
|
|
|
copy_object
|
2010-08-29 18:46:58 +00:00
|
|
|
|
|
|
|
|
"""
|
2012-03-30 23:47:22 +02:00
|
|
|
|
2010-08-29 18:46:58 +00:00
|
|
|
#
|
2012-03-30 23:47:22 +02:00
|
|
|
# ObjectManager Get methods
|
2010-08-29 18:46:58 +00:00
|
|
|
#
|
2010-09-12 15:07:12 +00:00
|
|
|
|
|
|
|
|
# user/player related
|
2012-03-30 23:47:22 +02:00
|
|
|
|
2010-09-04 12:18:00 +00:00
|
|
|
@returns_typeclass
|
2010-08-29 18:46:58 +00:00
|
|
|
def get_object_with_user(self, user):
|
|
|
|
|
"""
|
|
|
|
|
Matches objects with obj.player.user matching the argument.
|
2010-09-04 12:18:00 +00:00
|
|
|
A player<->user is a one-to-relationship, so this always
|
2012-03-30 23:47:22 +02:00
|
|
|
returns just one result or None.
|
2010-09-04 12:18:00 +00:00
|
|
|
|
2010-10-21 18:58:47 +00:00
|
|
|
user - may be a user object or user id.
|
2010-08-29 18:46:58 +00:00
|
|
|
"""
|
2012-08-22 16:15:52 +02:00
|
|
|
dbref = self.dbref(user)
|
|
|
|
|
if dbref:
|
2010-11-10 23:03:38 +00:00
|
|
|
try:
|
2012-08-22 16:15:52 +02:00
|
|
|
return self.get(db_player__user__id=dbref)
|
|
|
|
|
except self.model.DoesNotExist:
|
|
|
|
|
pass
|
2010-09-04 12:18:00 +00:00
|
|
|
try:
|
2012-08-22 16:15:52 +02:00
|
|
|
return self.get(db_player__user=user)
|
|
|
|
|
except self.model.DoesNotExist:
|
2010-09-04 12:18:00 +00:00
|
|
|
return None
|
2012-03-30 23:47:22 +02:00
|
|
|
|
|
|
|
|
# This returns typeclass since get_object_with_user and get_dbref does.
|
2012-08-22 16:15:52 +02:00
|
|
|
@returns_typeclass
|
2012-09-17 15:31:50 +02:00
|
|
|
def get_object_with_player(self, ostring, exact=True, candidates=None):
|
2012-03-30 23:47:22 +02:00
|
|
|
"""
|
|
|
|
|
Search for an object based on its player's name or dbref.
|
2010-08-29 18:46:58 +00:00
|
|
|
This search
|
|
|
|
|
is sometimes initiated by appending a * to the beginning of
|
2012-03-30 23:47:22 +02:00
|
|
|
the search criterion (e.g. in local_and_global_search).
|
2010-08-29 18:46:58 +00:00
|
|
|
search_string: (string) The name or dbref to search for.
|
|
|
|
|
"""
|
2012-09-17 15:31:50 +02:00
|
|
|
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 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__user__username__iexact=ostring))
|
|
|
|
|
else: # fuzzy matching
|
|
|
|
|
ply_cands = self.filter(cand_restriction & Q(playerdb__user__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)
|
2010-08-29 18:46:58 +00:00
|
|
|
|
2011-11-06 21:32:00 +01:00
|
|
|
@returns_typeclass_list
|
2012-09-17 15:31:50 +02:00
|
|
|
def get_objs_with_key_and_typeclass(self, oname, otypeclass_path, candidates=None):
|
2011-11-06 21:32:00 +01:00
|
|
|
"""
|
|
|
|
|
Returns objects based on simultaneous key and typeclass match.
|
|
|
|
|
"""
|
2012-09-17 15:31:50 +02:00
|
|
|
cand_restriction = candidates and Q(pk__in=[_GA(obj, "id") for obj in make_iter(candidates) if obj]) or Q()
|
|
|
|
|
return self.filter(cand_restriction & Q(db_key__iexact=oname, db_typeclass_path__exact=otypeclass_path))
|
2011-11-06 21:32:00 +01:00
|
|
|
|
2010-09-12 15:07:12 +00:00
|
|
|
# attr/property related
|
|
|
|
|
|
|
|
|
|
@returns_typeclass_list
|
2012-09-17 15:31:50 +02:00
|
|
|
def get_objs_with_attr(self, attribute_name, candidates=None):
|
2010-09-12 15:07:12 +00:00
|
|
|
"""
|
2012-08-22 16:15:52 +02:00
|
|
|
Returns all objects having the given attribute_name defined at all. Location
|
|
|
|
|
should be a valid location object.
|
2010-09-12 15:07:12 +00:00
|
|
|
"""
|
2012-09-17 15:31:50 +02:00
|
|
|
cand_restriction = candidates and Q(objattribute__db_obj__pk__in=[_GA(obj, "id") for obj in make_iter(candidates) if obj]) or Q()
|
|
|
|
|
return self.filter(cand_restriction & Q(objattribute__db_key=attribute_name))
|
2012-03-30 23:47:22 +02:00
|
|
|
|
2010-08-29 18:46:58 +00:00
|
|
|
@returns_typeclass_list
|
2012-09-17 15:31:50 +02:00
|
|
|
def get_objs_with_attr_value(self, attribute_name, attribute_value, candidates=None):
|
2010-08-29 18:46:58 +00:00
|
|
|
"""
|
2012-03-30 23:47:22 +02:00
|
|
|
Returns all objects having the valid
|
2010-08-29 18:46:58 +00:00
|
|
|
attrname set to the given value. Note that no conversion is made
|
2012-09-17 15:31:50 +02:00
|
|
|
to attribute_value, and so it can accept also non-strings. For this reason it does
|
|
|
|
|
not make sense to offer an "exact" type matching for this.
|
2012-03-30 23:47:22 +02:00
|
|
|
"""
|
2012-09-17 15:31:50 +02:00
|
|
|
cand_restriction = candidates and Q(db_obj__pk__in=[_GA(obj, "id") for obj in make_iter(candidates) if obj]) or Q()
|
2012-09-28 00:02:31 +02:00
|
|
|
if type(attribute_value) in (basestring, int, float):
|
|
|
|
|
# simple attribute_value - do direct lookup
|
2012-09-29 00:09:55 +02:00
|
|
|
return self.filter(cand_restriction & Q(objattribute__db_key=attribute_name, objattribute__db_value=_DUMPS(("simple", attribute_value))))
|
2012-09-28 00:02:31 +02:00
|
|
|
else:
|
|
|
|
|
# go via attribute conversion
|
|
|
|
|
attrs= self.model.objattribute_set.related.model.objects.select_related("db_obj").filter(cand_restriction & Q(db_key=attribute_name))
|
|
|
|
|
return [attr.db_obj for attr in attrs if attribute_value == attr.value]
|
2012-03-30 23:47:22 +02:00
|
|
|
|
2010-09-12 15:07:12 +00:00
|
|
|
@returns_typeclass_list
|
2012-09-17 15:31:50 +02:00
|
|
|
def get_objs_with_db_property(self, property_name, candidates=None):
|
2010-09-12 15:07:12 +00:00
|
|
|
"""
|
2010-10-30 18:42:37 +00:00
|
|
|
Returns all objects having a given db field property.
|
2012-03-30 23:47:22 +02:00
|
|
|
property_name = search string
|
2012-09-17 15:31:50 +02:00
|
|
|
candidates - list of candidate objects to search
|
2010-08-29 18:46:58 +00:00
|
|
|
"""
|
2012-09-17 15:31:50 +02:00
|
|
|
property_name = "db_%s" % property_name.lstrip('db_')
|
2012-09-27 22:18:46 +02:00
|
|
|
cand_restriction = candidates and Q(pk__in=[_GA(obj, "id") for obj in make_iter(candidates) if obj]) or Q()
|
2010-10-30 18:42:37 +00:00
|
|
|
try:
|
2012-09-17 15:31:50 +02:00
|
|
|
return self.filter(cand_restriction).exclude(Q(property_name=None))
|
2010-10-30 18:42:37 +00:00
|
|
|
except exceptions.FieldError:
|
|
|
|
|
return []
|
2012-03-30 23:47:22 +02:00
|
|
|
|
2010-08-29 18:46:58 +00:00
|
|
|
@returns_typeclass_list
|
2012-09-17 15:31:50 +02:00
|
|
|
def get_objs_with_db_property_value(self, property_name, property_value, candidates=None):
|
2010-08-29 18:46:58 +00:00
|
|
|
"""
|
2010-09-12 15:07:12 +00:00
|
|
|
Returns all objects having a given db field property
|
2010-08-29 18:46:58 +00:00
|
|
|
"""
|
2012-09-17 15:31:50 +02:00
|
|
|
if isinstance(property_value, basestring):
|
|
|
|
|
property_value = to_unicode(property_value)
|
|
|
|
|
property_name = "db_%s" % property_name.lstrip('db_')
|
2012-09-27 22:18:46 +02:00
|
|
|
cand_restriction = candidates and Q(pk__in=[_GA(obj, "id") for obj in make_iter(candidates) if obj]) or Q()
|
2010-09-12 15:07:12 +00:00
|
|
|
try:
|
2012-09-17 15:31:50 +02:00
|
|
|
return self.filter(cand_restriction & Q(property_name=property_value))
|
2010-10-30 18:42:37 +00:00
|
|
|
except exceptions.FieldError:
|
2010-09-12 15:07:12 +00:00
|
|
|
return []
|
2010-08-29 18:46:58 +00:00
|
|
|
|
|
|
|
|
@returns_typeclass_list
|
|
|
|
|
def get_contents(self, location, excludeobj=None):
|
|
|
|
|
"""
|
|
|
|
|
Get all objects that has a location
|
|
|
|
|
set to this one.
|
2012-08-19 11:45:13 +02:00
|
|
|
|
2012-09-17 15:31:50 +02:00
|
|
|
excludeobj - one or more object keys to exclude from the match
|
|
|
|
|
"""
|
|
|
|
|
exclude_restriction = excludeobj and Q(pk__in=[_GA(obj, "in") for obj in make_iter(excludeobj)]) or Q()
|
|
|
|
|
return self.filter(db_location=location).exclude(exclude_restriction)
|
|
|
|
|
|
|
|
|
|
@returns_typeclass_list
|
|
|
|
|
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.
|
2010-08-29 18:46:58 +00:00
|
|
|
"""
|
2012-09-17 15:31:50 +02:00
|
|
|
# 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:
|
2012-10-23 20:29:03 +02:00
|
|
|
# exact match - do direct search
|
2012-09-27 21:29:01 +02:00
|
|
|
return self.filter(cand_restriction & (Q(db_key__iexact=ostring) | Q(alias__db_key__iexact=ostring))).distinct()
|
2012-10-23 20:29:03 +02:00
|
|
|
elif candidates:
|
|
|
|
|
# fuzzy with candidates
|
|
|
|
|
key_candidates = self.filter(cand_restriction)
|
2012-09-17 15:31:50 +02:00
|
|
|
else:
|
2012-10-23 20:29:03 +02:00
|
|
|
# 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 []
|
2012-09-17 15:31:50 +02:00
|
|
|
|
|
|
|
|
# main search methods and helper functions
|
2012-03-30 23:47:22 +02:00
|
|
|
|
2010-08-29 18:46:58 +00:00
|
|
|
@returns_typeclass_list
|
2011-04-24 11:26:51 +00:00
|
|
|
def object_search(self, ostring, caller=None,
|
2012-09-17 15:31:50 +02:00
|
|
|
attribute_name=None,
|
|
|
|
|
candidates=None,
|
|
|
|
|
exact=True):
|
2010-08-29 18:46:58 +00:00
|
|
|
"""
|
2011-04-23 11:54:08 +00:00
|
|
|
Search as an object and return results. The result is always an Object.
|
|
|
|
|
If * is appended (player search, a Character controlled by this Player
|
|
|
|
|
is looked for. The Character is returned, not the Player. Use player_search
|
2011-04-24 11:26:51 +00:00
|
|
|
to find Player objects. Always returns a list.
|
2012-03-30 23:47:22 +02:00
|
|
|
|
|
|
|
|
Arguments:
|
2010-08-29 18:46:58 +00:00
|
|
|
ostring: (string) The string to compare names against.
|
2012-09-17 22:16:18 +02:00
|
|
|
Can be a dbref. If name is prepended by *, a player is searched for.
|
2012-03-30 23:47:22 +02:00
|
|
|
caller: (Object) The optional object performing the search.
|
|
|
|
|
attribute_name: (string) Which object attribute to match ostring against. If not
|
|
|
|
|
set, the "key" and "aliases" properties are searched in order.
|
2012-09-17 15:31:50 +02:00
|
|
|
candidates (list obj ObjectDBs): If objlist is supplied, global_search keyword is ignored
|
|
|
|
|
and search will only be performed among the candidates in this list. A common list
|
|
|
|
|
of candidates is the contents of the current location searched.
|
|
|
|
|
exact (bool): Match names/aliases exactly or partially. Partial matching matches the
|
|
|
|
|
beginning of words in the names/aliases, using a matching routine to separate
|
|
|
|
|
multiple matches in names with multiple components (so "bi sw" will match
|
|
|
|
|
"Big sword"). Since this is more expensive than exact matching, it is
|
|
|
|
|
recommended to be used together with the objlist keyword to limit the number
|
|
|
|
|
of possibilities. This value has no meaning if searching for attributes/properties.
|
2012-03-30 23:47:22 +02:00
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
A list of matching objects (or a list with one unique match)
|
|
|
|
|
|
2010-08-29 18:46:58 +00:00
|
|
|
"""
|
2012-09-17 15:31:50 +02:00
|
|
|
def _searcher(ostring, exact=False):
|
|
|
|
|
"Helper method for searching objects"
|
|
|
|
|
if attribute_name:
|
|
|
|
|
# attribute/property search (always exact).
|
|
|
|
|
matches = self.get_objs_with_db_property_value(attribute_name, ostring, candidates=candidates)
|
2012-09-29 00:09:55 +02:00
|
|
|
if matches:
|
|
|
|
|
return matches
|
|
|
|
|
return self.get_objs_with_attr_value(attribute_name, ostring, candidates=candidates)
|
2012-09-27 22:18:46 +02:00
|
|
|
if ostring.startswith("*"):
|
|
|
|
|
# Player search - try to find obj by its player's name
|
|
|
|
|
player_match = self.get_object_with_player(ostring, candidates=candidates)
|
|
|
|
|
if player_match is not None:
|
|
|
|
|
return [player_match]
|
2012-10-14 17:27:57 +02:00
|
|
|
return []
|
2012-09-17 15:31:50 +02:00
|
|
|
else:
|
|
|
|
|
# normal key/alias search
|
|
|
|
|
return self.get_objs_with_key_or_alias(ostring, exact=exact, candidates=candidates)
|
|
|
|
|
|
2012-08-18 23:17:14 +02:00
|
|
|
if not ostring and ostring != 0:
|
2012-03-30 23:47:22 +02:00
|
|
|
return []
|
2010-09-12 15:07:12 +00:00
|
|
|
|
2012-10-22 18:03:10 -07:00
|
|
|
# Convenience check to make sure candidates are really dbobjs
|
|
|
|
|
if candidates:
|
|
|
|
|
candidates = [cand.dbobj for cand in make_iter(candidates) if hasattr(cand, "dbobj")]
|
|
|
|
|
|
2012-10-23 20:29:03 +02:00
|
|
|
# If candidates is given as an empty list, don't go any further.
|
2012-10-22 18:03:10 -07:00
|
|
|
if candidates == []:
|
|
|
|
|
return []
|
|
|
|
|
|
2012-09-29 00:09:55 +02:00
|
|
|
dbref = not attribute_name and self.dbref(ostring)
|
2012-09-29 16:24:47 +02:00
|
|
|
if dbref or dbref == 0:
|
2012-09-29 00:09:55 +02:00
|
|
|
# Easiest case - dbref matching (always exact)
|
2010-08-29 18:46:58 +00:00
|
|
|
dbref_match = self.dbref_search(dbref)
|
|
|
|
|
if dbref_match:
|
2012-10-22 18:03:10 -07:00
|
|
|
if candidates == None or dbref_match.dbobj in candidates:
|
|
|
|
|
return [dbref_match]
|
|
|
|
|
else:
|
|
|
|
|
return []
|
2012-10-14 13:25:25 +02:00
|
|
|
|
2010-10-30 18:42:37 +00:00
|
|
|
# Search through all possibilities.
|
|
|
|
|
|
|
|
|
|
match_number = None
|
2012-09-17 15:31:50 +02:00
|
|
|
# always run first check exact - we don't want partial matches if on the form of 1-keyword etc.
|
|
|
|
|
matches = _searcher(ostring, exact=True)
|
2010-10-30 18:42:37 +00:00
|
|
|
if not matches:
|
2012-09-17 15:31:50 +02:00
|
|
|
# no matches found - check if we are dealing with N-keyword query - if so, strip it.
|
2012-03-31 15:09:22 +02:00
|
|
|
match_number, ostring = _AT_MULTIMATCH_INPUT(ostring)
|
2012-09-17 15:31:50 +02:00
|
|
|
# run search again, with the exactness set by caller
|
|
|
|
|
matches = _searcher(ostring, exact=exact)
|
|
|
|
|
|
|
|
|
|
# deal with result
|
2010-10-30 18:42:37 +00:00
|
|
|
if len(matches) > 1 and match_number != None:
|
2012-09-17 15:31:50 +02:00
|
|
|
# multiple matches, but a number was given to separate them
|
2010-10-30 18:42:37 +00:00
|
|
|
try:
|
|
|
|
|
matches = [matches[match_number]]
|
|
|
|
|
except IndexError:
|
|
|
|
|
pass
|
2012-09-17 15:31:50 +02:00
|
|
|
# return a list (possibly empty)
|
2010-10-30 18:42:37 +00:00
|
|
|
return matches
|
2012-03-30 23:47:22 +02:00
|
|
|
|
2010-08-29 18:46:58 +00:00
|
|
|
#
|
|
|
|
|
# ObjectManager Copy method
|
|
|
|
|
#
|
|
|
|
|
|
2011-05-01 18:04:15 +00:00
|
|
|
def copy_object(self, original_object, new_key=None,
|
2012-03-30 23:47:22 +02:00
|
|
|
new_location=None, new_player=None, new_home=None,
|
2011-05-01 18:04:15 +00:00
|
|
|
new_permissions=None, new_locks=None, new_aliases=None, new_destination=None):
|
2010-08-29 18:46:58 +00:00
|
|
|
"""
|
2011-05-01 18:04:15 +00:00
|
|
|
Create and return a new object as a copy of the original object. All will
|
2012-03-30 23:47:22 +02:00
|
|
|
be identical to the original except for the arguments given specifically
|
2010-10-30 18:42:37 +00:00
|
|
|
to this method.
|
2010-08-29 18:46:58 +00:00
|
|
|
|
|
|
|
|
original_object (obj) - the object to make a copy from
|
2012-03-30 23:47:22 +02:00
|
|
|
new_key (str) - name the copy differently from the original.
|
2010-10-30 18:42:37 +00:00
|
|
|
new_location (obj) - if not None, change the location
|
|
|
|
|
new_home (obj) - if not None, change the Home
|
|
|
|
|
new_aliases (list of strings) - if not None, change object aliases.
|
2011-05-01 18:04:15 +00:00
|
|
|
new_destination (obj) - if not None, change destination
|
2010-08-29 18:46:58 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
# get all the object's stats
|
|
|
|
|
typeclass_path = original_object.typeclass_path
|
2012-03-30 23:47:22 +02:00
|
|
|
if not new_key:
|
2011-05-01 18:04:15 +00:00
|
|
|
new_key = original_object.key
|
2010-10-30 18:42:37 +00:00
|
|
|
if not new_location:
|
|
|
|
|
new_location = original_object.location
|
|
|
|
|
if not new_home:
|
2011-04-07 22:10:51 +00:00
|
|
|
new_home = original_object.home
|
|
|
|
|
if not new_player:
|
|
|
|
|
new_player = original_object.player
|
2010-10-30 18:42:37 +00:00
|
|
|
if not new_aliases:
|
2012-03-30 23:47:22 +02:00
|
|
|
new_aliases = original_object.aliases
|
2011-04-07 22:10:51 +00:00
|
|
|
if not new_locks:
|
|
|
|
|
new_locks = original_object.db_lock_storage
|
|
|
|
|
if not new_permissions:
|
2012-03-30 23:47:22 +02:00
|
|
|
new_permissions = original_object.permissions
|
2011-05-01 18:04:15 +00:00
|
|
|
if not new_destination:
|
|
|
|
|
new_destination = original_object.destination
|
2012-03-30 23:47:22 +02:00
|
|
|
|
|
|
|
|
# create new object
|
|
|
|
|
from src.utils import create
|
2011-04-07 22:10:51 +00:00
|
|
|
from src.scripts.models import ScriptDB
|
2011-05-01 18:04:15 +00:00
|
|
|
new_object = create.create_object(typeclass_path, key=new_key, location=new_location,
|
2012-03-30 23:47:22 +02:00
|
|
|
home=new_home, player=new_player, permissions=new_permissions,
|
2011-05-01 18:04:15 +00:00
|
|
|
locks=new_locks, aliases=new_aliases, destination=new_destination)
|
2010-08-29 18:46:58 +00:00
|
|
|
if not new_object:
|
2012-03-30 23:47:22 +02:00
|
|
|
return None
|
2010-08-29 18:46:58 +00:00
|
|
|
|
2012-03-30 23:47:22 +02:00
|
|
|
# copy over all attributes from old to new.
|
2011-04-07 22:10:51 +00:00
|
|
|
for attr in original_object.get_all_attributes():
|
|
|
|
|
new_object.set_attribute(attr.key, attr.value)
|
2010-08-29 18:46:58 +00:00
|
|
|
|
2012-03-30 23:47:22 +02:00
|
|
|
# copy over all cmdsets, if any
|
2011-04-07 22:10:51 +00:00
|
|
|
for icmdset, cmdset in enumerate(original_object.cmdset.all()):
|
|
|
|
|
if icmdset == 0:
|
|
|
|
|
new_object.cmdset.add_default(cmdset)
|
|
|
|
|
else:
|
|
|
|
|
new_object.cmdset.add(cmdset)
|
|
|
|
|
|
2012-03-30 23:47:22 +02:00
|
|
|
# copy over all scripts, if any
|
2011-04-07 22:10:51 +00:00
|
|
|
for script in original_object.scripts.all():
|
|
|
|
|
ScriptDB.objects.copy_script(script, new_obj=new_object.dbobj)
|
2012-03-30 23:47:22 +02:00
|
|
|
|
2010-08-29 18:46:58 +00:00
|
|
|
return new_object
|
2013-02-03 17:00:46 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def clear_all_sessids(self):
|
|
|
|
|
"""
|
|
|
|
|
Clear the db_sessid field of all objects having also the db_player field
|
|
|
|
|
set.
|
|
|
|
|
"""
|
|
|
|
|
self.filter(db_sessid__isnull=False).update(db_sessid=None)
|
|
|
|
|
|