|
| 1 | +import os |
| 2 | +import json |
| 3 | + |
| 4 | +import aikido_zen # Aikido package import |
| 5 | +aikido_zen.protect() |
| 6 | + |
| 7 | +from flask import Flask, render_template, request |
| 8 | +import psycopg2 |
| 9 | + |
| 10 | +app = Flask(__name__) |
| 11 | + |
| 12 | +def get_db_connection(): |
| 13 | + return psycopg2.connect( |
| 14 | + host="localhost", |
| 15 | + database="db", |
| 16 | + user="user", |
| 17 | + password="password") |
| 18 | + |
| 19 | +@app.route("/") |
| 20 | +def homepage(): |
| 21 | + cursor = get_db_connection().cursor() |
| 22 | + cursor.execute("SELECT * FROM dogs") |
| 23 | + dogs = cursor.fetchall() |
| 24 | + return render_template('index.html', title='Homepage', dogs=dogs) |
| 25 | + |
| 26 | + |
| 27 | +@app.route('/dogpage/<int:dog_id>') |
| 28 | +def get_dogpage(dog_id): |
| 29 | + cursor = get_db_connection().cursor() |
| 30 | + cursor.execute("SELECT * FROM dogs WHERE id = " + str(dog_id)) |
| 31 | + dog = cursor.fetchmany(1)[0] |
| 32 | + return render_template('dogpage.html', title=f'Dog', dog=dog, isAdmin=("Yes" if dog[2] else "No")) |
| 33 | + |
| 34 | +@app.route("/create", methods=['GET']) |
| 35 | +def show_create_dog_form(): |
| 36 | + return render_template('create_dog.html') |
| 37 | + |
| 38 | +@app.route("/create_many", methods=['GET']) |
| 39 | +def show_create_dog_form_many(): |
| 40 | + return render_template('create_dog.html') |
| 41 | + |
| 42 | +@app.route("/create", methods=['POST']) |
| 43 | +def create_dog(): |
| 44 | + dog_name = request.form['dog_name'] |
| 45 | + conn = get_db_connection() |
| 46 | + cursor = conn.cursor() |
| 47 | + cursor.execute(f"INSERT INTO dogs (dog_name, isAdmin) VALUES ('%s', FALSE)" % (dog_name)) |
| 48 | + conn.commit() |
| 49 | + cursor.close() |
| 50 | + conn.close() |
| 51 | + return f'Dog {dog_name} created successfully' |
| 52 | + |
| 53 | +@app.route("/create/:id", methods=["GET"]) |
| 54 | +@app.route("/create_many", methods=['POST']) |
| 55 | +def create_dog_many(): |
| 56 | + dog_name = request.form['dog_name'] |
| 57 | + conn = get_db_connection() |
| 58 | + cursor = conn.cursor() |
| 59 | + cursor.executemany([f"INSERT INTO dogs (dog_name, isAdmin) VALUES ('%s', FALSE)" % (dog_name)], []) |
| 60 | + conn.commit() |
| 61 | + cursor.close() |
| 62 | + conn.close() |
| 63 | + return f'Dog {dog_name} created successfully' |
| 64 | + |
| 65 | +@app.route("/create_with_cookie", methods=['GET']) |
| 66 | +def create_dog_with_cookie(): |
| 67 | + dog_name = request.cookies.get('dog_name') |
| 68 | + |
| 69 | + conn = get_db_connection() |
| 70 | + cursor = conn.cursor() |
| 71 | + cursor.execute(f"INSERT INTO dogs (dog_name, isAdmin) VALUES ('%s', FALSE)" % (dog_name)) |
| 72 | + conn.commit() |
| 73 | + cursor.close() |
| 74 | + conn.close() |
| 75 | + return f'Dog {dog_name} created successfully' |
0 commit comments