2017-09-17 14:37:10 +02:00
|
|
|
from mailu import dockercli, app, db, models, forms, access
|
2016-03-19 21:37:48 +02:00
|
|
|
|
|
|
|
import flask
|
2016-08-13 20:51:54 +02:00
|
|
|
import flask_login
|
2016-11-10 11:48:37 +02:00
|
|
|
import smtplib
|
|
|
|
|
|
|
|
from email.mime import text
|
2017-09-10 15:56:08 +02:00
|
|
|
from urllib import parse
|
2016-03-19 21:37:48 +02:00
|
|
|
|
|
|
|
|
|
|
|
@app.route('/', methods=["GET"])
|
2016-08-29 19:24:39 +02:00
|
|
|
@access.authenticated
|
2016-03-19 21:37:48 +02:00
|
|
|
def index():
|
2016-03-20 16:36:56 +02:00
|
|
|
return flask.redirect(flask.url_for('.user_settings'))
|
2016-03-19 21:37:48 +02:00
|
|
|
|
|
|
|
|
|
|
|
@app.route('/login', methods=['GET', 'POST'])
|
|
|
|
def login():
|
|
|
|
form = forms.LoginForm()
|
|
|
|
if form.validate_on_submit():
|
|
|
|
user = models.User.login(form.email.data, form.pw.data)
|
|
|
|
if user:
|
|
|
|
flask_login.login_user(user)
|
2017-09-10 15:56:08 +02:00
|
|
|
redirect = flask.request.args.get('next')
|
|
|
|
parsed_redirect = parse.urlparse(redirect)
|
|
|
|
if parsed_redirect.scheme or parsed_redirect.netloc:
|
|
|
|
return flask.abort(400)
|
|
|
|
return flask.redirect(redirect or flask.url_for('.index'))
|
2016-03-19 21:37:48 +02:00
|
|
|
else:
|
2016-05-01 20:04:40 +02:00
|
|
|
flask.flash('Wrong e-mail or password', 'error')
|
2016-03-19 21:37:48 +02:00
|
|
|
return flask.render_template('login.html', form=form)
|
|
|
|
|
|
|
|
|
|
|
|
@app.route('/logout', methods=['GET'])
|
2016-08-29 19:24:39 +02:00
|
|
|
@access.authenticated
|
2016-03-19 21:37:48 +02:00
|
|
|
def logout():
|
|
|
|
flask_login.logout_user()
|
2016-03-20 16:36:56 +02:00
|
|
|
return flask.redirect(flask.url_for('.index'))
|
2016-04-24 19:17:40 +02:00
|
|
|
|
|
|
|
|
|
|
|
@app.route('/services', methods=['GET'])
|
2016-08-29 19:24:39 +02:00
|
|
|
@access.global_admin
|
2016-04-24 19:17:40 +02:00
|
|
|
def services():
|
2016-10-13 09:31:15 +02:00
|
|
|
try:
|
2016-11-09 17:38:17 +02:00
|
|
|
containers = dockercli.get()
|
2016-10-13 09:31:15 +02:00
|
|
|
except Exception as error:
|
|
|
|
return flask.render_template('docker-error.html', error=error)
|
2016-04-24 19:17:40 +02:00
|
|
|
return flask.render_template('services.html', containers=containers)
|
2016-11-10 11:48:37 +02:00
|
|
|
|
|
|
|
|
|
|
|
@app.route('/announcement', methods=['GET', 'POST'])
|
|
|
|
@access.global_admin
|
|
|
|
def announcement():
|
|
|
|
from_address = '{}@{}'.format(
|
2017-09-17 14:37:10 +02:00
|
|
|
app.config['POSTMASTER'], app.config['DOMAIN'])
|
2016-11-10 11:48:37 +02:00
|
|
|
form = forms.AnnouncementForm()
|
|
|
|
if form.validate_on_submit():
|
|
|
|
with smtplib.SMTP('smtp') as smtp:
|
|
|
|
for recipient in [user.email for user in models.User.query.all()]:
|
|
|
|
msg = text.MIMEText(form.announcement_body.data)
|
|
|
|
msg['Subject'] = form.announcement_subject.data
|
|
|
|
msg['From'] = from_address
|
|
|
|
msg['To'] = recipient
|
|
|
|
smtp.sendmail(from_address, [recipient], msg.as_string())
|
|
|
|
# Force-empty the form
|
|
|
|
form.announcement_subject.data = ''
|
|
|
|
form.announcement_body.data = ''
|
|
|
|
flask.flash('Your announcement was sent', 'success')
|
|
|
|
return flask.render_template('announcement.html', form=form,
|
|
|
|
from_address=from_address)
|