|
| 1 | +from flask import redirect, url_for, request |
| 2 | +from flask_login import current_user, login_required |
| 3 | +from flask_admin import Admin |
| 4 | +from flask_admin.contrib.sqla import ModelView |
| 5 | +from projeto.ext.auth import UserAuth |
| 6 | +from projeto.ext.api.models import Estacao, Sensor |
| 7 | +from projeto.ext.db import db |
| 8 | + |
| 9 | + |
| 10 | +class AdminView(ModelView): |
| 11 | + @login_required |
| 12 | + def is_accessible(self): |
| 13 | + if current_user.is_authenticated and current_user.is_admin: |
| 14 | + return True |
| 15 | + return False |
| 16 | + |
| 17 | + def inaccessible_callback(self, name, **kwargs): |
| 18 | + if not self.is_accessible(): |
| 19 | + return redirect(url_for("auth.login", next=request.url)) |
| 20 | + |
| 21 | + |
| 22 | +class UserView(AdminView): |
| 23 | + column_sortable_list = () |
| 24 | + |
| 25 | + |
| 26 | +class EstacaoView(AdminView): |
| 27 | + column_list = ('id', 'local', 'latitude', 'longitude') |
| 28 | + column_sortable_list = () |
| 29 | + |
| 30 | + |
| 31 | +class SensorView(AdminView): |
| 32 | + column_list = ('id', 'tipo', 'descricao', 'params', 'estacao') |
| 33 | + column_sortable_list = () |
| 34 | + |
| 35 | + form_excluded_columns = [ |
| 36 | + 'leituras', |
| 37 | + ] |
| 38 | + |
| 39 | + |
| 40 | +admin = Admin(name='flaskapi', template_mode='bootstrap3') |
| 41 | + |
| 42 | +admin.add_view(UserView(UserAuth, db.session)) |
| 43 | +admin.add_view(EstacaoView(Estacao, db.session)) |
| 44 | +admin.add_view(SensorView(Sensor, db.session)) |
| 45 | + |
| 46 | + |
| 47 | +def init_app(app): |
| 48 | + admin.init_app(app) |
0 commit comments