2010-08-29 18:46:58 +00:00
#
2013-02-12 19:54:11 +01:00
# This sets up how models are displayed
# in the web admin interface.
2010-08-29 18:46:58 +00:00
#
2011-10-02 22:37:07 +02:00
from django import forms
from django . db import models
2013-02-12 19:54:11 +01:00
from django . conf import settings
2010-08-29 18:46:58 +00:00
from django . contrib import admin
2011-09-15 00:28:26 +02:00
from django . contrib . auth . admin import UserAdmin as BaseUserAdmin
2011-10-02 22:37:07 +02:00
from django . contrib . admin import widgets
from django . contrib . auth . forms import UserChangeForm , UserCreationForm
from django . contrib . auth . models import User
2011-09-14 22:34:10 +02:00
from src . players . models import PlayerDB , PlayerAttribute
2011-11-13 18:46:14 +01:00
from src . utils import logger , create
2013-02-12 19:54:11 +01:00
2011-09-14 22:34:10 +02:00
# remove User itself from admin site
admin . site . unregister ( User )
2011-10-02 22:37:07 +02:00
# handle the custom User editor
class CustomUserChangeForm ( UserChangeForm ) :
2013-02-12 19:54:11 +01:00
username = forms . RegexField ( label = " Username " ,
max_length = 30 ,
2011-10-04 21:14:41 +02:00
regex = r ' ^[ \ w. @+-]+$ ' ,
widget = forms . TextInput ( attrs = { ' size ' : ' 30 ' } ) ,
2013-02-12 19:54:11 +01:00
error_messages = { ' invalid ' : " This value may contain only letters, spaces, numbers and @/./+/-/_ characters. " } ,
2011-11-13 18:46:14 +01:00
help_text = " 30 characters or fewer. Letters, spaces, digits and @/./+/-/_ only. " )
2013-05-13 12:30:00 -05:00
def clean_username ( self ) :
username = self . cleaned_data [ ' username ' ]
if username . upper ( ) == self . instance . username . upper ( ) :
return username
elif User . objects . filter ( username__iexact = username ) :
raise forms . ValidationError ( ' A player with that name already exists. ' )
return self . cleaned_data [ ' username ' ]
2011-10-04 21:14:41 +02:00
2011-10-02 22:37:07 +02:00
class CustomUserCreationForm ( UserCreationForm ) :
2013-02-12 19:54:11 +01:00
username = forms . RegexField ( label = " Username " ,
max_length = 30 ,
2011-10-04 21:14:41 +02:00
regex = r ' ^[ \ w. @+-]+$ ' ,
widget = forms . TextInput ( attrs = { ' size ' : ' 30 ' } ) ,
2013-02-12 19:54:11 +01:00
error_messages = { ' invalid ' : " This value may contain only letters, spaces, numbers and @/./+/-/_ characters. " } ,
2011-11-13 18:46:14 +01:00
help_text = " 30 characters or fewer. Letters, spaces, digits and @/./+/-/_ only. " )
2013-05-13 12:30:00 -05:00
def clean_username ( self ) :
username = self . cleaned_data [ ' username ' ]
if User . objects . filter ( username__iexact = username ) :
raise forms . ValidationError ( ' A player with that name already exists. ' )
return username
2013-02-12 19:54:11 +01:00
# # The Player editor
2011-11-13 18:46:14 +01:00
# class PlayerAttributeForm(forms.ModelForm):
# "Defines how to display the atttributes"
# class Meta:
# model = PlayerAttribute
2013-02-12 19:54:11 +01:00
# db_key = forms.CharField(label="Key",
2011-11-13 18:46:14 +01:00
# widget=forms.TextInput(attrs={'size':'15'}))
2013-02-12 19:54:11 +01:00
# db_value = forms.CharField(label="Value",
2011-11-13 18:46:14 +01:00
# widget=forms.Textarea(attrs={'rows':'2'}))
# class PlayerAttributeInline(admin.TabularInline):
2013-02-12 19:54:11 +01:00
# "Inline creation of player attributes"
2011-11-13 18:46:14 +01:00
# model = PlayerAttribute
# extra = 0
# form = PlayerAttributeForm
# fieldsets = (
# (None, {'fields' : (('db_key', 'db_value'))}),)
class PlayerForm ( forms . ModelForm ) :
" Defines how to display Players "
2011-09-15 00:28:26 +02:00
2011-10-02 22:37:07 +02:00
class Meta :
model = PlayerDB
2013-02-12 19:54:11 +01:00
db_key = forms . RegexField ( label = " Username " ,
2011-11-13 18:46:14 +01:00
initial = " PlayerDummy " ,
2013-02-12 19:54:11 +01:00
max_length = 30 ,
2011-11-13 18:46:14 +01:00
regex = r ' ^[ \ w. @+-]+$ ' ,
2013-02-12 19:54:11 +01:00
required = False ,
2011-10-04 21:14:41 +02:00
widget = forms . TextInput ( attrs = { ' size ' : ' 30 ' } ) ,
2013-02-12 19:54:11 +01:00
error_messages = { ' invalid ' : " This value may contain only letters, spaces, numbers and @/./+/-/_ characters. " } ,
2011-11-13 18:46:14 +01:00
help_text = " This should be the same as the connected Player ' s key name. 30 characters or fewer. Letters, spaces, digits and @/./+/-/_ only. " )
2011-10-04 21:14:41 +02:00
db_typeclass_path = forms . CharField ( label = " Typeclass " ,
2013-02-12 19:54:11 +01:00
initial = settings . BASE_PLAYER_TYPECLASS ,
2011-10-04 21:14:41 +02:00
widget = forms . TextInput ( attrs = { ' size ' : ' 78 ' } ) ,
2011-11-13 18:46:14 +01:00
help_text = " Required. Defines what ' type ' of entity this is. This variable holds a Python path to a module with a valid Evennia Typeclass. Defaults to settings.BASE_PLAYER_TYPECLASS. " )
2013-02-12 19:54:11 +01:00
db_permissions = forms . CharField ( label = " Permissions " ,
2011-10-04 21:14:41 +02:00
initial = settings . PERMISSION_PLAYER_DEFAULT ,
required = False ,
widget = forms . TextInput ( attrs = { ' size ' : ' 78 ' } ) ,
2011-11-13 18:46:14 +01:00
help_text = " In-game permissions. A comma-separated list of text strings checked by certain locks. They are often used for hierarchies, such as letting a Player have permission ' Wizards ' , ' Builders ' etc. A Player permission can be overloaded by the permissions of a controlled Character. Normal players use ' Players ' by default. " )
2013-02-12 19:54:11 +01:00
db_lock_storage = forms . CharField ( label = " Locks " ,
2011-10-04 21:14:41 +02:00
widget = forms . Textarea ( attrs = { ' cols ' : ' 100 ' , ' rows ' : ' 2 ' } ) ,
2011-10-02 22:37:07 +02:00
required = False ,
2011-11-13 18:46:14 +01:00
help_text = " In-game lock definition string. If not given, defaults will be used. This string should be on the form <i>type:lockfunction(args);type2:lockfunction2(args);... " )
2013-02-12 19:54:11 +01:00
db_cmdset_storage = forms . CharField ( label = " cmdset " ,
2013-04-11 01:02:05 +02:00
initial = settings . CMDSET_PLAYER ,
2011-10-04 21:14:41 +02:00
widget = forms . TextInput ( attrs = { ' size ' : ' 78 ' } ) ,
2011-10-02 22:37:07 +02:00
required = False ,
2013-04-11 01:02:05 +02:00
help_text = " python path to player cmdset class (set in settings.CMDSET_PLAYER by default) " )
2013-02-12 19:54:11 +01:00
2011-11-13 18:46:14 +01:00
class PlayerInline ( admin . StackedInline ) :
" Inline creation of Player "
model = PlayerDB
template = " admin/players/stacked.html "
form = PlayerForm
2011-10-02 01:21:03 +02:00
fieldsets = (
2011-11-13 18:46:14 +01:00
( " In-game Permissions and Locks " ,
{ ' fields ' : ( ' db_permissions ' , ' db_lock_storage ' ) ,
' description ' : " <i>These are permissions/locks for in-game use. They are unrelated to website access rights.</i> " } ) ,
( " In-game Player data " ,
{ ' fields ' : ( ' db_typeclass_path ' , ' db_cmdset_storage ' ) ,
' description ' : " <i>These fields define in-game-specific properties for the Player object in-game.</i> " } ) ,
2013-04-09 19:43:27 +02:00
)
2011-10-02 22:37:07 +02:00
2011-11-13 18:46:14 +01:00
extra = 1
max_num = 1
2013-02-12 19:54:11 +01:00
2011-11-13 18:46:14 +01:00
class UserAdmin ( BaseUserAdmin ) :
" This is the main creation screen for Users/players "
2011-10-02 23:02:43 +02:00
2011-11-13 18:46:14 +01:00
list_display = ( ' username ' , ' email ' , ' is_staff ' , ' is_superuser ' )
form = CustomUserChangeForm
add_form = CustomUserCreationForm
inlines = [ PlayerInline ]
add_form_template = " admin/players/add_form.html "
change_form_template = " admin/players/change_form.html "
change_list_template = " admin/players/change_list.html "
fieldsets = (
( None , { ' fields ' : ( ' username ' , ' password ' , ' email ' ) } ) ,
( ' Website profile ' , { ' fields ' : ( ' first_name ' , ' last_name ' ) ,
' description ' : " <i>These are not used in the default system.</i> " } ) ,
( ' Website dates ' , { ' fields ' : ( ' last_login ' , ' date_joined ' ) ,
' description ' : ' <i>Relevant only to the website.</i> ' } ) ,
( ' Website Permissions ' , { ' fields ' : ( ' is_active ' , ' is_staff ' , ' is_superuser ' , ' user_permissions ' , ' groups ' ) ,
' description ' : " <i>These are permissions/permission groups for accessing the admin site. They are unrelated to in-game access rights.</i> " } ) , )
2011-10-02 22:37:07 +02:00
2011-11-13 18:46:14 +01:00
add_fieldsets = (
2013-02-12 19:54:11 +01:00
( None ,
2011-11-13 18:46:14 +01:00
{ ' fields ' : ( ' username ' , ' password1 ' , ' password2 ' , ' email ' ) ,
' description ' : " <i>These account details are shared by the admin system and the game.</i> " } , ) , )
2013-02-12 19:54:11 +01:00
def save_formset ( self , request , form , formset , change ) :
2011-11-13 18:46:14 +01:00
" Run all hooks on the player object "
super ( UserAdmin , self ) . save_formset ( request , form , formset , change )
2013-02-12 19:54:11 +01:00
userobj = form . instance
playerobj = userobj . get_profile ( )
2013-05-13 12:30:00 -05:00
playerobj . name = userobj . username
2013-02-12 19:54:11 +01:00
if not change :
#uname, passwd, email = str(request.POST.get(u"username")), \
# str(request.POST.get(u"password1")), str(request.POST.get(u"email"))
typeclass = str ( request . POST . get ( u " playerdb_set-0-db_typeclass_path " ) )
create . create_player ( " " , " " , " " ,
user = userobj ,
typeclass = typeclass ,
2013-05-13 12:30:00 -05:00
player_dbobj = playerobj )
2013-02-12 19:54:11 +01:00
2011-11-13 18:46:14 +01:00
admin . site . register ( User , UserAdmin )