From 1d31e1e211b6b3e1971c6db3eda79978b7ada511 Mon Sep 17 00:00:00 2001 From: otalu Date: Sat, 11 Mar 2017 17:03:25 -0500 Subject: [PATCH 01/15] Have a working website --- flask_app.py | 70 +++++++++++++++++++++++++++++++++++++-- hello.py | 2 ++ templates/error_page.html | 17 ++++++++++ templates/hello.html | 7 ++++ templates/index.html | 30 +++++++++++++++++ templates/login.html | 15 +++++++++ templates/profile.html | 18 ++++++++++ 7 files changed, 157 insertions(+), 2 deletions(-) create mode 100644 templates/error_page.html create mode 100644 templates/hello.html create mode 100644 templates/index.html create mode 100644 templates/login.html create mode 100644 templates/profile.html diff --git a/flask_app.py b/flask_app.py index b03bdc6..77c1c36 100644 --- a/flask_app.py +++ b/flask_app.py @@ -1,3 +1,69 @@ """ -Put your Flask app code here. -""" \ No newline at end of file +Author: Onur Talu +""" + +from flask import Flask, request +from flask import render_template, redirect, url_for +app = Flask(__name__) + + +@app.route('/') +def hello_world(): + # name = input("Your Name") + return render_template('index.html') + + +@app.route('/hello/') +@app.route('/hello/') +def hello(name=None): + return render_template('hello.html', name=name) + + +@app.route('/login', methods=['POST', 'GET']) +def login(): + # error = None + print(request.method) + if request.method == 'POST': + inputfn = request.form['firstname'] + inputln = request.form['lastname'] + inputage = request.form['age'] + # render_template('error_page.html') + if valid_login(inputfn, inputln, inputage): + print('valid login') + return redirect(url_for('profile')) + else: + print('invalid login') + return redirect(url_for('error_page')) + else: + print('method is not post') + return redirect(url_for('error_page')) + # render_template('error_page.html') + # # the code below is executed if the request method + # # was GET or the credentials were invalid + # return render_template('profile.html', error=error) + + +@app.route('/error_page') +def error_page(): + return render_template('error_page.html') + + +def valid_login(inputfn, inputln, inputage): + firstname = 'Inigo' + lastname = 'Montoya' + age = '10' + # if firstname == inputfn and lastname == inputln and age == inputage: + # render_template('profile.html') + # else: + # render_template('error_page.html') + return firstname == inputfn and lastname == inputln and age == inputage + + +@app.route('/profile') +def profile(): + return render_template('profile.html') + + +if __name__ == '__main__': + app.debug = True + app.run() diff --git a/hello.py b/hello.py index 2420ed6..c3ea221 100644 --- a/hello.py +++ b/hello.py @@ -5,9 +5,11 @@ from flask import Flask app = Flask(__name__) + @app.route('/') def hello_world(): return 'Hello World!' + if __name__ == '__main__': app.run() diff --git a/templates/error_page.html b/templates/error_page.html new file mode 100644 index 0000000..1585d6f --- /dev/null +++ b/templates/error_page.html @@ -0,0 +1,17 @@ + + + + + + We're sorry :( + + + + +

Sorry

+ It appears that we don't have any users, matching your credentials.
+ Considering that we haven't figured out how to register a new user, + we would suggest you to stop using our services. + + + diff --git a/templates/hello.html b/templates/hello.html new file mode 100644 index 0000000..c291c55 --- /dev/null +++ b/templates/hello.html @@ -0,0 +1,7 @@ + +Hello from Flask +{% if name %} +

Hello {{ name }}!

+{% else %} +

Hello World!

+{% endif %} diff --git a/templates/index.html b/templates/index.html new file mode 100644 index 0000000..f84ff54 --- /dev/null +++ b/templates/index.html @@ -0,0 +1,30 @@ + + + + + + A Small Hello + + + + +

Helllllooooooooo there

+

Please tell us about yourself.

+
+
+ We are definitely not storing this information.
+ First name:
+
+ Last name:
+
+ Age:
+
+ Your favorite SoftDes Ninja?
+
+ Any other notes?
+

+ + + + + diff --git a/templates/login.html b/templates/login.html new file mode 100644 index 0000000..46024cf --- /dev/null +++ b/templates/login.html @@ -0,0 +1,15 @@ + + + + + + Login + + + + +

Helllllooooooooooooooooooooooo...again

+ Stuff + + + diff --git a/templates/profile.html b/templates/profile.html new file mode 100644 index 0000000..5bae608 --- /dev/null +++ b/templates/profile.html @@ -0,0 +1,18 @@ + + + + + + Homepage + + + + +

My Profile

+ Firstname: Inigo
+ Surname: Montoya
+ Age: 19
+ Favorite NINJA: Patrick Huston + + + From b70257f7bbdc0d2a7e2a6d686a38b1f09b5b28b4 Mon Sep 17 00:00:00 2001 From: otalu Date: Mon, 13 Mar 2017 02:41:48 -0400 Subject: [PATCH 02/15] Done! --- database.py | 9 ++++ flask_app.py | 86 ++++++++++++++++++++++++------------- templates/error_page.html | 3 +- templates/index.html | 6 +-- templates/profile.html | 10 ++--- templates/we_messed_up.html | 18 ++++++++ 6 files changed, 91 insertions(+), 41 deletions(-) create mode 100644 database.py create mode 100644 templates/we_messed_up.html diff --git a/database.py b/database.py new file mode 100644 index 0000000..e1e00d8 --- /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']] diff --git a/flask_app.py b/flask_app.py index 77c1c36..34a8899 100644 --- a/flask_app.py +++ b/flask_app.py @@ -1,69 +1,93 @@ """ +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 """ 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 hello_world(): - # name = input("Your Name") +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(): - # error = None - print(request.method) + """ + 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'] - # render_template('error_page.html') - if valid_login(inputfn, inputln, inputage): - print('valid login') - return redirect(url_for('profile')) - else: - print('invalid login') - return redirect(url_for('error_page')) - else: - print('method is not post') + 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')) - # render_template('error_page.html') - # # the code below is executed if the request method - # # was GET or the credentials were invalid - # return render_template('profile.html', error=error) + 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') -def valid_login(inputfn, inputln, inputage): - firstname = 'Inigo' - lastname = 'Montoya' - age = '10' - # if firstname == inputfn and lastname == inputln and age == inputage: - # render_template('profile.html') - # else: - # render_template('error_page.html') - return firstname == inputfn and lastname == inputln and age == inputage - - @app.route('/profile') -def profile(): - return render_template('profile.html') +@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 + app.debug = True # updates the page as the code is saved app.run() diff --git a/templates/error_page.html b/templates/error_page.html index 1585d6f..0dd6d73 100644 --- a/templates/error_page.html +++ b/templates/error_page.html @@ -10,8 +10,9 @@

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, - we would suggest you to stop using our services. + if you are a new potential user, we would suggest you to stop using our services. diff --git a/templates/index.html b/templates/index.html index f84ff54..d35b8cd 100644 --- a/templates/index.html +++ b/templates/index.html @@ -8,7 +8,7 @@ -

