Telezab/region_api.py
Влад Зверев 21834d7d71 Massive rework of menu,
Add endpoint telezab/users
Add endpoint telezab/users/add
Add endpoint telezab/users/del
Add endpoint telezab/users/get
Add endpoint telezab/regions
Add endpoint telezab/regions/add
Add endpoint telezab/regions/del
Add endpoint telezab/regions/get
Rework Active Triggers button now don't need subscription
Rework Help button
Add option to change what Notification type you want reciving All or Disaster Only
Rework Settings button removed some misc buttons
Rework Registration mechanism now using POST JSON users/add
Rework formating of Zabbix Triggers for Active triggers and Notification from Zabbix
2024-09-18 20:12:29 +05:00

73 lines
3.3 KiB
Python

# region_api.py
import sqlite3
from threading import Lock
db_lock = Lock()
class RegionAPI:
def __init__(self, db_path):
self.db_path = db_path
def add_region(self, region_id: int, region_name: str):
if not region_id.isdigit() == True:
return {"status": "failure", "message": "Region_id must be digit only"}
with db_lock, sqlite3.connect(self.db_path) as conn:
cursor = conn.cursor()
cursor.execute('SELECT COUNT(*) FROM regions WHERE region_id = ?', (region_id,))
count = cursor.fetchone()[0]
if count == 0:
cursor.execute('INSERT INTO regions (region_id, region_name, active) VALUES (?, ?, 1)', (region_id, region_name))
conn.commit()
return {"status": "success", "message": "Region added successfully"}
else:
return {"status": "error", "message": "Region already exists"}
def remove_region(self, region_id):
with db_lock, sqlite3.connect(self.db_path) as conn:
cursor = conn.cursor()
cursor.execute('SELECT COUNT(*) FROM regions WHERE region_id = ?', (region_id,))
count = cursor.fetchone()[0]
if count == 0:
return {"status": "error", "message": "Region not found"}
else:
cursor.execute('DELETE FROM regions WHERE region_id = ?', (region_id,))
conn.commit()
return {"status": "success", "message": "Region removed successfully"}
def get_regions(self):
with db_lock, sqlite3.connect(self.db_path) as conn:
cursor = conn.cursor()
cursor.execute('SELECT region_id, region_name, active FROM regions')
regions = cursor.fetchall()
return [{"region_id": r[0], "region_name": r[1], "regions_active": r[2]} for r in regions]
def change_region_status(self, region_id, active):
with db_lock, sqlite3.connect(self.db_path) as conn:
cursor = conn.cursor()
cursor.execute('SELECT COUNT(*) FROM regions WHERE region_id = ?', (region_id,))
count = cursor.fetchone()[0]
if count == 0:
return {"status": "error", "message": "Region not found"}
else:
cursor.execute('UPDATE regions SET active = ? WHERE region_id = ?', (active, region_id))
conn.commit()
return {"status": "success", "message": "Region status updated successfully"}
def update_region_status(self, region_id, active):
with db_lock, sqlite3.connect(self.db_path) as conn:
cursor = conn.cursor()
# Проверяем существование региона
cursor.execute("SELECT region_name FROM regions WHERE region_id = ?", (region_id,))
result = cursor.fetchone()
if not result:
return {"status": "error", "message": "Регион не найден"}
# Обновляем статус активности региона
cursor.execute("UPDATE regions SET active = ? WHERE region_id = ?", (int(active), region_id))
conn.commit()
action = "Активирован" if active else "Отключён"
return {"status": "success", "message": f"Регион {region_id} {action}"}