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

3
4
5
6
# local imports
from __future__ import print_function

import os
7
import os.path
8
import sys
9
import time
10

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


def create_app(config_name):

    app = Flask(__name__)
23
    Bootstrap(app)
24

25
26
    global return_url
    return_url = ''
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
        global return_url
35
36
        user = request.remote_user

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

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

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

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

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

53
54
55
56
57
58
        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)
59

60
        try:
61
62
63
64
65
66
            time_stamp = time.strftime("%Y%m%d-%H%M%S")
            blazer_path = "/var/www/ood/register/flask_user_reg"
            complete_file_name = os.path.join(blazer_path, time_stamp+".txt")
            file = open(complete_file_name, "w")
            file.write(fullname)
            file.close()
67
            return redirect(return_url, 302)
68

69
70
71
        except:
            flash("Registration Failed. Please try again.")
            return redirect(url_for('index'))
72

73
74
75
76
77
78
79
80
81
82
83
84
85
    @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