diff --git a/Procfile b/Procfile new file mode 100644 index 0000000..136cd22 --- /dev/null +++ b/Procfile @@ -0,0 +1 @@ +web: python3 flask_app.py diff --git a/README.md b/README.md index 61c4ebe..c8b2aab 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,14 @@ # Toolbox-Flask Web Apps Project Toolbox starter code - Full instructions on [the course website](https://sd17spring.github.io//toolboxes/web-apps/) - This toolbox exercise was developed by [Patrick Huston](https://github.com/phuston) + + + +The code takes a list of lists that serves as the database to the website user information. +This database can be found in database.py in the main directory. + +The user profiles can be accessed both through the index page and through the URL. +Each user's profile page is indexed as /profile/user_number. + +Website URL: http://my-intrusive-website.herokuapp.com/ diff --git a/database.py b/database.py new file mode 100644 index 0000000..40266d3 --- /dev/null +++ b/database.py @@ -0,0 +1,9 @@ +""" +A database for all the users. It is a list of lists, containing the firstname, +lastname and age information for the user. + +Author: Onur Talu +""" + +user_db = [['Inigo', 'Montoya', '10'], ['Onur', 'Talu', '5'], + ['Evan', 'New-Schmidt', '19'], ['Colvin', 'Chapman', '19']] diff --git a/flask_app.py b/flask_app.py index b03bdc6..853145c 100644 --- a/flask_app.py +++ b/flask_app.py @@ -1,3 +1,96 @@ """ -Put your Flask app code here. -""" \ No newline at end of file +Produces a website that has an index page, and profiles for three users, +as well as a "hello" page. The profiles can only be reached if the user +inputs correct information about themselves in the index page. + +After running in the terminal, go to 127.0.0.1:5000 to access the index page, +and ~/hello/ to reach the "hello" page. + +Author: Onur Talu +""" + +import os +from flask import Flask, request +from flask import render_template, redirect, url_for +import database +app = Flask(__name__) + +user_db = database.user_db + + +@app.route('/') +def index(): + """ + Renders the template index.html, which directs the user to a page with a + form that redirects the user to a profile or error page, dpeending on the + input. + """ + return render_template('index.html') + + +@app.route('/hello/') +@app.route('/hello/') +def hello(name=None): + """ + The "hello" page. Can be accessed through the URL. + """ + return render_template('hello.html', name=name) + + +@app.route('/login', methods=['POST', 'GET']) +def login(): + """ + Requests the information provided by the user and compares it to the + database, redirects the user to a profile page or an error page, + depending on the input. + """ + if request.method == 'POST': + inputfn = request.form['firstname'] + inputln = request.form['lastname'] + inputage = request.form['age'] + for i in range(len(user_db)): # checks if the credentials check out + if user_db[i] == [inputfn, inputln, inputage]: + print('Valid Login') + return redirect(url_for('profile', user_no=str(i+1))) + print('Invalid Login') + return redirect(url_for('error_page')) + else: + print('Method is not Post') # Useful to keep when debugging + return redirect(url_for('we_messed_up')) + + +@app.route('/sorry') +def sorry(): + """ + Returns an error page that notes that the problem is not in the input of + the user, but in the code and apologizes. + """ + return render_template('we_messed_up.html') + + +@app.route('/error_page') +def error_page(): + """ + Error page for when the input from the user does not match the database. + """ + return render_template('error_page.html') + + +@app.route('/profile') +@app.route('/profile/') +def profile(user_no=None): + """ + Returns the profile page for the user. + """ + user_info = user_db[int(user_no)-1] + info_dict = {'firstname': user_info[0], + 'surname': user_info[1], + 'age': user_info[2]} + return render_template('profile.html', **info_dict) + + +if __name__ == '__main__': + app.debug = True # updates the page as the code is saved + HOST = '0.0.0.0' if 'PORT' in os.environ else '127.0.0.1' + PORT = int(os.environ.get('PORT', 443)) + app.run(host=HOST, port=PORT) diff --git a/hello.py b/hello.py deleted file mode 100644 index 2420ed6..0000000 --- a/hello.py +++ /dev/null @@ -1,13 +0,0 @@ -""" -Simple "Hello, World" application using Flask -""" - -from flask import Flask -app = Flask(__name__) - -@app.route('/') -def hello_world(): - return 'Hello World!' - -if __name__ == '__main__': - app.run() diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..e3e9a71 --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +Flask diff --git a/runtime.txt b/runtime.txt new file mode 100644 index 0000000..c91e43b --- /dev/null +++ b/runtime.txt @@ -0,0 +1 @@ +python-3.6.1 diff --git a/static/index_style.css b/static/index_style.css new file mode 100644 index 0000000..3a33da8 --- /dev/null +++ b/static/index_style.css @@ -0,0 +1,8 @@ +body { + background-color: lightblue; +} + +h1 { + color: navy; + margin-left: 20px; +} diff --git a/templates/error_page.html b/templates/error_page.html new file mode 100644 index 0000000..76b7de5 --- /dev/null +++ b/templates/error_page.html @@ -0,0 +1,41 @@ + + + + + + We're sorry :( + + + + + + +Sorry, not sorry + + + +

Sorry

+

It appears that we don't have any users, matching your credentials.
+ Either you did not enter your credentials right, or you are a new user.

+ Considering that we haven't figured out how to register a new user, + if you are a new potential user, we would suggest you to stop using our services.

+ Thank you for your business!

+ + +