Helllllooooooooo there

+

Hellllllllllllllllllllllllloo there

Please tell us about yourself.

@@ -20,9 +20,7 @@

Helllllooooooooo there

Age:

Your favorite SoftDes Ninja?
-
- Any other notes?
-

+

diff --git a/templates/profile.html b/templates/profile.html index 5bae608..fda8f00 100644 --- a/templates/profile.html +++ b/templates/profile.html @@ -8,11 +8,11 @@ -

My Profile

- Firstname: Inigo
- Surname: Montoya
- Age: 19
- Favorite NINJA: Patrick Huston +

Hello {{firstname}}!

+ Firstname: {{firstname}}
+ Surname: {{surname}}
+ Age: {{age}}
+ Favorite NINJA: Patrick Huston


diff --git a/templates/we_messed_up.html b/templates/we_messed_up.html new file mode 100644 index 0000000..d83d5f4 --- /dev/null +++ b/templates/we_messed_up.html @@ -0,0 +1,18 @@ + + + + + + We're sorry :( + + + + +

Sorry

+ It appears that we messed up.
+ We probably broke the architecture, while trying to make our website better + for you.
+ We apologize on behalf of our non-existing company. + + + From 5ce7a65d9445a58db0d0bc1ada13d43b1678f0fc Mon Sep 17 00:00:00 2001 From: otalu Date: Mon, 13 Mar 2017 02:43:55 -0400 Subject: [PATCH 03/15] Removed hello.py --- hello.py | 15 --------------- 1 file changed, 15 deletions(-) delete mode 100644 hello.py diff --git a/hello.py b/hello.py deleted file mode 100644 index c3ea221..0000000 --- a/hello.py +++ /dev/null @@ -1,15 +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() From 71a0713d1ae907ab0c07bc33ebb905783c443439 Mon Sep 17 00:00:00 2001 From: otalu Date: Mon, 13 Mar 2017 02:47:32 -0400 Subject: [PATCH 04/15] Added some notes about the doe --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index 61c4ebe..b119479 100644 --- a/README.md +++ b/README.md @@ -4,3 +4,7 @@ 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. + +The profiles can be accessed both through the index page and through the URL. Each user's profile page is indexed as /profile/user_number. From 8659388b28c4a8d7b904c955f37937480513a935 Mon Sep 17 00:00:00 2001 From: otalu Date: Mon, 13 Mar 2017 02:49:46 -0400 Subject: [PATCH 05/15] Removed login.html - no longer used - and edited hello.html --- templates/login.html | 15 --------------- 1 file changed, 15 deletions(-) delete mode 100644 templates/login.html diff --git a/templates/login.html b/templates/login.html deleted file mode 100644 index 46024cf..0000000 --- a/templates/login.html +++ /dev/null @@ -1,15 +0,0 @@ - - - - - - Login - - - - -

