/
admin.py
289 lines (238 loc) · 9.24 KB
/
admin.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
from django.conf.urls import url
from django.contrib import admin
from django.forms import ModelForm, Select
from django.contrib.admin.views.decorators import staff_member_required
from django.contrib import messages
from django.shortcuts import redirect
from suit import apps
from suit.sortables import SortableTabularInline, SortableModelAdmin, SortableStackedInline
from suit.widgets import AutosizedTextarea
from .models import *
from .views import *
admin.site.site_header = 'Django Suit'
class CityInlineForm(ModelForm):
class Meta:
widgets = {
# 'area': EnclosedInput(prepend='icon-globe', append='km<sup>2</sup>',
# attrs={'class': 'input-small'}),
# 'population': EnclosedInput(append='icon-user',
# attrs={'class': 'input-small'}),
}
class CityInline(admin.TabularInline):
form = CityInlineForm
model = City
min_num = 3
extra = 0
verbose_name_plural = 'Cities'
suit_classes = 'suit-tab suit-tab-cities'
suit_form_inlines_hide_original = True
class CountryForm(ModelForm):
class Meta:
widgets = {
# 'code': TextInput(attrs={'class': 'input-mini'}),
# 'independence_day': SuitDateWidget,
# 'area': EnclosedInput(prepend='icon-globe', append='km<sup>2</sup>',
# attrs={'class': 'input-small'}),
# 'population': EnclosedInput(prepend='icon-user',
# append='<input type="button" '
# 'class="btn" onclick="window'
# '.open(\'https://www.google'
# '.com/\')" value="Search">',
# attrs={'class': 'input-small'}),
'description': AutosizedTextarea,
'architecture': AutosizedTextarea,
}
class CountryAdmin(admin.ModelAdmin):
form = CountryForm
search_fields = ('name', 'code')
list_display = ('name', 'code', 'continent', 'independence_day')
list_filter = ('continent',)
list_select_related = True
date_hierarchy = 'independence_day'
inlines = (CityInline,)
# fields = ('name', 'continent', 'code', 'independence_day')
fieldsets = [
(None, {
'classes': ('suit-tab suit-tab-general',),
'fields': ['name', 'code', 'continent', 'independence_day']
}),
('Statistics', {
'classes': ('suit-tab suit-tab-general',),
'description': 'EnclosedInput widget examples',
'fields': ['area', 'population']}),
('Autosized textarea', {
'classes': ('suit-tab suit-tab-general',),
'description': 'AutosizedTextarea widget example - adapts height '
'based on user input',
'fields': ['description']}),
('Architecture', {
'classes': ('suit-tab suit-tab-cities',),
'description': 'Tabs can contain any fieldsets and inlines',
'fields': ['architecture']}),
]
suit_form_size = {
'fields': {
'code': apps.SUIT_FORM_SIZE_INLINE
},
'widgets': {
'AutosizedTextarea': apps.SUIT_FORM_SIZE_XXX_LARGE,
},
}
suit_form_tabs = (
('general', 'General'),
('cities', 'Cities'),
('flag', 'Flag'),
('charts', 'Charts'),
('info', 'Info on tabs')
)
suit_form_includes = (
('admin/demo/country/tab_notice.html', 'middle', 'cities'),
('admin/demo/country/tab_flag.html', '', 'flag'),
('admin/demo/country/tab_charts.html', '', 'charts'),
('admin/demo/country/tab_docs.html', '', 'info'),
)
# fields = (('name', 'code', 'continent'), 'independence_day', 'population', 'description')
admin.site.register(Country, CountryAdmin)
# Inlines for ContinentAdmin
class CountryInlineForm(ModelForm):
class Meta:
widgets = {
# 'code': TextInput(attrs={'class': 'input-mini'}),
# 'population': TextInput(attrs={'class': 'input-medium'}),
# 'independence_day': SuitDateWidget,
}
class CountryInline(SortableTabularInline):
form = CountryInlineForm
model = Country
fields = ('name', 'code', 'population', 'continent')
extra = 1
verbose_name_plural = 'Countries (Sortable example)'
sortable = 'order'
show_change_link = True
class ContinentAdmin(SortableModelAdmin):
search_fields = ('name',)
list_display = ('name', 'countries')
sortable = 'order'
inlines = (CountryInline,)
def suit_row_attributes(self, obj):
class_map = {
'Europe': 'table-success',
'South America': 'table-warning',
'North America': 'table-success',
'Africa': 'table-danger',
'Australia': 'table-warning',
'Asia': 'table-info',
'Antarctica': 'table-info',
}
css_class = class_map.get(obj.name)
if css_class:
return {'class': css_class}
def suit_column_attributes(self, column):
if column == 'countries':
return {'class': 'text-xs-center'}
def suit_cell_attributes(self, obj, column):
if column == 'countries':
cls = 'text-xs-center'
if obj.name == 'Antarctica':
cls += ' table-danger'
return {'class': cls}
def countries(self, obj):
return len(obj.country_set.all())
admin.site.register(Continent, ContinentAdmin)
class ShowcaseForm(ModelForm):
class Meta:
widgets = {
'textfield': AutosizedTextarea,
}
@staff_member_required
def showcase_custom_view_example(request, pk):
instance = Showcase.objects.get(pk=pk)
# Do something legendary here
messages.success(request, 'Something legendary was done to "%s"' % instance)
return redirect('admin:demo_showcase_change', pk)
# Inlines for Showcase
class BookInline(SortableTabularInline):
model = Book
min_num = 1
extra = 0
verbose_name_plural = 'Books (Tabular inline)'
suit_form_inlines_hide_original = True
class MovieInlineForm(ModelForm):
class Meta:
fields = '__all__'
model = Movie
widgets = {
'description': AutosizedTextarea(attrs={'rows': 2}),
'type': Select(attrs={'class': 'input-small'}),
}
class MovieInline(SortableStackedInline):
model = Movie
form = MovieInlineForm
min_num = 1
extra = 0
verbose_name_plural = 'Movies (Stacked inline)'
fields = ['title', 'description', 'rating', 'is_released']
suit_form_size = {
'default': apps.SUIT_FORM_SIZE_X_LARGE,
}
class ShowcaseAdmin(admin.ModelAdmin):
form = ShowcaseForm
inlines = (BookInline, MovieInline)
search_fields = ['name']
# radio_fields = {"horizontal_choices": admin.HORIZONTAL,
# 'vertical_choices': admin.VERTICAL}
# list_editable = ('boolean',)
# list_filter = ('choices', 'date', CountryFilter)
# list_display = ('name', 'help_text', 'choices', 'horizontal_choices', 'boolean')
list_display = ('name', 'help_text')
readonly_fields = ('readonly_field',)
fieldsets = [
(None, {'fields': ['name', 'help_text', 'textfield',
('multiple_in_row', 'multiple2'),
'readonly_field']}),
('Date and time', {
'description': 'Original Django admin date/time widgets',
'fields': ['date_and_time', 'date', 'time_only']}),
# ('Date and time', {
# 'description': 'Improved date/time widgets (SuitDateWidget, '
# 'SuitSplitDateTimeWidget) . Uses original JS.',
# 'fields': ['date_widget', 'datetime_widget']}),
# ('Foreign key relations',
# {'description': 'Original select and linked select feature',
# 'fields': ['country', 'linked_foreign_key', 'raw_id_field']}),
#
# ('EnclosedInput widget',
# {
# 'description': 'Supports Twitter Bootstrap prepended, '
# 'appended inputs',
# 'fields': ['enclosed1', 'enclosed2']}),
#
# ('Boolean and choices',
# {'fields': ['boolean', 'boolean_with_help', 'choices',
# 'horizontal_choices', 'vertical_choices']}),
#
# ('Collapsed settings', {
# 'classes': ('collapse',),
# 'fields': ['hidden_checkbox', 'hidden_choice']}),
# ('And one more collapsable', {
# 'classes': ('collapse',),
# 'fields': ['hidden_charfield', 'hidden_charfield2']}),
]
suit_form_size = {
# 'fields': {
# 'code': apps.SUIT_FORM_SIZE_INLINE
# },
'widgets': {
# 'AutosizedTextarea': apps.SUIT_FORM_SIZE_XXX_LARGE,
},
}
def get_urls(self):
"""
Example how to extend Django ModelAdmin with extra actions and views
"""
urls = super(ShowcaseAdmin, self).get_urls()
my_urls = [
url(r'^(\d+)/clickme/$', showcase_custom_view_example, name='demo_showcase_clickme')
]
return my_urls + urls
admin.site.register(Showcase, ShowcaseAdmin)