]> WPIA git - motion.git/blobdiff - motion.py
add: implement unit tests
[motion.git] / motion.py
index c5fbe7de6ce3eccd787b736da97710ab4f703292..f6ddf1344004ee30bd5bbb40d096eadcd33cea90 100644 (file)
--- a/motion.py
+++ b/motion.py
@@ -4,8 +4,8 @@ from flask import render_template, redirect
 from flask import request
 import postgresql
 import filters
-
-times=[3,5,14]
+from flaskext.markdown import Markdown
+from markdown.extensions import Extension
 
 def get_db():
     db = getattr(g, '_database', None)
@@ -17,16 +17,33 @@ def get_db():
 app = Flask(__name__)
 app.register_blueprint(filters.blueprint)
 
+class EscapeHtml(Extension):
+    def extendMarkdown(self, md, md_globals):
+        del md.preprocessors['html_block']
+        del md.inlinePatterns['html']
+
+md = Markdown(app, extensions=[EscapeHtml()])
+
 # Load config
 app.config.from_pyfile('config.py')
 
-groups=app.config.get("groups")
-prefix=app.config.get("group_prefix")
+prefix=app.config.get("GROUP_PREFIX")
+
+times=app.config.get("DURATION")
+
+debuguser=app.config.get("DEBUGUSER")
 
 @app.before_request
 def lookup_user():
+    global prefix
+
     env = request.environ
     user = None
+    if debuguser is not None:
+        parts =debuguser[request.host].split("/", 1)
+        user = parts[0]
+        roles = parts[1]
+
     if "USER_ROLES" in env:
         parts = env.get("USER_ROLES").split("/", 1)
         user = parts[0]
@@ -36,6 +53,7 @@ def lookup_user():
         user = env.get("USER")
         roles = env.get("ROLES")
 
+
     if user is None:
         return "Server misconfigured", 500
     roles = roles.split(" ")
@@ -55,13 +73,14 @@ def lookup_user():
 
     for r in roles:
         a = r.split(":", 1)
-        val = a[1]
-        if a[0] not in g.roles:
-            g.roles[a[0]] = []
-        if val == "*":
-            g.roles[a[0]] = groups
-        else:
-            g.roles[a[0]].append(val)
+        if len(r)!=0:
+            val = a[1]
+            if a[0] not in g.roles:
+                g.roles[a[0]] = []
+            if val == "*":
+                g.roles[a[0]] = [group for group in prefix[request.host]]
+            else:
+                g.roles[a[0]].append(val)
     return None
 
 def get_allowed_cats(action):
@@ -94,7 +113,7 @@ def init_db():
             with app.open_resource('sql/from_1.sql', mode='r') as f:
                 db.execute(f.read())
                 ct={}
-                for g in groups:
+                for g in [group for group in prefix[app.config.get("DEFAULT_HOST")]]:
                     ct[g] = {"dt": "", "c": 0}
 
                 p = db.prepare("UPDATE \"motion\" SET \"identifier\"=$1 WHERE \"id\"=$2")
@@ -104,7 +123,7 @@ def init_db():
                         ct[row[1]]["dt"] = dt
                         ct[row[1]]["c"] = 0
                     ct[row[1]]["c"] = ct[row[1]]["c"] + 1
-                    name=prefix[row[1]]+"."+dt+"."+("%03d" % ct[row[1]]["c"])
+                    name=prefix[app.config.get("DEFAULT_HOST")][row[1]]+"."+dt+"."+("%03d" % ct[row[1]]["c"])
                     p(name, row[0])
                 db.prepare("ALTER TABLE \"motion\" ALTER COLUMN \"identifier\" SET NOT NULL")()
                 db.prepare("UPDATE \"schema_version\" SET \"version\"=2")()
@@ -142,7 +161,7 @@ def main():
             prev = rs[9][0]
         else:
             prev = -1
