Commit 4ea527c1 authored by Mitchell Moore's avatar Mitchell Moore Committed by Krish Moodbidri
Browse files

Modified basic flask app to populate username and password, pass it to ood and...

Modified basic flask app to populate username and password, pass it to ood and redirect to home page on password authentication

- Pruning of unnecessary files, and alteration form
- Update Readme and minor html tweaks
- Added code to pass username and name variables to ood.
- Added redirect to origin page after success
- Populate user and catch None error if form is submitted blank
- Updated return_url with placeholder value for stability.
- Update __init__.py
- Updated return_url with proper redirect value
parent 60c6ba82
...@@ -3,24 +3,20 @@ ...@@ -3,24 +3,20 @@
To clone this repo use the command: To clone this repo use the command:
``` ```
git clone https://gitlab.rc.uab.edu/noe121/flaskformwork.git git clone https://gitlab.rc.uab.edu/mmoo97/flask_user_reg.git
``` ```
## Prerequisites ## Prerequisites
-have pip installed - Ensure `pip` is installed (see: https://packaging.python.org/guides/installing-using-pip-and-virtualenv/ )
--follow link for installation - Install Flask using the `pip` command. Note, to install flask in your own `$HOME` use `pip install --user Flask`
```
https://packaging.python.org/guides/installing-using-pip-and-virtualenv/
```
-then install Flask
``` ```
pip install Flask pip install Flask
``` ```
## Starting the virtual machine for Flask ## Starting the virtual machine for Flask
To start go to formWork directory - Change to the `formWork` directory
-then start virtual machine: - then start virtual machine:
``` ```
source venv/bin/activate source venv/bin/activate
``` ```
......
# app/__init__.py # app/__init__.py
# local imports
from __future__ import print_function
import os
import sys
# third-party imports # third-party imports
from flask import Flask, redirect, url_for, request from flask import Flask, redirect, url_for, request
from flask import render_template from flask import render_template
from flask_bootstrap import Bootstrap
# local imports
def create_app(config_name): def create_app(config_name):
app = Flask(__name__) app = Flask(__name__)
Bootstrap(app)
global return_url
return_url = ''
@app.route('/success/<name>') @app.route('/success/<name>/<username>')
def success(name): def success(username, name):
return 'welcome new user %s' % name
global return_url
print(username, name, return_url, file=sys.stdout)
# Deliver arguments to script.
tempString = 'sudo user_create ' + username + " " + name
os.system("ssh ohpc " + tempString)
return redirect(return_url, 302)
@app.route('/', methods=['GET']) @app.route('/', methods=['GET'])
def index(): def index():
return render_template("auth/SignUp.html")
@app.route('/', methods=['POST']) global return_url
return_url = request.args.get("redir")
user = request.remote_user
return render_template("auth/SignUp.html", user=user, url=return_url)
@app.route('/', methods=['GET', 'POST'])
def SignUp(): def SignUp():
if request.method == 'POST':
email = request.form['email'] name = request.form['name']
# make username from email
# user = request.environ('REMOTE_USER') user = request.remote_user
# user = request.remote_user.name
# user = request.environ if request.method == 'GET':
user = 'Mitchell'
return redirect(url_for('success', name=user)) return render_template("auth/SignUp.html", user=user)
if request.method == 'POST' and name != "":
return redirect(url_for('success', username=str(user), name=name))
else:
return render_template("auth/SignUp.html", user=user)
@app.errorhandler(403) @app.errorhandler(403)
def forbidden(error): def forbidden(error):
......
# app/auth/__init__.py
from flask import Blueprint
auth = Blueprint('auth', __name__)
from . import views
# app/auth/forms.py
from flask_wtf import FlaskForm
from wtforms import PasswordField, StringField, SubmitField, ValidationError
from wtforms.validators import DataRequired, Email, EqualTo
class RegistrationForm(FlaskForm):
"""
Form for users to create new account
"""
email = StringField('Email', validators=[DataRequired(), Email()])
username = StringField('Username', validators=[DataRequired()])
first_name = StringField('First Name', validators=[DataRequired()])
last_name = StringField('Last Name', validators=[DataRequired()])
password = PasswordField('Password', validators=[
DataRequired(),
EqualTo('confirm_password')
])
confirm_password = PasswordField('Confirm Password')
submit = SubmitField('Register')
# def validate_email(self, field):
# if Employee.query.filter_by(email=field.data).first():
# raise ValidationError('Email is already in use.')
#
# def validate_username(self, field):
# if Employee.query.filter_by(username=field.data).first():
# raise ValidationError('Username is already in use.')
class LoginForm(FlaskForm):
"""
Form for users to login
"""
email = StringField('Email', validators=[DataRequired(), Email()])
password = PasswordField('Password', validators=[DataRequired()])
submit = SubmitField('Login')
from flask import flash, redirect, render_template, url_for
from forms import RegistrationForm
from . import auth
@auth.route('/register', methods=['GET', 'POST'])
def register():
"""
Handle requests to the /register route
Add an employee to the database through the registration form
"""
form = RegistrationForm()
if form.validate_on_submit():
employee = Employee(email=form.email.data,
username=form.username.data,
first_name=form.first_name.data,
last_name=form.last_name.data,
password=form.password.data)
# add employee to the database
db.session.add(employee)
db.session.commit()
flash('You have successfully registered! You may now login.')
# redirect to the login page
return redirect(url_for('auth.login'))
# load registration template
return render_template('auth/register.html', form=form, title='Register')
\ No newline at end of file
...@@ -2,41 +2,15 @@ ...@@ -2,41 +2,15 @@
<html> <html>
<body> <body>
<h2>Sign up Form</h2> <h1>Sign up Form</h1>
<h2>Hello, {{ user }}!</h2>
<form action="/" method="post"> <form action="/" method="post">
<div class ="signUpContainer"> <div class ="signUpContainer">
<label for="email"><b>Email:<br></b></label> <label for="name"><b>Full Name:<br></b></label>
<input type="email" placeholder="Enter Email" name="email" onkeyup='validateEmail(email);' required/><br> <input type="text" placeholder="Enter Full Name" name="name" />
<input type="submit" value="submit" onclick="return empty()"/>
<label>password : <br> </div>
<input name="password" placeholder="Enter Password" id="password" type="password" onkeyup='check();' required />
</label>`
<br>
<label>confirm password: <br>
<input type="password" placeholder="Re-enter Password" name="confirm_password" id="confirm_password" onkeyup='check();' required/>
<span id='message'></span>
</label>`<br>
<input type="submit" value = "Submit" ></input>
<script>
var check = function() {
if (document.getElementById('password').value ==
document.getElementById('confirm_password').value) {
document.getElementById('message').style.color = 'green';
document.getElementById('message').innerHTML = 'matching';
} else {
document.getElementById('message').style.color = 'red';
document.getElementById('message').innerHTML = 'not matching';
}
}
function validateEmail() {
var re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
return re.test(String(email).toLowerCase());
}
</script>
</div>
</form> </form>
</body> </body>
</html> </html>
\ No newline at end of file
<!-- app/templates/auth/register.html -->
{% import "bootstrap/wtf.html" as wtf %}
{% extends "base.html" %}
{% block title %}Register{% endblock %}
{% block body %}
<div class="content-section">
<div class="center">
<h1>Register for an account</h1>
<br/>
{{ wtf.quick_form(form) }}
</div>
</div>
{% endblock %}
\ No newline at end of file
...@@ -4,8 +4,12 @@ ...@@ -4,8 +4,12 @@
<meta charset="UTF-8"> <meta charset="UTF-8">
<title>403</title> <title>403</title>
<h1>403 Error</h1> <h1>403 Error</h1>
</head> </head>
<body> <body>
<p>Forbidden</p>
</body> </body>
</html> </html>
\ No newline at end of file
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
<h1>404 Error</h1> <h1>404 Error</h1>
</head> </head>
<body> <body>
<p>Page Not Found</p>
</body> </body>
</html> </html>
\ No newline at end of file
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
<h1>500 Error</h1> <h1>500 Error</h1>
</head> </head>
<body> <body>
<p>Internal Server Error</p>
</body> </body>
</html> </html>
\ No newline at end of file
Hello
\ No newline at end of file
...@@ -9,4 +9,4 @@ app = create_app(config_name) ...@@ -9,4 +9,4 @@ app = create_app(config_name)
if __name__ == '__main__': if __name__ == '__main__':
app.run(Debug=True) app.run()
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment