hglbbvdvvsscvsv / admin.py
Echo-ai
Upload admin.py
3c3e573 verified
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'))
@admin.route('/')
@login_required
def index():
gangs = db.get_all_gangs()
blogs = db.get_all_blogs()
return render_template('admin/index.html', gangs=gangs, blogs=blogs)
@admin.route('/add-gang', methods=['GET', 'POST'])
@login_required
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')
@admin.route('/edit-gang/<int:gang_id>', methods=['GET', 'POST'])
@login_required
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)
@admin.route('/delete-gang/<int:gang_id>', methods=['POST'])
@login_required
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'))
@admin.route('/add-blog', methods=['GET', 'POST'])
@login_required
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')
@admin.route('/edit-blog/<int:blog_id>', methods=['GET', 'POST'])
@login_required
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)
@admin.route('/delete-blog/<int:blog_id>', methods=['POST'])
@login_required
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'))