views.py 4.1 KB

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