-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
132 lines (101 loc) · 3.82 KB
/
app.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
"""
tempSrvr
Copyright (C) 2018 Marcel Beyer
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program 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 Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
import os
if os.getenv('CONFIGFILE') is not None:
import importlib.util
import sys
spec = importlib.util.spec_from_file_location("Config", os.getenv('CONFIGFILE'))
config = importlib.util.module_from_spec(spec)
sys.modules["config"] = config
spec.loader.exec_module(config)
Config = config.Config
else:
from config import Config
from flask import Flask, redirect, url_for, render_template, request
from flask_login import current_user
from flask_migrate import Migrate
from flask_nav import Nav, register_renderer
from flask_nav.elements import View, Navbar
from sqlalchemy import and_
#from config import Config
import model
from model import Sensor
from forms import RegistrationForm
from helpers import BootstrapNavRenderer, IconText
from blueprints import sensor, user, api
from blueprints.user import login_manager
nav = Nav()
def create_app():
app = Flask(__name__)
app.secret_key = Config.secret_key
app.config['SQLALCHEMY_DATABASE_URI'] = Config.database_url
app.app_context()
login_manager.init_app(app)
app.register_blueprint(sensor.bp)
app.register_blueprint(user.bp)
app.register_blueprint(api.bp)
register_renderer(app, 'bootstrap', BootstrapNavRenderer)
return app
app = create_app()
app.app_context().push()
nav.init_app(app)
with app.app_context():
model.db.init_app(app)
migrate = Migrate(app, model.db)
def get_app():
global app
return app
if __name__ == '__main__':
app.run()
@app.context_processor
def sensors():
return dict(sensors=Sensor.query.filter_by(public=True).all())
@nav.navigation()
def main_nav():
items = [
View(IconText('Home','home'), 'home')
]
# add public sensors
for sensor in Sensor.query.filter_by(public=True).all():
items.append(View(IconText(sensor.name,'thermometer'), 'sensor.show', id=sensor.id))
# add private sensors
if current_user.is_authenticated:
for sensor in Sensor.query.filter(Sensor.users.any(id=current_user.id)).all():
items.append(View(IconText(sensor.name, 'thermometer'), 'sensor.show', id=sensor.id))
# add admin stuff
if current_user.is_authenticated and current_user.admin is True:
items.append(View(IconText('Manage sensors', 'radio'), 'sensor.manage'))
items.append(View(IconText('Manage users','users'), 'user.manage'))
# add stuff for logged in users
if current_user.is_authenticated:
items.append(View(IconText('Settings','settings'), 'user.settings'))
return Navbar('', *items)
@app.route('/')
def home():
return render_template('index.html')
@app.route('/install', methods=['GET', 'POST'])
def install():
if len(model.User.query.all()) > 0:
return redirect(url_for('home'))
form = RegistrationForm(request.form)
if request.method == 'POST' and form.validate():
# register user
admin = model.User(form.username.data, form.password.data)
admin.admin = True
model.db.session.add(admin)
model.db.session.commit()
return redirect(url_for('home'))
else:
return render_template('install/admin.html', form=form)