Make all manager methods return querysets. This removes all the last remnants of the old return_typeclass/return_typeclass_list decorators that were a remnant of the old pre-proxy Typeclass system. Resolves #1206.

This commit is contained in:
Griatch 2017-04-20 19:43:39 +02:00
parent dd7d18f041
commit cf77d90c71
7 changed files with 25 additions and 98 deletions

View file

@ -5,10 +5,8 @@ Comm system components.
"""
from __future__ import print_function
from django.db import models
from django.db.models import Q
from evennia.typeclasses.managers import (TypedObjectManager, TypeclassManager,
returns_typeclass_list, returns_typeclass)
from evennia.typeclasses.managers import (TypedObjectManager, TypeclassManager)
from evennia.utils import logger
_GA = object.__getattribute__
@ -131,6 +129,7 @@ def to_object(inp, objtype='player'):
# an unknown
return None
#
# Msg manager
#
@ -314,6 +313,7 @@ class MsgManager(TypedObjectManager):
# back-compatibility alias
message_search = search_message
#
# Channel manager
#
@ -332,7 +332,6 @@ class ChannelDBManager(TypedObjectManager):
subscribed to the Channel.
"""
@returns_typeclass_list
def get_all_channels(self):
"""
Get all channels.
@ -343,7 +342,6 @@ class ChannelDBManager(TypedObjectManager):
"""
return self.all()
@returns_typeclass
def get_channel(self, channelkey):
"""
Return the channel object if given its key.
@ -366,7 +364,6 @@ class ChannelDBManager(TypedObjectManager):
return channels[0]
return None
@returns_typeclass_list
def get_subscriptions(self, subscriber):
"""
Return all channels a given entity is subscribed to.
@ -385,7 +382,6 @@ class ChannelDBManager(TypedObjectManager):
return subscriber.object_subscription_set.all()
return []
@returns_typeclass_list
def search_channel(self, ostring, exact=True):
"""
Search the channel database for a particular channel.
@ -397,7 +393,8 @@ class ChannelDBManager(TypedObjectManager):
"""
channels = []
if not ostring: return channels
if not ostring:
return channels
try:
# try an id match first
dbref = int(ostring.strip('#'))
@ -414,16 +411,14 @@ class ChannelDBManager(TypedObjectManager):
if not channels:
# still no match. Search by alias.
channels = [channel for channel in self.all()
if ostring.lower() in [a.lower
for a in channel.aliases.all()]]
if ostring.lower() in [a.lower for a in channel.aliases.all()]]
return channels
# back-compatibility alias
channel_search = search_channel
class ChannelManager(ChannelDBManager, TypeclassManager):
"""
Wrapper to group the typeclass manager to a consistent name.
"""
pass

View file

