-
Notifications
You must be signed in to change notification settings - Fork 0
/
advertisement.py
68 lines (59 loc) · 2.08 KB
/
advertisement.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
from flask import Flask,render_template,request,redirect,url_for, g
import sqlite3
# comment
app = Flask(__name__, template_folder='templates')
app.config['DATABASE'] = 'ads.db'
def get_db():
db = getattr(g, '_database', None)
if db is None:
db = g._database = sqlite3.connect(app.config['DATABASE'])
return db
@app.teardown_appcontext
def close_connection(exception):
db = getattr(g, '_database', None)
if db is not None:
db.close()
@app.route('/')
def index():
conn = get_db()
cursor = conn.cursor()
cursor.execute("SELECT * FROM advertisements")
advertisements = cursor.fetchall()
cursor.close()
return render_template('advertisement.html', advertisements=advertisements)
@app.route('/add', methods=['POST'])
def add_advertisement():
conn = get_db()
cursor = conn.cursor()
title = request.form['title']
description = request.form['description']
status = request.form['status']
price = request.form['price']
cursor.execute("INSERT INTO advertisements (title, description, status, price) VALUES (?, ?, ?, ?)", (title, description, status, price))
conn.commit()
cursor.close()
return redirect(url_for('index'))
@app.route('/edit', methods=['POST'])
def edit_advertisement():
conn = get_db()
cursor = conn.cursor()
advertisement_id = request.form['id']
title = request.form['title']
description = request.form['description']
status = request.form['status']
price = request.form['price']
cursor.execute("UPDATE advertisements SET title = ?, description = ?, status = ?, price = ? WHERE id = ?", (title, description, status, price, advertisement_id))
conn.commit()
cursor.close()
return redirect(url_for('index'))
@app.route('/delete', methods=['POST'])
def delete_advertisement():
conn = get_db()
cursor = conn.cursor()
advertisement_id = request.form['id']
cursor.execute("DELETE FROM advertisements WHERE id = ?", (advertisement_id,))
conn.commit()
cursor.close()
return redirect(url_for('index'))
if __name__ == '__main__':
app.run(debug=True)