Add blueprint scaffold for timer functions

This commit is contained in:
Peter Stockings
2025-02-16 13:45:34 +11:00
parent 9d48bbd285
commit 699ee50229
6 changed files with 162 additions and 9 deletions

8
app.py
View File

@@ -3,14 +3,14 @@ import os
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
import requests
from db import DataBase
from extensions import db, htmx, init_app
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
from routes.timer import timer
# Load environment variables from .env file in non-production environments
if os.environ.get('FLASK_ENV') != 'production':
@@ -23,8 +23,8 @@ app.secret_key = os.environ.get('SECRET_KEY', '2a661781919643cb8a5a8bc57642d99f'
login_manager.init_app(app)
login_manager.login_view = "login"
jinja_partials.register_extensions(app)
htmx = HTMX(app)
db = DataBase(app)
init_app(app)
app.register_blueprint(timer, url_prefix='/timer')
class User(UserMixin):
def __init__(self, id, username, password_hash, created_at):

2
db.py
View File

@@ -6,7 +6,7 @@ from urllib.parse import urlparse
from flask import g
class DataBase():
def __init__(self, app):
def __init__(self, app=None):
db_url = urlparse(os.environ['DATABASE_URL'])
# if db_url is null then throw error
if not db_url:

17
extensions.py Normal file
View File

@@ -0,0 +1,17 @@
from jinja2 import Environment, FileSystemLoader, select_autoescape
from db import DataBase
from flask_htmx import HTMX
from flask import url_for
db = DataBase()
htmx = HTMX()
environment = Environment(
loader=FileSystemLoader("templates"),
autoescape=select_autoescape(("html", "jinja2"))
)
environment.globals['url_for'] = url_for
def init_app(app):
htmx.init_app(app)

17
routes/timer.py Normal file
View File

@@ -0,0 +1,17 @@
from flask import Blueprint, render_template, redirect, url_for, flash
from jinja2_fragments import render_block
from werkzeug.security import generate_password_hash, check_password_hash
from flask_login import login_user, login_required, logout_user
from extensions import db, htmx, environment
timer = Blueprint('timer', __name__)
@timer.route('/overview')
@login_required
def overview():
if htmx:
return render_block(environment, 'dashboard/timer_functions/overview.html', 'page')
return render_template('dashboard/timer_functions/overview.html')

View File

@@ -33,16 +33,16 @@
</a><a
class="flex items-center gap-3 rounded-lg px-3 py-2 text-gray-500 transition-all hover:text-gray-900 dark:text-gray-400 dark:hover:text-gray-50 cursor-pointer"
data-id="15" hx-get="{{ url_for('dashboard_http_functions') }}" hx-target="#container"
hx-swap="innerHTML"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24"
viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"
hx-swap="innerHTML" hx-push-url="true"><svg xmlns="http://www.w3.org/2000/svg" width="24"
height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"
stroke-linecap="round" stroke-linejoin="round" class="h-4 w-4" data-id="16">
<path d="m19 21-7-4-7 4V5a2 2 0 0 1 2-2h10a2 2 0 0 1 2 2v16z"></path>
</svg>
HTTP
</a><a
class="flex items-center gap-3 rounded-lg px-3 py-2 text-gray-500 transition-all hover:text-gray-900 dark:text-gray-400 dark:hover:text-gray-50 cursor-pointer"
data-id="17" hx-get="{{ url_for('dashboard_timer_functions') }}" hx-target="#container"
hx-swap="innerHTML"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24"
data-id="17" hx-get="{{ url_for('timer.overview') }}" hx-target="#container" hx-swap="innerHTML"
hx-push-url="true"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24"
viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"
stroke-linecap="round" stroke-linejoin="round" class="h-4 w-4" data-id="18">
<path d="M16 21v-2a4 4 0 0 0-4-4H6a4 4 0 0 0-4 4v2"></path>

View File

@@ -0,0 +1,119 @@
{% extends 'dashboard.html' %}
{% block page %}
<div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-8">
<div class="flex items-center mb-6" data-id="51">
<h1 class="text-2xl font-bold text-gray-900">Timer Functions</h1>
<button
class="inline-flex items-center px-4 py-2 ml-auto bg-green-600 hover:bg-green-700 text-white font-medium rounded-lg transition-colors duration-200"
hx-get="{{ url_for('get_http_function_add_form') }}" hx-target="#container" hx-swap="innerHTML"
hx-push-url="true">
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5"
stroke="currentColor" class="w-5 h-5 mr-2">
<path stroke-linecap="round" stroke-linejoin="round" d="M12 4.5v15m7.5-7.5h-15"></path>
</svg>
Add Function
</button>
</div>
<div class="bg-white border border-gray-200 rounded-lg shadow-sm overflow-hidden">
<div class="overflow-x-auto">
<table class="w-full">
<thead>
<tr class="bg-gray-50 border-b border-gray-200">
<th class="px-6 py-4 text-left text-sm font-semibold text-gray-900">Name</th>
<th class="px-6 py-4 text-left text-sm font-semibold text-gray-900">URL
</th>
<th class="px-6 py-4 text-left text-sm font-semibold text-gray-900 hidden md:table-cell">Actions
</th>
</tr>
</thead>
<tbody class="divide-y divide-gray-200">
{% for function in http_functions %}
<tr class="hover:bg-gray-50">
<td class="px-6 py-4">
<div class="flex items-center gap-2 cursor-pointer"
hx-get="{{ url_for('http_function_editor', function_id=function.id) }}"
hx-target="#container" hx-swap="innerHTML" hx-push-url="true">
<span class="font-medium text-gray-900">{{ function.name }}</span>
<span
class="bg-green-100 text-green-800 text-xs font-medium px-2.5 py-0.5 rounded-full">
#{{ function.invoked_count }}
</span>
<span class="bg-blue-100 text-blue-800 text-xs font-medium px-2.5 py-0.5 rounded-full">
v{{ function.version_number }}
</span>
{% if function.is_public %}
<span class="text-gray-500">
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24"
stroke-width="1.5" stroke="currentColor" class="w-4 h-4">
<path stroke-linecap="round" stroke-linejoin="round"
d="M13.5 10.5V6.75a4.5 4.5 0 1 1 9 0v3.75M3.75 21.75h10.5a2.25 2.25 0 0 0 2.25-2.25v-6.75a2.25 2.25 0 0 0-2.25-2.25H3.75a2.25 2.25 0 0 0-2.25 2.25v6.75a2.25 2.25 0 0 0 2.25 2.25Z" />
</svg>
</span>
{% endif %}
</div>
</td>
<td class="px-6 py-4">
<div class="flex items-center">
<a href="{{ url_for('execute_http_function', user_id=function.user_id, function=function.name) }}"
class="text-blue-600 hover:text-blue-800">
{{ url_for('execute_http_function', user_id=function.user_id,
function=function.name) }}
</a>
<button class="ml-2 text-gray-400 hover:text-gray-600">
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24"
fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"
stroke-linejoin="round">
<path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path>
<path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path>
</svg>
</button>
</div>
</td>
<td class="px-6 py-4 hidden md:table-cell">
<div class="flex gap-3">
<button
class="inline-flex items-center px-3 py-1.5 text-sm font-medium text-indigo-700 bg-indigo-50 rounded-md hover:bg-indigo-100 transition-colors duration-200"
hx-get="{{ url_for('get_http_function_logs', function_id=function.id) }}"
hx-target="#container" hx-swap="innerHTML" hx-push-url="true">
<svg xmlns="http://www.w3.org/2000/svg" class="w-4 h-4 mr-1.5" fill="none"
viewBox="0 0 24 24" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
d="M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z" />
</svg>
Logs
</button>
<button
class="inline-flex items-center px-3 py-1.5 text-sm font-medium text-indigo-700 bg-indigo-50 rounded-md hover:bg-indigo-100 transition-colors duration-200"
hx-get="{{ url_for('client', function_id=function.id) }}" hx-target="#container"
hx-swap="innerHTML" hx-push-url="true">
<svg xmlns="http://www.w3.org/2000/svg" class="w-4 h-4 mr-1.5" fill="none"
viewBox="0 0 24 24" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
d="M14.752 11.168l-3.197-2.132A1 1 0 0010 9.87v4.263a1 1 0 001.555.832l3.197-2.132a1 1 0 000-1.664z" />
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
d="M21 12a9 9 0 11-18 0 9 9 0 0118 0z" />
</svg>
Try
</button>
</div>
</td>
</tr>
{% endfor %}
{% if http_functions|length == 0 %}
<tr>
<td colspan="3" class="px-6 py-8 text-center">
<p class="text-gray-500 text-lg">No functions found</p>
</td>
</tr>
{% endif %}
</tbody>
</table>
</div>
</div>
</div>
{% endblock %}