Starte Flask-Transformation

This commit is contained in:
phil 2020-09-03 12:15:26 +00:00
parent 2992abd0f3
commit 170e2d21fc
6 changed files with 62 additions and 47 deletions

View file

@ -1,7 +1,5 @@
#!/usr/bin/python2.5
# -*- coding: utf-8 -*-
#!/usr/bin/env python2
"""
$Id$
A web interface for managing htpasswd files.
@ -25,14 +23,13 @@ import sys
import os
# add the current path to the python path - for "htpasswd"
sys.path.insert(0, os.path.dirname(__file__))
# necessary for etch
sys.path.insert(1, '/usr/share/pyshared')
import htpasswd
import bobo
import genshi.template
import genshi.filters
from flask_bootstrap import Bootstrap
from flask import *
import ConfigParser
import re
import htpasswd
BASE_DIR = os.path.dirname(__file__)
CONFIG_FILE_LOCATIONS = [os.path.join(BASE_DIR, "htman.conf"), '/etc/htman/htman.conf']
@ -42,24 +39,30 @@ REGEX = {
"password": r"[a-zA-Z0-9_\-\.%\$_\;,<>=+\[\]\{\}\(\)#\'\"\/\&\*@]+$",
"mapping": r"[a-zA-Z0-9_\-\.]+$",
}
app = Flask(__name__)
app.config['BOOTSTRAP_SERVE_LOCAL'] = False
bootstrap = Bootstrap(app)
# use the environment variable HTMAN_CONFIG as the config file location if the
# variable is defined
if "HTMAN_CONFIG" in os.environ:
CONFIG_FILE_LOCATIONS = [os.environ["HTMAN_CONFIG"]]
@bobo.query('')
def redirect_frontpage():
return bobo.redirect(web_defaults["base_url"])
#@app.route('')
#def redirect_frontpage():
# return bobo.redirect(web_defaults["base_url"])
@bobo.query('/')
@app.route('/')
def show_frontpage():
values = web_defaults.copy()
return render("frontpage.html", **values)
# return render("frontpage.html", **values)
return render_template("frontpage.html")
@bobo.query('/password')
@app.route('/password')
def change_password(zone=None, username=None, old_password=None,
new_password=None, new_password2=None):
if zone:
@ -95,10 +98,10 @@ def change_password(zone=None, username=None, old_password=None,
values["success"] = "Password changed successfully."
else:
values["error"] = "Authentication error: zone, username or password is invalid."
return render("password_change.html", input_data=input_data, **values)
return render_template("password_change.html", input_data=input_data, **values)
@bobo.query('/admin')
@app.route('/admin')
def show_files():
values = web_defaults.copy()
# The template expects a list of tuples: (mapping name, admin-url).
@ -107,14 +110,14 @@ def show_files():
all_zones = get_mapping().keys()
all_zones.sort()
values["mapping"] = [(zone_name, "%s%s/%s" % (web_defaults["base_url"], "admin/manage", zone_name)) for zone_name in all_zones]
return render("list_mappings.html", **values)
return render_template("list_mappings.html", **values)
def is_zone_valid(zone):
return zone and (zone in get_mapping()) and re.match(REGEX["mapping"], zone)
@bobo.query('/manage')
@app.route('/manage')
def show_htpasswd(zone=None):
if not is_zone_valid(zone):
return bobo.redirect(web_defaults["base_url"])
@ -134,8 +137,8 @@ def get_htpasswd(zone, auto_create=False):
# the alternative "/admin" URL allows to define super-user htaccess rules via
# Apache's "Location" directive
@bobo.query('/admin/manage/:zone')
@bobo.query('/manage/:zone')
@app.route('/admin/manage/:zone')
@app.route('/manage/:zone')
def manage_htpasswd(zone=None, action=None, username=None, password=None):
values = web_defaults.copy()
values["error"] = None
@ -184,7 +187,7 @@ def manage_htpasswd(zone=None, action=None, username=None, password=None):
values["error"] = "Invalid action"
values["usernames"] = htdb.get_usernames()
# show the current htpasswd file
return render("manage_users.html", **values)
return render_template("manage_users.html", **values)
def get_config():
@ -272,14 +275,9 @@ config = get_config()
mapping = get_mapping()
templates_dir = get_templates_dir(config)
web_defaults = dict(config.items("Web"))
if not web_defaults["base_url"].endswith("/"):
web_defaults["base_url"] += "/"
loader = genshi.template.TemplateLoader([templates_dir], auto_reload=False)
# this line allows to use mod_wsgi
# see: http://groups.google.com/group/bobo-web/msg/2ba55fc381658cd1
# see: http://blog.dscpl.com.au/2009/08/using-bobo-on-top-of-modwsgi.html
if __name__.startswith("_mod_wsgi_"):
application = bobo.Application(bobo_resources=__name__)
#if not web_defaults["base_url"].endswith("/"):
# web_defaults["base_url"] += "/"
#loader = genshi.template.TemplateLoader([templates_dir], auto_reload=False)
if __name__ == '__main__':
app.run()

6
htman.wsgi Normal file
View file

@ -0,0 +1,6 @@
#!/usr/bin/env python2
import sys
sys.path.insert(0, '/data/htman/src')
from htman import app as application

View file

@ -1,8 +1,5 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
#!/usr/bin/env python3
"""
$Id$
A python module for managing htpasswd files.
The code is based on http://trac.edgewall.org/export/9825/trunk/contrib/htpasswd.py

21
templates/base.html Normal file
View file

@ -0,0 +1,21 @@
<!doctype html>
<html lang="en">
<head>
{% block head %}
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
{% block styles %}
<!-- Bootstrap CSS -->
{{ bootstrap.load_css() }}
{% endblock %}
<title>Wiki-Zugangsverwaltung</title>
{% endblock %}
</head>
<body>
{% block content %}{% endblock %}
{% block scripts %}
{{ bootstrap.load_js() }}
{% endblock %}
</body>
</html>

View file

@ -1,18 +1,11 @@
<!DOCTYPE html>
<html xml:lang="de" lang="de"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:xi="http://www.w3.org/2001/XInclude"
xmlns:py="http://genshi.edgewall.org/">
<xi:include href="layout.html" />
{% extends "base.html" %}
<head/>
{% block content %}
<body>
<form name="show_htpasswd" action="manage" method="POST">
<label for="name">Wiki:</label>
<input type="text" size="30" name="zone" id="name" />
<input type="submit" name="submit" value="Show" />
</form>
</body>
</html>
{% endblock %}

View file

@ -3,7 +3,7 @@
xmlns:py="http://genshi.edgewall.org/" py:strip="">
<py:match path="head" once="true">
<head py:attrs="select('@*')">
# <head py:attrs="select('@*')">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>${title}</title>
<link rel="stylesheet" href="${css_url}" type="text/css" />