Browse Source

initial commit

Julien Rabier 13 years ago
commit
cadc3894e8
8 changed files with 5242 additions and 0 deletions
  1. 70 0
      main.py
  2. 7 0
      schema.sql
  3. 3365 0
      static/css/bootstrap.css
  4. 2 0
      static/css/jquery.ui.all.css
  5. 1722 0
      static/js/bootstrap.js
  6. 35 0
      templates/layout.html
  7. 14 0
      templates/login.html
  8. 27 0
      templates/show_votes.html

+ 70 - 0
main.py

@@ -0,0 +1,70 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+from flask import Flask, request, session, g, redirect, url_for, abort, \
+    render_template, flash
+import sqlite3
+from datetime import date
+
+DATABASE = '/tmp/cavote.db'
+SECRET_KEY = '{J@uRKO,xO-PK7B,jF?>iHbxLasF9s#zjOoy=+:'
+DEBUG = True
+USERNAME = 'admin'
+PASSWORD = 'admin'
+
+app = Flask(__name__)
+app.config.from_object(__name__)
+
+def connect_db():
+        return sqlite3.connect(app.config['DATABASE'])
+
+@app.before_request
+def before_request():
+    g.db = connect_db()
+
+@app.teardown_request
+def teardown_request(exception):
+    g.db.close()
+
+@app.route('/admin/votes')
+def show_votes():
+    cur = g.db.execute('select title, description from votes order by id desc')
+    votes = [dict(title=row[0], description=row[1]) for row in cur.fetchall()]
+    return render_template('show_votes.html', votes=votes)
+
+@app.route('/admin/vote/add', methods=['POST'])
+def add_vote():
+    if not session.get('logged_in'):
+        abort(401)
+    g.db.execute('insert into votes (title, description) values (?, ?)',
+                 [request.form['title'], request.form['description']])
+    g.db.commit()
+    flash('New entry was successfully posted')
+    return redirect(url_for('show_votes'))
+
+@app.route('/login', methods=['GET', 'POST'])
+def login():
+    error = None
+    if request.method == 'POST':
+        if request.form['username'] != app.config['USERNAME']:
+            error = 'Invalid username'
+        elif request.form['password'] != app.config['PASSWORD']:
+            error = 'Invalid password'
+        else:
+            session['logged_in'] = True
+            flash('You were logged in')
+            return redirect(url_for('show_votes'))
+    return render_template('login.html', error=error)
+
+@app.route('/logout')
+def logout():
+    session.pop('logged_in', None)
+    flash('You were logged out')
+    return redirect(url_for('show_votes'))
+
+
+
+if __name__ == '__main__':
+    app.run()
+
+

+ 7 - 0
schema.sql

@@ -0,0 +1,7 @@
+drop table if exists votes;
+create table votes (
+    id integer primary key autoincrement,
+    title string not null,
+    description stringnot null
+);
+

File diff suppressed because it is too large
+ 3365 - 0
static/css/bootstrap.css


+ 2 - 0
static/css/jquery.ui.all.css

@@ -0,0 +1,2 @@
+@import "jquery.ui.base.css";
+@import "jquery.ui.theme.css";

File diff suppressed because it is too large
+ 1722 - 0
static/js/bootstrap.js


+ 35 - 0
templates/layout.html

@@ -0,0 +1,35 @@
+<!doctype html>
+<html lang="fr">
+  <head>
+    <meta charset="utf-8">
+    <title>CA vote ou pas </title>
+    <!-- meta -->
+    <!-- icon
+    <link rel="shortcut icon" href="favicon.ico"> -->
+    <!-- ma template.css -->
+    <link rel=stylesheet type=text/css href="{{ url_for('static', filename='css/bootstrap.css') }}">
+    <!-- css javascript -->
+    <link rel=stylesheet type=text/css href="{{ url_for('static', filename='jquery.ui.all.css') }}">
+    <!-- javascript -->
+</head>
+<div class="container-fluid">
+  <div class=metanav>
+  {% if not session.logged_in %}
+    <a href="{{ url_for('login') }}">log in</a>
+  {% else %}
+    <a href="{{ url_for('logout') }}">log out</a>
+  {% endif %}
+  </div>
+<h1><a href="/" style="text-decoration:none;">votes</a></h1>
+{% with messages = get_flashed_messages() %}
+  {% if messages %}
+    <ul class=flashes>
+    {% for message in messages %}
+      <li>{{ message }}</li>
+    {% endfor %}
+    </ul>
+  {% endif %}
+{% endwith %}
+{% block body %}{% endblock %}
+</div>    
+    <script src="{{ url_for('static', filename='js/boostrap.js') }}"></script>

+ 14 - 0
templates/login.html

@@ -0,0 +1,14 @@
+{% extends "layout.html" %}
+{% block body %}
+  <h2>Login</h2>
+  {% if error %}<p class=error><strong>Error:</strong> {{ error }}{% endif %}
+  <form action="{{ url_for('login') }}" method=post>
+    <dl>
+      <dt>Username:
+      <dd><input type=text name=username>
+      <dt>Password:
+      <dd><input type=password name=password>
+      <dd><input type=submit value=Login>
+    </dl>
+  </form>
+{% endblock %}

+ 27 - 0
templates/show_votes.html

@@ -0,0 +1,27 @@
+{% extends "layout.html" %}
+{% block body %}
+    <h2>Administration - Liste des votes</h2>
+
+    <h3> Votes </h3>
+  {% if session.logged_in %}
+    <form action="{{ url_for('add_vote') }}" method=post class=add-vote>
+      <dl>
+        <dt>Titre&nbsp;:
+        <dd><input type=text size=30 name=title>
+        <dt>Description&nbsp;:
+        <dd><textarea name=description rows=5 cols=40></textarea>
+        <dd><input type=submit value=Paf>
+      </dl>
+    </form>
+  {% endif %}
+  <ul class=votes>
+  {% for vote in votes %}
+    <li><h2>{{ vote.title }}</h2>{{ vote.description|safe }}
+  {% else %}
+    <li><em>Pas encore de votes</em>
+  {% endfor %}
+  </ul>
+    Retour à l'<a href="/">accueil</a>
+
+{% endblock %}
+