views.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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
  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. class PanoramaGenTiles(CelutzLoginMixin, RedirectView):
  34. permanent = False
  35. pattern_name = "panorama:view_pano"
  36. def get_redirect_url(self, *args, **kwargs):
  37. pano = get_object_or_404(Panorama, pk=kwargs['pk'])
  38. pano.generate_tiles()
  39. return super(PanoramaGenTiles, self).get_redirect_url(*args, **kwargs)
  40. class PanoramaList(CelutzLoginMixin, ListView):
  41. model = Panorama
  42. template_name = "panorama/list.html"
  43. context_object_name = "panoramas"
  44. class LocatePointView(CelutzLoginMixin, TemplateView):
  45. """View to choose a point to locate (either an existing reference point,
  46. or from GPS coordinates)"""
  47. template_name = 'panorama/locate_point.html'
  48. def get_context_data(self, **kwargs):
  49. context = super(LocatePointView, self).get_context_data(**kwargs)
  50. context['refpoints_form'] = SelectReferencePointForm
  51. context['custom_point_form'] = CustomPointForm
  52. return context
  53. def compute_interesting_panoramas(self, point):
  54. """Compute all panoramas that see the given point, along with the distance
  55. and direction from each panorama towards the point. Returns a
  56. list of (panorama, distance, bearing, elevation) triples.
  57. """
  58. if isinstance(point, ReferencePoint):
  59. queryset = Panorama.objects.exclude(id=point.id)
  60. else:
  61. queryset = Panorama.objects
  62. l = [(pano, pano.line_distance(point), pano.bearing(point), pano.elevation(point))
  63. for pano in queryset.all() if pano.is_visible(point)]
  64. # Sort by increasing distance
  65. return sorted(l, key=lambda x: x[1])
  66. class LocateReferencePointView(LocatePointView):
  67. """Subclass that handles locating a reference point"""
  68. def post(self, request, *args, **kwargs):
  69. context = self.get_context_data()
  70. form = SelectReferencePointForm(request.POST)
  71. context['refpoints_form'] = form
  72. if form.is_valid():
  73. point = form.cleaned_data['reference_point']
  74. context['panoramas'] = self.compute_interesting_panoramas(point)
  75. context['point_name'] = point.name
  76. return super(LocateReferencePointView, self).render_to_response(context)
  77. class LocateCustomPointView(LocatePointView):
  78. """Subclass that handles locating a custom point"""
  79. def post(self, request, *args, **kwargs):
  80. context = self.get_context_data()
  81. form = CustomPointForm(request.POST)
  82. context['custom_point_form'] = form
  83. if form.is_valid():
  84. point = Point(**form.cleaned_data)
  85. context['panoramas'] = self.compute_interesting_panoramas(point)
  86. context['point_lat'] = point.latitude
  87. context['point_lon'] = point.longitude
  88. return super(LocateCustomPointView, self).render_to_response(context)