Parcourir la source

saving to db !

Jocelyn Delande il y a 10 ans
Parent
commit
e0fe10960a
3 fichiers modifiés avec 84 ajouts et 3 suppressions
  1. 12 0
      README.md
  2. 2 1
      TODO.md
  3. 70 2
      backend.py

+ 12 - 0
README.md

@@ -25,3 +25,15 @@ To run in debug mode (auto-reload)
 
 
 Bottle will reload on source change, but not on template change if you're using
 Bottle will reload on source change, but not on template change if you're using
 an old version of bottle.
 an old version of bottle.
+
+Create the DataBase
+===================
+
+    $ python backend.py createdb
+
+Drop the database
+=================
+
+    $ rm db.sqlite3
+
+What else ?

+ 2 - 1
TODO.md

@@ -6,7 +6,8 @@ Bugs
 Features
 Features
 ========
 ========
 
 
-- saving in db
+- Redirection and thanks message
+- pass the DB name as enviornment
 
 
 Cosmetics/Optional
 Cosmetics/Optional
 ==================
 ==================

+ 70 - 2
backend.py

@@ -1,6 +1,8 @@
 #!/usr/bin/env python
 #!/usr/bin/env python
 # -*- coding: utf-8 -*-
 # -*- coding: utf-8 -*-
 import os
 import os
+import sys
+import sqlite3
 from os.path import join, dirname
 from os.path import join, dirname
 
 
 from bottle import route, run, static_file, request, template, FormsDict
 from bottle import route, run, static_file, request, template, FormsDict
@@ -16,11 +18,49 @@ ORIENTATIONS = (
     ('NE', 'Nord-Est'),
     ('NE', 'Nord-Est'),
 )
 )
 
 
+TABLE_NAME = 'contribs'
+DB_FILENAME = join(dirname(__file__), 'db.sqlite3')
+DB = sqlite3.connect(DB_FILENAME)
+
+DB_COLS = (
+('id', 'INTEGER PRIMARY KEY'),
+('name', 'TEXT'),
+('contrib_type', 'TEXT'),
+('latitude', 'REAL'),
+('longitude', 'REAL'),
+('phone', 'TEXT'),
+('email', 'TEXT'),
+('access_type', 'TEXT'),
+('bandwidth', 'REAL'),
+('share_part', 'REAL'),
+('floor', 'INTEGER'),
+('orientations', 'TEXT'),
+('comment', 'TEXT'),
+('privacy_name', 'INTEGER'),
+('privacy_email', 'INTEGER'),
+('privacy_coordinates', 'INTEGER'),
+('privacy_place_details', 'INTEGER'),
+('privacy_comment', 'INTEGER'),
+)
+
 @route('/wifi-form')
 @route('/wifi-form')
 def show_wifi_form():
 def show_wifi_form():
     return template('wifi-form', errors=None, data = FormsDict(),
     return template('wifi-form', errors=None, data = FormsDict(),
                     orientations=ORIENTATIONS)
                     orientations=ORIENTATIONS)
 
 
+def create_tabble(db, name, columns):
+    col_defs = ','.join(['{} {}'.format(*i) for i in columns])
+    db.execute('CREATE TABLE {} ({})'.format(name, col_defs))
+
+def save_to_db(db, dic):
+    return db.execute("""
+INSERT INTO {}
+(name, contrib_type, latitude, longitude, phone, email, access_type, bandwidth, share_part, floor, orientations, comment,
+privacy_name, privacy_email, privacy_place_details, privacy_coordinates, privacy_comment)
+VALUES (:name, :contrib_type, :latitude, :longitude, :phone, :email, :access_type, :bandwidth, :share_part, :floor, :orientations, :comment,
+        :privacy_name, :privacy_email, :privacy_place_details, :privacy_coordinates, :privacy_comment)
+""".format(TABLE_NAME), dic)
+
 @route('/wifi-form', method='POST')
 @route('/wifi-form', method='POST')
 def submit_wifi_form():
 def submit_wifi_form():
     required = ('name', 'contrib-type',
     required = ('name', 'contrib-type',
@@ -65,7 +105,28 @@ def submit_wifi_form():
         return template('wifi-form', errors=errors, data=request.forms,
         return template('wifi-form', errors=errors, data=request.forms,
                         orientations=ORIENTATIONS)
                         orientations=ORIENTATIONS)
     else:
     else:
-        return 'OK'
+        d = request.forms
+        save_to_db(DB, {
+                'name'         : d.get('name'),
+                'contrib_type' : d.get('contrib-type'),
+                'latitude'     : d.get('latitude'),
+                'longitude'    : d.get('longitude'),
+                'phone'        : d.get('phone'),
+                'email'        : d.get('email'),
+                'phone'        : d.get('phone'),
+                'access_type'          : d.get('access-type'),
+                'bandwidth'            : d.get('bandwidth'),
+                'share_part'           : d.get('share-part'),
+                'floor'                : d.get('floor'),
+                'orientations'         : ','.join(d.getall('orientation')),
+                'comment'              : d.get('comment'),
+                'privacy_name'         : 'name' in d.getall('privacy'),
+                'privacy_email'        : 'email' in d.getall('privacy'),
+                'privacy_place_details': 'details' in d.getall('privacy'),
+                'privacy_coordinates'  : 'coordinates' in d.getall('privacy'),
+                'privacy_comment'      : 'comment' in d.getall('privacy'),
+        })
+        DB.commit()
 
 
 
 
 @route('/assets/<filename:path>')
 @route('/assets/<filename:path>')
@@ -73,4 +134,11 @@ def send_asset(filename):
     return static_file(filename, root=join(dirname(__file__), 'assets'))
     return static_file(filename, root=join(dirname(__file__), 'assets'))
 
 
 DEBUG = bool(os.environ.get('DEBUG', False))
 DEBUG = bool(os.environ.get('DEBUG', False))
-run(host='localhost', port=8080, reloader=DEBUG)
+
+if __name__ == '__main__':
+    if len(sys.argv) > 1:
+        if sys.argv[1] == 'createdb':
+            create_tabble(DB, TABLE_NAME, DB_COLS)
+    else:
+        run(host='localhost', port=8080, reloader=DEBUG)
+        DB.close()