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 4 from flask.cli import ScriptInfo
5 5 from webassets.loaders import PythonLoader as PythonAssetsLoader
6 6  
  7 +
7 8 from flaskr import assets
8   -from flaskr.models import db
  9 +from flaskr.models import db, Estimation, EstimationView
9 10 from flaskr.controllers.main_controller import main
10 11  
11 12 from flaskr.extensions import (
  13 + admin,
12 14 cache,
13 15 assets_env,
14 16 debug_toolbar,
... ... @@ -39,16 +41,13 @@ def create_app(object_name):
39 41 # Load configuration
40 42 app.config.from_object(object_name)
41 43  
42   - # initialize the cache
  44 + # Initialize
43 45 cache.init_app(app)
44   -
45   - # initialize the debug tool bar
46 46 debug_toolbar.init_app(app)
47   -
48   - # initialize SQLAlchemy
49 47 db.init_app(app)
50   -
51 48 login_manager.init_app(app)
  49 + admin.init_app(app)
  50 + admin.add_view(EstimationView(Estimation, db.session))
52 51  
53 52 # Import and register the different asset bundles
54 53 assets_env.init_app(app)
... ...
flaskr/controllers/admin_controller.py 0 → 100644
... ... @@ -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 50 # estimation.email = form.email.data
51 51 estimation.first_name = form.first_name.data
52 52 estimation.last_name = form.last_name.data
  53 + estimation.institution = form.institution.data
53 54 estimation.status = StatusEnum.pending
54 55 estimation.origin_addresses = form.origin_addresses.data
55 56 estimation.destination_addresses = form.destination_addresses.data
... ...
flaskr/extensions.py
  1 +from flask_admin import Admin
1 2 from flask_caching import Cache
2 3 from flask_debugtoolbar import DebugToolbarExtension
3 4 from flask_login import LoginManager
... ... @@ -17,6 +18,8 @@ login_manager = LoginManager()
17 18 login_manager.login_view = "main.login"
18 19 login_manager.login_message_category = "warning"
19 20  
  21 +admin = Admin()
  22 +
20 23  
21 24 @login_manager.user_loader
22 25 def load_user(userid):
... ...
flaskr/models.py
  1 +from flask_admin.contrib.sqla import ModelView
  2 +
1 3 from flaskr.core import generate_unique_id
2 4 from flask_sqlalchemy import SQLAlchemy
3 5 from flask_login import UserMixin, AnonymousUserMixin
... ... @@ -32,12 +34,13 @@ class Estimation(db.Model):
32 34 email = db.Column(db.Unicode(1024))
33 35 first_name = db.Column(db.Unicode(1024)) # Antoine
34 36 last_name = db.Column(db.Unicode(1024)) # Goutenoir
  37 + institution = db.Column(db.Unicode(1024)) # IRAP
35 38 status = db.Column(db.Enum(StatusEnum), default=StatusEnum.pending)
36 39  
37 40 # City, Country
38 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 45 compute_optimal_destination = db.Column(db.Boolean())
43 46  
... ... @@ -55,6 +58,27 @@ class Estimation(db.Model):
55 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 82 # USERS #######################################################################
59 83  
60 84 class User(db.Model, UserMixin):
... ...
requirements.txt
1 1 Flask==1.1.1
2 2  
3 3 # Flask Extensions
  4 +Flask-Admin==1.5.4
4 5 Flask-Assets==0.12
5 6 Flask-Caching==1.7.2
6 7 Flask-DebugToolbar==0.10.0
... ...