128 lines
4.6 KiB
Python
128 lines
4.6 KiB
Python
from flask import Blueprint, request, jsonify, current_app
|
|
from flask_jwt_extended import jwt_required, get_jwt_identity
|
|
from datetime import datetime, date
|
|
from models import db, TodoUserPref
|
|
from utils.logger import get_logger
|
|
|
|
users_bp = Blueprint('users', __name__)
|
|
logger = get_logger(__name__)
|
|
|
|
@users_bp.route('/search', methods=['GET'])
|
|
@jwt_required()
|
|
def search_users():
|
|
"""Search for AD users"""
|
|
try:
|
|
search_term = request.args.get('q', '').strip()
|
|
|
|
if len(search_term) < 1:
|
|
return jsonify({'error': 'Search term cannot be empty'}), 400
|
|
|
|
# Search LDAP (or mock for development)
|
|
try:
|
|
if current_app.config.get('USE_MOCK_LDAP', False):
|
|
from utils.mock_ldap import search_ldap_principals
|
|
else:
|
|
from utils.ldap_utils import search_ldap_principals
|
|
|
|
results = search_ldap_principals(search_term, limit=20)
|
|
except Exception as e:
|
|
logger.error(f"LDAP search error, falling back to mock: {str(e)}")
|
|
from utils.mock_ldap import search_ldap_principals
|
|
results = search_ldap_principals(search_term, limit=20)
|
|
|
|
return jsonify({'users': results}), 200
|
|
|
|
except Exception as e:
|
|
logger.error(f"User search error: {str(e)}")
|
|
return jsonify({'error': 'Search failed'}), 500
|
|
|
|
@users_bp.route('/preferences', methods=['GET'])
|
|
@jwt_required()
|
|
def get_preferences():
|
|
"""Get user preferences"""
|
|
try:
|
|
identity = get_jwt_identity()
|
|
|
|
user_pref = TodoUserPref.query.filter_by(ad_account=identity).first()
|
|
if not user_pref:
|
|
return jsonify({'error': 'User preferences not found'}), 404
|
|
|
|
return jsonify(user_pref.to_dict()), 200
|
|
|
|
except Exception as e:
|
|
logger.error(f"Error fetching preferences: {str(e)}")
|
|
return jsonify({'error': 'Failed to fetch preferences'}), 500
|
|
|
|
@users_bp.route('/preferences', methods=['PATCH'])
|
|
@jwt_required()
|
|
def update_preferences():
|
|
"""Update user preferences"""
|
|
try:
|
|
identity = get_jwt_identity()
|
|
data = request.get_json()
|
|
|
|
user_pref = TodoUserPref.query.filter_by(ad_account=identity).first()
|
|
if not user_pref:
|
|
return jsonify({'error': 'User preferences not found'}), 404
|
|
|
|
# Update allowed fields
|
|
if 'theme' in data and data['theme'] in ['light', 'dark', 'auto']:
|
|
user_pref.theme = data['theme']
|
|
|
|
if 'language' in data:
|
|
user_pref.language = data['language']
|
|
|
|
if 'timezone' in data:
|
|
user_pref.timezone = data['timezone']
|
|
|
|
if 'notification_enabled' in data:
|
|
user_pref.notification_enabled = bool(data['notification_enabled'])
|
|
|
|
if 'email_reminder_enabled' in data:
|
|
user_pref.email_reminder_enabled = bool(data['email_reminder_enabled'])
|
|
|
|
if 'weekly_summary_enabled' in data:
|
|
user_pref.weekly_summary_enabled = bool(data['weekly_summary_enabled'])
|
|
|
|
|
|
user_pref.updated_at = datetime.utcnow()
|
|
db.session.commit()
|
|
|
|
logger.info(f"Preferences updated for user: {identity}")
|
|
return jsonify(user_pref.to_dict()), 200
|
|
|
|
except Exception as e:
|
|
db.session.rollback()
|
|
logger.error(f"Error updating preferences: {str(e)}")
|
|
return jsonify({'error': 'Failed to update preferences'}), 500
|
|
|
|
@users_bp.route('/fire-email-quota', methods=['GET'])
|
|
@jwt_required()
|
|
def get_fire_email_quota():
|
|
"""Get user's fire email quota for today"""
|
|
try:
|
|
identity = get_jwt_identity()
|
|
|
|
user_pref = TodoUserPref.query.filter_by(ad_account=identity).first()
|
|
if not user_pref:
|
|
return jsonify({'error': 'User not found'}), 404
|
|
|
|
# Reset counter if it's a new day
|
|
today = date.today()
|
|
if user_pref.fire_email_last_reset != today:
|
|
user_pref.fire_email_today_count = 0
|
|
user_pref.fire_email_last_reset = today
|
|
db.session.commit()
|
|
|
|
from flask import current_app
|
|
daily_limit = current_app.config['FIRE_EMAIL_DAILY_LIMIT']
|
|
|
|
return jsonify({
|
|
'used': user_pref.fire_email_today_count,
|
|
'limit': daily_limit,
|
|
'remaining': max(0, daily_limit - user_pref.fire_email_today_count)
|
|
}), 200
|
|
|
|
except Exception as e:
|
|
logger.error(f"Error fetching fire email quota: {str(e)}")
|
|
return jsonify({'error': 'Failed to fetch quota'}), 500 |