@ -34,10 +34,9 @@ from django.conf import settings
from evennia.players.models import PlayerDB
from evennia.objects.models import ObjectDB
from evennia.server.models import ServerConfig
from evennia.comms.models import ChannelDB
from evennia.commands.cmdset import CmdSet
from evennia.utils import create, logger, utils, ansi
from evennia.utils import logger, utils, ansi
from evennia.commands.default.muxcommand import MuxCommand
from evennia.commands.cmdhandler import CMD_LOGINSTART
from evennia.commands.default import unloggedin as default_unloggedin # Used in CmdUnconnectedCreate
@ -100,14 +99,13 @@ class CmdUnconnectedConnect(MuxCommand):
session.msg(string)
return
# We have at least one result, so we can check the password.
if not player.check_password(password):
if not player[0].check_password(password):
session.msg("Incorrect password.")
return
# Check IP and/or name bans
bans = ServerConfig.objects.conf("server_bans")
if bans and (any(tup[0] == player.name for tup in bans)
or
if bans and (any(tup[0] == player.name for tup in bans) or
any(tup[2].match(session.address[0]) for tup in bans if tup[2])):
# this is a banned IP or name!
string = "|rYou have been banned and cannot continue from here."
@ -207,8 +205,7 @@ class CmdUnconnectedCreate(MuxCommand):
# Check IP and/or name bans
bans = ServerConfig.objects.conf("server_bans")
if bans and (any(tup[0] == playername.lower() for tup in bans)
or
if bans and (any(tup[0] == playername.lower() for tup in bans) or
any(tup[2].match(session.address) for tup in bans if tup[2])):
# this is a banned IP or name!
string = "|rYou have been banned and cannot continue from here." \

View file

@ -92,7 +92,7 @@ class CmdMail(default_cmds.MuxCommand):
player = self.caller.player
except AttributeError:
player = self.caller
messages = Msg.objects.get_by_tag(category="mail", raw_queryset=True).filter(db_receivers_players=player)
messages = Msg.objects.get_by_tag(category="mail").filter(db_receivers_players=player)
return messages
def send_mail(self, recipients, subject, message, caller):

View file

@ -7,7 +7,6 @@ from django.db.models import Q
from django.conf import settings
from django.db.models.fields import exceptions
from evennia.typeclasses.managers import TypedObjectManager, TypeclassManager
from evennia.typeclasses.managers import returns_typeclass, returns_typeclass_list
from evennia.utils.utils import to_unicode, is_iter, make_iter, string_partial_matching
from builtins import int
@ -56,7 +55,6 @@ class ObjectDBManager(TypedObjectManager):
# player related
@returns_typeclass
def get_object_with_player(self, ostring, exact=True, candidates=None):
"""
Search for an object based on its player's name or dbref.
@ -93,7 +91,6 @@ class ObjectDBManager(TypedObjectManager):
else:
return string_partial_matching(ply_cands, ostring, ret_index=False)
@returns_typeclass_list
def get_objs_with_key_and_typeclass(self, oname, otypeclass_path, candidates=None):
"""
Returns objects based on simultaneous key and typeclass match.
@ -112,7 +109,6 @@ class ObjectDBManager(TypedObjectManager):
# attr/property related
@returns_typeclass_list
def get_objs_with_attr(self, attribute_name, candidates=None):
"""
Get objects based on having a certain Attribute defined.
@ -130,7 +126,6 @@ class ObjectDBManager(TypedObjectManager):
if obj]) or Q()
return list(self.filter(cand_restriction & Q(db_attributes__db_key=attribute_name)))
@returns_typeclass_list
def get_objs_with_attr_value(self, attribute_name, attribute_value, candidates=None, typeclasses=None):
"""
Get all objects having the given attrname set to the given value.
@ -169,7 +164,6 @@ class ObjectDBManager(TypedObjectManager):
db_value=attribute_value)]
return chain(*results)
@returns_typeclass_list
def get_objs_with_db_property(self, property_name, candidates=None):
"""
Get all objects having a given db field property.
@ -191,7 +185,6 @@ class ObjectDBManager(TypedObjectManager):
except exceptions.FieldError:
return []
@returns_typeclass_list
def get_objs_with_db_property_value(self, property_name, property_value, candidates=None, typeclasses=None):
"""
Get objects with a specific field name and value.
@ -222,7 +215,6 @@ class ObjectDBManager(TypedObjectManager):
(property_name, type(property_value)))
return []
@returns_typeclass_list
def get_contents(self, location, excludeobj=None):
"""
Get all objects that has a location set to this one.
@ -238,7 +230,6 @@ class ObjectDBManager(TypedObjectManager):
exclude_restriction = Q(pk__in=[_GA(obj, "id") for obj in make_iter(excludeobj)]) if excludeobj else 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, typeclasses=None):
"""
@ -303,7 +294,6 @@ class ObjectDBManager(TypedObjectManager):
# main search methods and helper functions
@returns_typeclass_list
def search_object(self, searchdata,
attribute_name=None,
typeclass=None,

View file

@ -5,10 +5,7 @@ The managers for the custom Player object and permissions.
import datetime
from django.utils import timezone
from django.contrib.auth.models import UserManager
#from functools import update_wrapper
from evennia.typeclasses.managers import (returns_typeclass_list, returns_typeclass,
TypedObjectManager, TypeclassManager)
from evennia.utils.utils import make_iter
from evennia.typeclasses.managers import (TypedObjectManager, TypeclassManager)
__all__ = ("PlayerManager",)
@ -51,7 +48,6 @@ class PlayerDBManager(TypedObjectManager, UserManager):
"""
return self.count()
@returns_typeclass_list
def get_connected_players(self):
"""
Get all currently connected players.
@ -63,7 +59,6 @@ class PlayerDBManager(TypedObjectManager, UserManager):
"""
return self.filter(db_is_connected=True)
@returns_typeclass_list
def get_recently_created_players(self, days=7):
"""
Get players recently created.
@ -80,7 +75,6 @@ class PlayerDBManager(TypedObjectManager, UserManager):
start_date = end_date - tdelta
return self.filter(date_joined__range=(start_date, end_date))
@returns_typeclass_list
def get_recently_connected_players(self, days=7):
"""
Get players recently connected to the game.
@ -99,7 +93,6 @@ class PlayerDBManager(TypedObjectManager, UserManager):
return self.filter(last_login__range=(
start_date, end_date)).order_by('-last_login')
@returns_typeclass
def get_player_from_email(self, uemail):
"""
Search player by
@ -114,7 +107,6 @@ class PlayerDBManager(TypedObjectManager, UserManager):
"""
return self.filter(email__iexact=uemail)
@returns_typeclass
def get_player_from_uid(self, uid):
"""
Get a player by id.
@ -131,7 +123,6 @@ class PlayerDBManager(TypedObjectManager, UserManager):
except self.model.DoesNotExist:
return None
@returns_typeclass
def get_player_from_name(self, uname):
"""
Get player object based on name.
@ -148,7 +139,6 @@ class PlayerDBManager(TypedObjectManager, UserManager):
except self.model.DoesNotExist:
return None
@returns_typeclass_list
def search_player(self, ostring, exact=True, typeclass=None):
"""
Searches for a particular player by name or
@ -185,5 +175,6 @@ class PlayerDBManager(TypedObjectManager, UserManager):
# back-compatibility alias
player_search = search_player
class PlayerManager(PlayerDBManager, TypeclassManager):
pass

View file

@ -4,7 +4,6 @@ The custom manager for Scripts.
from django.db.models import Q
from evennia.typeclasses.managers import TypedObjectManager, TypeclassManager
from evennia.typeclasses.managers import returns_typeclass_list
from evennia.utils.utils import make_iter
__all__ = ("ScriptManager",)
_GA = object.__getattribute__
@ -35,7 +34,6 @@ class ScriptDBManager(TypedObjectManager):
copy_script
"""
@returns_typeclass_list
def get_all_scripts_on_obj(self, obj, key=None):
"""
Find all Scripts related to a particular object.
@ -68,7 +66,6 @@ class ScriptDBManager(TypedObjectManager):
else:
return self.filter(db_obj=obj)
@returns_typeclass_list
def get_all_scripts(self, key=None):
"""
Get all scripts in the database.
@ -200,7 +197,7 @@ class ScriptDBManager(TypedObjectManager):
elif obj:
scripts = self.get_all_scripts_on_obj(obj, key=key)
else:
scripts = self.get_all_scripts(key=key) #self.model.get_all_cached_instances()
scripts = self.get_all_scripts(key=key)
if not scripts:
# no scripts available to validate
@ -216,7 +213,6 @@ class ScriptDBManager(TypedObjectManager):
VALIDATE_ITERATION -= 1
return nr_started, nr_stopped
@returns_typeclass_list
def search_script(self, ostring, obj=None, only_timed=False):
"""
Search for a particular script.
@ -236,8 +232,8 @@ class ScriptDBManager(TypedObjectManager):
if dbref or dbref == 0:
# this is a dbref, try to find the script directly
dbref_match = self.dbref_search(dbref)
if dbref_match and not ((obj and obj != dbref_match.obj)
or (only_timed and dbref_match.interval)):
if dbref_match and not ((obj and obj != dbref_match.obj) or
(only_timed and dbref_match.interval)):
return [dbref_match]
# not a dbref; normal search
@ -273,5 +269,6 @@ class ScriptDBManager(TypedObjectManager):
locks=new_locks, autostart=True)
return new_script
class ScriptManager(ScriptDBManager, TypeclassManager):
pass

