-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
162 lines (137 loc) · 5.14 KB
/
app.py
File metadata and controls
162 lines (137 loc) · 5.14 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import sqlite3
import uuid
from datetime import datetime, timezone
from flask import Flask, render_template, request, url_for, flash, redirect
from werkzeug.exceptions import abort
app = Flask(__name__)
app.config['SECRET_KEY'] = 'WYSI'
class Post:
def __init__(self):
self.conn = None
def get_db_connection(self):
self.conn = sqlite3.connect('database.db')
self.conn.row_factory = sqlite3.Row # Returns results with column names
def execute(self, query, tuple, fetch_type):
self.get_db_connection()
if tuple:
temp_result = self.conn.execute(query,tuple)
else:
temp_result = self.conn.execute(query)
result = None
if fetch_type == "one":
result = temp_result.fetchone()
elif fetch_type == "all":
result = temp_result.fetchall()
else:
self.conn.commit()
self.conn.close()
return result
def record_changes(self, change):
fetch_type = None
query = 'INSERT INTO changes (id, title, content, change_made) VALUES (?, ?, ?, ?)'
self.execute(query, change, fetch_type)
def get_post(self, post_id):
query = 'SELECT * FROM posts WHERE id = ?'
tuple = (post_id,)
fetch_type = "one"
post = self.execute(query, tuple, fetch_type)
if post is None:
abort(404)
return post
def get_all_posts(self):
query = 'SELECT * FROM posts'
tuple = None
fetch_type = "all"
posts = self.execute(query, tuple, fetch_type)
return posts
def get_all_changes(self):
query = 'SELECT * FROM changes'
tuple = None
fetch_type = "all"
changes = self.execute(query, tuple, fetch_type)
return changes
def create_post(self, title, content):
query = 'INSERT INTO posts (id, title, content) VALUES (?, ?, ?)'
id = str(uuid.uuid4())
tuple = (id, title, content)
fetch_type = None
self.execute(query, tuple, fetch_type)
change_type = "Post Created: "
change = (id, title, content, change_type)
self.record_changes(change)
def edit_post(self, title, original_title, content, original_content, id):
query = 'UPDATE posts SET title = ?, updated_on = ?, content = ? WHERE id = ?'
current_time = datetime.now(timezone.utc)
updated_on = current_time.strftime('%Y-%m-%d %H:%M:%S')
tuple = (title, updated_on, content, id)
fetch_type = None
self.execute(query, tuple, fetch_type)
# Determine type based on whether the title and or content was changed
if title != original_title and original_content != content:
change_type = f"Post Title and Content Edited: {original_title} to "
elif title != original_title and original_content == content:
change_type = f"Post Title Edited: {original_title} to "
elif title == original_title and original_content != content:
change_type = "Post Content Edited: "
else:
change_type = "Post Edited, No Change: "
change = (id, title, content, change_type)
self.record_changes(change)
def del_post(self, title, content, id):
query = 'DELETE FROM posts WHERE id = ?'
tuple = (id, )
fetch_type = None
self.execute(query, tuple, fetch_type)
change_type = "Post Deleted: "
change = (id, title, content, change_type)
self.record_changes(change)
p = Post()
@app.route('/')
def index():
posts = p.get_all_posts()
#print(len(posts))
#print(posts[0])
return render_template('index.html', posts=posts)
#index()
@app.route('/<string:post_id>')
def post(post_id):
post = p.get_post(post_id)
return render_template('post.html', post=post)
@app.route('/create', methods=('GET', 'POST'))
def create():
if request.method == 'POST':
title = request.form['title']
content = request.form['content']
# Title required
if not title:
flash('Title is required!')
else:
p.create_post(title, content)
return redirect(url_for('index'))
return render_template('create.html')
@app.route('/<string:id>/edit', methods=('GET', 'POST'))
def edit(id):
post =p.get_post(id)
original_title = post[2]
original_content = post[3]
if request.method == 'POST':
title = request.form['title']
content = request.form['content']
if not title:
flash('Title is required!')
else:
p.edit_post(title, original_title, content, original_content, id,)
return redirect(url_for('index'))
return render_template('edit.html', post=post)
@app.route('/<string:id>/delete', methods=('POST',))
def delete(id):
post = p.get_post(id)
title = post['title']
content = post['content']
p.del_post(title, content, id)
flash('"{}" was successfully deleted!'.format(post['title']))
return redirect(url_for('index'))
@app.route('/audit')
def audit():
changes = p.get_all_changes()
return render_template('audit.html', changes=changes)