wip
This commit is contained in:
parent
3b2f1db4ce
commit
5c16964b76
47 changed files with 2080 additions and 1053 deletions
BIN
scripts/__pycache__/check_routes.cpython-313-pytest-8.3.5.pyc
Normal file
BIN
scripts/__pycache__/check_routes.cpython-313-pytest-8.3.5.pyc
Normal file
Binary file not shown.
|
@ -9,79 +9,102 @@ import re
|
|||
from flask import Flask
|
||||
|
||||
# Add the parent directory to sys.path
|
||||
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
|
||||
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
|
||||
|
||||
|
||||
def find_all_routes():
|
||||
"""Find all route definitions in Python files"""
|
||||
routes = []
|
||||
route_pattern = re.compile(r'@\w+\.route\([\'"]([^\'"]+)[\'"]')
|
||||
|
||||
for root, _, files in os.walk('app'):
|
||||
|
||||
for root, _, files in os.walk("app"):
|
||||
for file in files:
|
||||
if file.endswith('.py'):
|
||||
if file.endswith(".py"):
|
||||
file_path = os.path.join(root, file)
|
||||
with open(file_path, 'r') as f:
|
||||
with open(file_path, "r") as f:
|
||||
content = f.read()
|
||||
matches = route_pattern.findall(content)
|
||||
for match in matches:
|
||||
routes.append(match)
|
||||
|
||||
|
||||
return routes
|
||||
|
||||
|
||||
def find_template_references():
|
||||
"""Find all url_for calls in template files"""
|
||||
references = []
|
||||
url_for_pattern = re.compile(r'url_for\([\'"]([^\'"]+)[\'"]')
|
||||
|
||||
for root, _, files in os.walk('app/templates'):
|
||||
|
||||
# Add patterns for direct links and HTMX references
|
||||
direct_href_pattern = re.compile(r'href=[\'"]([^\'"]+)[\'"]')
|
||||
htmx_pattern = re.compile(r'hx-(get|post|put|delete)=[\'"]([^\'"]+)[\'"]')
|
||||
|
||||
# Template files scanning
|
||||
for root, _, files in os.walk("app/templates"):
|
||||
for file in files:
|
||||
if file.endswith('.html'):
|
||||
if file.endswith(".html"):
|
||||
file_path = os.path.join(root, file)
|
||||
with open(file_path, 'r') as f:
|
||||
with open(file_path, "r") as f:
|
||||
content = f.read()
|
||||
# Find url_for references
|
||||
matches = url_for_pattern.findall(content)
|
||||
for match in matches:
|
||||
references.append(match)
|
||||
|
||||
|
||||
# Also check for direct route references in hrefs that aren't url_for
|
||||
href_matches = direct_href_pattern.findall(content)
|
||||
for href in href_matches:
|
||||
if href.startswith("/") and not href.startswith("//"):
|
||||
references.append(href)
|
||||
|
||||
# Check HTMX references
|
||||
htmx_matches = htmx_pattern.findall(content)
|
||||
for _, url in htmx_matches:
|
||||
if url.startswith("/") and not url.startswith("//"):
|
||||
references.append(url)
|
||||
|
||||
# Also check Python files for url_for calls
|
||||
for root, _, files in os.walk('app'):
|
||||
for root, _, files in os.walk("app"):
|
||||
for file in files:
|
||||
if file.endswith('.py'):
|
||||
if file.endswith(".py"):
|
||||
file_path = os.path.join(root, file)
|
||||
with open(file_path, 'r') as f:
|
||||
with open(file_path, "r") as f:
|
||||
content = f.read()
|
||||
matches = url_for_pattern.findall(content)
|
||||
for match in matches:
|
||||
references.append(match)
|
||||
|
||||
|
||||
return references
|
||||
|
||||
|
||||
def check_unused_routes():
|
||||
"""Find routes that are not referenced by url_for"""
|
||||
from app import create_app
|
||||
|
||||
|
||||
app = create_app()
|
||||
|
||||
|
||||
# Get all route endpoints from the app
|
||||
all_endpoints = set()
|
||||
for rule in app.url_map.iter_rules():
|
||||
all_endpoints.add(rule.endpoint)
|
||||
|
||||
|
||||
# Get all url_for references
|
||||
all_references = set(find_template_references())
|
||||
|
||||
|
||||
# Find unused endpoints
|
||||
unused_endpoints = all_endpoints - all_references
|
||||
|
||||
|
||||
if unused_endpoints:
|
||||
print("The following routes are defined but not referenced in templates or code:")
|
||||
print(
|
||||
"The following routes are defined but not referenced in templates or code:"
|
||||
)
|
||||
for endpoint in sorted(unused_endpoints):
|
||||
# Skip static routes, error handlers, etc.
|
||||
if endpoint.startswith('static') or endpoint == 'static':
|
||||
if endpoint.startswith("static") or endpoint == "static":
|
||||
continue
|
||||
|
||||
|
||||
print(f" - {endpoint}")
|
||||
|
||||
|
||||
# Find the URL for this endpoint
|
||||
for rule in app.url_map.iter_rules():
|
||||
if rule.endpoint == endpoint:
|
||||
|
@ -90,5 +113,6 @@ def check_unused_routes():
|
|||
else:
|
||||
print("All routes are referenced in templates or code. Good job!")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
check_unused_routes()
|
||||
check_unused_routes()
|
||||
|
|
|
@ -8,18 +8,19 @@ import os
|
|||
import shutil
|
||||
import argparse
|
||||
|
||||
|
||||
def cleanup(directory, verbose=False):
|
||||
"""Clean up cache files and database files"""
|
||||
cleaned_dirs = 0
|
||||
cleaned_files = 0
|
||||
|
||||
|
||||
# Files to clean
|
||||
file_patterns = ['.pyc', '.pyo', '.~', '.swp', '.swo']
|
||||
db_patterns = ['.db', '.sqlite', '.sqlite3', '-journal']
|
||||
|
||||
file_patterns = [".pyc", ".pyo", ".~", ".swp", ".swo"]
|
||||
db_patterns = [".db", ".sqlite", ".sqlite3", "-journal"]
|
||||
|
||||
# Directories to clean
|
||||
dir_patterns = ['__pycache__', '.pytest_cache', '.coverage', 'htmlcov']
|
||||
|
||||
dir_patterns = ["__pycache__", ".pytest_cache", ".coverage", "htmlcov"]
|
||||
|
||||
# Clean main directory
|
||||
for root, dirs, files in os.walk(directory):
|
||||
# Clean directories
|
||||
|
@ -31,7 +32,7 @@ def cleanup(directory, verbose=False):
|
|||
shutil.rmtree(dir_path)
|
||||
cleaned_dirs += 1
|
||||
dirs.remove(dir_name)
|
||||
|
||||
|
||||
# Clean files
|
||||
for file in files:
|
||||
if any(file.endswith(pattern) for pattern in file_patterns + db_patterns):
|
||||
|
@ -40,9 +41,9 @@ def cleanup(directory, verbose=False):
|
|||
print(f"Removing file: {file_path}")
|
||||
os.remove(file_path)
|
||||
cleaned_files += 1
|
||||
|
||||
|
||||
# Clean instance directory
|
||||
instance_dir = os.path.join(directory, 'instance')
|
||||
instance_dir = os.path.join(directory, "instance")
|
||||
if os.path.exists(instance_dir):
|
||||
for file in os.listdir(instance_dir):
|
||||
if any(file.endswith(pattern) for pattern in db_patterns):
|
||||
|
@ -51,13 +52,25 @@ def cleanup(directory, verbose=False):
|
|||
print(f"Removing database file: {file_path}")
|
||||
os.remove(file_path)
|
||||
cleaned_files += 1
|
||||
|
||||
print(f"Cleanup completed! Removed {cleaned_dirs} directories and {cleaned_files} files.")
|
||||
|
||||
print(
|
||||
f"Cleanup completed! Removed {cleaned_dirs} directories and {cleaned_files} files."
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
parser = argparse.ArgumentParser(description="Clean up Flask application cache and database files")
|
||||
parser.add_argument("-v", "--verbose", action="store_true", help="Show detailed output")
|
||||
parser.add_argument("-d", "--directory", default=".", help="Directory to clean (default: current directory)")
|
||||
|
||||
parser = argparse.ArgumentParser(
|
||||
description="Clean up Flask application cache and database files"
|
||||
)
|
||||
parser.add_argument(
|
||||
"-v", "--verbose", action="store_true", help="Show detailed output"
|
||||
)
|
||||
parser.add_argument(
|
||||
"-d",
|
||||
"--directory",
|
||||
default=".",
|
||||
help="Directory to clean (default: current directory)",
|
||||
)
|
||||
|
||||
args = parser.parse_args()
|
||||
cleanup(args.directory, args.verbose)
|
||||
cleanup(args.directory, args.verbose)
|
||||
|
|
|
@ -10,59 +10,61 @@ from flask import Flask
|
|||
from werkzeug.security import generate_password_hash
|
||||
|
||||
# Add the parent directory to sys.path
|
||||
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
|
||||
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
|
||||
|
||||
from app import create_app
|
||||
from app.core.extensions import db
|
||||
from app.core.auth import User
|
||||
|
||||
|
||||
def create_admin_user(email=None, password=None):
|
||||
"""Create an admin user in the database"""
|
||||
app = create_app()
|
||||
|
||||
|
||||
with app.app_context():
|
||||
# Check if users already exist
|
||||
if User.query.count() > 0:
|
||||
print("Users already exist in the database.")
|
||||
choice = input("Do you want to create another admin user? (y/n): ")
|
||||
if choice.lower() != 'y':
|
||||
if choice.lower() != "y":
|
||||
print("Operation cancelled.")
|
||||
return
|
||||
|
||||
|
||||
# Prompt for email if not provided
|
||||
if not email:
|
||||
email = input("Enter admin email: ")
|
||||
|
||||
|
||||
# Check if user with this email already exists
|
||||
existing_user = User.query.filter_by(email=email).first()
|
||||
if existing_user:
|
||||
print(f"User with email {email} already exists!")
|
||||
return
|
||||
|
||||
|
||||
# Prompt for password if not provided
|
||||
if not password:
|
||||
password = getpass.getpass("Enter admin password: ")
|
||||
confirm_password = getpass.getpass("Confirm password: ")
|
||||
|
||||
|
||||
if password != confirm_password:
|
||||
print("Passwords do not match!")
|
||||
return
|
||||
|
||||
|
||||
# Create the admin user
|
||||
admin = User(email=email, is_admin=True)
|
||||
admin.set_password(password)
|
||||
|
||||
|
||||
db.session.add(admin)
|
||||
db.session.commit()
|
||||
|
||||
|
||||
print(f"Admin user created successfully: {email}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import argparse
|
||||
|
||||
|
||||
parser = argparse.ArgumentParser(description="Create an admin user")
|
||||
parser.add_argument("--email", help="Admin user email")
|
||||
parser.add_argument("--password", help="Admin user password")
|
||||
|
||||
|
||||
args = parser.parse_args()
|
||||
create_admin_user(args.email, args.password)
|
||||
create_admin_user(args.email, args.password)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue