Spaces:
Sleeping
Sleeping
Download projects/admin_tools_bp.py from alfarouqwebserver/SWS-Smarta: direct link, hf CLI and curl.
- Browser
- Download file 4.77 kB
-
https://huggingface.co/spaces/alfarouqwebserver/SWS-Smarta/resolve/main/projects/admin_tools_bp.py
- Command line
-
hf download hf://spaces/alfarouqwebserver/SWS-Smarta/projects/admin_tools_bp.py
-
curl -L -o admin_tools_bp.py https://huggingface.co/spaces/alfarouqwebserver/SWS-Smarta/resolve/main/projects/admin_tools_bp.py
4.77 kB
| import psutil | |
| import os | |
| from flask import Blueprint, render_template, request, jsonify, flash, redirect, url_for | |
| from flask_login import login_required, current_user | |
| from extensions import db, socketio, bcrypt | |
| from models import SystemLog, User | |
| from datetime import datetime | |
| admin_tools_bp = Blueprint('admin_tools', __name__, url_prefix='/admin_tools') | |
| # --- 1. لوحة تحكم الأدوات (Dashboard) --- | |
| def admin_dashboard(): | |
| if current_user.role != 'server_admin': | |
| flash('غير مسموح لك بدخول هذه المنطقة.', 'danger') | |
| return redirect(url_for('home')) | |
| # جلب آخر 100 سجل بترتيب تنازلي (الأحدث أولاً) | |
| logs = SystemLog.query.order_by(SystemLog.timestamp.desc()).limit(100).all() | |
| return render_template('admin_tools_dashboard.html', logs=logs) | |
| # --- 2. API: مراقبة الهاردوير (CPU, RAM) --- | |
| # يتم استدعاؤها بواسطة JavaScript في admin_tools_dashboard.html | |
| def hardware_stats(): | |
| if current_user.role != 'server_admin': | |
| return jsonify({'cpu': 0, 'ram': 0, 'disk': 0}) | |
| try: | |
| # psutil: مكتبة قراءة موارد النظام | |
| cpu = psutil.cpu_percent(interval=None) | |
| ram = psutil.virtual_memory().percent | |
| disk = psutil.disk_usage(os.path.abspath(os.sep)).percent | |
| except Exception as e: | |
| print(f"Hardware Monitor Error: {e}") | |
| cpu, ram, disk = 0, 0, 0 | |
| return jsonify({ | |
| 'cpu': cpu, | |
| 'ram': ram, | |
| 'disk': disk | |
| }) | |
| # --- 3. Action: إرسال إشعار عام (Broadcast) --- | |
| def send_broadcast(): | |
| if current_user.role != 'server_admin': | |
| return jsonify({'status': 'error', 'message': 'Unauthorized'}), 403 | |
| msg = request.form.get('message') | |
| if msg: | |
| # إرسال لكل المتصلين عبر SocketIO | |
| # المتصفح يستقبلها عبر: socket.on('server_broadcast', ...) | |
| socketio.emit('server_broadcast', {'message': msg, 'sender': current_user.username}) | |
| # تسجيل العملية | |
| log = SystemLog(level='INFO', message=f'Broadcast sent by {current_user.username}: {msg}') | |
| db.session.add(log) | |
| db.session.commit() | |
| return jsonify({'status': 'sent'}) | |
| return jsonify({'status': 'error', 'message': 'Empty message'}), 400 | |
| # --- 4. Action: مسح السجلات (Clear Logs) --- | |
| def clear_logs(): | |
| if current_user.role != 'server_admin': | |
| flash('صلاحيات غير كافية.', 'danger') | |
| return redirect(url_for('admin_tools.admin_dashboard')) | |
| try: | |
| # حذف جميع السجلات | |
| db.session.query(SystemLog).delete() | |
| # إضافة سجل جديد يوضح من قام بالحذف | |
| new_log = SystemLog(level='WARNING', message=f'All system logs cleared by {current_user.username}') | |
| db.session.add(new_log) | |
| db.session.commit() | |
| flash('تم مسح جميع السجلات بنجاح.', 'success') | |
| except Exception as e: | |
| db.session.rollback() | |
| flash(f'حدث خطأ أثناء المسح: {str(e)}', 'danger') | |
| return redirect(url_for('admin_tools.admin_dashboard')) | |
| # --- 5. Action: تحديث الحساب السريع (من الداشبورد الرئيسية) --- | |
| def update_profile(): | |
| new_password = request.form.get('new_password') | |
| if new_password: | |
| try: | |
| # تحديث كلمة المرور وتشفيرها | |
| current_user.password = bcrypt.generate_password_hash(new_password).decode('utf-8') | |
| db.session.commit() | |
| # تسجيل العملية | |
| db.session.add(SystemLog(level='INFO', message=f'User {current_user.username} changed password via Dashboard.')) | |
| db.session.commit() | |
| flash('تم تحديث كلمة المرور بنجاح.', 'success') | |
| except Exception as e: | |
| db.session.rollback() | |
| flash('حدث خطأ أثناء التحديث.', 'danger') | |
| else: | |
| flash('يرجى كتابة كلمة مرور جديدة.', 'warning') | |
| # إعادة التوجيه للصفحة التي جاء منها الطلب (عادة dashboard) | |
| return redirect(request.referrer or url_for('home')) | |