Spaces:
Sleeping
Sleeping
from flask import Blueprint, render_template, request, redirect, url_for, flash | |
from flask_login import login_required | |
from database import JSONDatabase | |
import os | |
admin = Blueprint('admin', __name__, url_prefix='/admin') | |
db = JSONDatabase(os.path.join(os.path.dirname(__file__), 'data')) | |
def index(): | |
gangs = db.get_all_gangs() | |
blogs = db.get_all_blogs() | |
return render_template('admin/index.html', gangs=gangs, blogs=blogs) | |
def add_gang(): | |
if request.method == 'POST': | |
gang_data = { | |
'name': request.form['name'], | |
'description': request.form['description'], | |
'known_members': request.form['known_members'], | |
'territory': request.form['territory'], | |
'known_activities': request.form['known_activities'], | |
'image_url': request.form['image_url'] | |
} | |
db.add_gang(gang_data) | |
flash('Gang added successfully!', 'success') | |
return redirect(url_for('admin.index')) | |
return render_template('admin/add_gang.html') | |
def edit_gang(gang_id): | |
gang = db.get_gang(gang_id) | |
if gang is None: | |
flash('Gang not found!', 'error') | |
return redirect(url_for('admin.index')) | |
if request.method == 'POST': | |
gang_data = { | |
'name': request.form['name'], | |
'description': request.form['description'], | |
'known_members': request.form['known_members'], | |
'territory': request.form['territory'], | |
'known_activities': request.form['known_activities'], | |
'image_url': request.form['image_url'] | |
} | |
db.update_gang(gang_id, gang_data) | |
flash('Gang updated successfully!', 'success') | |
return redirect(url_for('admin.index')) | |
# Convert lists back to newline-separated strings for form | |
gang['known_members'] = '\n'.join(gang['known_members']) | |
gang['territory'] = '\n'.join(gang['territory']) | |
gang['known_activities'] = '\n'.join(gang['known_activities']) | |
return render_template('admin/edit_gang.html', gang=gang) | |
def delete_gang(gang_id): | |
if db.delete_gang(gang_id): | |
flash('Gang deleted successfully!', 'success') | |
else: | |
flash('Gang not found!', 'error') | |
return redirect(url_for('admin.index')) | |
def add_blog(): | |
if request.method == 'POST': | |
blog_data = { | |
'title': request.form['title'], | |
'content': request.form['content'], | |
'image_url': request.form['image_url'] | |
} | |
db.add_blog(blog_data) | |
flash('Blog added successfully!', 'success') | |
return redirect(url_for('admin.index')) | |
return render_template('admin/add_blog.html') | |
def edit_blog(blog_id): | |
blog = db.get_blog(blog_id) | |
if blog is None: | |
flash('Blog not found!', 'error') | |
return redirect(url_for('admin.index')) | |
if request.method == 'POST': | |
blog_data = { | |
'title': request.form['title'], | |
'content': request.form['content'], | |
'image_url': request.form['image_url'] | |
} | |
db.update_blog(blog_id, blog_data) | |
flash('Blog updated successfully!', 'success') | |
return redirect(url_for('admin.index')) | |
return render_template('admin/edit_blog.html', blog=blog) | |
def delete_blog(blog_id): | |
if db.delete_blog(blog_id): | |
flash('Blog deleted successfully!', 'success') | |
else: | |
flash('Blog not found!', 'error') | |
return redirect(url_for('admin.index')) | |