views.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 thanks(request):
  38. return render(request, 'contribmap/thanks.html')
  39. class JSONContribView(View):
  40. def get(self, request):
  41. return JsonResponse({
  42. "id": self.ID,
  43. "license": self.LICENSE,
  44. "features": self.get_features(),
  45. })
  46. PLACE_PROPERTIES = [
  47. 'floor', 'angles', 'orientations', 'roof', 'floor', 'floor_total']
  48. class PublicJSON(JSONContribView):
  49. ID = 'public'
  50. LICENSE = {
  51. "type": "ODC-BY-1.0",
  52. "url": "http:\/\/opendatacommons.org\/licenses\/by\/1.0\/"
  53. }
  54. def get_features(self):
  55. contribs = Contrib.objects.all()
  56. data = []
  57. for i in contribs:
  58. if not i.is_public():
  59. continue
  60. data.append({
  61. "id": i.pk,
  62. "type": "Feature",
  63. "geometry": {
  64. "coordinates": [
  65. i.latitude,
  66. i.longitude,
  67. ],
  68. "type": "Point",
  69. },
  70. "properties": {
  71. "contrib_type": i.contrib_type,
  72. "name": i.get_public_field('name'),
  73. "place": {
  74. k: i.get_public_field(k) for k in self.PLACE_PROPERTIES
  75. },
  76. "comment": i.get_public_field('comment'),
  77. }
  78. })
  79. return data
  80. class PrivateJSON(JSONContribView):
  81. ID = 'private'
  82. LICENSE = {
  83. "type": "Copyright",
  84. }
  85. def dispatch(self, request, *args, **kwargs):
  86. if hasattr(request, 'user') and request.user.is_staff:
  87. return super().dispatch(request, *args, **kwargs)
  88. else:
  89. return HttpResponseForbidden('Need staff access')
  90. def get_features(self):
  91. contribs = Contrib.objects.all()
  92. data = []
  93. for i in contribs:
  94. if not i.is_public():
  95. continue
  96. data.append({
  97. "id": i.pk,
  98. "type": "Feature",
  99. "geometry": {
  100. "coordinates": [
  101. i.latitude,
  102. i.longitude,
  103. ],
  104. "type": "Point",
  105. },
  106. "properties": {
  107. "contrib_type": i.contrib_type,
  108. "name": i.name,
  109. "place": {
  110. k: getattr(i, k) for k in self.PLACE_PROPERTIES
  111. },
  112. "comment": i.comment,
  113. "phone": i.phone,
  114. "email": i.email
  115. }
  116. })
  117. return data