__init__.py 2.35 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
import datetime
import subprocess
12
# third-party imports
13
from flask import Flask, redirect, url_for, request, render_template, flash
14
from flask_bootstrap import Bootstrap
15
16
17
18
19


def create_app(config_name):

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

22
23
    global return_url
    return_url = ''
24

25
    @app.route('/', methods=['GET', 'POST'])
26
    def index():
27

28
29
        user = request.remote_user

30
        if "redir" in request.args and return_url == "":
31
            return_url = request.args.get("redir") or "/pun/sys/dashboard"
32

33
            if name != "":
34

35
                return redirect(url_for('success', username=str(user), fullname=name))
36

37
38
            else:
                return render_template("auth/SignUp.html", user=user)
39

40
41
    @app.route('/success/<username>/<fullname>')
    def success(username, fullname):
42

43
44
45
46
47
48
        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)
49

50
        try:
51
52
53
54
55
56
57
58
59
            # function to write out a flatdb with the name of the file being a timestamp and the content
            # of the file beieng blazerID the user submitted from the flask form (fullname)
            time_stamp = time.strftime("%m-%d-%Y_%H:%M:%S")
            directory = "/var/www/ood/register/flask_user_reg/app/flat_db"
            complete_file_name = os.path.join(directory, time_stamp + ".txt")

            if not os.path.exists(directory):
                os.makedirs(directory)

60
61
62
            file = open(complete_file_name, "w")
            file.write(fullname)
            file.close()
63
            return redirect(return_url, 302)
64

65
        except:
66
67
            flash("Registration Failed")
            return redirect(return_url)
68

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