full refactor fml
This commit is contained in:
82
app/views.py
82
app/views.py
@@ -0,0 +1,82 @@
|
||||
import os
|
||||
import glob
|
||||
from flask import render_template
|
||||
|
||||
from app.forms import ScrapingForm
|
||||
from app.util import get_size
|
||||
from app.config import load_config
|
||||
from app.api import scraper as scraper# Import the scraper instance
|
||||
from app.logging_config import get_logger
|
||||
|
||||
from app.state import log_file_name
|
||||
|
||||
print(f"A imported log_file_name: {log_file_name}")
|
||||
|
||||
config = load_config()
|
||||
logger = get_logger()
|
||||
|
||||
def register_views(app):
|
||||
@app.route('/')
|
||||
def index():
|
||||
form = ScrapingForm()
|
||||
return render_template('index.html', form=form)
|
||||
|
||||
@app.route('/results')
|
||||
def results():
|
||||
return render_template('results.html')
|
||||
|
||||
@app.route('/analyze')
|
||||
def analyze():
|
||||
return render_template('analyze.html')
|
||||
|
||||
@app.route('/log_viewer')
|
||||
def log_viewer():
|
||||
return render_template('log_viewer.html')
|
||||
|
||||
@app.route('/download_results')
|
||||
def download_results():
|
||||
log_file_name = os.path.abspath(app.config['LOG_FILE_NAME'])
|
||||
scraper = app.config.get('SCRAPER')
|
||||
|
||||
if scraper:
|
||||
print(scraper.data_file_name)
|
||||
if not scraper:
|
||||
print("Scraper not initialized")
|
||||
|
||||
data_dir = os.path.abspath(config['DATA']['DATA_DIR'])
|
||||
log_dir = os.path.abspath(config['LOGGING']['LOG_DIR'])
|
||||
|
||||
data_files = glob.glob(os.path.join(data_dir, "*.csv"))
|
||||
log_files = glob.glob(os.path.join(log_dir, "*.log"))
|
||||
|
||||
def get_file_info(file_path):
|
||||
return {
|
||||
"name": file_path,
|
||||
"name_display": os.path.basename(file_path),
|
||||
"last_modified": os.path.getmtime(file_path),
|
||||
"created": os.path.getctime(file_path),
|
||||
"size": get_size(file_path)
|
||||
}
|
||||
|
||||
data_files_info = [get_file_info(file) for file in data_files]
|
||||
log_files_info = [get_file_info(file) for file in log_files]
|
||||
|
||||
if scraper and scraper.scraping_active:
|
||||
for data_file in data_files_info:
|
||||
if os.path.abspath(scraper.data_file_name) == data_file['name']:
|
||||
data_file['active'] = True
|
||||
else:
|
||||
data_file['active'] = False
|
||||
|
||||
for log_file in log_files_info:
|
||||
if log_file_name == os.path.abspath(log_file['name']):
|
||||
log_file['active'] = True
|
||||
else:
|
||||
log_file['active'] = False
|
||||
|
||||
data_files_info.sort(key=lambda x: x['last_modified'], reverse=True)
|
||||
log_files_info.sort(key=lambda x: x['last_modified'], reverse=True)
|
||||
|
||||
files = {"data": data_files_info, "log": log_files_info}
|
||||
|
||||
return render_template('download_results.html', files=files)
|
||||
Reference in New Issue
Block a user