View file

@ -5,7 +5,6 @@ all Attributes and TypedObjects).
"""
import shlex
from functools import update_wrapper
from django.db.models import Q
from evennia.utils import idmapper
from evennia.utils.utils import make_iter, variable_from_module, to_unicode
@ -14,40 +13,6 @@ __all__ = ("TypedObjectManager", )
_GA = object.__getattribute__
_Tag = None
#
# Decorators
#
def returns_typeclass_list(method):
"""
Decorator: Always returns a list, even if it is empty.
"""
def func(self, *args, **kwargs):
self.__doc__ = method.__doc__
raw_queryset = kwargs.pop('raw_queryset', False)
result = method(self, *args, **kwargs)
if raw_queryset:
return result
else:
return list(result)
return update_wrapper(func, method)
def returns_typeclass(method):
"""
Decorator: Returns a single typeclass match or None.
"""
def func(self, *args, **kwargs):
self.__doc__ = method.__doc__
query = method(self, *args, **kwargs)
if hasattr(query, "__iter__"):
result = list(query)
return result[0] if result else None
else:
return query
return update_wrapper(func, method)
# Managers
@ -59,7 +24,6 @@ class TypedObjectManager(idmapper.manager.SharedMemoryManager):
# common methods for all typed managers. These are used
# in other methods. Returns querysets.
# Attribute manager methods
def get_attribute(self, key=None, category=None, value=None, strvalue=None, obj=None, attrtype=None):
"""
@ -125,7 +89,6 @@ class TypedObjectManager(idmapper.manager.SharedMemoryManager):
"""
return self.get_attribute(key=key, category=category, value=value, strvalue=strvalue, obj=obj)
@returns_typeclass_list
def get_by_attribute(self, key=None, category=None, value=None, strvalue=None, attrtype=None):
"""
Return objects having attributes with the given key, category,
@ -257,7 +220,6 @@ class TypedObjectManager(idmapper.manager.SharedMemoryManager):
"""
return self.get_tag(key=key, category=category, obj=obj, tagtype="alias")
@returns_typeclass_list
def get_by_tag(self, key=None, category=None, tagtype=None):
"""
Return objects having tags with a given key or category or
@ -384,7 +346,6 @@ class TypedObjectManager(idmapper.manager.SharedMemoryManager):
return None
return dbref
@returns_typeclass
def get_id(self, dbref):
"""
Find object with given dbref.
@ -416,7 +377,6 @@ class TypedObjectManager(idmapper.manager.SharedMemoryManager):
"""
return self.get_id(dbref)
@returns_typeclass_list
def get_dbref_range(self, min_dbref=None, max_dbref=None):
"""
Get objects within a certain range of dbrefs.
@ -455,7 +415,6 @@ class TypedObjectManager(idmapper.manager.SharedMemoryManager):
self.filter(db_typeclass_path=typeclass_path).count()
return dbtotals
@returns_typeclass_list
def typeclass_search(self, typeclass, include_children=False, include_parents=False):
"""
Searches through all objects returning those which has a
@ -585,7 +544,7 @@ class TypeclassManager(TypedObjectManager):
on the model base used.
"""
kwargs.update({"db_typeclass_path":self.model.path})
kwargs.update({"db_typeclass_path": self.model.path})
return super(TypeclassManager, self).get(**kwargs)
def filter(self, *args, **kwargs):
@ -603,7 +562,7 @@ class TypeclassManager(TypedObjectManager):
objects (queryset): The objects found.
"""
kwargs.update({"db_typeclass_path":self.model.path})
kwargs.update({"db_typeclass_path": self.model.path})
return super(TypeclassManager, self).filter(*args, **kwargs)
def all(self):
@ -683,8 +642,8 @@ class TypeclassManager(TypedObjectManager):
"""
paths = [self.model.path] + ["%s.%s" % (cls.__module__, cls.__name__)
for cls in self._get_subclasses(self.model)]
kwargs.update({"db_typeclass_path__in":paths})
for cls in self._get_subclasses(self.model)]
kwargs.update({"db_typeclass_path__in": paths})
return super(TypeclassManager, self).get(**kwargs)
def filter_family(self, *args, **kwargs):
@ -704,8 +663,8 @@ class TypeclassManager(TypedObjectManager):
"""
# query, including all subclasses
paths = [self.model.path] + ["%s.%s" % (cls.__module__, cls.__name__)
for cls in self._get_subclasses(self.model)]
kwargs.update({"db_typeclass_path__in":paths})
for cls in self._get_subclasses(self.model)]
kwargs.update({"db_typeclass_path__in": paths})
return super(TypeclassManager, self).filter(*args, **kwargs)
def all_family(self):
@ -718,7 +677,5 @@ class TypeclassManager(TypedObjectManager):
"""
paths = [self.model.path] + ["%s.%s" % (cls.__module__, cls.__name__)
for cls in self._get_subclasses(self.model)]
for cls in self._get_subclasses(self.model)]
return super(TypeclassManager, self).all().filter(db_typeclass_path__in=paths)