From 2b4ca571ca189fc2272e34b71e67b7db5644d841 Mon Sep 17 00:00:00 2001 From: Peter Stockings Date: Sat, 18 May 2024 21:55:11 +1000 Subject: [PATCH] Add in version history for http functions and also load .env in dev mode --- app.py | 40 ++++- db.py | 11 +- requirements.txt | 3 +- templates/base.html | 3 + templates/dashboard/http_functions/edit.html | 3 +- .../dashboard/http_functions/header.html | 13 ++ .../dashboard/http_functions/history.html | 170 ++++++++++++++++++ templates/dashboard/http_functions/logs.html | 3 +- 8 files changed, 239 insertions(+), 7 deletions(-) create mode 100644 templates/dashboard/http_functions/history.html diff --git a/app.py b/app.py index dcb3e38..7a4678d 100644 --- a/app.py +++ b/app.py @@ -1,6 +1,6 @@ import json import os -from flask import Flask, Response, jsonify, redirect, render_template, request, url_for +from flask import Flask, Response, jsonify, redirect, render_template, render_template_string, request, url_for import jinja_partials from jinja2_fragments import render_block from flask_htmx import HTMX @@ -9,6 +9,12 @@ from db import DataBase from services import create_http_function_view_model, create_http_functions_view_model from flask_login import LoginManager, UserMixin, current_user, login_required, login_user, logout_user from werkzeug.security import check_password_hash, generate_password_hash +import os +from dotenv import load_dotenv + +# Load environment variables from .env file in non-production environments +if os.environ.get('FLASK_ENV') != 'production': + load_dotenv() login_manager = LoginManager() app = Flask(__name__) @@ -222,6 +228,38 @@ def client(function_id): return render_block(app.jinja_env, 'dashboard/http_functions/client.html', 'page', function_id=function_id, **http_function) return render_template("dashboard/http_functions/client.html", function_id=function_id, **http_function) +@app.route("/dashboard/http_functions//history", methods=["GET"]) +@login_required +def get_http_function_history(function_id): + user_id = current_user.id + http_function = db.get_http_function_by_id(user_id, function_id) + if not http_function: + return jsonify({'error': 'Function not found'}), 404 + + name = http_function['name'] + version_number = http_function['version_number'] + + http_function_history = [] + if version_number > 1: + raw_history = db.get_http_function_history(function_id) + + for i in range(len(raw_history) - 1): + pre_version = raw_history[i + 1] + post_version = raw_history[i] + + http_function_history.append({ + 'pre': pre_version['script_content'], + 'post': post_version['script_content'], + 'version_id': post_version['version_id'], + 'version_number': post_version['version_number'], + 'updated_at': post_version['updated_at'] + }) + + + if htmx: + return render_block(app.jinja_env, 'dashboard/http_functions/history.html', 'page', user_id=user_id, function_id=function_id, name=name, http_function=http_function, http_function_history=http_function_history) + return render_template("dashboard/http_functions/history.html", user_id=user_id, name=name, function_id=function_id, http_function=http_function, http_function_history=http_function_history) + @ app.route("/dashboard/timer_functions", methods=["GET"]) @login_required def dashboard_timer_functions(): diff --git a/db.py b/db.py index 20c0fe2..30f1d5d 100644 --- a/db.py +++ b/db.py @@ -54,12 +54,12 @@ class DataBase(): def get_http_function(self, user_id, name): http_function = self.execute( - 'SELECT id, user_id, NAME, script_content, invoked_count, environment_info, is_public, log_request, log_response, version_number FROM http_functions WHERE user_id=%s AND NAME=%s', [user_id, name], one=True) + 'SELECT id, user_id, NAME, script_content, invoked_count, environment_info, is_public, log_request, log_response, version_number, created_at FROM http_functions WHERE user_id=%s AND NAME=%s', [user_id, name], one=True) return http_function def get_http_function_by_id(self, user_id, http_function_id): http_function = self.execute( - 'SELECT id, user_id, NAME, script_content, invoked_count, environment_info, is_public, log_request, log_response, version_number FROM http_functions WHERE user_id=%s AND id=%s', [user_id, http_function_id], one=True) + 'SELECT id, user_id, NAME, script_content, invoked_count, environment_info, is_public, log_request, log_response, version_number, created_at FROM http_functions WHERE user_id=%s AND id=%s', [user_id, http_function_id], one=True) return http_function def create_new_http_function(self, user_id, name, script_content, environment_info, is_public, log_request, log_response): @@ -110,4 +110,9 @@ ORDER BY invocation_time DESC""", [http_function_id]) def create_new_user(self, username, password_hash): new_user = self.execute( 'INSERT INTO users (username, password_hash) VALUES (%s, %s) RETURNING id, username, password_hash, created_at', [username, password_hash], commit=True, one=True) - return new_user \ No newline at end of file + return new_user + + def get_http_function_history(self, function_id): + http_function_history = self.execute( + 'SELECT version_id, http_function_id, script_content, version_number, updated_at FROM http_functions_versions WHERE http_function_id=%s AND version_number > 1 ORDER BY version_number DESC', [function_id]) + return http_function_history \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index c758f53..4e4e434 100644 --- a/requirements.txt +++ b/requirements.txt @@ -8,4 +8,5 @@ python-dateutil==2.8.2 jinja2-fragments==0.3.0 Werkzeug==2.2.2 requests==2.26.0 -Flask-Login==0.6.3 \ No newline at end of file +Flask-Login==0.6.3 +python-dotenv==1.0.1 \ No newline at end of file diff --git a/templates/base.html b/templates/base.html index 2d2e74b..2df5f65 100644 --- a/templates/base.html +++ b/templates/base.html @@ -24,6 +24,9 @@ + + +