-    return render_template('index.html', motions=rv[:10], more=rv[10]["id"] if len(rv) == 11 else None, times=times, prev=prev,
+    return render_template('index.html', motions=rv[:10], more=rv[10]["id"] if len(rv) == 11 else None, times=times[request.host], prev=prev,
                            categories=get_allowed_cats("create"))
 
 def rel_redirect(loc):
@@ -156,18 +175,18 @@ def put_motion():
     if cat not in get_allowed_cats("create"):
         return "Forbidden", 403
     time = int(request.form.get("days", "3"));
-    if time not in times:
+    if time not in times[request.host]:
         return "Error, invalid length", 500
     db = get_db()
     with db.xact():
         t = db.prepare("SELECT CURRENT_TIMESTAMP")()[0][0];
-        s = db.prepare("SELECT MAX(\"identifier\") FROM \"motion\" WHERE \"type\"=$1 AND \"host\"=$2 DATE(\"posed\")=DATE(CURRENT_TIMESTAMP)")
+        s = db.prepare("SELECT MAX(\"identifier\") FROM \"motion\" WHERE \"type\"=$1 AND \"host\"=$2 AND DATE(\"posed\")=DATE(CURRENT_TIMESTAMP)")
         sr = s(cat, request.host)
         ident=""
         if len(sr) == 0 or sr[0][0] is None:
-            ident=prefix[cat]+"."+t.strftime("%Y%m%d")+".001"
+            ident=prefix[request.host][cat]+"."+t.strftime("%Y%m%d")+".001"
         else:
-            ident=prefix[cat]+"."+t.strftime("%Y%m%d")+"."+("%03d" % (int(sr[0][0].split(".")[2])+1))
+            ident=prefix[request.host][cat]+"."+t.strftime("%Y%m%d")+"."+("%03d" % (int(sr[0][0].split(".")[2])+1))
         p = db.prepare("INSERT INTO motion(\"name\", \"content\", \"deadline\", \"posed_by\", \"type\", \"identifier\", \"host\") VALUES($1, $2, CURRENT_TIMESTAMP + $3 * interval '1 days', $4, $5, $6, $7)")
         p(request.form.get("title", ""), request.form.get("content",""), time, g.voter, cat, ident, request.host)
     return rel_redirect("/")
@@ -177,7 +196,7 @@ def motion_edited(motion):
 
 @app.route("/motion/<string:motion>/cancel", methods=['POST'])
 def cancel_motion(motion):
-    rv = get_db().prepare("SELECT id, type FROM motion WHERE identifier=$1 AND request.host")(motion, request.host);
+    rv = get_db().prepare("SELECT id, type FROM motion WHERE identifier=$1 AND host=$2")(motion, request.host);
     if len(rv) == 0:
         return "Error, Not found", 404
     id = rv[0].get("id")
@@ -206,7 +225,7 @@ def vote(motion):
     v = request.form.get("vote", "abstain")
     db = get_db()
     with db.xact():
-        rv = db.prepare("SELECT id, type FROM motion WHERE identifier=$1 AND host=$2")(motion, host);
+        rv = db.prepare("SELECT id, type FROM motion WHERE identifier=$1 AND host=$2")(motion, request.host);
         if len(rv) == 0:
             return "Error, Not found", 404
         if not may("vote", rv[0].get("type")):
@@ -215,8 +234,8 @@ def vote(motion):
         id = rv[0].get("id")
         if not p(motion, request.host)[0][0]:
             return "Error, motion deadline has passed", 500
-        p = db.prepare("SELECT * FROM vote WHERE motion_id = $1 AND voter_id = $2 AND host=$3")
-        rv = p(id, g.voter, request.host)
+        p = db.prepare("SELECT * FROM vote WHERE motion_id = $1 AND voter_id = $2")
+        rv = p(id, g.voter)
         if len(rv) == 0:
             db.prepare("INSERT INTO vote(motion_id, voter_id, result) VALUES($1,$2,$3)")(id, g.voter, v)
         else: