Further cleanup of source; making class methods _private for clarity in the API.

This commit is contained in:
Griatch 2012-03-31 15:09:22 +02:00
parent fc156b5a54
commit c8df141e89
18 changed files with 607 additions and 588 deletions

View file

@ -2,12 +2,13 @@
The managers for the custom Player object and permissions.
"""
import datetime
import datetime
from functools import update_wrapper
from django.db import models
from django.contrib.auth.models import User
from src.typeclasses.managers import returns_typeclass_list, returns_typeclass, TypedObjectManager
from src.utils import logger
__all__ = ("PlayerManager",)
#
# Player Manager
@ -35,16 +36,16 @@ def returns_player_list(method):
players.append(user.get_profile())
except Exception:
# there is something wrong with get_profile. But
# there is a 1-1 relation between Users-Players, so we
# there is a 1-1 relation between Users-Players, so we
# try to go the other way instead.
from src.players.models import PlayerDB
match = PlayerDB.objects.filter(user=user)
if match:
players.append(match[0])
else:
logger.log_trace("No connection User<->Player, maybe database was partially reset?")
logger.log_trace("No connection User<->Player, maybe database was partially reset?")
return players
return update_wrapper(func, method)
return update_wrapper(func, method)
def returns_player(method):
"""
@ -57,18 +58,18 @@ def returns_player(method):
if match:
return match[0]
else:
return None
return None
return update_wrapper(func, method)
class PlayerManager(TypedObjectManager):
"""
This PlayerManager implements methods for searching
This PlayerManager implements methods for searching
and manipulating Players directly from the database.
Evennia-specific search methods (will return Characters if
possible or a Typeclass/list of Typeclassed objects, whereas
Django-general methods will return Querysets or database objects):
dbref (converter)
dbref_search
get_dbref_range
@ -83,14 +84,14 @@ class PlayerManager(TypedObjectManager):
get_player_from_name
player_search (equivalent to ev.search_player)
swap_character
"""
def num_total_players(self):
"""
Returns the total number of registered users/players.
"""
return self.count()
@returns_typeclass_list
def get_connected_players(self):
"""
@ -99,7 +100,7 @@ class PlayerManager(TypedObjectManager):
return [player for player in self.all() if player.sessions]
@returns_typeclass_list
@returns_player_list
@returns_player_list
def get_recently_created_players(self, days=7):
"""
Returns a QuerySet containing the player User accounts that have been
@ -145,8 +146,8 @@ class PlayerManager(TypedObjectManager):
players = self.filter(user__username=uname)
if players:
return players[0]
return None
return None
# @returns_typeclass_list
# def get_players_with_perm(self, permstring):
# """
@ -159,7 +160,7 @@ class PlayerManager(TypedObjectManager):
# @returns_typeclass_list
# def get_players_with_group(self, groupstring):
# """
# Returns all players belonging to the given group.
# Returns all players belonging to the given group.
# """
# return [player.user for player in self.all()
# if player.has_group(groupstring)]
@ -167,9 +168,9 @@ class PlayerManager(TypedObjectManager):
@returns_typeclass_list
def player_search(self, ostring):
"""
Searches for a particular player by name or
Searches for a particular player by name or
database id.
ostring = a string or database id.
"""
ostring = ostring.lstrip("*")
@ -178,7 +179,7 @@ class PlayerManager(TypedObjectManager):
matches = self.filter(id=dbref)
if matches:
return matches
return self.filter(user__username__iexact=ostring)
return self.filter(user__username__iexact=ostring)
def swap_character(self, player, new_character, delete_old_character=False):
"""
@ -192,7 +193,7 @@ class PlayerManager(TypedObjectManager):
return False
# do the swap
old_character = player.character
old_character = player.character
if old_character:
old_character.player = None
try:
@ -202,8 +203,7 @@ class PlayerManager(TypedObjectManager):
# recover old setup
old_character.player = player
player.character = old_character
return False
return False
if delete_old_character:
old_character.delete()
return True
return True