]> WPIA git - motion.git/blobdiff - motion.py
upd: make sure that database is populated with tables
[motion.git] / motion.py
index 06cde68fcac8834943c8aa5a6540651a0661934e..e2f3f15ab28854e27e927f7260615ed3e696b0d7 100644 (file)
--- a/motion.py
+++ b/motion.py
@@ -113,6 +113,8 @@ def init_db():
             ver = db.prepare("SELECT version FROM schema_version")()[0][0];
             print("Database Schema version: ", ver)
         except postgresql.exceptions.UndefinedTableError:
+            g._database = None
+            db = get_db()
             ver = 0
 
         if ver < 1:
@@ -124,8 +126,8 @@ def init_db():
             with app.open_resource('sql/from_1.sql', mode='r') as f:
                 db.execute(f.read())
                 ct={}
-                for g in [group for group in prefix[app.config.get("DEFAULT_HOST")]]:
-                    ct[g] = {"dt": "", "c": 0}
+                for group in [group for group in prefix[app.config.get("DEFAULT_HOST")]]:
+                    ct[group] = {"dt": "", "c": 0}
 
                 p = db.prepare("UPDATE \"motion\" SET \"identifier\"=$1 WHERE \"id\"=$2")
                 for row in db.prepare("SELECT id, \"type\", \"posed\" FROM \"motion\" ORDER BY \"id\" ASC"):
@@ -187,7 +189,16 @@ def put_motion():
         return "Forbidden", 403
     time = int(request.form.get("days", "3"));
     if time not in times.per_host:
-        return "Error, invalid length", 500
+        return "Error, invalid length", 400
+    title=request.form.get("title", "")
+    title=title.strip()
+    if title =='':
+        return "Error, missing title", 400
+    content=request.form.get("content", "")
+    content=content.strip()
+    if content =='':
+        return "Error, missing content", 400
+
     db = get_db()
     with db.xact():
         t = db.prepare("SELECT CURRENT_TIMESTAMP")()[0][0];
@@ -199,7 +210,7 @@ def put_motion():
         else:
             ident=prefix.per_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)
+        p(title, content, time, g.voter, cat, ident, request.host)
     return rel_redirect("/")
 
 def motion_edited(motion):