views.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. # -*- coding: utf-8 -*-
  2. from __future__ import unicode_literals
  3. from django.conf import settings
  4. from django.core.urlresolvers import reverse_lazy
  5. from django.http import HttpResponse, JsonResponse
  6. from django.shortcuts import render, get_object_or_404
  7. from django.views.generic import CreateView, DetailView, RedirectView, ListView, TemplateView
  8. from django.contrib.auth.mixins import LoginRequiredMixin
  9. from .models import Point, Panorama, ReferencePoint
  10. from .forms import SelectReferencePointForm, CustomPointForm, PanoramaForm
  11. class CelutzLoginMixin(LoginRequiredMixin):
  12. """Mixin that acts like LoginRequiredMixin if settings.LOGIN_REQUIRED is
  13. True, and does nothing otherwise. It allows to choose whether
  14. accessing celutz requires an account or is open to anybody.
  15. """
  16. login_url = '/admin/login/'
  17. def dispatch(self, request, *args, **kwargs):
  18. """Small hack: either call our parent (LoginRequiredMixin) or bypass it"""
  19. if settings.LOGIN_REQUIRED:
  20. return super(CelutzLoginMixin, self).dispatch(request, *args, **kwargs)
  21. else:
  22. return super(LoginRequiredMixin, self).dispatch(request, *args, **kwargs)
  23. class PanoramaUpload(CelutzLoginMixin, CreateView):
  24. model = Panorama
  25. fields = ('name', 'image', 'loop', 'latitude', 'longitude', 'altitude')
  26. template_name = "panorama/new.html"
  27. def get_success_url(self):
  28. return reverse_lazy("panorama:gen_tiles", kwargs={"pk": self.object.id})
  29. class PanoramaView(CelutzLoginMixin, DetailView):
  30. model = Panorama
  31. template_name = "panorama/view.html"
  32. context_object_name = "panorama"
  33. def get_context_data(self, **kwargs):
  34. context = super(PanoramaView, self).get_context_data(**kwargs)
  35. pano = context['panorama']
  36. context['panoramas'] = [p for p in Panorama.objects.all() if pano.great_circle_distance(p) <= settings.PANORAMA_MAX_DISTANCE]
  37. return context
  38. class PanoramaGenTiles(CelutzLoginMixin, RedirectView):
  39. permanent = False
  40. pattern_name = "panorama:view_pano"
  41. def get_redirect_url(self, *args, **kwargs):
  42. pano = get_object_or_404(Panorama, pk=kwargs['pk'])
  43. pano.generate_tiles()
  44. return super(PanoramaGenTiles, self).get_redirect_url(*args, **kwargs)
  45. class MainView(CelutzLoginMixin, TemplateView):
  46. template_name = "panorama/main.html"
  47. def get_context_data(self, **kwargs):
  48. context = super(MainView, self).get_context_data(**kwargs)
  49. context['refpoints_form'] = SelectReferencePointForm
  50. context['custom_point_form'] = CustomPointForm
  51. context['newpanorama_form'] = PanoramaForm
  52. context['panoramas'] = Panorama.objects.all()
  53. return context
  54. def compute_interesting_panoramas(self, point):
  55. """Compute all panoramas that see the given point, along with the distance
  56. and direction from each panorama towards the point. Returns a
  57. list of (panorama, distance, bearing, elevation) triples.
  58. """
  59. if isinstance(point, ReferencePoint):
  60. queryset = Panorama.objects.exclude(id=point.id)
  61. else:
  62. queryset = Panorama.objects
  63. l = [(pano, pano.line_distance(point), pano.bearing(point), pano.elevation(point))
  64. for pano in queryset.all() if pano.is_visible(point)]
  65. # Sort by increasing distance
  66. return sorted(l, key=lambda x: x[1])
  67. class LocateReferencePointView(MainView):
  68. """Displays a located reference point"""
  69. template_name = 'panorama/locate_point.html'
  70. def post(self, request, *args, **kwargs):
  71. context = self.get_context_data()
  72. form = SelectReferencePointForm(request.POST)
  73. if form.is_valid():
  74. point = form.cleaned_data['reference_point']
  75. context['located_panoramas'] = self.compute_interesting_panoramas(point)
  76. context['located_point_name'] = point.name
  77. context['located_point_lat'] = point.latitude
  78. context['located_point_lon'] = point.longitude
  79. return super(LocateReferencePointView, self).render_to_response(context)
  80. class LocateCustomPointView(MainView):
  81. """Displays a located custom GPS point"""
  82. template_name = 'panorama/locate_point.html'
  83. def post(self, request, *args, **kwargs):
  84. context = self.get_context_data()
  85. form = CustomPointForm(request.POST)
  86. if form.is_valid():
  87. point = Point(**form.cleaned_data)
  88. context['located_panoramas'] = self.compute_interesting_panoramas(point)
  89. context['located_point_lat'] = point.latitude
  90. context['located_point_lon'] = point.longitude
  91. return super(LocateCustomPointView, self).render_to_response(context)