107 lines
4.0 KiB
Python
107 lines
4.0 KiB
Python
from flask import Blueprint, render_template, request, redirect, url_for
|
|
import whois
|
|
from app.models import Domain
|
|
from app import db
|
|
from app.utils import get_ssl_expiration, get_whois_expiration
|
|
from datetime import datetime, timedelta
|
|
|
|
domain_bp = Blueprint('domain', __name__)
|
|
|
|
@domain_bp.route('/')
|
|
def home():
|
|
now = datetime.now()
|
|
three_months_later = now + timedelta(days=90)
|
|
all_domains = Domain.query.all()
|
|
domains_expiring = []
|
|
ssl_expiring = []
|
|
|
|
for d in all_domains:
|
|
# Check WHOIS expiration date (assumed format "mm/dd/yyyy")
|
|
try:
|
|
dt_whois = datetime.strptime(d.expiration_date, "%m/%d/%Y")
|
|
if now <= dt_whois <= three_months_later:
|
|
domains_expiring.append((dt_whois, d))
|
|
except Exception:
|
|
pass
|
|
|
|
# Check SSL expiration date (assumed format "mm/dd/yyyy")
|
|
try:
|
|
dt_ssl = datetime.strptime(d.ssl_expiration_date, "%m/%d/%Y")
|
|
if now <= dt_ssl <= three_months_later:
|
|
ssl_expiring.append((dt_ssl, d))
|
|
except Exception:
|
|
pass
|
|
|
|
# Sort lists in ascending order by date
|
|
domains_expiring.sort(key=lambda x: x[0])
|
|
ssl_expiring.sort(key=lambda x: x[0])
|
|
# Extract just the domain objects
|
|
domains_expiring = [item[1] for item in domains_expiring]
|
|
ssl_expiring = [item[1] for item in ssl_expiring]
|
|
|
|
return render_template('home.html',
|
|
domains_expiring=domains_expiring,
|
|
ssl_expiring=ssl_expiring)
|
|
|
|
@domain_bp.route('/domains')
|
|
def domains():
|
|
# Only show domains that were added normally (ssl_only is False)
|
|
domains_list = Domain.query.filter_by(ssl_only=False).all()
|
|
return render_template('domains.html', domains=domains_list)
|
|
|
|
@domain_bp.route('/add', methods=['GET', 'POST'])
|
|
def add_domain():
|
|
if request.method == 'POST':
|
|
domain_list = request.form.getlist('domain')
|
|
for domain_name in domain_list:
|
|
if domain_name.strip():
|
|
# WHOIS lookup
|
|
whois_exp_date_str = get_whois_expiration(domain_name)
|
|
# SSL lookup
|
|
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=False
|
|
)
|
|
db.session.add(new_domain)
|
|
db.session.commit()
|
|
return redirect(url_for('domain.domains'))
|
|
return render_template('add_domain.html')
|
|
|
|
@domain_bp.route('/delete/<int:domain_id>', methods=['POST'])
|
|
def delete_domain(domain_id):
|
|
domain_to_delete = Domain.query.get_or_404(domain_id)
|
|
db.session.delete(domain_to_delete)
|
|
db.session.commit()
|
|
return redirect(url_for('domain.domains'))
|
|
|
|
|
|
@domain_bp.route('/domains/refresh')
|
|
def refresh_domains():
|
|
# Refresh only non-SSL-only domains
|
|
domains_list = Domain.query.filter_by(ssl_only=False).all()
|
|
for d in domains_list:
|
|
d.expiration_date = get_whois_expiration(d.domain)
|
|
# Optionally update SSL expiration as well if desired:
|
|
d.ssl_expiration_date = get_ssl_expiration(d.domain)
|
|
db.session.commit()
|
|
return redirect(url_for('domain.domains'))
|
|
|
|
@domain_bp.route('/refresh/<int:domain_id>', methods=['GET'])
|
|
def refresh_individual(domain_id):
|
|
domain = Domain.query.get_or_404(domain_id)
|
|
from app.utils import get_whois_expiration, get_ssl_expiration
|
|
# Update WHOIS expiration date
|
|
domain.expiration_date = get_whois_expiration(domain.domain)
|
|
# Update SSL expiration date
|
|
domain.ssl_expiration_date = get_ssl_expiration(domain.domain)
|
|
db.session.commit()
|
|
# Redirect back to the appropriate page based on the domain type
|
|
if domain.ssl_only:
|
|
return redirect(url_for('ssl.ssl_page'))
|
|
else:
|
|
return redirect(url_for('domain.domains'))
|