forked from EUDAT-B2SHARE/b2share
/
admin.py
135 lines (108 loc) · 3.98 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
# -*- coding: utf-8 -*-
#
# This file is part of Invenio.
# Copyright (C) 2014, 2015 CERN.
#
# Invenio is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of the
# License, or (at your option) any later version.
#
# Invenio is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Invenio; if not, write to the Free Software Foundation, Inc.,
# 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
"""Flask-Admin page to configure facets sets per collection."""
from invenio.ext.admin.views import ModelView
from invenio.ext.sqlalchemy import db
from invenio.modules.search.models import Collection, FacetCollection
from invenio.modules.search.registry import facets
from wtforms.fields import IntegerField, SelectField
from wtforms.validators import DataRequired, ValidationError
def is_place_taken(form, field):
"""Check if the given place for given collection is already taken.
:param form: the form containing the validated field
:param field: the validated field
"""
order = field.data
collection = form.data['collection']
if not collection:
return
if FacetCollection.is_place_taken(collection.id, order):
raise ValidationError('A facet on the given place already exists')
def is_duplicated(form, field):
"""Check if the given facet is already assigned to this collection.
:param form: the form containing the validated field
:param field: the validated field
"""
facet_name = field.data
collection = form.data['collection']
if not collection:
return
if FacetCollection.is_duplicated(collection.id, facet_name):
raise ValidationError(
'This facet is already assigned to this collection.')
def is_module_facet_module(form, field):
"""Check if the given module is a proper one.
:param form: the form containing the validated field
:param field: the validated field
"""
facet_name = field.data
if facets.get(facet_name) is None:
raise ValidationError('The given facet does not exist.')
class FacetsAdmin(ModelView):
"""Flask-Admin module to manage facets configuration."""
_can_create = True
_can_edit = True
_can_delete = True
column_list = (
'collection', 'order', 'facet_name',
)
form_args = {
'collection': {
'validators': [
DataRequired(),
],
'allow_blank': False,
'query_factory':
lambda: db.session.query(Collection).order_by(Collection.id),
},
'order': {
'validators': [
is_place_taken,
DataRequired(),
],
},
'facet_name': {
'validators': [
is_module_facet_module,
is_duplicated,
DataRequired(),
],
},
}
form_overrides = {
'facet_name': SelectField,
'order': IntegerField,
}
column_default_sort = 'id_collection'
def __init__(self, app, *args, **kwargs):
"""Constructor.
:param app: flask application
"""
# these lines must be in the application context
with app.app_context():
# because of the access to FacetsRegistry
self.form_args['facet_name']['choices'] = \
[(facet_name, facet_name) for facet_name in facets.keys()]
super(FacetsAdmin, self).__init__(*args, **kwargs)
def register_admin(app, admin):
"""Called on app initialization to register administration interface."""
admin.add_view(
FacetsAdmin(app, FacetCollection, db.session, name='Facets',
category="")
)