__init__.py 2.02 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

10
11
12
# third-party imports
from flask import Flask, redirect, url_for, request
from flask import render_template
13
from flask_bootstrap import Bootstrap
14
15
16
17
18


def create_app(config_name):

    app = Flask(__name__)
19
    Bootstrap(app)
20

21
22
    global return_url
    return_url = ''
23

24
25
26
27
28
    @app.route('/success/<name>/<username>')
    def success(username, name):

        global return_url
        print(username, name, return_url, file=sys.stdout)
29

30
        # Deliver arguments to script.
31
        tempString = 'echo ssh ohpc "sudo /opt/ohpc_user_create/user_create ' + username + ' \'' + name + '\'"'
32
33
34
35
        print(tempString, file=sys.stdout)

        output = subprocess.check_output([tempString], shell=True)

36
        print(output.split('\n'), file=sys.stdout)
37
38

        return redirect(return_url, 302)
39

40
    @app.route('/', methods=['GET', 'POST'])
41
42
    def index():

43
44
        user = request.remote_user

45
        if request.method == 'GET':
46

47
            global return_url
48
49
            if "redir" in request.args:
                return_url = request.args.get("redir") or "/pun/sys/dashboard"
50

51
            return render_template("auth/SignUp.html", user=user)
52

53
        if request.method == 'POST':
54

55
            name = request.form['name']
56

57
            if name != "":
58

59
                return redirect(url_for('success', username=str(user), name=name))
60

61
62
            else:
                return render_template("auth/SignUp.html", user=user)
63

64
65
66
67
    with app.test_request_context(
            '/', environ_base={'REMOTE_USER': 'short'}):
        pass

68
69
70
71
72
73
74
75
76
77
78
79
80
    @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