-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainlocal.py
More file actions
687 lines (589 loc) · 24.6 KB
/
Copy pathmainlocal.py
File metadata and controls
687 lines (589 loc) · 24.6 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
#!/usr/bin/env python3
"""
BlogSphere - Local JSON file storage version
Uses JSON files in the data folder instead of database
"""
import os
import re
import sys
import json
import logging
from datetime import datetime
from flask import Flask, render_template, request, redirect, url_for, flash, jsonify, send_from_directory
from flask_login import LoginManager, UserMixin, login_user, logout_user, login_required, current_user
from werkzeug.security import generate_password_hash, check_password_hash
from werkzeug.utils import secure_filename
# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# Configuration
class Config:
DATA_FOLDER = 'data'
SECRET_KEY = "secretkey"
UPLOAD_FOLDER = 'uploads'
MAX_CONTENT_LENGTH = 100 * 1024 * 1024 # 60MB
# Initialize Flask app
app = Flask(__name__)
config = Config()
app.secret_key = config.SECRET_KEY
app.config['UPLOAD_FOLDER'] = config.UPLOAD_FOLDER
app.config['MAX_CONTENT_LENGTH'] = config.MAX_CONTENT_LENGTH
# Create data and uploads directories
os.makedirs(config.DATA_FOLDER, exist_ok=True)
os.makedirs(config.UPLOAD_FOLDER, exist_ok=True)
# File storage helper functions
def load_json_file(filename):
"""Load data from JSON file"""
file_path = os.path.join(config.DATA_FOLDER, filename)
if os.path.exists(file_path):
try:
with open(file_path, 'r', encoding='utf-8') as f:
return json.load(f)
except (json.JSONDecodeError, Exception) as e:
logger.error(f"Error loading {filename}: {e}")
return []
return []
def save_json_file(filename, data):
"""Save data to JSON file"""
file_path = os.path.join(config.DATA_FOLDER, filename)
try:
with open(file_path, 'w', encoding='utf-8') as f:
json.dump(data, f, indent=2, default=str)
return True
except Exception as e:
logger.error(f"Error saving {filename}: {e}")
return False
def get_next_id(data_list):
"""Get the next available ID"""
if not data_list:
return 1
return max(item.get('id', 0) for item in data_list) + 1
# Simple User class for Flask-Login
class User(UserMixin):
def __init__(self, id, username, email, password_hash, is_admin=False, created_at=None):
self.id = id
self.username = username
self.email = email
self.password_hash = password_hash
self.is_admin = is_admin
self.created_at = created_at
# Login manager setup
login_manager = LoginManager()
login_manager.init_app(app)
login_manager.login_view = 'login' #type: ignore
@login_manager.user_loader
def load_user(user_id):
try:
users = load_json_file('Users.json')
user_data = next((u for u in users if u['id'] == int(user_id)), None)
if user_data:
return User(
user_data['id'],
user_data['username'],
user_data['email'],
user_data['password_hash'],
user_data.get('is_admin', False),
user_data.get('created_at')
)
return None
except Exception as e:
logger.error(f"Error loading user: {e}")
return None
@app.template_filter('autolink')
def autolink(text):
# Step 1: Replace newline characters with <br>
text = text.replace('\r\n', '<br>').replace('\n', '<br>').replace('\r', '<br>')
# Step 2: Replace URLs with clickable links (with optional custom text)
pattern = r'((?:https?://|http://|www\.)[^\s<]+)(?:\s+__([^_]+)__)?'
def replace(match):
url = match.group(1)
display_text = match.group(2) if match.group(2) else url
href = url if url.startswith('http') else f'https://{url}' # Add https for www.
return f'<a href="{href}" target="_blank" rel="noopener noreferrer">{display_text}</a>'
return re.sub(pattern, replace, text)
# Template filter for JSON
@app.template_filter('from_json')
def from_json_filter(value):
if value:
try:
if isinstance(value, str):
return json.loads(value)
return value
except:
return []
return []
# Template filter for datetime formatting
@app.template_filter('format_datetime')
def format_datetime(value):
if value:
try:
from datetime import datetime
if isinstance(value, str):
# Try to parse datetime string
dt = datetime.fromisoformat(value.replace('Z', '+00:00'))
return dt.strftime('%B %d, %Y at %I:%M %p')
elif hasattr(value, 'strftime'):
# It's already a datetime object
return value.strftime('%B %d, %Y at %I:%M %p')
else:
return str(value)
except (ValueError, AttributeError):
return str(value)
return 'No date'
# Helper functions
def allowed_file(filename):
ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg', 'gif', 'mp4', 'avi', 'mov', 'wmv', 'pdf', 'doc', 'docx', 'txt', 'ppt', 'pptx', 'xls', 'xlsx', 'sql', 'zip'}
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
def get_posts():
try:
posts = load_json_file('Posts.json')
users = load_json_file('Users.json')
likes = load_json_file('likes.json')
comments = load_json_file('Comments.json')
# Create user lookup
user_lookup = {u['id']: u for u in users}
# Add additional data to posts
enriched_posts = []
for post in posts:
# Get author info
author = user_lookup.get(post['user_id'], {})
# Count likes and comments
like_count = len([l for l in likes if l['post_id'] == post['id']])
comment_count = len([c for c in comments if c['post_id'] == post['id']])
enriched_post = {
'id': post['id'],
'user_id': post['user_id'],
'title': post['title'],
'content': post['content'],
'images': post.get('images'),
'videos': post.get('videos'),
'created_at': post['created_at'],
'author': {'username': author.get('username', 'Unknown')},
'like_count': like_count,
'comment_count': comment_count
}
enriched_posts.append(enriched_post)
# Sort by created_at (newest first)
enriched_posts.sort(key=lambda x: x['created_at'], reverse=True)
return enriched_posts
except Exception as e:
logger.error(f"Error getting posts: {e}")
return []
# Routes
@app.route('/')
def index():
posts = get_posts()
return render_template('index.html', posts=posts)
@app.route('/register', methods=['GET', 'POST'])
def register():
if request.method == 'POST':
username = request.form['username']
email = request.form['email']
password = request.form['password']
try:
users = load_json_file('Users.json')
# Check if username exists
if any(u['username'] == username for u in users):
flash('Username already exists', 'error')
return redirect(url_for('register'))
# Check if email exists
if any(u['email'] == email for u in users):
flash('Email already exists', 'error')
return redirect(url_for('register'))
# Create user
password_hash = generate_password_hash(password)
new_user = {
'id': get_next_id(users),
'username': username,
'email': email,
'password_hash': password_hash,
'is_admin': False,
'created_at': datetime.utcnow().isoformat()
}
users.append(new_user)
save_json_file('Users.json', users)
flash('Registration successful! Please login.', 'success')
return redirect(url_for('login'))
except Exception as e:
logger.error(f"Registration error: {e}")
flash('Registration failed. Please try again.', 'error')
return render_template('register.html')
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
username = request.form['username']
password = request.form['password']
try:
users = load_json_file('Users.json')
user_data = next((u for u in users if u['username'] == username), None)
if user_data and check_password_hash(user_data['password_hash'], password):
user = User(
user_data['id'],
user_data['username'],
user_data['email'],
user_data['password_hash'],
user_data.get('is_admin', False),
user_data.get('created_at')
)
login_user(user)
next_page = request.args.get('next')
return redirect(next_page) if next_page else redirect(url_for('index'))
else:
flash('Invalid username or password', 'error')
except Exception as e:
logger.error(f"Login error: {e}")
flash('Login failed. Please try again.', 'error')
return render_template('login.html')
@app.route('/logout')
@login_required
def logout():
logout_user()
flash('You have been logged out successfully.', 'info')
return redirect(url_for('index'))
@app.route('/create_post', methods=['GET', 'POST'])
@login_required
def create_post():
if request.method == 'POST':
title = request.form['title']
content = request.form['content']
images = []
videos = []
documents = []
if 'images' in request.files:
for file in request.files.getlist('images'):
if file and file.filename and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
images.append(filename)
if 'videos' in request.files:
for file in request.files.getlist('videos'):
if file and file.filename and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
videos.append(filename)
# Handle document uploads
if 'documents' in request.files:
for file in request.files.getlist('documents'):
if file and file.filename and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
documents.append(filename)
try:
posts = load_json_file('Posts.json')
new_post = {
'id': get_next_id(posts),
'user_id': current_user.id,
'title': title,
'content': content,
'images': images if images else None,
'videos': videos if videos else None,
'documents': documents if documents else None,
'created_at': datetime.utcnow().isoformat()
}
posts.append(new_post)
save_json_file('Posts.json', posts)
flash('Post created successfully!', 'success')
return redirect(url_for('index'))
except Exception as e:
logger.error(f"Create post error: {e}")
flash('Failed to create post. Please try again.', 'error')
return render_template('create_post.html')
@app.route('/post/<int:post_id>')
def post_detail(post_id):
try:
posts = load_json_file('Posts.json')
users = load_json_file('Users.json')
likes = load_json_file('likes.json')
comments = load_json_file('Comments.json')
# Find the post
post_data = next((p for p in posts if p['id'] == post_id), None)
if not post_data:
flash('Post not found', 'error')
return redirect(url_for('index'))
# Get author info
author = next((u for u in users if u['id'] == post_data['user_id']), {})
# Count likes and comments
like_count = len([l for l in likes if l['post_id'] == post_id])
comment_count = len([c for c in comments if c['post_id'] == post_id])
post = {
'id': post_data['id'],
'user_id': post_data['user_id'],
'title': post_data['title'],
'content': post_data['content'],
'images': post_data.get('images'),
'videos': post_data.get('videos'),
'documents': post_data.get('documents'),
'created_at': post_data['created_at'],
'author': {
'username': author.get('username', 'Unknown'),
'created_at': author.get('created_at')
},
'like_count': like_count,
'comment_count': comment_count
}
# Get comments with author info
post_comments = []
for comment in comments:
if comment['post_id'] == post_id:
comment_author = next((u for u in users if u['id'] == comment['user_id']), {})
post_comments.append({
'id': comment['id'],
'user_id': comment['user_id'],
'content': comment['content'],
'created_at': comment['created_at'],
'author': {'username': comment_author.get('username', 'Unknown')}
})
# Sort comments by created_at
post_comments.sort(key=lambda x: x['created_at'])
return render_template('post_detail.html', post=post, comments=post_comments)
except Exception as e:
logger.error(f"Post detail error: {e}")
import traceback
logger.error(f"Full traceback: {traceback.format_exc()}")
flash(f'Error loading post: {str(e)}', 'error')
return redirect(url_for('index'))
@app.route('/like/<int:post_id>', methods=['POST'])
@login_required
def like_post(post_id):
try:
likes = load_json_file('likes.json')
# Check if already liked
existing_like = next((l for l in likes if l['user_id'] == current_user.id and l['post_id'] == post_id), None)
if existing_like:
# Remove like
likes = [l for l in likes if not (l['user_id'] == current_user.id and l['post_id'] == post_id)]
else:
# Add like
new_like = {
'id': get_next_id(likes),
'user_id': current_user.id,
'post_id': post_id,
'created_at': datetime.utcnow().isoformat()
}
likes.append(new_like)
save_json_file('likes.json', likes)
# Redirect back to the post detail page
return redirect(url_for('post_detail', post_id=post_id))
except Exception as e:
logger.error(f"Like post error: {e}")
return jsonify({'error': 'Failed to like post'}), 500
@app.route('/comment/<int:post_id>', methods=['POST'])
@login_required
def add_comment(post_id):
content = request.form['content']
try:
comments = load_json_file('Comments.json')
new_comment = {
'id': get_next_id(comments),
'user_id': current_user.id,
'post_id': post_id,
'content': content,
'created_at': datetime.utcnow().isoformat()
}
comments.append(new_comment)
save_json_file('Comments.json', comments)
flash('Comment added successfully!', 'success')
except Exception as e:
logger.error(f"Add comment error: {e}")
flash('Failed to add comment', 'error')
return redirect(url_for('post_detail', post_id=post_id))
@app.route('/admin')
@login_required
def admin_dashboard():
if not current_user.is_admin:
flash('Access denied. Admin privileges required.', 'error')
return redirect(url_for('index'))
try:
users = load_json_file('Users.json')
posts = load_json_file('Posts.json')
comments = load_json_file('Comments.json')
likes = load_json_file('likes.json')
# Prepare user and post lookups
user_lookup = {u['id']: u for u in users}
post_lookup = {p['id']: p for p in posts}
# Enrich comments
enriched_comments = []
for c in comments:
enriched_comments.append({
'id': c['id'],
'user': user_lookup.get(c['user_id'], {'username': 'Unknown'}),
'post': post_lookup.get(c['post_id'], {'title': 'Unknown'}),
'content': c['content'],
'created_at': c['created_at']
})
# Enrich likes
enriched_likes = []
for l in likes:
enriched_likes.append({
'id': l['id'],
'user': user_lookup.get(l['user_id'], {'username': 'Unknown'}),
'post': post_lookup.get(l['post_id'], {'title': 'Unknown'}),
'created_at': l['created_at']
})
stats = {
'total_posts': len(posts),
'total_users': len(users),
'total_comments': len(comments),
'total_likes': len(likes)
}
return render_template('admin_dashboard.html', users=users, posts=get_posts(), stats=stats, comments=enriched_comments, likes=enriched_likes)
except Exception as e:
logger.error(f"Admin dashboard error: {e}")
flash('Error loading admin dashboard', 'error')
return redirect(url_for('index'))
@app.route('/admin/delete_post/<int:post_id>', methods=['POST'])
@login_required
def admin_delete_post(post_id):
if not current_user.is_admin:
flash('Access denied. Admin privileges required.', 'error')
return redirect(url_for('index'))
try:
# Delete likes related to this post
likes = load_json_file('likes.json')
likes = [l for l in likes if l['post_id'] != post_id]
save_json_file('likes.json', likes)
# Delete comments related to this post
comments = load_json_file('Comments.json')
comments = [c for c in comments if c['post_id'] != post_id]
save_json_file('Comments.json', comments)
# Delete the post
posts = load_json_file('Posts.json')
posts = [p for p in posts if p['id'] != post_id]
save_json_file('Posts.json', posts)
flash('Post deleted successfully!', 'success')
except Exception as e:
logger.error(f"Delete post error: {e}")
flash('Failed to delete post.', 'error')
return redirect(url_for('admin_dashboard'))
@app.route('/profile')
@login_required
def profile():
try:
posts = load_json_file('Posts.json')
likes = load_json_file('likes.json')
comments = load_json_file('Comments.json')
# Filter posts by current user
user_posts = []
for post in posts:
if post['user_id'] == current_user.id:
like_count = len([l for l in likes if l['post_id'] == post['id']])
comment_count = len([c for c in comments if c['post_id'] == post['id']])
user_posts.append({
'id': post['id'],
'title': post['title'],
'content': post['content'],
'images': post.get('images'),
'videos': post.get('videos'),
'created_at': post['created_at'],
'like_count': like_count,
'comment_count': comment_count
})
# Sort by created_at (newest first)
user_posts.sort(key=lambda x: x['created_at'], reverse=True)
return render_template('profile.html', posts=user_posts)
except Exception as e:
logger.error(f"Profile error: {e}")
flash('Error loading profile', 'error')
return redirect(url_for('index'))
@app.route('/uploads/<filename>')
def uploaded_file(filename):
return send_from_directory(app.config['UPLOAD_FOLDER'], filename)
@app.route('/admin/comments')
@login_required
def admin_comments():
if not current_user.is_admin:
flash('Access denied. Admin privileges required.', 'error')
return redirect(url_for('index'))
comments = load_json_file('Comments.json')
users = load_json_file('Users.json')
posts = load_json_file('Posts.json')
user_lookup = {u['id']: u for u in users}
post_lookup = {p['id']: p for p in posts}
# Enrich comments
enriched_comments = []
for c in comments:
enriched_comments.append({
'id': c['id'],
'user': user_lookup.get(c['user_id'], {'username': 'Unknown'}),
'post': post_lookup.get(c['post_id'], {'title': 'Unknown'}),
'content': c['content'],
'created_at': c['created_at']
})
return render_template('admin_comments.html', comments=enriched_comments)
@app.route('/admin/delete_comment/<int:comment_id>', methods=['POST'])
@login_required
def admin_delete_comment(comment_id):
if not current_user.is_admin:
flash('Access denied. Admin privileges required.', 'error')
return redirect(url_for('index'))
comments = load_json_file('Comments.json')
comments = [c for c in comments if c['id'] != comment_id]
save_json_file('Comments.json', comments)
flash('Comment deleted successfully!', 'success')
return redirect(url_for('admin_comments'))
@app.route('/admin/likes')
@login_required
def admin_likes():
if not current_user.is_admin:
flash('Access denied. Admin privileges required.', 'error')
return redirect(url_for('index'))
likes = load_json_file('likes.json')
users = load_json_file('Users.json')
posts = load_json_file('Posts.json')
user_lookup = {u['id']: u for u in users}
post_lookup = {p['id']: p for p in posts}
enriched_likes = []
for l in likes:
enriched_likes.append({
'id': l['id'],
'user': user_lookup.get(l['user_id'], {'username': 'Unknown'}),
'post': post_lookup.get(l['post_id'], {'title': 'Unknown'}),
'created_at': l['created_at']
})
return render_template('admin_likes.html', likes=enriched_likes)
@app.route('/admin/delete_like/<int:like_id>', methods=['POST'])
@login_required
def admin_delete_like(like_id):
if not current_user.is_admin:
flash('Access denied. Admin privileges required.', 'error')
return redirect(url_for('index'))
likes = load_json_file('likes.json')
likes = [l for l in likes if l['id'] != like_id]
save_json_file('likes.json', likes)
flash('Like removed successfully!', 'success')
return redirect(url_for('admin_likes'))
def initialize_data():
"""Initialize data files and create admin user"""
try:
# Initialize empty files if they don't exist
for filename in ['Users.json', 'Posts.json', 'Comments.json', 'likes.json']:
file_path = os.path.join(config.DATA_FOLDER, filename)
if not os.path.exists(file_path):
save_json_file(filename, [])
# Check if admin user exists
users = load_json_file('Users.json')
if not any(u['username'] == 'admin' for u in users):
password_hash = generate_password_hash('admin123')
admin_user = {
'id': get_next_id(users),
'username': 'admin',
'email': 'admin@example.com',
'password_hash': password_hash,
'is_admin': True,
'created_at': datetime.utcnow().isoformat()
}
users.append(admin_user)
save_json_file('Users.json', users)
logger.info('Admin user created: username=admin, password=admin123')
logger.info('Data files initialized successfully')
except Exception as e:
logger.error(f"Data initialization error: {e}")
raise
if __name__ == '__main__':
try:
initialize_data()
logger.info('Starting BlogSphere application...')
app.run(host='0.0.0.0', port=5000, debug=True)
except Exception as e:
logger.error(f'Failed to start application: {e}')
sys.exit(1)