backend.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. import os
  4. import sys
  5. import sqlite3
  6. import urlparse
  7. from os.path import join, dirname
  8. from bottle import route, run, static_file, request, template, FormsDict, redirect
  9. ORIENTATIONS = (
  10. ('N', 'Nord'),
  11. ('NO', 'Nord-Ouest'),
  12. ('O', 'Ouest'),
  13. ('SO', 'Sud-Ouest'),
  14. ('S', 'Sud'),
  15. ('SE', 'Sud-Est'),
  16. ('E', 'Est'),
  17. ('NE', 'Nord-Est'),
  18. )
  19. TABLE_NAME = 'contribs'
  20. DB_FILENAME = join(dirname(__file__), 'db.sqlite3')
  21. DB = sqlite3.connect(DB_FILENAME)
  22. DB_COLS = (
  23. ('id', 'INTEGER PRIMARY KEY'),
  24. ('name', 'TEXT'),
  25. ('contrib_type', 'TEXT'),
  26. ('latitude', 'REAL'),
  27. ('longitude', 'REAL'),
  28. ('phone', 'TEXT'),
  29. ('email', 'TEXT'),
  30. ('access_type', 'TEXT'),
  31. ('bandwidth', 'REAL'),
  32. ('share_part', 'REAL'),
  33. ('floor', 'INTEGER'),
  34. ('orientations', 'TEXT'),
  35. ('comment', 'TEXT'),
  36. ('privacy_name', 'INTEGER'),
  37. ('privacy_email', 'INTEGER'),
  38. ('privacy_coordinates', 'INTEGER'),
  39. ('privacy_place_details', 'INTEGER'),
  40. ('privacy_comment', 'INTEGER'),
  41. )
  42. @route('/wifi-form')
  43. def show_wifi_form():
  44. return template('wifi-form', errors=None, data = FormsDict(),
  45. orientations=ORIENTATIONS)
  46. def create_tabble(db, name, columns):
  47. col_defs = ','.join(['{} {}'.format(*i) for i in columns])
  48. db.execute('CREATE TABLE {} ({})'.format(name, col_defs))
  49. def save_to_db(db, dic):
  50. return db.execute("""
  51. INSERT INTO {}
  52. (name, contrib_type, latitude, longitude, phone, email, access_type, bandwidth, share_part, floor, orientations, comment,
  53. privacy_name, privacy_email, privacy_place_details, privacy_coordinates, privacy_comment)
  54. VALUES (:name, :contrib_type, :latitude, :longitude, :phone, :email, :access_type, :bandwidth, :share_part, :floor, :orientations, :comment,
  55. :privacy_name, :privacy_email, :privacy_place_details, :privacy_coordinates, :privacy_comment)
  56. """.format(TABLE_NAME), dic)
  57. @route('/wifi-form', method='POST')
  58. def submit_wifi_form():
  59. required = ('name', 'contrib-type',
  60. 'latitude', 'longitude')
  61. required_or = (('email', 'phone'),)
  62. required_if = (
  63. ('contrib-type', 'share',('access-type', 'bandwidth',
  64. 'share-part')),
  65. )
  66. field_names = {
  67. 'name' : 'Nom/Pseudo',
  68. 'contrib-type': 'Type de participation',
  69. 'latitude' : 'Localisation',
  70. 'longitude' : 'Localisation',
  71. 'phone' : 'Téléphone',
  72. 'email' : 'Email',
  73. 'access-type' : 'Type de connexion',
  74. 'bandwidth' : 'Bande passante',
  75. 'share-part' : 'Débit partagé',
  76. }
  77. errors = []
  78. for name in required:
  79. if (not request.forms.get(name)):
  80. errors.append((field_names[name], 'ce champ est requis'))
  81. for name_list in required_or:
  82. filleds = [True for name in name_list if request.forms.get(name)]
  83. if len(filleds) <= 0:
  84. errors.append((
  85. ' ou '.join([field_names[i] for i in name_list]),
  86. 'au moins un des de ces champs est requis'))
  87. for key, value, fields in required_if:
  88. if request.forms.get('key') == value:
  89. for name in fields:
  90. if not request.forms.get(name):
  91. errors.append(
  92. (field_names[name], 'ce champ est requis'))
  93. if errors:
  94. return template('wifi-form', errors=errors, data=request.forms,
  95. orientations=ORIENTATIONS)
  96. else:
  97. d = request.forms
  98. save_to_db(DB, {
  99. 'name' : d.get('name'),
  100. 'contrib_type' : d.get('contrib-type'),
  101. 'latitude' : d.get('latitude'),
  102. 'longitude' : d.get('longitude'),
  103. 'phone' : d.get('phone'),
  104. 'email' : d.get('email'),
  105. 'phone' : d.get('phone'),
  106. 'access_type' : d.get('access-type'),
  107. 'bandwidth' : d.get('bandwidth'),
  108. 'share_part' : d.get('share-part'),
  109. 'floor' : d.get('floor'),
  110. 'orientations' : ','.join(d.getall('orientation')),
  111. 'comment' : d.get('comment'),
  112. 'privacy_name' : 'name' in d.getall('privacy'),
  113. 'privacy_email' : 'email' in d.getall('privacy'),
  114. 'privacy_place_details': 'details' in d.getall('privacy'),
  115. 'privacy_coordinates' : 'coordinates' in d.getall('privacy'),
  116. 'privacy_comment' : 'comment' in d.getall('privacy'),
  117. })
  118. DB.commit()
  119. return redirect(urlparse.urljoin(request.path,'thanks'))
  120. @route('/thanks')
  121. def wifi_form_thanks():
  122. return static_file('thanks.html',
  123. root=join(dirname(__file__), 'views/'))
  124. @route('/assets/<filename:path>')
  125. def send_asset(filename):
  126. return static_file(filename, root=join(dirname(__file__), 'assets'))
  127. DEBUG = bool(os.environ.get('DEBUG', False))
  128. if __name__ == '__main__':
  129. if len(sys.argv) > 1:
  130. if sys.argv[1] == 'createdb':
  131. create_tabble(DB, TABLE_NAME, DB_COLS)
  132. else:
  133. run(host='localhost', port=8080, reloader=DEBUG)
  134. DB.close()