views.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. from django.http import JsonResponse
  2. from django.views.generic import View
  3. from .models import Contrib
  4. class JSONView(View):
  5. def get(self, request):
  6. return JsonResponse({
  7. "id": self.ID,
  8. "license": self.LICENSE,
  9. "features": self.get_features(),
  10. })
  11. class PublicJSON(JSONView):
  12. ID = 'public'
  13. LICENSE = {
  14. "type": "ODC-BY-1.0",
  15. "url": "http:\/\/opendatacommons.org\/licenses\/by\/1.0\/"
  16. }
  17. PLACE_PROPERTIES = [
  18. 'floor', 'angles', 'orientations', 'roof', 'floor', 'floor_total']
  19. def get_features(self):
  20. contribs = Contrib.objects.all()
  21. data = []
  22. for i in contribs:
  23. if not i.is_public():
  24. continue
  25. data.append({
  26. "id": i.pk,
  27. "type": "Feature",
  28. "geometry": {
  29. "coordinates": [
  30. i.latitude,
  31. i.longitude,
  32. ],
  33. "type": "Point",
  34. },
  35. "properties": {
  36. "name": i.get_public_field('name'),
  37. "place": {
  38. k: i.get_public_field(k) for k in self.PLACE_PROPERTIES
  39. },
  40. "comment": i.get_public_field('comment'),
  41. }
  42. })
  43. return data