backend.py 4.9 KB

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