Example #1
0
 def handle_GET(self, request, context, slug, entities):
     
     if len(context['entities']) < 2:
         # Need at least 2 entities to be a tour
         return self.bad_request(request)
     
     # Now attempt to order entities optimally
     if len(context['entities']) > 2 and len(context['entities']) <= 10:
         context['entities'] = optimise_points(
             [(entity, entity.routing_point().location)
                 for entity in context['entities']])
         context['optimised_entities'] = True
     
     # Come up with a name for this tour
     name = _('%(type)s; visiting %(number)d places (created on %(creation)s)') % {
                 'type': _(context['tour_type']['name']),
                 'number': len(context['entities']),
                 'creation': datetime.now().strftime('%c')
             }
     
     # Save back to database
     tour = Tour.objects.create(name=name, type=context['tour_type']['slug'])
     for i, entity in enumerate(context['entities']):
         StopOnTour.objects.create(entity=entity, tour=tour, order=i)
     
     # Add any suggested "passing-by" entities to the context to be presented
     # back to the user. We can only do this query if the database backend
     # supports distance operations on geographies (i.e., things more complex
     # than points)
     if 'suggested_entities' in context['tour_type'] \
     and connection.ops.geography and request.GET.get('nosuggestions') is None:
         route = generate_route([e.location for e in context['entities']], 'foot')
         suggestion_filter = Q()
         for sv in context['tour_type']['suggested_entities']:
             scheme, value = sv.split(':')
             suggestion_filter |= Q(_identifiers__scheme=scheme,
                                    _identifiers__value=value)
         context['suggestions'] = Entity.objects.filter(
             suggestion_filter,
             location__distance_lt=(route['path'],
                     D(m=getattr(self.conf, 'suggestion_distance', 100)))
             ).exclude(id__in=[e.pk for e in context['entities']])
     
     context.update({
         'tour': tour,
         'short_url': get_shortened_url(tour.get_absolute_url(), request),
     })
     
     if 'generic_web_browser' in device_parents[request.browser.devid]:
         # Desktop
         return self.render(request, context, 'tours/save_desktop')
     else:
         # Redirect if no suggestions, otherwise show suggestions page
         if len(context.get('suggestions', [])) > 0:
             return self.render(request, context, 'tours/save')
         else:
             return self.redirect(
                 context['tour'].get_absolute_url() + '?created', request)
Example #2
0
    def handle_GET(self, request, context):
        try:
            path = request.GET['path']
        except (KeyError):
            raise Http404()

        if IndexView in getattr(context['view'], '__mro__', ()):
            return self.redirect(path, request, 'perm')

        context['url'] = get_shortened_url(path, request, context['complex_shorten'])

        return self.render(request, context, 'url_shortener/index',
                           expires=timedelta(days=365))