94 lines
2.8 KiB
Python
94 lines
2.8 KiB
Python
from flask import Flask, make_response, render_template, request, redirect, url_for
|
|
from markupsafe import escape
|
|
import csv
|
|
app = Flask(__name__)
|
|
|
|
class User:
|
|
MAX_CHAIR_AMOUNT = 6
|
|
|
|
def __init__(
|
|
self,
|
|
username,
|
|
email
|
|
):
|
|
self.username = username
|
|
self.email = email
|
|
|
|
def save(self):
|
|
with open('users.csv', 'a', newline='') as csvfile:
|
|
spamwriter = csv.writer(csvfile)
|
|
spamwriter.writerow([self.username, self.email])
|
|
|
|
@app.route('/')
|
|
def home():
|
|
return render_template("home.html")
|
|
|
|
@app.route('/<username>')
|
|
def index(username = None):
|
|
return render_template("index.html", name = username)
|
|
|
|
@app.route('/contact', methods=['GET', 'POST'])
|
|
def contact(name=None):
|
|
if request.method == 'POST':
|
|
username = request.form['username']
|
|
return redirect(url_for('contact_name', name=escape(username)))
|
|
return render_template("contact.html")
|
|
|
|
@app.route('/contact/<name>', methods=['GET', 'POST'])
|
|
def contact_name(name):
|
|
if request.method == 'POST':
|
|
comment = request.form['subject']
|
|
print(comment)
|
|
with open('comments.csv', 'a', newline='') as csvfile:
|
|
spamwriter = csv.writer(csvfile)
|
|
spamwriter.writerow([comment])
|
|
|
|
comments = []
|
|
with open('comments.csv', 'r', newline='') as csvfile:
|
|
spamreader = csv.reader(csvfile)
|
|
for row in spamreader:
|
|
comments.append(str(row[0]))
|
|
|
|
print(comments)
|
|
return render_template('contact.html', name=escape(name), comments=comments)
|
|
return render_template("contact.html", name=name)
|
|
|
|
@app.route('/login', methods=['GET', 'POST'])
|
|
def login():
|
|
if request.method == 'POST':
|
|
username = request.form['username']
|
|
email = request.form['email']
|
|
remember = request.form.get('remember')
|
|
|
|
if remember == 'yes':
|
|
user = User(username, email)
|
|
user.save()
|
|
|
|
return redirect(url_for('index', username=escape(username)))
|
|
return render_template("form.html")
|
|
|
|
@app.route('/say_hello/<test>')
|
|
def greetings(test):
|
|
return render_template("test.html", test=escape(test))
|
|
|
|
|
|
import unittest
|
|
|
|
class TestStringMethods(unittest.TestCase):
|
|
def test_upper(self):
|
|
client = app.test_client()
|
|
response = client.post("/login", data = {'username': "Test"})
|
|
response = client.get(response.location)
|
|
self.assertEqual(response.status_code, 200)
|
|
self.assertIn("Test", response.text)
|
|
|
|
def test_test(self):
|
|
client = app.test_client()
|
|
response = client.get("/say_hello/Maria")
|
|
self.assertEqual(response.status_code, 200)
|
|
self.assertIn("Hi Maria!", response.text)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main(verbosity=2)
|