Helllllooooooooooooooooooooooo...again

- Stuff - - - From b1c043d595af4da4965bc1d22cd0e9cb5fbb87dd Mon Sep 17 00:00:00 2001 From: otalu Date: Mon, 13 Mar 2017 02:51:10 -0400 Subject: [PATCH 06/15] Looked over the writing --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index b119479..e2d7678 100644 --- a/README.md +++ b/README.md @@ -7,4 +7,4 @@ This toolbox exercise was developed by [Patrick Huston](https://github.com/phust The code takes a list of lists that serves as the database to the website user information. -The profiles can be accessed both through the index page and through the URL. Each user's profile page is indexed as /profile/user_number. +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. From d5f72b930b86777a72d615bace2e1d8745004549 Mon Sep 17 00:00:00 2001 From: otalu Date: Mon, 24 Apr 2017 22:26:11 -0400 Subject: [PATCH 07/15] Added the necessary files for Heroku to run --- Procfile | 1 + database.py | 2 +- requirements.txt | 1 + runtime.txt | 1 + static/index_style.css | 8 ++++++++ templates/error_page.html | 26 +++++++++++++++++++++++-- templates/index.html | 41 +++++++++++++++++++++++++++++++++++---- templates/login.html | 15 ++++++++++++++ templates/profile.html | 9 +++++++++ 9 files changed, 97 insertions(+), 7 deletions(-) create mode 100644 Procfile create mode 100644 requirements.txt create mode 100644 runtime.txt create mode 100644 static/index_style.css create mode 100644 templates/login.html 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/database.py b/database.py index e1e00d8..40266d3 100644 --- a/database.py +++ b/database.py @@ -6,4 +6,4 @@ """ user_db = [['Inigo', 'Montoya', '10'], ['Onur', 'Talu', '5'], - ['Evan', 'New-Schmidt', '19']] + ['Evan', 'New-Schmidt', '19'], ['Colvin', 'Chapman', '19']] 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 index 0dd6d73..1202e40 100644 --- a/templates/error_page.html +++ b/templates/error_page.html @@ -5,14 +5,36 @@ We're sorry :( + + + +Sorry, not sorry + +

Sorry

- It appears that we don't have any users, matching your credentials.
+

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. + if you are a new potential user, we would suggest you to stop using our services.

+ Thank you for your business!

+ diff --git a/templates/index.html b/templates/index.html index d35b8cd..8fed049 100644 --- a/templates/index.html +++ b/templates/index.html @@ -2,16 +2,48 @@ - + <title> A Small Hello + + + + + + -

Hellllllllllllllllllllllllloo there

-

Please tell us about yourself.

+

Hello

+

Is it a SoftDes MP5 you are looking for?

+

Please tell us about yourself:

+ + +
-
+ We are definitely not storing this information.
First name:

@@ -23,6 +55,7 @@

Hellllllllllllllllllllllllloo there



+
diff --git a/templates/login.html b/templates/login.html new file mode 100644 index 0000000..46024cf --- /dev/null +++ b/templates/login.html @@ -0,0 +1,15 @@ + + + + + + Login + + + + +

Helllllooooooooooooooooooooooo...again

+ Stuff + + + diff --git a/templates/profile.html b/templates/profile.html index fda8f00..57496b9 100644 --- a/templates/profile.html +++ b/templates/profile.html @@ -5,6 +5,15 @@ Homepage + From 506a3d96044df0243eabd246e77004dc082ff490 Mon Sep 17 00:00:00 2001 From: otalu Date: Mon, 24 Apr 2017 22:51:44 -0400 Subject: [PATCH 08/15] Updated the HOST and TCP/IP --- flask_app.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/flask_app.py b/flask_app.py index 34a8899..22f4a84 100644 --- a/flask_app.py +++ b/flask_app.py @@ -9,6 +9,7 @@ Author: Onur Talu """ +import os.environ from flask import Flask, request from flask import render_template, redirect, url_for import database @@ -90,4 +91,6 @@ def profile(user_no=None): if __name__ == '__main__': app.debug = True # updates the page as the code is saved - app.run() + HOST = '0.0.0.0' if 'PORT' in os.environ else '127.0.0.1' + PORT = int(os.environ.get('PORT', 5000))) + app.run(host=HOST, port=PORT) From 1c8340d1c5604e4a1a0d944aa31ecfe0ce6644ef Mon Sep 17 00:00:00 2001 From: otalu Date: Mon, 24 Apr 2017 23:13:48 -0400 Subject: [PATCH 09/15] Remove extra paranthesis in main function in flask_app.py. --- flask_app.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/flask_app.py b/flask_app.py index 22f4a84..2294cf4 100644 --- a/flask_app.py +++ b/flask_app.py @@ -92,5 +92,5 @@ def profile(user_no=None): 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', 5000))) + PORT = int(os.environ.get('PORT', 5000)) app.run(host=HOST, port=PORT) From 2fe93bc5a227f5d92154f197e1ace95672811a6c Mon Sep 17 00:00:00 2001 From: otalu Date: Mon, 24 Apr 2017 23:16:42 -0400 Subject: [PATCH 10/15] Change port number. --- flask_app.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/flask_app.py b/flask_app.py index 2294cf4..62a039d 100644 --- a/flask_app.py +++ b/flask_app.py @@ -92,5 +92,5 @@ def profile(user_no=None): 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', 5000)) + PORT = int(os.environ.get('PORT', 443)) app.run(host=HOST, port=PORT) From ede66bc3ccd34d0e4fbd5af5e5cecce361de3d53 Mon Sep 17 00:00:00 2001 From: otalu Date: Mon, 24 Apr 2017 23:23:47 -0400 Subject: [PATCH 11/15] Import os instead of os.environ --- flask_app.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/flask_app.py b/flask_app.py index 62a039d..853145c 100644 --- a/flask_app.py +++ b/flask_app.py @@ -9,7 +9,7 @@ Author: Onur Talu """ -import os.environ +import os from flask import Flask, request from flask import render_template, redirect, url_for import database From 6ef5211a893e918e93aa7d28cb284af63f14d651 Mon Sep 17 00:00:00 2001 From: otalu Date: Mon, 24 Apr 2017 23:31:07 -0400 Subject: [PATCH 12/15] Add a GIF of Adele singing. --- templates/error_page.html | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/templates/error_page.html b/templates/error_page.html index 1202e40..703b87e 100644 --- a/templates/error_page.html +++ b/templates/error_page.html @@ -7,7 +7,7 @@