backend.py 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. import os
  4. import sys
  5. import sqlite3
  6. import urlparse
  7. import datetime
  8. import json
  9. from email import utils
  10. from os.path import join, dirname
  11. from bottle import route, run, static_file, request, template, FormsDict, redirect, response
  12. ORIENTATIONS = (
  13. ('N', 'Nord'),
  14. ('NO', 'Nord-Ouest'),
  15. ('O', 'Ouest'),
  16. ('SO', 'Sud-Ouest'),
  17. ('S', 'Sud'),
  18. ('SE', 'Sud-Est'),
  19. ('E', 'Est'),
  20. ('NE', 'Nord-Est'),
  21. )
  22. TABLE_NAME = 'contribs'
  23. DB_FILENAME = join(dirname(__file__), 'db.sqlite3')
  24. DB = sqlite3.connect(DB_FILENAME)
  25. DB_COLS = (
  26. ('id', 'INTEGER PRIMARY KEY'),
  27. ('name', 'TEXT'),
  28. ('contrib_type', 'TEXT'),
  29. ('latitude', 'REAL'),
  30. ('longitude', 'REAL'),
  31. ('phone', 'TEXT'),
  32. ('email', 'TEXT'),
  33. ('access_type', 'TEXT'),
  34. ('bandwidth', 'REAL'),
  35. ('share_part', 'REAL'),
  36. ('floor', 'INTEGER'),
  37. ('floor_total', 'INTEGER'),
  38. ('orientations', 'TEXT'),
  39. ('roof', 'INTEGER'),
  40. ('comment', 'TEXT'),
  41. ('privacy_name', 'INTEGER'),
  42. ('privacy_email', 'INTEGER'),
  43. ('privacy_coordinates', 'INTEGER'),
  44. ('privacy_place_details', 'INTEGER'),
  45. ('privacy_comment', 'INTEGER'),
  46. ('date', 'TEXT'),
  47. )
  48. @route('/')
  49. def home():
  50. redirect("/wifi-form")
  51. @route('/wifi-form')
  52. def show_wifi_form():
  53. return template('wifi-form', errors=None, data = FormsDict(),
  54. orientations=ORIENTATIONS)
  55. def create_tabble(db, name, columns):
  56. col_defs = ','.join(['{} {}'.format(*i) for i in columns])
  57. db.execute('CREATE TABLE {} ({})'.format(name, col_defs))
  58. def save_to_db(db, dic):
  59. # SQLite is picky about encoding else
  60. tosave = {bytes(k):v.decode('utf-8') if isinstance(v,str) else v for k,v in dic.items()}
  61. tosave['date'] = utils.formatdate()
  62. return db.execute("""
  63. INSERT INTO {}
  64. (name, contrib_type, latitude, longitude, phone, email, access_type, bandwidth, share_part, floor, floor_total, orientations, roof, comment,
  65. privacy_name, privacy_email, privacy_place_details, privacy_coordinates, privacy_comment, date)
  66. VALUES (:name, :contrib_type, :latitude, :longitude, :phone, :email, :access_type, :bandwidth, :share_part, :floor, :floor_total, :orientations, :roof, :comment,
  67. :privacy_name, :privacy_email, :privacy_place_details, :privacy_coordinates, :privacy_comment, :date)
  68. """.format(TABLE_NAME), tosave)
  69. @route('/wifi-form', method='POST')
  70. def submit_wifi_form():
  71. required = ('name', 'contrib-type',
  72. 'latitude', 'longitude')
  73. required_or = (('email', 'phone'),)
  74. required_if = (
  75. ('contrib-type', 'share',('access-type', 'bandwidth',
  76. 'share-part')),
  77. )
  78. field_names = {
  79. 'name' : 'Nom/Pseudo',
  80. 'contrib-type': 'Type de participation',
  81. 'latitude' : 'Localisation',
  82. 'longitude' : 'Localisation',
  83. 'phone' : 'Téléphone',
  84. 'email' : 'Email',
  85. 'access-type' : 'Type de connexion',
  86. 'bandwidth' : 'Bande passante',
  87. 'share-part' : 'Débit partagé',
  88. 'floor' : 'Étage',
  89. 'floor_total' : 'Nombre d\'étages total'
  90. }
  91. errors = []
  92. for name in required:
  93. if (not request.forms.get(name)):
  94. errors.append((field_names[name], 'ce champ est requis'))
  95. for name_list in required_or:
  96. filleds = [True for name in name_list if request.forms.get(name)]
  97. if len(filleds) <= 0:
  98. errors.append((
  99. ' ou '.join([field_names[i] for i in name_list]),
  100. 'au moins un des de ces champs est requis'))
  101. for key, value, fields in required_if:
  102. if request.forms.get('key') == value:
  103. for name in fields:
  104. if not request.forms.get(name):
  105. errors.append(
  106. (field_names[name], 'ce champ est requis'))
  107. floor = request.forms.get('floor')
  108. floor_total = request.forms.get('floor_total')
  109. if floor and not floor_total:
  110. errors.append((field_names['floor_total'], "ce champ est requis"))
  111. elif not floor and floor_total:
  112. errors.append((field_names['floor'], "ce champ est requis"))
  113. elif floor and floor_total and (int(floor) > int(floor_total)):
  114. errors.append((field_names['floor'], "Étage supérieur au nombre total"))
  115. if errors:
  116. return template('wifi-form', errors=errors, data=request.forms,
  117. orientations=ORIENTATIONS)
  118. else:
  119. d = request.forms
  120. save_to_db(DB, {
  121. 'name' : d.get('name'),
  122. 'contrib_type' : d.get('contrib-type'),
  123. 'latitude' : d.get('latitude'),
  124. 'longitude' : d.get('longitude'),
  125. 'phone' : d.get('phone'),
  126. 'email' : d.get('email'),
  127. 'phone' : d.get('phone'),
  128. 'access_type' : d.get('access-type'),
  129. 'bandwidth' : d.get('bandwidth'),
  130. 'share_part' : d.get('share-part'),
  131. 'floor' : d.get('floor'),
  132. 'floor_total' : d.get('floor_total'),
  133. 'orientations' : ','.join(d.getall('orientation')),
  134. 'roof' : d.get('roof'),
  135. 'comment' : d.get('comment'),
  136. 'privacy_name' : 'name' in d.getall('privacy'),
  137. 'privacy_email' : 'email' in d.getall('privacy'),
  138. 'privacy_place_details': 'place_details' in d.getall('privacy'),
  139. 'privacy_coordinates' : 'coordinates' in d.getall('privacy'),
  140. 'privacy_comment' : 'comment' in d.getall('privacy'),
  141. })
  142. DB.commit()
  143. # Rebuild GeoJSON
  144. build_geojson()
  145. return redirect(urlparse.urljoin(request.path,'thanks'))
  146. @route('/thanks')
  147. def wifi_form_thanks():
  148. return template('thanks')
  149. @route('/assets/<filename:path>')
  150. def send_asset(filename):
  151. return static_file(filename, root=join(dirname(__file__), 'assets'))
  152. @route('/legal')
  153. def legal():
  154. return template('legal')
  155. """
  156. Results Map
  157. """
  158. @route('/map')
  159. def public_map():
  160. geojsonPath = 'public.json'
  161. return template('map', geojson=geojsonPath)
  162. @route('/public.json')
  163. def public_geojson():
  164. return static_file('public.json', root=join(dirname(__file__), 'json/'))
  165. """
  166. GeoJSON Functions
  167. """
  168. # Save feature collection to a json file
  169. def save_featurecollection_json(id, features):
  170. with open('json/' + id + '.json', 'w') as outfile:
  171. json.dump({
  172. "type" : "FeatureCollection",
  173. "features" : features,
  174. "id" : id,
  175. }, outfile)
  176. # Build GeoJSON files from DB
  177. def build_geojson():
  178. # Read from DB
  179. DB.row_factory = sqlite3.Row
  180. cur = DB.execute("""
  181. SELECT * FROM {} ORDER BY id DESC
  182. """.format(TABLE_NAME))
  183. public_features = []
  184. private_features = []
  185. # Loop through results
  186. rows = cur.fetchall()
  187. for row in rows:
  188. # Private JSON file
  189. private_features.append({
  190. "type" : "Feature",
  191. "geometry" : {
  192. "type": "Point",
  193. "coordinates": [row['longitude'], row['latitude']],
  194. },
  195. "id" : row['id'],
  196. "properties": {
  197. "name" : row['name'],
  198. "place" : {
  199. 'floor' : row['floor'],
  200. 'floor_total' : row['floor_total'],
  201. 'orientations' : row['orientations'].split(','),
  202. 'roof' : row['roof'],
  203. },
  204. "comment" : row['comment']
  205. }
  206. })
  207. # Bypass non-public points
  208. if not row['privacy_coordinates']:
  209. continue
  210. # Public JSON file
  211. public_feature = {
  212. "type" : "Feature",
  213. "geometry" : {
  214. "type": "Point",
  215. "coordinates": [row['longitude'], row['latitude']],
  216. },
  217. "id" : row['id'],
  218. "properties": {}
  219. }
  220. # Add optionnal variables
  221. if row['privacy_name']:
  222. public_feature['properties']['name'] = row['name']
  223. if row['privacy_comment']:
  224. public_feature['properties']['comment'] = row['comment']
  225. if row['privacy_place_details']:
  226. public_feature['properties']['place'] = {
  227. 'floor' : row['floor'],
  228. 'floor_total' : row['floor_total'],
  229. 'orientations' : row['orientations'].split(','),
  230. 'roof' : row['roof'],
  231. }
  232. # Add to public features list
  233. public_features.append(public_feature)
  234. # Build GeoJSON Feature Collection
  235. save_featurecollection_json('private', private_features)
  236. save_featurecollection_json('public', public_features)
  237. DEBUG = bool(os.environ.get('DEBUG', False))
  238. if __name__ == '__main__':
  239. if len(sys.argv) > 1:
  240. if sys.argv[1] == 'createdb':
  241. create_tabble(DB, TABLE_NAME, DB_COLS)
  242. if sys.argv[1] == 'buildgeojson':
  243. build_geojson()
  244. else:
  245. run(host='localhost', port=8080, reloader=DEBUG)
  246. DB.close()