-
Notifications
You must be signed in to change notification settings - Fork 0
/
app (copy 1).py
173 lines (160 loc) · 6.45 KB
/
app (copy 1).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
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
163
164
165
166
167
168
169
170
171
172
173
from flask import Flask, render_template, request, redirect, url_for, flash
import sqlite3
import os
import base64
app = Flask(__name__)
app.static_folder = 'static' # Set the static folder to 'static'
app.config['SECRET_KEY'] = 'your_secret_key'
DATABASE = 'instance/blog.db'
# Initialize SQLite database if not exists
def init_db():
with sqlite3.connect(DATABASE) as conn:
cursor = conn.cursor()
cursor.execute('''
CREATE TABLE IF NOT EXISTS post (
id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT NOT NULL,
content TEXT NOT NULL,
image BLOB
)
''')
conn.commit()
# Function to fetch all posts
'''
def get_posts(limit=None):
with sqlite3.connect(DATABASE) as conn:
cursor = conn.cursor()
if limit:
cursor.execute('SELECT * FROM post ORDER BY id DESC LIMIT ?', (limit,))
else:
cursor.execute('SELECT * FROM post ORDER BY id DESC')
posts = cursor.fetchall()
return posts
# Function to fetch a single post by ID
def get_post(post_id):
with sqlite3.connect(DATABASE) as conn:
cursor = conn.cursor()
cursor.execute('SELECT * FROM post WHERE id = ?', (post_id,))
post = cursor.fetchone()
return post
'''
@app.route('/')
def home():
posts = get_posts(limit=4) # Limit to last 4 posts
return render_template('home.html', posts=posts)
@app.route('/post/<int:post_id>')
def post(post_id):
post = get_post(post_id)
if post:
return render_template('post.html', post=post)
else:
flash('Post not found!', 'error')
return redirect(url_for('home'))
@app.route('/new', methods=['GET', 'POST'])
def new_post():
if request.method == 'POST':
title = request.form['title']
content = request.form['content']
image = request.files['image'].read() if 'image' in request.files and request.files['image'].filename != '' else None
if image:
image = base64.b64encode(image)
with sqlite3.connect(DATABASE) as conn:
cursor = conn.cursor()
cursor.execute('INSERT INTO post (title, content, image) VALUES (?, ?, ?)', (title, content, image))
conn.commit()
flash('Post created successfully!', 'success')
return redirect(url_for('home'))
return render_template('new_post.html')
@app.route('/edit/<int:post_id>', methods=['GET', 'POST'])
def edit_post(post_id):
post = get_post(post_id)
if request.method == 'POST':
title = request.form['title']
content = request.form['content']
image = request.files['image'].read() if 'image' in request.files and request.files['image'].filename != '' else None
if image:
image = base64.b64encode(image)
with sqlite3.connect(DATABASE) as conn:
cursor = conn.cursor()
cursor.execute('UPDATE post SET title = ?, content = ?, image = ? WHERE id = ?', (title, content, image, post_id))
conn.commit()
flash('Post updated successfully!', 'success')
return redirect(url_for('post', post_id=post_id))
return render_template('edit_post.html', post=post)
@app.route('/contents')
def contents():
posts = get_posts()
contents_data = []
for post in posts:
excerpt = post[2][:300] + '...' if len(post[2]) > 300 else post[2] # Assuming content is in the third column (index 2)
contents_data.append({
'id': post[0],
'title': post[1],
'excerpt': excerpt
})
return render_template('contents.html', contents_data=contents_data)
@app.route('/delete/<int:post_id>', methods=['POST'])
def delete_post(post_id):
with sqlite3.connect(DATABASE) as conn:
cursor = conn.cursor()
cursor.execute('DELETE FROM post WHERE id = ?', (post_id,))
conn.commit()
flash('Post deleted successfully!', 'success')
return redirect(url_for('home'))
def load_txt_files(directory):
init_db() # Initialize the SQLite database if not already created
conn = sqlite3.connect(DATABASE)
cursor = conn.cursor()
try:
for filename in os.listdir(directory):
if filename.endswith('.txt'):
filepath = os.path.join(directory, filename)
with open(filepath, 'r', encoding='utf-8') as file:
title = os.path.splitext(filename)[0]
content = file.read()
cursor.execute('SELECT id FROM post WHERE title = ?', (title,))
existing_post = cursor.fetchone()
if not existing_post:
cursor.execute('INSERT INTO post (title, content) VALUES (?, ?)', (title, content))
conn.commit()
print(f'Added post: {title}')
else:
print(f'Skipped existing post: {title}')
except sqlite3.Error as e:
print(f'SQLite error: {e}')
finally:
conn.close()
@app.route('/search', methods=['GET', 'POST'])
def search():
if request.method == 'POST':
search_terms = request.form['search_terms'].split(",")
search_conditions = [f"content LIKE '%{term.strip()}%'" for term in search_terms]
query = 'SELECT * FROM post WHERE ' + ' AND '.join(search_conditions)
with sqlite3.connect(DATABASE) as conn:
cursor = conn.cursor()
cursor.execute(query)
results = cursor.fetchall()
return render_template('search_results.html', results=results, search_terms=search_terms)
return redirect(url_for('home'))
# Function to fetch a single post by ID
def get_post(post_id):
with sqlite3.connect(DATABASE) as conn:
cursor = conn.cursor()
cursor.execute('SELECT id, title, content, image FROM post WHERE id = ?', (post_id,))
post = cursor.fetchone()
return post
# Function to fetch all posts
def get_posts(limit=None):
with sqlite3.connect(DATABASE) as conn:
cursor = conn.cursor()
if limit:
cursor.execute('SELECT id, title, content, image FROM post ORDER BY id DESC LIMIT ?', (limit,))
else:
cursor.execute('SELECT id, title, content, image FROM post ORDER BY id DESC')
posts = cursor.fetchall()
return posts
if __name__ == "__main__":
with app.app_context():
directory = 'static/TEXT' # Replace with your directory path containing .txt files
load_txt_files(directory)
app.run(debug=True)