views.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. from django.conf import settings
  2. from django.core.urlresolvers import reverse
  3. from django.core.mail import send_mail
  4. from django.http import JsonResponse, HttpResponseForbidden
  5. from django.shortcuts import render, redirect
  6. from django.template.loader import get_template
  7. from django.views.generic import View
  8. from .forms import PublicContribForm
  9. from .models import Contrib
  10. def add_contrib(request):
  11. if request.method == 'GET':
  12. form = PublicContribForm()
  13. elif request.method == 'POST':
  14. form = PublicContribForm(request.POST)
  15. if form.is_valid():
  16. contrib = form.save()
  17. # Send notification email
  18. if len(settings.NOTIFICATION_EMAILS) > 0:
  19. context = {
  20. 'site_url': settings.SITE_URL,
  21. 'contrib': contrib,
  22. }
  23. subject = get_template(
  24. 'contribmap/mails/new_contrib_notice.subject')
  25. body = get_template(
  26. 'contribmap/mails/new_contrib_notice.txt')
  27. send_mail(
  28. subject.render(context),
  29. body.render(context),
  30. settings.DEFAULT_FROM_EMAIL,
  31. settings.NOTIFICATION_EMAILS,
  32. )
  33. return redirect(reverse('thanks'))
  34. return render(request, 'contribmap/wifi-form.html', {
  35. 'form': form,
  36. })
  37. def display_map(request):
  38. private_mode = request.user.is_authenticated()
  39. if private_mode:
  40. json_url = reverse('private_json')
  41. else:
  42. json_url = reverse('public_json')
  43. return render(request, 'contribmap/map.html', {
  44. 'private_mode': private_mode,
  45. 'json_url': json_url
  46. })
  47. def thanks(request):
  48. return render(request, 'contribmap/thanks.html')
  49. class JSONContribView(View):
  50. def get(self, request):
  51. return JsonResponse({
  52. "id": self.ID,
  53. "license": self.LICENSE,
  54. "features": self.get_features(),
  55. })
  56. PLACE_PROPERTIES = [
  57. 'floor', 'angles', 'orientations', 'roof', 'floor', 'floor_total']
  58. class PublicJSON(JSONContribView):
  59. ID = 'public'
  60. LICENSE = {
  61. "type": "ODC-BY-1.0",
  62. "url": "http:\/\/opendatacommons.org\/licenses\/by\/1.0\/"
  63. }
  64. def get_features(self):
  65. contribs = Contrib.objects.all()
  66. data = []
  67. for i in contribs:
  68. if not i.is_public():
  69. continue
  70. data.append({
  71. "id": i.pk,
  72. "type": "Feature",
  73. "geometry": {
  74. "coordinates": [
  75. i.longitude,
  76. i.latitude
  77. ],
  78. "type": "Point",
  79. },
  80. "properties": {
  81. "contrib_type": i.contrib_type,
  82. "name": i.get_public_field('name'),
  83. "place": {
  84. k: i.get_public_field(k) for k in self.PLACE_PROPERTIES
  85. },
  86. "comment": i.get_public_field('comment'),
  87. }
  88. })
  89. return data
  90. class PrivateJSON(JSONContribView):
  91. ID = 'private'
  92. LICENSE = {
  93. "type": "Copyright",
  94. }
  95. def dispatch(self, request, *args, **kwargs):
  96. if hasattr(request, 'user') and request.user.is_staff:
  97. return super().dispatch(request, *args, **kwargs)
  98. else:
  99. return HttpResponseForbidden('Need staff access')
  100. def get_features(self):
  101. contribs = Contrib.objects.all()
  102. data = []
  103. for i in contribs:
  104. data.append({
  105. "id": i.pk,
  106. "type": "Feature",
  107. "geometry": {
  108. "coordinates": [
  109. i.longitude,
  110. i.latitude,
  111. ],
  112. "type": "Point",
  113. },
  114. "properties": {
  115. "contrib_type": i.contrib_type,
  116. "name": i.name,
  117. "place": {
  118. k: getattr(i, k) for k in self.PLACE_PROPERTIES
  119. },
  120. "comment": i.comment,
  121. "phone": i.phone,
  122. "email": i.email
  123. }
  124. })
  125. return data