]> WPIA git - motion.git/commitdiff
initial commit for motion applicaiton
authorFelix Dörre <felix@dogcraft.de>
Tue, 14 Nov 2017 13:34:54 +0000 (14:34 +0100)
committerFelix Dörre <felix@dogcraft.de>
Tue, 14 Nov 2017 14:01:59 +0000 (15:01 +0100)
.gitignore [new file with mode: 0644]
README.md [new file with mode: 0644]
config.py.example [new file with mode: 0644]
motion.py [new file with mode: 0644]
requirements.txt [new file with mode: 0644]
schema.sql [new file with mode: 0644]
templates/base.html [new file with mode: 0644]
templates/index.html [new file with mode: 0644]
templates/motion.html [new file with mode: 0644]
templates/single_motion.html [new file with mode: 0644]

diff --git a/.gitignore b/.gitignore
new file mode 100644 (file)
index 0000000..9a2dabd
--- /dev/null
@@ -0,0 +1,12 @@
+/__pycache__\r
+# virtualenv\r
+/pip-selfcheck.json\r
+/bin\r
+/include\r
+/lib\r
+\r
+/config.py\r
+\r
+# emacs\r
+\#*\#\r
+*~\r
diff --git a/README.md b/README.md
new file mode 100644 (file)
index 0000000..5eb4ec2
--- /dev/null
+++ b/README.md
@@ -0,0 +1,20 @@
+# Installation
+Requires 3.
+To install:
+```
+virtualenv -p python3 .
+. bin/activate
+pip install -r requirements.txt
+```
+Then edit config.py.example into config.py with your database connection
+
+To debug-run:
+```
+LANG=C.UTF-8 FLASK_DEBUG=1 FLASK_APP=motion.py flask run
+```
+
+To install database schema, run in an interactive python shell (`python`):
+```
+import motion
+motion.init_db()
+```
diff --git a/config.py.example b/config.py.example
new file mode 100644 (file)
index 0000000..daa9594
--- /dev/null
@@ -0,0 +1,3 @@
+DATABASE="..."
+USER="..."
+PASSWORD="..."
diff --git a/motion.py b/motion.py
new file mode 100644 (file)
index 0000000..52547ce
--- /dev/null
+++ b/motion.py
@@ -0,0 +1,74 @@
+from flask import g
+from flask import Flask
+from flask import render_template, redirect
+from flask import request
+import postgresql
+import config
+
+
+def get_db():
+    db = getattr(g, '_database', None)
+    if db is None:
+        db = g._database = postgresql.open(config.DATABASE, user=config.USER, password=config.PASSWORD)
+    #db.row_factory = sqlite3.Row
+    return db
+
+app = Flask(__name__)
+
+@app.teardown_appcontext
+def close_connection(exception):
+    db = getattr(g, '_database', None)
+    if db is not None:
+        db.close()
+
+def init_db():
+    with app.app_context():
+        db = get_db()
+        with app.open_resource('schema.sql', mode='r') as f:
+            db.execute(f.read())
+
+@app.route("/")
+def main():
+    start=int(request.args.get("start", "-1"));
+    q = "SELECT *, motion.deadline > CURRENT_TIMESTAMP AS running FROM motion LEFT JOIN (SELECT motion_id, voter_id, "\
+                             + "COUNT(CASE WHEN result='yes' THEN 'yes' ELSE NULL END) as yes, "\
+                             + "COUNT(CASE WHEN result='no' THEN 'no' ELSE NULL END) as no, "\
+                             + "COUNT(CASE WHEN result='abstain' THEN 'abstain' ELSE NULL END) as abstain "\
+                             + "FROM vote GROUP BY motion_id, voter_id) as votes ON votes.motion_id=motion.id "
+    if start == -1:
+        p = get_db().prepare(q + "ORDER BY id DESC LIMIT 11")
+        rv = p()
+    else:
+        p = get_db().prepare(q + "WHERE id <= $1 ORDER BY id DESC LIMIT 11")
+        rv = p(start)
+    return render_template('index.html', motions=rv[:10], more=rv[10]["id"] if len(rv) == 11 else None)
+
+@app.route("/motion", methods=['POST'])
+def put_motion():
+    p = get_db().prepare("INSERT INTO motion(\"name\", \"content\") VALUES($1, $2)")
+    p(request.form.get("title", ""), request.form.get("content",""))
+    return redirect("/")
+
+voter=1
+
+@app.route("/motion/<int:id>")
+def show_motion(id):
+    p = get_db().prepare("SELECT motion.*, motion.deadline > CURRENT_TIMESTAMP AS running, vote.result FROM motion LEFT JOIN vote on vote.motion_id=motion.id AND vote.voter_id=$2 WHERE id=$1")
+    rv = p(id,voter)
+    return render_template('single_motion.html', motion=rv[0])
+
+@app.route("/motion/<int:motion>/vote", methods=['POST'])
+def vote(motion):
+    v = request.form.get("vote", "abstain")
+    db = get_db()
+    with db.xact():
+        p = db.prepare("SELECT deadline > CURRENT_TIMESTAMP FROM motion WHERE id = $1")
+        if not p(motion)[0][0]:
+            return "Error, motion deadline has passed"
+        p = db.prepare("SELECT * FROM vote WHERE motion_id = $1 AND voter_id = $2")
+        rv = p(motion, voter)
+        if len(rv) == 0:
+            db.prepare("INSERT INTO vote (motion_id, voter_id, result) VALUES($1,$2,$3)")(motion,voter,v)
+        else:
+            db.prepare("UPDATE vote SET result=$3, entered=CURRENT_TIMESTAMP WHERE motion_id=$1 AND voter_id = $2")(motion,voter,v)
+    return redirect("/motion/" + str(motion))
diff --git a/requirements.txt b/requirements.txt
new file mode 100644 (file)
index 0000000..e3bed75
--- /dev/null
@@ -0,0 +1,7 @@
+click==6.7
+Flask==0.12.2
+itsdangerous==0.24
+Jinja2==2.10
+MarkupSafe==1.0
+py-postgresql==1.2.1
+Werkzeug==0.12.2
diff --git a/schema.sql b/schema.sql
new file mode 100644 (file)
index 0000000..6fa7a67
--- /dev/null
@@ -0,0 +1,20 @@
+DROP TABLE IF EXISTS voter;
+CREATE TABLE voter (id serial NOT NULL, name VARCHAR(10) NOT NULL, PRIMARY KEY(id));
+
+
+DROP TABLE IF EXISTS motion;
+CREATE TABLE motion (id serial NOT NULL,
+                   name VARCHAR(250) NOT NULL,
+                   content text NOT NULL,
+                   posed timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
+                   deadline timestamp NOT NULL DEFAULT (CURRENT_TIMESTAMP + interval '3 days'),
+                   PRIMARY KEY(id));
+
+DROP TABLE IF EXISTS vote;
+DROP TYPE IF EXISTS "vote_type";
+CREATE TYPE "vote_type" AS ENUM ('yes', 'no', 'abstain');
+CREATE TABLE vote (motion_id INTEGER NOT NULL,
+                 voter_id INTEGER NOT NULL,
+                 result vote_type NOT NULL,
+                 entered timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
+                 PRIMARY KEY(motion_id, voter_id));
diff --git a/templates/base.html b/templates/base.html
new file mode 100644 (file)
index 0000000..8f2b041
--- /dev/null
@@ -0,0 +1,42 @@
+<!DOCTYPE html>
+<html>
+<head>
+<title>{% block title %}Motion list{% endblock %}</title>
+<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
+<style type="text/css">
+.motion {
+  border: 1px solid black;
+}
+.motion .motion-title input {
+  width: 80%;
+}
+.motion textarea {
+  display: block;
+}
+.motion .motion-title{
+  border-bottom: 1px solid black;
+}
+.motion .motion-title .title-text{
+  font-size: 15pt;
+  font-weight: bold;
+}
+.motion .anchor {
+  text-decoration-line: none;
+  color: inherit;
+  margin-right: 5px;
+}
+.motion .motion-title .date {
+  position: relative;
+  right: 0px;
+  margin-right: 5px;
+}
+.motion p {
+  padding: 10px;
+}
+</style>
+</head>
+<body>
+{% block body %}
+{% endblock %}
+</body>
+</html>
diff --git a/templates/index.html b/templates/index.html
new file mode 100644 (file)
index 0000000..57a73f8
--- /dev/null
@@ -0,0 +1,20 @@
+{% extends "base.html" %}
+{% block body %}
+<div class="container">
+<form action="/motion" method="POST">
+<div class="motion panel panel-default">
+  <div class="motion-title panel-heading"><input class="form-control" placeholder="Motion title" type="text" name="title" id="title"></div>
+  <div class="panel-body">
+    <textarea class="form-control" placeholder="Motion content" name="content" rows="8" cols="70"></textarea>
+    <button class="btn btn-primary" type="submit">Submit Motion</button>
+  </div>
+</div>
+</form>
+{% for motion in motions %}
+{% include 'motion.html' %}
+{% endfor %}
+{%- if more %}
+<a href="/?start={{ more }}">Next</a>
+{%- endif %}
+</div>
+{%- endblock %}
diff --git a/templates/motion.html b/templates/motion.html
new file mode 100644 (file)
index 0000000..687d2a3
--- /dev/null
@@ -0,0 +1,18 @@
+<div class="motion panel panel-default" id="motion-{{motion.id}}">
+  <div class="motion-title panel-heading">
+    <a href="/motion/{{motion.id}}" class="anchor">#</a>
+    <span class="title-text">{{motion.name}}</span> ({{ 'Running' if motion.running else 'Finished' }})
+    <div class="date"><div>Posed: {{motion.posed}}</div><div>Votes until: {{motion.deadline}}</div></div>
+  </div>
+  <div class="panel-body">
+    <p>{{motion.content}}</p>
+{%- if motion.yes or motion.no or motion.abstain %}
+    <p>
+{%- for vote in ['yes', 'no', 'abstain'] %}
+{{vote|capitalize}} <span class="badge">{{motion[vote]}}</span><br>
+{%- endfor %}
+    </p>
+{%- endif %}
+  </div>
+{%- block content %}{% endblock %}
+</div>
diff --git a/templates/single_motion.html b/templates/single_motion.html
new file mode 100644 (file)
index 0000000..1078343
--- /dev/null
@@ -0,0 +1,14 @@
+{% extends "base.html" %}
+{% block title -%}
+Motion: {{motion.name}}
+{%- endblock %}
+{% block body %}
+{%- include 'motion.html' %}
+{%- if motion.running %}
+<form action="/motion/{{motion.id}}/vote" method="POST">
+{%- for vote in ['yes','no','abstain'] %}
+<button type="submit" class="btn btn-{{ 'success' if vote == motion.result else 'primary' }}" name="vote" value="{{vote}}" id="vote-{{vote}}">{{vote}}</button>
+{%- endfor %}
+</form>
+{%- endif %}
+{%- endblock %}