backend.py 5.3 KB

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