DomainExpiration/app/routes/tools.py

90 lines
3.7 KiB
Python

import json
from flask import Blueprint, render_template, request, redirect, url_for, flash, Response
from app.models import Domain, PasswordCategory
from app import db
tools_bp = Blueprint('tools', __name__)
@tools_bp.route('/tools', methods=['GET'])
def tools():
return render_template('tools.html')
@tools_bp.route('/tools/export', methods=['GET'])
def export_data():
domains = Domain.query.all()
data = []
for d in domains:
data.append({
'id': d.id,
'domain': d.domain,
'expiration_date': d.expiration_date,
'ssl_expiration_date': d.ssl_expiration_date,
'ssl_only': d.ssl_only
})
json_data = json.dumps(data)
response = Response(json_data, mimetype='application/json')
response.headers['Content-Disposition'] = 'attachment; filename=domains_data.json'
return response
@tools_bp.route('/tools/import', methods=['GET', 'POST'])
def import_data():
if request.method == 'POST':
file = request.files.get('file')
if file:
try:
data = json.load(file)
for record in data:
existing = Domain.query.filter_by(domain=record.get('domain')).first()
if not existing:
new_domain = Domain(
domain=record.get('domain'),
expiration_date=record.get('expiration_date'),
ssl_expiration_date=record.get('ssl_expiration_date'),
ssl_only=record.get('ssl_only', False)
)
db.session.add(new_domain)
db.session.commit()
flash("Data imported successfully", "success")
except Exception as e:
flash(f"Error importing data: {str(e)}", "danger")
else:
flash("No file uploaded", "warning")
return redirect(url_for('tools.tools'))
return render_template('tools.html')
@tools_bp.route('/tools/password_categories', methods=['GET', 'POST'])
def password_categories():
if request.method == 'POST':
category_name = request.form.get('category_name')
if category_name:
existing = PasswordCategory.query.filter_by(name=category_name.strip()).first()
if not existing:
new_cat = PasswordCategory(name=category_name.strip())
db.session.add(new_cat)
db.session.commit()
categories = PasswordCategory.query.all()
return render_template('password_categories.html', categories=categories)
@tools_bp.route('/tools/password_categories/delete/<int:category_id>', methods=['POST'])
def delete_password_category(category_id):
from app.models import ClientPassword, PasswordCategory
# Retrieve the category to be deleted
cat = PasswordCategory.query.get_or_404(category_id)
# Do not allow deletion of the default "General" category
if cat.name == "General":
flash("Cannot delete the default category.", "warning")
return redirect(url_for('tools.password_categories'))
# Find the default "General" category or create it if it doesn't exist
default_cat = PasswordCategory.query.filter_by(name="General").first()
if not default_cat:
default_cat = PasswordCategory(name="General")
db.session.add(default_cat)
db.session.commit()
# Reassign all passwords using the category to be deleted to "General"
ClientPassword.query.filter_by(category_id=cat.id).update({"category_id": default_cat.id})
# Delete the category
db.session.delete(cat)
db.session.commit()
flash("Category deleted and associated passwords reassigned to General.", "success")
return redirect(url_for('tools.password_categories'))