82 lines
2.7 KiB
Python
82 lines
2.7 KiB
Python
from flask import Blueprint, render_template, request, redirect, url_for, flash, session, jsonify
|
|
from ..utils.db.operations import DatabaseManager
|
|
from ..utils.db.models import SubscriptionStatus
|
|
from datetime import datetime
|
|
import logging
|
|
|
|
logger = logging.getLogger(__name__)
|
|
user_bp = Blueprint('user', __name__)
|
|
|
|
@user_bp.route('/login', methods=['GET', 'POST'])
|
|
def login():
|
|
if request.method == 'POST':
|
|
user_id = request.form.get('user_id')
|
|
if not user_id:
|
|
return jsonify({'error': 'User ID is required'}), 400
|
|
|
|
user = DatabaseManager.get_user_by_uuid(user_id)
|
|
if user:
|
|
session['user_id'] = user_id
|
|
return jsonify({'redirect': url_for('user.dashboard')})
|
|
return jsonify({'error': 'Invalid User ID'}), 401
|
|
|
|
return render_template('user/login.html')
|
|
|
|
@user_bp.route('/dashboard')
|
|
def dashboard():
|
|
if 'user_id' not in session:
|
|
return redirect(url_for('user.login'))
|
|
|
|
user = DatabaseManager.get_user_by_uuid(session['user_id'])
|
|
if not user:
|
|
session.pop('user_id', None)
|
|
return redirect(url_for('user.login'))
|
|
|
|
subscriptions = DatabaseManager.get_user_subscriptions(user.id)
|
|
active_subscription = next(
|
|
(sub for sub in subscriptions
|
|
if sub.status == SubscriptionStatus.ACTIVE),
|
|
None
|
|
)
|
|
|
|
subscription_data = []
|
|
for sub in subscriptions:
|
|
subscription_data.append({
|
|
'id': sub.id,
|
|
'status': sub.status.value,
|
|
'start_time': sub.start_time,
|
|
'expiry_time': sub.expiry_time,
|
|
'assigned_ip': sub.assigned_ip
|
|
})
|
|
|
|
return render_template(
|
|
'user/dashboard.html',
|
|
user=user,
|
|
subscriptions=subscription_data,
|
|
active_subscription=active_subscription
|
|
)
|
|
|
|
@user_bp.route('/logout')
|
|
def logout():
|
|
session.pop('user_id', None)
|
|
return redirect(url_for('user.login'))
|
|
|
|
@user_bp.route('/api/subscription/config/<subscription_id>')
|
|
def get_subscription_config(subscription_id):
|
|
if 'user_id' not in session:
|
|
return jsonify({'error': 'Unauthorized'}), 401
|
|
|
|
user = DatabaseManager.get_user_by_uuid(session['user_id'])
|
|
if not user:
|
|
return jsonify({'error': 'User not found'}), 404
|
|
|
|
subscription = DatabaseManager.get_subscription_by_id(subscription_id)
|
|
if not subscription or subscription.user_id != user.id:
|
|
return jsonify({'error': 'Subscription not found'}), 404
|
|
|
|
return jsonify({
|
|
'config': subscription.config_data,
|
|
'assigned_ip': subscription.assigned_ip,
|
|
'expiry_time': subscription.expiry_time.isoformat()
|
|
})
|