MyAdminIndexView   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 7
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 2
c 2
b 0
f 0
dl 0
loc 7
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A index() 0 5 2
1
from flask import request, redirect, url_for
2
from flask_admin import Admin, AdminIndexView, expose
3
from flask_admin.contrib.geoa import ModelView as _ModelView
4
from flask_admin.base import MenuLink
5
from flask_admin.model import InlineFormAdmin
6
from flask_admin.contrib.fileadmin import FileAdmin as _FileAdmin
7
from flask_security import roles_required, current_user
8
9
import logging
10
import os.path as op
11
12
from ..database import db
13
from ..models import User, Region, River, Section, Gage, Sensor, Correlation
14
15
path = op.join(op.dirname(__file__), '../static/images')
16
17
h_w = {'data-height': 400, 'data-width': 600}
18
19
logger = logging.getLogger(__name__)
20
21
22
class ModelView(_ModelView):
23
    def is_accessible(self):
24
        return current_user.is_authenticated
25
26
27
    def inaccessible_callback(self, name, **kwargs):
28
        return redirect(url_for('security.login', next=request.url))
29
30
31
class FileAdmin(_FileAdmin):
32
    def is_accessible(self):
33
        return current_user.is_authenticated
34
35
    def inaccessible_callback(self, name, **kwargs):
36
        return redirect(url_for('security.login', next=request.url))
37
38
39
class UserView(ModelView):
40
    def is_accessible(self):
41
        """
42
        Only allow admins to see other users
43
        """
44
        return current_user.has_role('admin')
45
46
47
class CorrelationInlineView(InlineFormAdmin):
48
    pass
49
50
51
class SectionView(ModelView):
52
    column_list = ('name', 'path', 'slug', 'river', 'location')
53
    column_labels = dict(slug='URL Slug')
54
    column_searchable_list = ('name', River.name)
55
    form_widget_args = {'putin': h_w,
56
                        'takeout': h_w,
57
                        'path': h_w}
58
    #inline_models = (Correlation,)
59
60
61
class GageView(ModelView):
62
    can_create = True
63
    column_exclude_list = ('key',
64
                           'elevationUnits',
65
                           'zipcode',
66
                           'visible',
67
                           'elevation',
68
                           'backend_notes',
69
                           'description',
70
                           'short_description',
71
                           'started',
72
                           'ended')
73
    column_labels = dict(slug='URL Slug')
74
    column_searchable_list = ('name',
75
                              River.name,
76
                              'slug',
77
                              'local_town',
78
                              'location')
79
    # inline_models = (Sensor,)
80
    form_widget_args = {'point': h_w}
81
82
83
class SensorView(ModelView):
84
    form_excluded_columns = ['samples']
85
86
87
class MyAdminIndexView(AdminIndexView):
88
89
    @expose('/')
90
    def index(self):
91
        if not current_user.is_authenticated:
92
            return redirect(url_for('security.login', next=request.url))
93
        return super(MyAdminIndexView, self).index()
94
95
96
class AuthenticatedMenuLink(MenuLink):
97
    def is_accessible(self):
98
        return current_user.is_authenticated
99
    
100
101
102
admin = Admin(name="Riverflo.ws",
103
              index_view=MyAdminIndexView(),
104
              template_mode='bootstrap3',
105
              )
106
admin.add_view(UserView(User, db.session))
107
admin.add_view(ModelView(Region, db.session))
108
admin.add_view(ModelView(River, db.session))
109
admin.add_view(SectionView(Section, db.session))
110
admin.add_view(GageView(Gage, db.session))
111
admin.add_view(SensorView(Sensor, db.session))
112
admin.add_view(ModelView(Correlation, db.session))
113
admin.add_view(FileAdmin(path, '/static/images/', name='Images'))
114
admin.add_link(AuthenticatedMenuLink(name='Logout',
115
                                     endpoint='security.logout'))
116