2010-08-29 18:46:58 +00:00
#
2012-03-30 23:47:22 +02: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 . conf import settings
2009-04-17 03:08:18 +00:00
from django . contrib import admin
2013-09-23 22:08:14 +02:00
from src . typeclasses . models import Attribute , Tag
2013-07-18 08:40:48 -05:00
from src . objects . models import ObjectDB
2009-04-17 03:08:18 +00:00
2013-07-11 09:51:52 +02:00
class AttributeInline ( admin . TabularInline ) :
2013-08-04 09:25:15 -05:00
# This class is currently not used, because PickleField objects are not editable.
# It's here for us to ponder making a way that allows them to be edited.
2013-07-11 09:51:52 +02:00
model = Attribute
2011-09-15 00:28:26 +02:00
fields = ( ' db_key ' , ' db_value ' )
2011-10-04 21:14:41 +02:00
extra = 0
2011-10-02 22:37:07 +02:00
2013-07-18 08:40:48 -05:00
class TagInline ( admin . TabularInline ) :
2013-08-04 09:25:15 -05:00
model = ObjectDB . db_tags . through
raw_id_fields = ( ' tag ' , )
2011-11-13 18:46:14 +01:00
extra = 0
2013-08-04 09:25:15 -05:00
class TagAdmin ( admin . ModelAdmin ) :
fields = ( ' db_key ' , ' db_category ' , ' db_data ' )
2011-11-13 18:46:14 +01:00
class ObjectCreateForm ( forms . ModelForm ) :
2011-10-02 22:37:07 +02:00
" This form details the look of the fields "
class Meta :
model = ObjectDB
2011-11-13 18:46:14 +01:00
db_key = forms . CharField ( label = " Name/Key " ,
widget = forms . TextInput ( attrs = { ' size ' : ' 78 ' } ) ,
help_text = " Main identifier, like ' apple ' , ' strong guy ' , ' Elizabeth ' etc. If creating a Character, check so the name is unique among characters! " , )
2013-11-12 17:37:04 +01:00
db_typeclass_path = forms . CharField ( label = " Typeclass " ,
initial = settings . BASE_OBJECT_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 = " This defines what ' type ' of entity this is. This variable holds a Python path to a module with a valid Evennia Typeclass. If you are creating a Character you should use the typeclass defined by settings.BASE_CHARACTER_TYPECLASS or one derived from that. " )
2013-09-23 22:08:14 +02:00
#db_permissions = forms.CharField(label="Permissions",
# initial=settings.PERMISSION_PLAYER_DEFAULT,
# required=False,
# widget=forms.TextInput(attrs={'size':'78'}),
# help_text="a comma-separated list of text strings checked by certain locks. They are mainly of use for Character objects. Character permissions overload permissions defined on a controlling Player. Most objects normally don't have any permissions defined.")
2012-03-30 23:47:22 +02:00
db_cmdset_storage = forms . CharField ( label = " CmdSet " ,
2013-11-12 17:37:04 +01:00
initial = " " ,
2011-11-13 18:46:14 +01:00
required = False ,
widget = forms . TextInput ( attrs = { ' size ' : ' 78 ' } ) ,
help_text = " Most non-character objects don ' t need a cmdset and can leave this field blank. " )
2013-07-20 13:17:41 -05:00
raw_id_fields = ( ' db_destination ' , ' db_location ' , ' db_home ' )
2012-03-30 23:47:22 +02:00
2011-11-13 18:46:14 +01:00
class ObjectEditForm ( ObjectCreateForm ) :
" Form used for editing. Extends the create one with more fields "
2012-03-30 23:47:22 +02:00
db_lock_storage = forms . CharField ( label = " Locks " ,
required = False ,
2011-10-04 21:14:41 +02:00
widget = forms . Textarea ( attrs = { ' cols ' : ' 100 ' , ' rows ' : ' 2 ' } ) ,
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);... " )
2011-10-02 22:37:07 +02:00
2012-03-30 23:47:22 +02:00
class ObjectDBAdmin ( admin . ModelAdmin ) :
2011-10-02 22:37:07 +02:00
2013-07-20 13:17:41 -05:00
list_display = ( ' id ' , ' db_key ' , ' db_player ' , ' db_typeclass_path ' )
2010-08-29 18:46:58 +00:00
list_display_links = ( ' id ' , ' db_key ' )
2011-09-15 00:28:26 +02:00
ordering = [ ' db_player ' , ' db_typeclass_path ' , ' id ' ]
2010-08-29 18:46:58 +00:00
search_fields = [ ' ^db_key ' , ' db_typeclass_path ' ]
2013-07-20 12:42:10 -05:00
raw_id_fields = ( ' db_destination ' , ' db_location ' , ' db_home ' )
2012-03-30 23:47:22 +02:00
save_as = True
2009-04-17 03:08:18 +00:00
save_on_top = True
2012-03-30 23:47:22 +02:00
list_select_related = True
2013-09-23 22:08:14 +02:00
list_filter = ( ' db_typeclass_path ' , )
#list_filter = ('db_permissions', 'db_typeclass_path')
2011-10-02 22:37:07 +02:00
# editing fields setup
form = ObjectEditForm
fieldsets = (
( None , {
2013-09-23 22:08:14 +02:00
' fields ' : ( ( ' db_key ' , ' db_typeclass_path ' ) , ( ' db_lock_storage ' , ) ,
2011-10-02 22:37:07 +02:00
( ' db_location ' , ' db_home ' ) , ' db_destination ' , ' db_cmdset_storage '
) } ) ,
)
2013-09-23 22:08:14 +02:00
#fieldsets = (
# (None, {
# 'fields': (('db_key','db_typeclass_path'), ('db_permissions', 'db_lock_storage'),
# ('db_location', 'db_home'), 'db_destination','db_cmdset_storage'
# )}),
# )
2011-10-02 22:37:07 +02:00
#deactivated temporarily, they cause empty objects to be created in admin
2013-08-04 09:25:15 -05:00
inlines = [ TagInline ]
2011-10-02 22:37:07 +02:00
# Custom modification to give two different forms wether adding or not.
add_form = ObjectCreateForm
add_fieldsets = (
( None , {
2013-09-23 22:08:14 +02:00
' fields ' : ( ( ' db_key ' , ' db_typeclass_path ' ) ,
2013-07-20 13:17:41 -05:00
( ' db_location ' , ' db_home ' ) , ' db_destination ' , ' db_cmdset_storage '
2011-10-02 22:37:07 +02:00
) } ) ,
)
2013-09-23 22:08:14 +02:00
#add_fieldsets = (
# (None, {
# 'fields': (('db_key','db_typeclass_path'), 'db_permissions',
# ('db_location', 'db_home'), 'db_destination', 'db_cmdset_storage'
# )}),
# )
2011-10-02 22:37:07 +02:00
def get_fieldsets ( self , request , obj = None ) :
if not obj :
return self . add_fieldsets
return super ( ObjectDBAdmin , self ) . get_fieldsets ( request , obj )
def get_form ( self , request , obj = None , * * kwargs ) :
"""
Use special form during creation
"""
defaults = { }
if obj is None :
defaults . update ( {
' form ' : self . add_form ,
' fields ' : admin . util . flatten_fieldsets ( self . add_fieldsets ) ,
} )
defaults . update ( kwargs )
return super ( ObjectDBAdmin , self ) . get_form ( request , obj , * * defaults )
def save_model ( self , request , obj , form , change ) :
2013-05-24 21:10:31 +02:00
obj . save ( )
2011-10-02 22:37:07 +02:00
if not change :
# adding a new object
obj = obj . typeclass
obj . basetype_setup ( )
obj . basetype_posthook_setup ( )
2012-03-30 23:47:22 +02:00
obj . at_object_creation ( )
2011-10-02 22:37:07 +02:00
obj . at_init ( )
2012-03-30 23:47:22 +02:00
2011-10-02 01:21:03 +02:00
2010-08-29 18:46:58 +00:00
admin . site . register ( ObjectDB , ObjectDBAdmin )
2013-09-23 22:08:14 +02:00
admin . site . register ( Tag , TagAdmin )