Added the possibility to use #dbrefs as arguments to ev.create_object (e.g. for home and location). If a non-existing #dbref is given, a DoesNotExist error will be raised.

This commit is contained in:
Griatch 2014-01-18 22:53:30 +01:00
parent 2db2141b90
commit 64fd3580a3

View file

@ -49,12 +49,37 @@ __all__ = ("create_object", "create_script", "create_help_entry",
_GA = object.__getattribute__
# Helper functions
def handle_dbref(inp, objclass=None, raise_errors=True):
"""
Convert a #dbid to a valid object of objclass. objclass
should be a valid object class to filter against (objclass.filter ...)
If not raise_errors is set, this will swallow errors of non-existing
objects.
"""
if not (isinstance(inp, basestring) and inp.startswith("#")):
return inp
inp = inp.lstrip('#')
try:
if int(inp) < 0:
return None
except ValueError:
return None
# if we get to this point, inp is an integer dbref
try:
return objclass.objects.get(id=inp)
except _GA(objclass, "DoesNotExist"):
if raise_errors:
raise
return inp
#
# Game Object creation
#
def create_object(typeclass, key=None, location=None,
def create_object(typeclass=None, key=None, location=None,
home=None, permissions=None, locks=None,
aliases=None, destination=None, report_to=None, nohome=False):
"""
@ -81,6 +106,8 @@ def create_object(typeclass, key=None, location=None,
if not _ObjectDB:
from src.objects.models import ObjectDB as _ObjectDB
# input validation
if not typeclass:
typeclass = settings.BASE_OBJECT_TYPECLASS
elif isinstance(typeclass, _ObjectDB):
@ -90,6 +117,12 @@ def create_object(typeclass, key=None, location=None,
# this is already an object typeclass, extract its path
typeclass = typeclass.path
# handle eventual #dbref input
location = handle_dbref(location, _ObjectDB)
home = handle_dbref(home, _ObjectDB)
destination = handle_dbref(destination, _ObjectDB)
report_to = handle_dbref(report_to, _ObjectDB)
# create new database object
new_db_object = _ObjectDB()
@ -454,6 +487,10 @@ def create_player(key, email, password,
email = "dummy@dummy.com"
if _PlayerDB.objects.filter(username__iexact=key):
raise ValueError("A Player with this name already exists.")
# this handles a given dbref-relocate to a player.
report_to = handle_dbref(report_to, _PlayerDB)
try:
# create the correct Player object