-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Solomon Berhe
committed
Feb 5, 2025
1 parent
00e27e0
commit b7218e0
Showing
3 changed files
with
88 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
from flask import Flask, render_template, request, redirect | ||
import psycopg2 | ||
|
||
app = Flask(__name__) | ||
DATABASE_URL = "postgresql://neondb_owner:npg_M5sVheSzQLv4@ep-shrill-tree-a819xf7v-pooler.eastus2.azure.neon.tech/neondb?sslmode=require" | ||
|
||
def get_db_connection(): | ||
return psycopg2.connect(DATABASE_URL) | ||
|
||
@app.route('/') | ||
def index(): | ||
return ''' | ||
<h2>Flower Shop Management</h2> | ||
<button onclick="location.href='/flowers'">Manage Flowers</button> | ||
<button onclick="location.href='/customers'">Manage Customers</button> | ||
<button onclick="location.href='/orders'">Manage Orders</button> | ||
''' | ||
|
||
@app.route('/flowers') | ||
def manage_flowers(): | ||
conn = get_db_connection() | ||
cur = conn.cursor() | ||
cur.execute("SELECT * FROM flowers") | ||
flowers = cur.fetchall() | ||
cur.close() | ||
conn.close() | ||
return render_template('flowers.html', flowers=flowers) | ||
|
||
@app.route('/add_flower', methods=['POST']) | ||
def add_flower(): | ||
name = request.form['name'] | ||
color = request.form['color'] | ||
price = request.form['price'] | ||
stock = request.form['stock'] | ||
conn = get_db_connection() | ||
cur = conn.cursor() | ||
cur.execute("INSERT INTO flowers (name, color, price, stock) VALUES (%s, %s, %s, %s)", (name, color, price, stock)) | ||
conn.commit() | ||
cur.close() | ||
conn.close() | ||
return redirect('/flowers') | ||
|
||
@app.route('/delete_flower/<int:flower_id>') | ||
def delete_flower(flower_id): | ||
conn = get_db_connection() | ||
cur = conn.cursor() | ||
cur.execute("DELETE FROM flowers WHERE flower_id = %s", (flower_id,)) | ||
conn.commit() | ||
cur.close() | ||
conn.close() | ||
return redirect('/flowers') | ||
|
||
if __name__ == '__main__': | ||
app.run(debug=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>Flower Shop</title> | ||
</head> | ||
<body> | ||
<h2>Flower Management</h2> | ||
|
||
<form action="/add_flower" method="post"> | ||
<input type="text" name="name" placeholder="Flower Name" required> | ||
<input type="text" name="color" placeholder="Color"> | ||
<input type="number" name="price" placeholder="Price" required> | ||
<input type="number" name="stock" placeholder="Stock" required> | ||
<button type="submit">Add Flower</button> | ||
</form> | ||
|
||
<h3>Available Flowers</h3> | ||
<ul> | ||
{% for flower in flowers %} | ||
<li> | ||
{{ flower.name }} ({{ flower.color }}) - ${{ flower.price }} | Stock: {{ flower.stock }} | ||
<a href="/delete_flower/{{ flower.flower_id }}">Delete</a> | ||
</li> | ||
{% endfor %} | ||
</ul> | ||
</body> | ||
</html> |