/
base.py
894 lines (692 loc) · 26.1 KB
/
base.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
from flask import request, url_for, redirect, flash
from jinja2 import contextfunction
from flask.ext.admin.babel import gettext
from flask.ext.admin.base import BaseView, expose
from flask.ext.admin.tools import rec_getattr
from flask.ext.admin.model import filters
from flask.ext.admin.actions import ActionsMixin
class BaseModelView(BaseView, ActionsMixin):
"""
Base model view.
View does not make any assumptions on how models are stored or managed, but expects following:
1. Model is an object
2. Model contains properties
3. Each model contains attribute which uniquely identifies it (i.e. primary key for database model)
4. You can get list of sorted models with pagination applied from a data source
5. You can get one model by its identifier from the data source
Essentially, if you want to support new data store, all you have to do:
1. Derive from `BaseModelView` class
2. Implement various data-related methods (`get_list`, `get_one`, `create_model`, etc)
3. Implement automatic form generation from the model representation (`scaffold_form`)
"""
# Permissions
can_create = True
"""Is model creation allowed"""
can_edit = True
"""Is model editing allowed"""
can_delete = True
"""Is model deletion allowed"""
# Templates
list_template = 'admin/model/list.html'
"""Default list view template"""
edit_template = 'admin/model/edit.html'
"""Default edit template"""
create_template = 'admin/model/create.html'
"""Default create template"""
# Customizations
list_columns = None
"""
Collection of the model field names for the list view.
If set to `None`, will get them from the model.
For example::
class MyModelView(BaseModelView):
list_columns = ('name', 'last_name', 'email')
"""
excluded_list_columns = None
"""
Collection of excluded list column names.
For example::
class MyModelView(BaseModelView):
excluded_list_columns = ('last_name', 'email')
"""
list_formatters = dict()
"""
Dictionary of list view column formatters.
For example, if you want to show price multiplied by
two, you can do something like this::
class MyModelView(BaseModelView):
list_formatters = dict(price=lambda c, m, p: m.price*2)
Callback function has following prototype::
def formatter(context, model, name):
# context is instance of jinja2.runtime.Context
# model is model instance
# name is property name
pass
"""
rename_columns = None
"""
Dictionary where key is column name and value is string to display.
For example::
class MyModelView(BaseModelView):
rename_columns = dict(name='Name', last_name='Last Name')
"""
sortable_columns = None
"""
Collection of the sortable columns for the list view.
If set to `None`, will get them from the model.
For example::
class MyModelView(BaseModelView):
sortable_columns = ('name', 'last_name')
If you want to explicitly specify field/column to be used while
sorting, you can use tuple::
class MyModelView(BaseModelView):
sortable_columns = ('name', ('user', 'user.username'))
When using SQLAlchemy models, model attributes can be used instead
of the string::
class MyModelView(BaseModelView):
sortable_columns = ('name', ('user', User.username))
"""
searchable_columns = None
"""
Collection of the searchable columns. It is assumed that only
text-only fields are searchable, but it is up for a model
implementation to make decision.
Example::
class MyModelView(BaseModelView):
searchable_columns = ('name', 'email')
"""
column_filters = None
"""
Collection of the column filters.
Can contain either field names or instances of :class:`~flask.ext.admin.model.filters.BaseFilter` classes.
Example::
class MyModelView(BaseModelView):
column_filters = ('user', 'email')
"""
list_display_pk = False
"""
Controls if primary key should be displayed in list view.
"""
form = None
"""
Form class. Override if you want to use custom form for your model.
For example::
class MyForm(wtf.Form):
pass
class MyModelView(BaseModelView):
form = MyForm
"""
form_args = None
"""
Dictionary of form field arguments. Refer to WTForms documentation for
list of possible options.
Example::
class MyModelView(BaseModelView):
form_args = dict(
name=dict(label='First Name', validators=[wtf.required()])
)
"""
form_columns = None
"""
Collection of the model field names for the form. If set to `None` will
get them from the model.
Example::
class MyModelView(BaseModelView):
form_columns = ('name', 'email')
"""
excluded_form_columns = None
"""
Collection of excluded form field names.
For example::
class MyModelView(BaseModelView):
excluded_form_columns = ('last_name', 'email')
"""
form_overrides = None
"""
Dictionary of form column overrides.
Example::
class MyModelView(BaseModelView):
form_overrides = dict(name=wtf.FileField)
"""
# Actions
disallowed_actions = []
"""
Set of disallowed action names. For example, if you want to disable
mass model deletion, do something like this:
class MyModelView(BaseModelView):
disallowed_actions = ['delete']
"""
# Various settings
page_size = 20
"""
Default page size.
"""
def __init__(self, model,
name=None, category=None, endpoint=None, url=None):
"""
Constructor.
:param model:
Model class
:param name:
View name. If not provided, will use model class name
:param category:
View category
:param endpoint:
Base endpoint. If not provided, will use model name + 'view'.
For example if model name was 'User', endpoint will be
'userview'
:param url:
Base URL. If not provided, will use endpoint as a URL.
"""
# If name not provided, it is model name
if name is None:
name = '%s' % self._prettify_name(model.__name__)
# If endpoint not provided, it is model name + 'view'
if endpoint is None:
endpoint = ('%sview' % model.__name__).lower()
super(BaseModelView, self).__init__(name, category, endpoint, url)
self.model = model
# Actions
self.init_actions()
# Scaffolding
self._refresh_cache()
# Caching
def _refresh_cache(self):
"""
Refresh various cached variables.
"""
# List view
self._list_columns = self.get_list_columns()
self._sortable_columns = self.get_sortable_columns()
# Forms
self._create_form_class = self.get_create_form()
self._edit_form_class = self.get_edit_form()
# Search
self._search_supported = self.init_search()
# Filters
self._filters = self.get_filters()
if self._filters:
self._filter_groups = []
self._filter_dict = dict()
for i, n in enumerate(self._filters):
if n.name not in self._filter_dict:
group = []
self._filter_dict[n.name] = group
self._filter_groups.append((n.name, group))
else:
group = self._filter_dict[n.name]
group.append((i, n.operation()))
self._filter_types = dict((i, f.data_type)
for i, f in enumerate(self._filters)
if f.data_type)
else:
self._filter_groups = None
self._filter_types = None
# Primary key
def get_pk_value(self, model):
"""
Return PK value from a model object.
"""
raise NotImplemented()
# List view
def scaffold_list_columns(self):
"""
Return list of the model field names. Must be implemented in
the child class.
Expected return format is list of tuples with field name and
display text. For example::
['name', 'first_name', 'last_name']
"""
raise NotImplemented('Please implement scaffold_list_columns method')
def get_column_name(self, field):
"""
Return human-readable column name.
:param field:
Model field name.
"""
if self.rename_columns and field in self.rename_columns:
return self.rename_columns[field]
else:
return self.prettify_name(field)
def get_list_columns(self):
"""
Returns list of the model field names. If `list_columns` was
set, returns it. Otherwise calls `scaffold_list_columns`
to generate list from the model.
"""
if self.list_columns is None:
columns = self.scaffold_list_columns()
else:
columns = self.list_columns
return [(c, self.get_column_name(c)) for c in columns]
def scaffold_sortable_columns(self):
"""
Returns dictionary of sortable columns. Must be implemented in
the child class.
Expected return format is dictionary, where key is field name and
value is property name.
"""
raise NotImplemented('Please implement scaffold_sortable_columns method')
def get_sortable_columns(self):
"""
Returns dictionary of the sortable columns. Key is a model
field name and value is sort column (for example - attribute).
If `sortable_columns` is set, will use it. Otherwise, will call
`scaffold_sortable_columns` to get them from the model.
"""
if self.sortable_columns is None:
return self.scaffold_sortable_columns() or dict()
else:
result = dict()
for c in self.sortable_columns:
if isinstance(c, tuple):
result[c[0]] = c[1]
else:
result[c] = c
return result
def init_search(self):
"""
Initialize search. If data provider does not support search,
`init_search` will return `False`.
"""
return False
def scaffold_filters(self, name):
"""
Generate filter object for the given name
:param name:
Name of the field
"""
return None
def is_valid_filter(self, filter):
"""
Verify that provided filter object is valid.
Override in model backend implementation to verify if
provided filter type is allowed.
:param filter:
Filter object to verify.
"""
return isinstance(filter, filters.BaseFilter)
def get_filters(self):
"""
Return list of filter objects.
If your model backend implementation does not support filters,
override this method and return `None`.
"""
if self.column_filters:
collection = []
for n in self.column_filters:
if not self.is_valid_filter(n):
flt = self.scaffold_filters(n)
if flt:
collection.extend(flt)
else:
raise Exception('Unsupported filter type %s' % n)
else:
collection.append(n)
return collection
else:
return None
def scaffold_form(self):
"""
Create `form.BaseForm` inherited class from the model. Must be
implemented in the child class.
"""
raise NotImplemented('Please implement scaffold_form method')
def get_form(self):
"""
Get form class.
If ``self.form`` is set, will return it and will call
``self.scaffold_form`` otherwise.
Override to implement customized behavior.
"""
if self.form is not None:
return self.form
return self.scaffold_form()
def get_create_form(self):
"""
Create form class for model creation view.
Override to implement customized behavior.
"""
return self.get_form()
def get_edit_form(self):
"""
Create form class for model editing view.
Override to implement customized behavior.
"""
return self.get_form()
def create_form(self, form, obj=None):
"""
Instantiate model creation form and return it.
Override to implement custom behavior.
"""
return self._create_form_class(obj=obj)
def edit_form(self, form, obj=None):
"""
Instantiate model editing form and return it.
Override to implement custom behavior.
"""
return self._edit_form_class(form, obj)
# Helpers
def is_sortable(self, name):
"""
Verify if column is sortable.
:param name:
Column name.
"""
return name in self._sortable_columns
def _get_column_by_idx(self, idx):
"""
Return column index by
"""
if idx is None or idx < 0 or idx >= len(self._list_columns):
return None
return self._list_columns[idx]
# Database-related API
def get_list(self, page, sort_field, sort_desc, search, filters):
"""
Return list of models from the data source with applied pagination
and sorting.
Must be implemented in child class.
:param page:
Page number, 0 based. Can be set to None if it is first page.
:param sort_field:
Sort column name or None.
:param sort_desc:
If set to True, sorting is in descending order.
:param search:
Search query
:param filters:
List of filter tuples. First value in a tuple is a search
index, second value is a search value.
"""
raise NotImplemented('Please implement get_list method')
def get_one(self, id):
"""
Return one model by its id.
Must be implemented in the child class.
:param id:
Model id
"""
raise NotImplemented('Please implement get_one method')
# Model handlers
def on_model_change(self, form, model):
"""
Allow to do some actions after a model was created or updated.
Called from create_model and update_model in the same transaction
(if it has any meaning for a store backend).
By default do nothing.
"""
pass
def on_model_delete(self, model):
"""
Allow to do some actions before a model will be deleted.
Called from delete_model in the same transaction
(if it has any meaning for a store backend).
By default do nothing.
"""
pass
def create_model(self, form):
"""
Create model from the form.
Returns `True` if operation succeeded.
Must be implemented in the child class.
:param form:
Form instance
"""
raise NotImplemented()
def update_model(self, form, model):
"""
Update model from the form.
Returns `True` if operation succeeded.
Must be implemented in the child class.
:param form:
Form instance
:param model:
Model instance
"""
raise NotImplemented()
def delete_model(self, model):
"""
Delete model.
Returns `True` if operation succeeded.
Must be implemented in the child class.
:param model:
Model instance
"""
raise NotImplemented()
# Various helpers
def prettify_name(self, name):
"""
Prettify pythonic variable name.
For example, 'hello_world' will be converted to 'Hello World'
:param name:
Name to prettify
"""
return name.replace('_', ' ').title()
# URL generation helper
def _get_extra_args(self):
"""
Return arguments from query string.
"""
page = request.args.get('page', 0, type=int)
sort = request.args.get('sort', None, type=int)
sort_desc = request.args.get('desc', None, type=int)
search = request.args.get('search', None)
# Gather filters
if self._filters:
sfilters = []
for n in request.args:
if n.startswith('flt'):
ofs = n.find('_')
if ofs == -1:
continue
try:
pos = int(n[3:ofs])
idx = int(n[ofs + 1:])
except ValueError:
continue
if idx >= 0 and idx < len(self._filters):
flt = self._filters[idx]
value = request.args[n]
if flt.validate(value):
sfilters.append((pos, (idx, flt.clean(value))))
filters = [v[1] for v in sorted(sfilters, key=lambda n: n[0])]
else:
filters = None
return page, sort, sort_desc, search, filters
def _get_url(self, view=None, page=None, sort=None, sort_desc=None,
search=None, filters=None):
"""
Generate page URL with current page, sort column and
other parameters.
:param view:
View name
:param page:
Page number
:param sort:
Sort column index
:param sort_desc:
Use descending sorting order
:param search:
Search query
:param filters:
List of active filters
"""
if not search:
search = None
if not page:
page = None
kwargs = dict(page=page, sort=sort, desc=sort_desc, search=search)
if filters:
for i, flt in enumerate(filters):
key = 'flt%d_%d' % (i, flt[0])
kwargs[key] = flt[1]
return url_for(view, **kwargs)
def is_action_allowed(self, name):
"""
Override this method to allow or disallow actions based
on some condition.
Default implementation only checks if particular action
is not in `disallowed_actions`.
"""
return name not in self.disallowed_actions
@contextfunction
def get_list_value(self, context, model, name):
"""
Returns value to be displayed in list view
:param context:
:py:class:`jinja2.runtime.Context`
:param model:
Model instance
:param name:
Field name
"""
if name in self.list_formatters:
return self.list_formatters[name](context, model, name)
return rec_getattr(model, name)
# Views
@expose('/')
def index_view(self):
"""
List view
"""
# Grab parameters from URL
page, sort_idx, sort_desc, search, filters = self._get_extra_args()
# Map column index to column name
sort_column = self._get_column_by_idx(sort_idx)
if sort_column is not None:
sort_column = sort_column[0]
# Get count and data
count, data = self.get_list(page, sort_column, sort_desc,
search, filters)
# Calculate number of pages
num_pages = count / self.page_size
if count % self.page_size != 0:
num_pages += 1
# Pregenerate filters
if self._filters:
filters_data = dict()
for idx, f in enumerate(self._filters):
flt_data = f.get_options(self)
if flt_data:
filters_data[idx] = flt_data
else:
filters_data = None
# Various URL generation helpers
def pager_url(p):
# Do not add page number if it is first page
if p == 0:
p = None
return self._get_url('.index_view', p, sort_idx, sort_desc,
search, filters)
def sort_url(column, invert=False):
desc = None
if invert and not sort_desc:
desc = 1
return self._get_url('.index_view', page, column, desc,
search, filters)
# Actions
actions, actions_confirmation = self.get_actions_list()
return self.render(self.list_template,
data=data,
# List
list_columns=self._list_columns,
sortable_columns=self._sortable_columns,
# Stuff
enumerate=enumerate,
get_pk_value=self.get_pk_value,
get_value=self.get_list_value,
return_url=self._get_url('.index_view',
page,
sort_idx,
sort_desc,
search,
filters),
# Pagination
count=count,
pager_url=pager_url,
num_pages=num_pages,
page=page,
# Sorting
sort_column=sort_idx,
sort_desc=sort_desc,
sort_url=sort_url,
# Search
search_supported=self._search_supported,
clear_search_url=self._get_url('.index_view',
None,
sort_idx,
sort_desc),
search=search,
# Filters
filters=self._filters,
filter_groups=self._filter_groups,
filter_types=self._filter_types,
filter_data=filters_data,
active_filters=filters,
# Actions
actions=actions,
actions_confirmation=actions_confirmation
)
@expose('/new/', methods=('GET', 'POST'))
def create_view(self):
"""
Create model view
"""
return_url = request.args.get('url') or url_for('.index_view')
if not self.can_create:
return redirect(return_url)
form = self.create_form(request.form)
if form.validate_on_submit():
if self.create_model(form):
if '_add_another' in request.form:
flash(gettext('Model was successfully created.'))
return redirect(url_for('.create_view', url=return_url))
else:
return redirect(return_url)
return self.render(self.create_template,
form=form,
return_url=return_url)
@expose('/edit/', methods=('GET', 'POST'))
def edit_view(self):
"""
Edit model view
"""
return_url = request.args.get('url') or url_for('.index_view')
if not self.can_edit:
return redirect(return_url)
id = request.args.get('id')
if id is None:
return redirect(return_url)
model = self.get_one(id)
if model is None:
return redirect(return_url)
form = self.edit_form(request.form, model)
if form.validate_on_submit():
if self.update_model(form, model):
return redirect(return_url)
return self.render(self.edit_template,
form=form,
return_url=return_url)
@expose('/delete/', methods=('POST',))
def delete_view(self):
"""
Delete model view. Only POST method is allowed.
"""
return_url = request.args.get('url') or url_for('.index_view')
# TODO: Use post
if not self.can_delete:
return redirect(return_url)
id = request.args.get('id')
if id is None:
return redirect(return_url)
model = self.get_one(id)
if model:
self.delete_model(model)
return redirect(return_url)
@expose('/action/', methods=('POST',))
def action_view(self):
"""
Mass-model action view.
"""
return self.handle_action()