__init__.py 2.29 KB
Newer Older
1
2
# app/__init__.py

3
4
5
6
7
# local imports
from __future__ import print_function

import os
import sys
8
import subprocess
9
import time
10

11
# third-party imports
12
13
from flask import Flask, redirect, url_for, request, render_template, flash
from flask_wtf import FlaskForm
14
from flask_bootstrap import Bootstrap
15
from wtforms import StringField, SubmitField, validators
16
17
18
19
20


def create_app(config_name):

    app = Flask(__name__)
21
    Bootstrap(app)
22

23
24
    global return_url
    return_url = ''
25
26
    global loading
    loading = False
27

28
29
30
    class MainForm(FlaskForm):
        fullname = StringField('Full Name: ', [validators.DataRequired(), ])
        submit = SubmitField('Submit')
31

32
    @app.route('/', methods=['GET', 'POST'])
33
    def index():
34
35
        global return_url
        global loading
36
37
        user = request.remote_user

38
39
        if "redir" in request.args:
            return_url = request.args.get("redir") or "/pun/sys/dashboard"
40

41
42
43
44
45
        username = False
        form = MainForm()
        if form.is_submitted():
            username = form.fullname.data
            form.fullname.data = ''
46

47
            return redirect(url_for('success', username=str(user), fullname=username))
48

49
        return render_template('auth/SignUp.html', form=form, user=user, loading=loading)
50

51
52
    @app.route('/success/<username>/<fullname>')
    def success(username, fullname):
53

54
55
56
57
58
59
        global return_url
        print(username, fullname, return_url, file=sys.stdout)

        # Deliver arguments to script.
        tempString = 'ssh ohpc "sudo /opt/ohpc_user_create/user_create ' + username + " " + fullname + '"'
        print(tempString, file=sys.stdout)
60

61
        try:
62

63
64
            subprocess.check_output([tempString], shell=True)
            return redirect(return_url, 302)
65

66
67
68
69
70
        except:
            flash("Registration Failed. Please try again.")
            global loading
            loading = True
            return redirect(url_for('index'))
71

72
73
74
75
76
77
78
79
80
81
82
83
84
    @app.errorhandler(403)
    def forbidden(error):
        return render_template('errors/403.html', title='Forbidden'), 403

    @app.errorhandler(404)
    def page_not_found(error):
        return render_template('errors/404.html', title='Page Not Found'), 404

    @app.errorhandler(500)
    def internal_server_error(error):
        return render_template('errors/500.html', title='Server Error'), 500

    return app