DomainExpiration/app/routes/ssl.py

52 lines
2.1 KiB
Python

from flask import Blueprint, render_template, request, redirect, url_for
from app.models import Domain
from app import db
from app.utils import get_ssl_expiration
ssl_bp = Blueprint('ssl', __name__)
@ssl_bp.route('/ssl')
def ssl_page():
# Get all domains (both normal and ssl_only)
domains = Domain.query.all()
# Update each domain's SSL expiration (optional, to refresh if needed)
for d in domains:
current_ssl_exp = get_ssl_expiration(d.domain)
if d.ssl_expiration_date != current_ssl_exp:
d.ssl_expiration_date = current_ssl_exp
db.session.commit()
return render_template('ssl.html', domains=domains)
@ssl_bp.route('/ssl/add', methods=['GET', 'POST'])
def add_ssl_domain():
if request.method == 'POST':
domain_list = request.form.getlist('domain')
for domain_name in domain_list:
if domain_name.strip():
# Both lookups are performed when adding an SSL-only domain.
try:
w = whois.whois(domain_name)
exp_date = w.expiration_date
if isinstance(exp_date, list):
exp_date = exp_date[0]
if exp_date and hasattr(exp_date, "strftime"):
whois_exp_date_str = exp_date.strftime("%m/%d/%Y")
else:
whois_exp_date_str = str(exp_date)
except Exception as e:
whois_exp_date_str = f"Error: {e}"
ssl_exp_date_str = get_ssl_expiration(domain_name)
if not Domain.query.filter_by(domain=domain_name).first():
new_domain = Domain(
domain=domain_name,
expiration_date=whois_exp_date_str,
ssl_expiration_date=ssl_exp_date_str,
ssl_only=True
)
db.session.add(new_domain)
db.session.commit()
return redirect(url_for('ssl.ssl_page'))
return render_template('add_ssl_domain.html')