Commit 16f69d07b2d366343791eb53ad5481f6deb4b7e1

Authored by Antoine Goutenoir
1 parent 0daf0dc8
Exists in master

Add an (unsecured!) admin backend.

flaskr/__init__.py
@@ -4,11 +4,13 @@ from flask import Flask @@ -4,11 +4,13 @@ from flask import Flask
4 from flask.cli import ScriptInfo 4 from flask.cli import ScriptInfo
5 from webassets.loaders import PythonLoader as PythonAssetsLoader 5 from webassets.loaders import PythonLoader as PythonAssetsLoader
6 6
  7 +
7 from flaskr import assets 8 from flaskr import assets
8 -from flaskr.models import db 9 +from flaskr.models import db, Estimation, EstimationView
9 from flaskr.controllers.main_controller import main 10 from flaskr.controllers.main_controller import main
10 11
11 from flaskr.extensions import ( 12 from flaskr.extensions import (
  13 + admin,
12 cache, 14 cache,
13 assets_env, 15 assets_env,
14 debug_toolbar, 16 debug_toolbar,
@@ -39,16 +41,13 @@ def create_app(object_name): @@ -39,16 +41,13 @@ def create_app(object_name):
39 # Load configuration 41 # Load configuration
40 app.config.from_object(object_name) 42 app.config.from_object(object_name)
41 43
42 - # initialize the cache 44 + # Initialize
43 cache.init_app(app) 45 cache.init_app(app)
44 -  
45 - # initialize the debug tool bar  
46 debug_toolbar.init_app(app) 46 debug_toolbar.init_app(app)
47 -  
48 - # initialize SQLAlchemy  
49 db.init_app(app) 47 db.init_app(app)
50 -  
51 login_manager.init_app(app) 48 login_manager.init_app(app)
  49 + admin.init_app(app)
  50 + admin.add_view(EstimationView(Estimation, db.session))
52 51
53 # Import and register the different asset bundles 52 # Import and register the different asset bundles
54 assets_env.init_app(app) 53 assets_env.init_app(app)
flaskr/controllers/admin_controller.py 0 → 100644
@@ -0,0 +1,11 @@ @@ -0,0 +1,11 @@
  1 +from flask import Blueprint, render_template, flash, request, redirect, url_for
  2 +from flask_login import login_user, logout_user, login_required
  3 +
  4 +from flaskr.forms import LoginForm
  5 +from flaskr.models import db, User
  6 +
  7 +from flaskr.controllers.main_controller import main
  8 +
  9 +
  10 +# from flask.ext.admin import Admin
  11 +# from flask.ext.admin.contrib.sqla import ModelView
flaskr/controllers/main_controller.py
@@ -50,6 +50,7 @@ def estimate(): @@ -50,6 +50,7 @@ def estimate():
50 # estimation.email = form.email.data 50 # estimation.email = form.email.data
51 estimation.first_name = form.first_name.data 51 estimation.first_name = form.first_name.data
52 estimation.last_name = form.last_name.data 52 estimation.last_name = form.last_name.data
  53 + estimation.institution = form.institution.data
53 estimation.status = StatusEnum.pending 54 estimation.status = StatusEnum.pending
54 estimation.origin_addresses = form.origin_addresses.data 55 estimation.origin_addresses = form.origin_addresses.data
55 estimation.destination_addresses = form.destination_addresses.data 56 estimation.destination_addresses = form.destination_addresses.data
flaskr/extensions.py
  1 +from flask_admin import Admin
1 from flask_caching import Cache 2 from flask_caching import Cache
2 from flask_debugtoolbar import DebugToolbarExtension 3 from flask_debugtoolbar import DebugToolbarExtension
3 from flask_login import LoginManager 4 from flask_login import LoginManager
@@ -17,6 +18,8 @@ login_manager = LoginManager() @@ -17,6 +18,8 @@ login_manager = LoginManager()
17 login_manager.login_view = "main.login" 18 login_manager.login_view = "main.login"
18 login_manager.login_message_category = "warning" 19 login_manager.login_message_category = "warning"
19 20
  21 +admin = Admin()
  22 +
20 23
21 @login_manager.user_loader 24 @login_manager.user_loader
22 def load_user(userid): 25 def load_user(userid):
flaskr/models.py
  1 +from flask_admin.contrib.sqla import ModelView
  2 +
1 from flaskr.core import generate_unique_id 3 from flaskr.core import generate_unique_id
2 from flask_sqlalchemy import SQLAlchemy 4 from flask_sqlalchemy import SQLAlchemy
3 from flask_login import UserMixin, AnonymousUserMixin 5 from flask_login import UserMixin, AnonymousUserMixin
@@ -32,12 +34,13 @@ class Estimation(db.Model): @@ -32,12 +34,13 @@ class Estimation(db.Model):
32 email = db.Column(db.Unicode(1024)) 34 email = db.Column(db.Unicode(1024))
33 first_name = db.Column(db.Unicode(1024)) # Antoine 35 first_name = db.Column(db.Unicode(1024)) # Antoine
34 last_name = db.Column(db.Unicode(1024)) # Goutenoir 36 last_name = db.Column(db.Unicode(1024)) # Goutenoir
  37 + institution = db.Column(db.Unicode(1024)) # IRAP
35 status = db.Column(db.Enum(StatusEnum), default=StatusEnum.pending) 38 status = db.Column(db.Enum(StatusEnum), default=StatusEnum.pending)
36 39
37 # City, Country 40 # City, Country
38 # One address per line 41 # One address per line
39 - origin_addresses = db.Column(db.Unicode())  
40 - destination_addresses = db.Column(db.Unicode()) 42 + origin_addresses = db.Column(db.UnicodeText())
  43 + destination_addresses = db.Column(db.UnicodeText())
41 44
42 compute_optimal_destination = db.Column(db.Boolean()) 45 compute_optimal_destination = db.Column(db.Boolean())
43 46
@@ -55,6 +58,27 @@ class Estimation(db.Model): @@ -55,6 +58,27 @@ class Estimation(db.Model):
55 return 'cities' in self.get_output_dict() 58 return 'cities' in self.get_output_dict()
56 59
57 60
  61 +class EstimationView(ModelView):
  62 + # Show only name and email columns in list view
  63 + column_list = (
  64 + 'public_id',
  65 + 'status',
  66 + 'first_name',
  67 + 'last_name',
  68 + 'origin_addresses',
  69 + 'destination_addresses',
  70 + 'warnings',
  71 + 'errors',
  72 + )
  73 +
  74 + # Enable search functionality - it will search for terms in
  75 + # name and email fields
  76 + # column_searchable_list = ('name', 'email')
  77 +
  78 + # Add filters for name and email columns
  79 + column_filters = ('first_name', 'last_name')
  80 +
  81 +
58 # USERS ####################################################################### 82 # USERS #######################################################################
59 83
60 class User(db.Model, UserMixin): 84 class User(db.Model, UserMixin):
requirements.txt
1 Flask==1.1.1 1 Flask==1.1.1
2 2
3 # Flask Extensions 3 # Flask Extensions
  4 +Flask-Admin==1.5.4
4 Flask-Assets==0.12 5 Flask-Assets==0.12
5 Flask-Caching==1.7.2 6 Flask-Caching==1.7.2
6 Flask-DebugToolbar==0.10.0 7 Flask-DebugToolbar==0.10.0