backend.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. import os
  4. from os.path import join, dirname
  5. from bottle import route, run, static_file, request
  6. form_names = {
  7. 'name' : 'Nom/Pseudo',
  8. 'contrib-type': 'Type de participation',
  9. 'latitude' : 'Localisation',
  10. 'longitude' : 'Localisation',
  11. 'phone' : 'Téléphone',
  12. 'email' : 'Email',
  13. 'access-type' : 'Type de connexion',
  14. 'bandwidth' : 'Bande passante',
  15. 'share-part' : 'Débit partagé',
  16. }
  17. @route('/wifi-form')
  18. def show_wifi_form():
  19. return open('index.html').read()
  20. @route('/wifi-form', method='POST')
  21. def submit_wifi_form():
  22. required = ('name', 'contrib-type',
  23. 'latitude', 'longitude', 'orientation')
  24. required_or = (('email', 'phone'),)
  25. required_if = (
  26. ('contrib-type', 'share',('access-type', 'bandwidth',
  27. 'share-part')),
  28. )
  29. errors = []
  30. for name in required:
  31. if (not request.forms.get(name)):
  32. errors.append((name, 'Ce champ est requis'))
  33. for name_list in required_or:
  34. filleds = [True for name in name_list if request.forms.get(name)]
  35. if len(filleds) <= 0:
  36. errors.append((name_list,
  37. 'Au moins un des de ces champs est requis'))
  38. for key, value, fields in required_if:
  39. if request.forms.get('key') == value:
  40. for name in fields:
  41. if not request.forms.get(name):
  42. errors.append((name, 'Ce champ est requis'))
  43. if errors:
  44. return str(errors)
  45. else:
  46. return 'OK'
  47. @route('/assets/<filename:path>')
  48. def send_asset(filename):
  49. return static_file(filename, root=join(dirname(__file__), 'assets'))
  50. DEBUG = bool(os.environ.get('DEBUG', False))
  51. run(host='localhost', port=8080, reloader=DEBUG)