Starte Flask-Transformation
This commit is contained in:
parent
2992abd0f3
commit
170e2d21fc
6 changed files with 62 additions and 47 deletions
62
htman.py
62
htman.py
|
@ -1,7 +1,5 @@
|
||||||
#!/usr/bin/python2.5
|
#!/usr/bin/env python2
|
||||||
# -*- coding: utf-8 -*-
|
|
||||||
"""
|
"""
|
||||||
$Id$
|
|
||||||
|
|
||||||
A web interface for managing htpasswd files.
|
A web interface for managing htpasswd files.
|
||||||
|
|
||||||
|
@ -25,14 +23,13 @@ import sys
|
||||||
import os
|
import os
|
||||||
# add the current path to the python path - for "htpasswd"
|
# add the current path to the python path - for "htpasswd"
|
||||||
sys.path.insert(0, os.path.dirname(__file__))
|
sys.path.insert(0, os.path.dirname(__file__))
|
||||||
# necessary for etch
|
|
||||||
sys.path.insert(1, '/usr/share/pyshared')
|
from flask_bootstrap import Bootstrap
|
||||||
import htpasswd
|
from flask import *
|
||||||
import bobo
|
|
||||||
import genshi.template
|
|
||||||
import genshi.filters
|
|
||||||
import ConfigParser
|
import ConfigParser
|
||||||
import re
|
import re
|
||||||
|
import htpasswd
|
||||||
|
|
||||||
|
|
||||||
BASE_DIR = os.path.dirname(__file__)
|
BASE_DIR = os.path.dirname(__file__)
|
||||||
CONFIG_FILE_LOCATIONS = [os.path.join(BASE_DIR, "htman.conf"), '/etc/htman/htman.conf']
|
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_\-\.%\$_\;,<>=+\[\]\{\}\(\)#\'\"\/\&\*@]+$",
|
"password": r"[a-zA-Z0-9_\-\.%\$_\;,<>=+\[\]\{\}\(\)#\'\"\/\&\*@]+$",
|
||||||
"mapping": 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
|
# use the environment variable HTMAN_CONFIG as the config file location if the
|
||||||
# variable is defined
|
# variable is defined
|
||||||
if "HTMAN_CONFIG" in os.environ:
|
if "HTMAN_CONFIG" in os.environ:
|
||||||
CONFIG_FILE_LOCATIONS = [os.environ["HTMAN_CONFIG"]]
|
CONFIG_FILE_LOCATIONS = [os.environ["HTMAN_CONFIG"]]
|
||||||
|
|
||||||
|
|
||||||
@bobo.query('')
|
#@app.route('')
|
||||||
def redirect_frontpage():
|
#def redirect_frontpage():
|
||||||
return bobo.redirect(web_defaults["base_url"])
|
# return bobo.redirect(web_defaults["base_url"])
|
||||||
|
|
||||||
|
|
||||||
@bobo.query('/')
|
@app.route('/')
|
||||||
def show_frontpage():
|
def show_frontpage():
|
||||||
values = web_defaults.copy()
|
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,
|
def change_password(zone=None, username=None, old_password=None,
|
||||||
new_password=None, new_password2=None):
|
new_password=None, new_password2=None):
|
||||||
if zone:
|
if zone:
|
||||||
|
@ -95,10 +98,10 @@ def change_password(zone=None, username=None, old_password=None,
|
||||||
values["success"] = "Password changed successfully."
|
values["success"] = "Password changed successfully."
|
||||||
else:
|
else:
|
||||||
values["error"] = "Authentication error: zone, username or password is invalid."
|
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():
|
def show_files():
|
||||||
values = web_defaults.copy()
|
values = web_defaults.copy()
|
||||||
# The template expects a list of tuples: (mapping name, admin-url).
|
# 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 = get_mapping().keys()
|
||||||
all_zones.sort()
|
all_zones.sort()
|
||||||
values["mapping"] = [(zone_name, "%s%s/%s" % (web_defaults["base_url"], "admin/manage", zone_name)) for zone_name in all_zones]
|
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):
|
def is_zone_valid(zone):
|
||||||
return zone and (zone in get_mapping()) and re.match(REGEX["mapping"], 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):
|
def show_htpasswd(zone=None):
|
||||||
if not is_zone_valid(zone):
|
if not is_zone_valid(zone):
|
||||||
return bobo.redirect(web_defaults["base_url"])
|
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
|
# the alternative "/admin" URL allows to define super-user htaccess rules via
|
||||||
# Apache's "Location" directive
|
# Apache's "Location" directive
|
||||||
@bobo.query('/admin/manage/:zone')
|
@app.route('/admin/manage/:zone')
|
||||||
@bobo.query('/manage/:zone')
|
@app.route('/manage/:zone')
|
||||||
def manage_htpasswd(zone=None, action=None, username=None, password=None):
|
def manage_htpasswd(zone=None, action=None, username=None, password=None):
|
||||||
values = web_defaults.copy()
|
values = web_defaults.copy()
|
||||||
values["error"] = None
|
values["error"] = None
|
||||||
|
@ -184,7 +187,7 @@ def manage_htpasswd(zone=None, action=None, username=None, password=None):
|
||||||
values["error"] = "Invalid action"
|
values["error"] = "Invalid action"
|
||||||
values["usernames"] = htdb.get_usernames()
|
values["usernames"] = htdb.get_usernames()
|
||||||
# show the current htpasswd file
|
# show the current htpasswd file
|
||||||
return render("manage_users.html", **values)
|
return render_template("manage_users.html", **values)
|
||||||
|
|
||||||
|
|
||||||
def get_config():
|
def get_config():
|
||||||
|
@ -272,14 +275,9 @@ config = get_config()
|
||||||
mapping = get_mapping()
|
mapping = get_mapping()
|
||||||
templates_dir = get_templates_dir(config)
|
templates_dir = get_templates_dir(config)
|
||||||
web_defaults = dict(config.items("Web"))
|
web_defaults = dict(config.items("Web"))
|
||||||
if not web_defaults["base_url"].endswith("/"):
|
#if not web_defaults["base_url"].endswith("/"):
|
||||||
web_defaults["base_url"] += "/"
|
# web_defaults["base_url"] += "/"
|
||||||
loader = genshi.template.TemplateLoader([templates_dir], auto_reload=False)
|
#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 __name__ == '__main__':
|
||||||
|
app.run()
|
||||||
|
|
6
htman.wsgi
Normal file
6
htman.wsgi
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
#!/usr/bin/env python2
|
||||||
|
|
||||||
|
import sys
|
||||||
|
sys.path.insert(0, '/data/htman/src')
|
||||||
|
|
||||||
|
from htman import app as application
|
|
@ -1,8 +1,5 @@
|
||||||
#!/usr/bin/python
|
#!/usr/bin/env python3
|
||||||
# -*- coding: utf-8 -*-
|
|
||||||
"""
|
"""
|
||||||
$Id$
|
|
||||||
|
|
||||||
A python module for managing htpasswd files.
|
A python module for managing htpasswd files.
|
||||||
|
|
||||||
The code is based on http://trac.edgewall.org/export/9825/trunk/contrib/htpasswd.py
|
The code is based on http://trac.edgewall.org/export/9825/trunk/contrib/htpasswd.py
|
||||||
|
|
21
templates/base.html
Normal file
21
templates/base.html
Normal 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>
|
|
@ -1,18 +1,11 @@
|
||||||
<!DOCTYPE html>
|
{% extends "base.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" />
|
|
||||||
|
|
||||||
<head/>
|
{% block content %}
|
||||||
|
|
||||||
<body>
|
|
||||||
<form name="show_htpasswd" action="manage" method="POST">
|
<form name="show_htpasswd" action="manage" method="POST">
|
||||||
<label for="name">Wiki:</label>
|
<label for="name">Wiki:</label>
|
||||||
<input type="text" size="30" name="zone" id="name" />
|
<input type="text" size="30" name="zone" id="name" />
|
||||||
<input type="submit" name="submit" value="Show" />
|
<input type="submit" name="submit" value="Show" />
|
||||||
</form>
|
</form>
|
||||||
</body>
|
|
||||||
</html>
|
|
||||||
|
|
||||||
|
{% endblock %}
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
xmlns:py="http://genshi.edgewall.org/" py:strip="">
|
xmlns:py="http://genshi.edgewall.org/" py:strip="">
|
||||||
|
|
||||||
<py:match path="head" once="true">
|
<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"/>
|
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
|
||||||
<title>${title}</title>
|
<title>${title}</title>
|
||||||
<link rel="stylesheet" href="${css_url}" type="text/css" />
|
<link rel="stylesheet" href="${css_url}" type="text/css" />
|
||||||
|
|
Loading…
Reference in a new issue