@ -17,6 +17,25 @@ import autocomplete_light
admin . site . register ( Nastaveni , SingletonModelAdmin )
admin . site . register ( Nastaveni , SingletonModelAdmin )
### UTILS (pro verbose_name a help_text)
def field_labels ( model , fieldname ) :
f = [ i for i in model . _meta . fields if i . name == fieldname ] [ 0 ]
return { ' label ' : f . verbose_name . capitalize ( ) , ' help_text ' : f . help_text , }
def create_modeladmin ( modeladmin , model , name = None , verbose_name = None , verbose_name_plural = None ) :
class Meta :
proxy = True
app_label = model . _meta . app_label
Meta . verbose_name = verbose_name
Meta . verbose_name_plural = verbose_name_plural
attrs = { ' __module__ ' : ' ' , ' Meta ' : Meta }
newmodel = type ( name , ( model , ) , attrs )
admin . site . register ( newmodel , modeladmin )
return modeladmin
### INLINES
### INLINES
class ResitelInline ( admin . TabularInline ) :
class ResitelInline ( admin . TabularInline ) :
@ -224,10 +243,12 @@ admin.site.register(Reseni, ReseniAdmin)
from autocomplete_light . contrib . taggit_field import TaggitField , TaggitWidget
from autocomplete_light . contrib . taggit_field import TaggitField , TaggitWidget
#TODO: Autocomplete autor/opravovatel
#TODO: Autocomplete autor/opravovatel
class ProblemAdminForm ( forms . ModelForm ) :
class ProblemAdminForm ( forms . ModelForm ) :
text_problemu = forms . CharField ( widget = CKEditorWidget ( ) )
text_zadani = forms . CharField ( widget = CKEditorWidget ( ) , required = False , * * field_labels ( Problem , ' text_zadani ' ) )
text_problemu_org = forms . CharField ( widget = CKEditorWidget ( ) )
text_reseni = forms . CharField ( widget = CKEditorWidget ( ) , required = False , * * field_labels ( Problem , ' text_reseni ' ) )
zamereni = TaggitField ( widget = TaggitWidget ( ' TagAutocomplete ' ) )
text_org = forms . CharField ( widget = CKEditorWidget ( ) , required = False , * * field_labels ( Problem , ' text_org ' ) )
zamereni = TaggitField ( widget = TaggitWidget ( ' TagAutocomplete ' ) , required = False )
class Meta :
class Meta :
model = Problem
model = Problem
exclude = [ ]
exclude = [ ]
@ -236,32 +257,56 @@ class ProblemAdmin(reversion.VersionAdmin):
form = ProblemAdminForm
form = ProblemAdminForm
fieldsets = [
fieldsets = [
( None , { ' fields ' : [ ' nazev ' , ' typ ' , ' stav ' , ' autor ' , ' zamereni ' , ' body ' , ' timestamp ' , ' import_dakos_id ' ] } ) ,
( None , { ' fields ' : [ ' nazev ' , ' typ ' , ' stav ' , ' autor ' , ' zamereni ' , ' body ' , ' timestamp ' , ' import_dakos_id ' ] } ) ,
( u ' Vydání ' , { ' fields ' : [ ' cislo_zadani ' , ' kod ' , ' cislo_reseni ' , ' opravovatel ' , ' text_problemu ' ] } ) ,
( u ' Vydání ' , { ' fields ' : [ ' cislo_zadani ' , ' kod ' , ' cislo_reseni ' , ' opravovatel ' , ] } ) ,
( None , { ' fields ' : [ ' text_problemu_org ' ] } ) ,
( None , { ' fields ' : [ ' text_zadani ' , ' text_reseni ' , ' text_org ' , ] } ) ,
]
]
readonly_fields = [ ' timestamp ' , ' import_dakos_id ' ]
readonly_fields = [ ' timestamp ' , ' import_dakos_id ' ]
list_display = [ ' nazev ' , ' typ ' , ' kod ' , ' stav ' , ' autor ' , ' opravovatel ' , ' verejne ' , ' cislo_zadani ' , ' pocet_reseni ' ]
list_select_related = True
list_select_related = True
list_filter = [ ' typ ' , ' stav ' , ' timestamp ' ]
search_fields = [ ' nazev ' , ' text_zadani ' , ' text_reseni ' , ' text_org ' ]
search_fields = [ ' nazev ' , ' kod ' , ' text_problemu_org ' , ' text_problemu ' ]
inlines = [ ReseniKProblemuInline ]
view_on_site = Problem . verejne_url
view_on_site = Problem . verejne_url
def get_queryset ( self , request ) :
def get_queryset ( self , request ) :
qs = super ( ProblemAdmin , self ) . get_queryset ( request )
qs = super ( ProblemAdmin , self ) . get_queryset ( request )
return qs . select_related ( ' autor ' , ' opravovatel ' , ' cislo_zadani ' , ' cislo_reseni ' ) . annotate ( pocet_reseni = Count ( ' reseni ' ) )
return qs . select_related ( ' autor ' , ' opravovatel ' , ' cislo_zadani ' , ' cislo_reseni ' )
def pocet_reseni ( self , obj ) :
def pocet_reseni ( self , obj ) :
return obj . pocet_reseni
return obj . pocet_reseni
admin . site . register ( Problem , ProblemAdmin )
class ProblemNavrhAdmin ( ProblemAdmin ) :
list_display = [ ' nazev ' , ' typ ' , ' stav ' , ' autor ' , ' timestamp ' ]
list_filter = [ ' typ ' , ' stav ' , ' timestamp ' ]
def get_queryset ( self , request ) :
qs = super ( ProblemNavrhAdmin , self ) . get_queryset ( request )
return qs . filter ( stav__in = [ Problem . STAV_NAVRH , Problem . STAV_SMAZANY ] )
create_modeladmin ( ProblemNavrhAdmin , Problem , ' ProblemNavrh ' , verbose_name = u ' Problém (návrh) ' , verbose_name_plural = u ' Problémy (návrhy) ' )
class ProblemZadanyAdmin ( ProblemAdmin ) :
list_display = [ ' nazev ' , ' typ ' , ' autor ' , ' opravovatel ' , ' kod ' , ' cislo_zadani ' , ' pocet_reseni ' , ' verejne ' ]
list_filter = [ ' typ ' , ' cislo_zadani__rocnik ' ]
inlines = [ ReseniKProblemuInline ]
def get_queryset ( self , request ) :
qs = super ( ProblemZadanyAdmin , self ) . get_queryset ( request )
return qs . filter ( stav = Problem . STAV_ZADANY ) . annotate ( pocet_reseni = Count ( ' reseni ' ) )
create_modeladmin ( ProblemZadanyAdmin , Problem , ' ProblemZadany ' , verbose_name = u ' Problém (zadaný) ' , verbose_name_plural = u ' Problémy (zadané) ' )
#admin.site.register(Problem, ProblemAdmin)
### Soustredeni
### Soustredeni
class SoustredeniAdminForm ( forms . ModelForm ) :
text = forms . CharField ( widget = CKEditorWidget ( ) , required = False , * * field_labels ( Soustredeni , ' text ' ) )
class Meta :
model = Soustredeni
exclude = [ ]
class SoustredeniAdmin ( reversion . VersionAdmin ) :
class SoustredeniAdmin ( reversion . VersionAdmin ) :
form = SoustredeniAdminForm
fieldsets = [
fieldsets = [
( None , { ' fields ' : [ ' rocnik ' , ' misto ' , ' verejne_db ' ] } ) ,
( None , { ' fields ' : [ ' rocnik ' , ' misto ' , ' verejne_db ' , ' text ' ] } ) ,
( u ' Data ' , { ' fields ' : [ ' datum_zacatku ' , ' datum_konce ' ] } ) ,
( u ' Data ' , { ' fields ' : [ ' datum_zacatku ' , ' datum_konce ' ] } ) ,
]
]
list_display = [ ' rocnik ' , ' misto ' , ' datum_zacatku ' , ' verejne ' ]
list_display = [ ' rocnik ' , ' misto ' , ' datum_zacatku ' , ' verejne ' ]