This commit is contained in:
+174
@@ -1166,6 +1166,157 @@ def api_category_details(category_id):
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
@bp.route('/api/categories', methods=['POST'])
|
||||
@login_required
|
||||
def api_create_category():
|
||||
"""Создает новую категорию"""
|
||||
db = get_db()
|
||||
try:
|
||||
data = request.json
|
||||
with db.conn.cursor() as cursor:
|
||||
cursor.execute("""
|
||||
INSERT INTO categories (ID, TITLE, description, image_url, tg_id, marked_for_publication)
|
||||
VALUES (%s, %s, %s, %s, %s, %s)
|
||||
""", (
|
||||
data.get('id'),
|
||||
data.get('title', ''),
|
||||
data.get('description', ''),
|
||||
data.get('image_url', ''),
|
||||
data.get('tg_id'),
|
||||
bool(data.get('marked_for_publication', False))
|
||||
))
|
||||
db.conn.commit()
|
||||
client_ip = get_client_ip()
|
||||
log_event(f"Создана площадка: {data.get('id')}, IP {client_ip}")
|
||||
return jsonify({'success': True})
|
||||
except Exception as e:
|
||||
return jsonify({'success': False, 'error': str(e)}), 500
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
@bp.route('/api/categories/<category_id>', methods=['PUT'])
|
||||
@login_required
|
||||
def api_update_category(category_id):
|
||||
"""Обновляет категорию"""
|
||||
db = get_db()
|
||||
try:
|
||||
data = request.json
|
||||
with db.conn.cursor() as cursor:
|
||||
cursor.execute("""
|
||||
UPDATE categories
|
||||
SET TITLE = %s, description = %s, image_url = %s, tg_id = %s, marked_for_publication = %s
|
||||
WHERE ID = %s
|
||||
""", (
|
||||
data.get('title', ''),
|
||||
data.get('description', ''),
|
||||
data.get('image_url', ''),
|
||||
data.get('tg_id'),
|
||||
bool(data.get('marked_for_publication', False)),
|
||||
category_id
|
||||
))
|
||||
db.conn.commit()
|
||||
if cursor.rowcount > 0:
|
||||
client_ip = get_client_ip()
|
||||
log_event(f"Обновлена площадка: {category_id}, IP {client_ip}")
|
||||
return jsonify({'success': True})
|
||||
else:
|
||||
return jsonify({'success': False, 'error': 'Category not found'}), 404
|
||||
except Exception as e:
|
||||
return jsonify({'success': False, 'error': str(e)}), 500
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
@bp.route('/api/categories/<category_id>', methods=['DELETE'])
|
||||
@login_required
|
||||
def api_delete_category(category_id):
|
||||
"""Удаляет категорию"""
|
||||
db = get_db()
|
||||
try:
|
||||
with db.conn.cursor() as cursor:
|
||||
# Проверяем наличие привязанных событий
|
||||
cursor.execute("""
|
||||
SELECT COUNT(*) as event_count
|
||||
FROM events
|
||||
WHERE unit_name = %s
|
||||
""", (category_id,))
|
||||
event_count_result = cursor.fetchone()
|
||||
event_count = event_count_result['event_count'] if event_count_result else 0
|
||||
|
||||
if event_count > 0:
|
||||
return jsonify({
|
||||
'success': False,
|
||||
'error': 'Cannot delete category with linked events',
|
||||
'event_count': event_count
|
||||
}), 400
|
||||
|
||||
# Получаем название категории для лога
|
||||
cursor.execute("SELECT TITLE FROM categories WHERE ID = %s", (category_id,))
|
||||
category = cursor.fetchone()
|
||||
category_title = category['TITLE'] if category else category_id
|
||||
|
||||
cursor.execute("DELETE FROM categories WHERE ID = %s", (category_id,))
|
||||
db.conn.commit()
|
||||
|
||||
if cursor.rowcount > 0:
|
||||
client_ip = get_client_ip()
|
||||
log_event(f"Удалена площадка: {category_title}, ID: {category_id}, IP {client_ip}")
|
||||
return jsonify({'success': True})
|
||||
else:
|
||||
return jsonify({'success': False, 'error': 'Category not found'}), 404
|
||||
except Exception as e:
|
||||
return jsonify({'success': False, 'error': str(e)}), 500
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
@bp.route('/api/categories/<category_id>/toggle_publication', methods=['POST'])
|
||||
@login_required
|
||||
def api_toggle_category_publication(category_id):
|
||||
"""Переключает статус публикации категории"""
|
||||
db = get_db()
|
||||
try:
|
||||
with db.conn.cursor() as cursor:
|
||||
# Получаем текущее значение
|
||||
cursor.execute("SELECT marked_for_publication FROM categories WHERE ID = %s", (category_id,))
|
||||
category = cursor.fetchone()
|
||||
if not category:
|
||||
return jsonify({'success': False, 'error': 'Category not found'}), 404
|
||||
|
||||
# Переключаем значение
|
||||
new_value = not bool(category['marked_for_publication'])
|
||||
cursor.execute("""
|
||||
UPDATE categories
|
||||
SET marked_for_publication = %s
|
||||
WHERE ID = %s
|
||||
""", (new_value, category_id))
|
||||
db.conn.commit()
|
||||
|
||||
client_ip = get_client_ip()
|
||||
log_event(f"Изменен статус публикации площадки: {category_id}, новый статус: {new_value}, IP {client_ip}")
|
||||
return jsonify({'success': True, 'marked_for_publication': new_value})
|
||||
except Exception as e:
|
||||
return jsonify({'success': False, 'error': str(e)}), 500
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
@bp.route('/api/categories/<category_id>/events_count')
|
||||
@login_required
|
||||
def api_category_events_count(category_id):
|
||||
"""Получает количество событий, привязанных к категории"""
|
||||
db = get_db()
|
||||
try:
|
||||
with db.conn.cursor() as cursor:
|
||||
cursor.execute("""
|
||||
SELECT COUNT(*) as event_count
|
||||
FROM events
|
||||
WHERE unit_name = %s
|
||||
""", (category_id,))
|
||||
result = cursor.fetchone()
|
||||
return jsonify({'count': result['event_count'] if result else 0})
|
||||
except Exception as e:
|
||||
return jsonify({'error': str(e)}), 500
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
@bp.route('/api/events/count')
|
||||
@login_required
|
||||
def api_events_count():
|
||||
@@ -1599,6 +1750,29 @@ def edit_event_page(event_id):
|
||||
def add_event_page():
|
||||
return render_template('edit_event.html', event_id=None)
|
||||
|
||||
@bp.route('/edit_category/<category_id>')
|
||||
@login_required
|
||||
def edit_category_page(category_id):
|
||||
db = get_db()
|
||||
try:
|
||||
with db.conn.cursor() as cursor:
|
||||
cursor.execute("SELECT ID, TITLE FROM categories WHERE ID = %s", (category_id,))
|
||||
category = cursor.fetchone()
|
||||
if not category:
|
||||
flash('Площадка не найдена', 'danger')
|
||||
return redirect(url_for('zilant.index'))
|
||||
|
||||
client_ip = get_client_ip()
|
||||
log_event(f"Открыта площадка на редактирование: {category['TITLE']}, ID: {category_id}, IP {client_ip}")
|
||||
finally:
|
||||
db.close()
|
||||
return render_template('edit_category.html', category_id=category_id)
|
||||
|
||||
@bp.route('/add_category')
|
||||
@login_required
|
||||
def add_category_page():
|
||||
return render_template('edit_category.html', category_id=None)
|
||||
|
||||
# Регистрируем Blueprint в приложении
|
||||
app.register_blueprint(bp)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user