<s> import re <EOL> from operator import itemgetter <EOL> from django . contrib . gis . geos import Point <EOL> from django . utils . translation import ugettext as _ <EOL> from django . core . urlresolvers import reverse <EOL> from django . http import Http404 <EOL> from django . template . defaultfilters import capfirst <EOL> from molly . conf import app_by_application_name <EOL> from molly . utils . views import BaseView <EOL> from molly . utils . breadcrumbs import * <EOL> from molly . favourites import get_favourites <EOL> from molly . apps . places import get_entity , bus_route_sorter <EOL> from molly . apps . places . models import Entity , EntityType , Route <EOL> class TransportView ( BaseView ) : <EOL> def record_page ( self , request , page ) : <EOL> request . session [ '<STR_LIT>' ] = page <EOL> request . session . save ( ) <EOL> def augment_metadata ( self , entities , ** kwargs ) : <EOL> places_conf = app_by_application_name ( '<STR_LIT>' ) <EOL> for provider in reversed ( places_conf . providers ) : <EOL> provider . augment_metadata ( entities , ** kwargs ) <EOL> def initial_context ( self , request ) : <EOL> context = super ( TransportView , self ) . initial_context ( request ) <EOL> location = request . session . get ( '<STR_LIT>' ) <EOL> if location : <EOL> location = Point ( location , srid = <NUM_LIT> ) <EOL> context [ '<STR_LIT:location>' ] = location <EOL> context [ '<STR_LIT>' ] = hasattr ( self . conf , '<STR_LIT>' ) <EOL> context [ '<STR_LIT>' ] = getattr ( self . conf , '<STR_LIT>' , False ) <EOL> context [ '<STR_LIT>' ] = hasattr ( self . conf , '<STR_LIT>' ) <EOL> context [ '<STR_LIT>' ] = dict ( ( key , True ) <EOL> for key in getattr ( self . conf , '<STR_LIT>' , { } ) ) <EOL> return context <EOL> class IndexView ( BaseView ) : <EOL> @ BreadcrumbFactory <EOL> def breadcrumb ( self , request , context ) : <EOL> return Breadcrumb ( <EOL> self . conf . local_name , <EOL> None , <EOL> _ ( '<STR_LIT>' ) , <EOL> lazy_reverse ( '<STR_LIT>' % self . conf . local_name ) , <EOL> ) <EOL> def handle_GET ( self , request , context ) : <EOL> last_part = request . session . get ( '<STR_LIT>' , <EOL> getattr ( self . conf , '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> if last_part in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> redirect_to = reverse ( '<STR_LIT>' % last_part ) <EOL> else : <EOL> redirect_to = reverse ( '<STR_LIT>' , args = [ last_part ] ) <EOL> return self . redirect ( redirect_to , request ) <EOL> class RailView ( TransportView ) : <EOL> @ BreadcrumbFactory <EOL> def breadcrumb ( self , request , context ) : <EOL> return Breadcrumb ( <EOL> self . conf . local_name , <EOL> None , <EOL> _ ( '<STR_LIT>' ) if context [ '<STR_LIT>' ] == '<STR_LIT>' else _ ( '<STR_LIT>' ) , <EOL> lazy_reverse ( '<STR_LIT>' % self . conf . local_name ) , <EOL> ) <EOL> def initial_context ( self , request ) : <EOL> context = super ( RailView , self ) . initial_context ( request ) <EOL> if context [ '<STR_LIT>' ] : <EOL> if getattr ( self . conf , '<STR_LIT>' , False ) and context [ '<STR_LIT:location>' ] : <EOL> et = EntityType . objects . get ( slug = '<STR_LIT>' ) <EOL> entity = et . entities_completion . filter ( location__isnull = False ) <EOL> entity = entity . distance ( context [ '<STR_LIT:location>' ] ) . order_by ( '<STR_LIT>' ) [ <NUM_LIT:0> ] <EOL> else : <EOL> scheme , value = self . conf . train_station . split ( '<STR_LIT::>' ) <EOL> entity = get_entity ( scheme , value ) <EOL> context [ '<STR_LIT>' ] = entity <EOL> else : <EOL> raise Http404 ( ) <EOL> places_conf = app_by_application_name ( '<STR_LIT>' ) <EOL> attributions = [ provider . _ATTRIBUTION for provider in places_conf . providers <EOL> if hasattr ( provider , '<STR_LIT>' ) ] <EOL> context [ '<STR_LIT>' ] = attributions <EOL> context [ '<STR_LIT>' ] = request . GET . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . augment_metadata ( [ entity ] , board = context [ '<STR_LIT>' ] ) <EOL> return context <EOL> def handle_GET ( self , request , context ) : <EOL> self . record_page ( request , '<STR_LIT>' ) <EOL> return self . render ( request , context , '<STR_LIT>' ) <EOL> class TravelNewsView ( TransportView ) : <EOL> @ BreadcrumbFactory <EOL> def breadcrumb ( self , request , context ) : <EOL> return Breadcrumb ( <EOL> self . conf . local_name , <EOL> None , <EOL> _ ( '<STR_LIT>' ) , <EOL> lazy_reverse ( '<STR_LIT>' % self . conf . local_name ) , <EOL> ) <EOL> def initial_context ( self , request ) : <EOL> context = super ( TravelNewsView , self ) . initial_context ( request ) <EOL> if context [ '<STR_LIT>' ] : <EOL> es = Entity . objects . filter ( primary_type__slug = '<STR_LIT>' ) <EOL> if context [ '<STR_LIT:location>' ] : <EOL> es = es . filter ( location__isnull = False ) . distance ( location ) . order_by ( '<STR_LIT>' ) <EOL> else : <EOL> es = es . order_by ( '<STR_LIT:title>' ) <EOL> context [ '<STR_LIT>' ] = es <EOL> else : <EOL> raise Http404 ( ) <EOL> return context <EOL> def handle_GET ( self , request , context ) : <EOL> self . record_page ( request , '<STR_LIT>' ) <EOL> return self . render ( request , context , '<STR_LIT>' ) <EOL> class ParkAndRideView ( TransportView ) : <EOL> @ BreadcrumbFactory <EOL> def breadcrumb ( self , request , context ) : <EOL> return Breadcrumb ( <EOL> self . conf . local_name , <EOL> None , <EOL> _ ( '<STR_LIT>' ) , <EOL> lazy_reverse ( '<STR_LIT>' % self . conf . local_name ) , <EOL> ) <EOL> def initial_context ( self , request ) : <EOL> context = super ( ParkAndRideView , self ) . initial_context ( request ) <EOL> if context [ '<STR_LIT>' ] : <EOL> park_and_rides = [ ] <EOL> for park_and_ride in self . conf . park_and_rides : <EOL> scheme , value = park_and_ride . split ( '<STR_LIT::>' ) <EOL> entity = get_entity ( scheme , value ) <EOL> park_and_rides . append ( entity ) <EOL> context [ '<STR_LIT>' ] = park_and_rides <EOL> else : <EOL> raise Http404 ( ) <EOL> self . augment_metadata ( park_and_rides ) <EOL> return context <EOL> def handle_GET ( self , request , context ) : <EOL> self . record_page ( request , '<STR_LIT>' ) <EOL> return self . render ( request , context , '<STR_LIT>' ) <EOL> class PublicTransportView ( TransportView ) : <EOL> @ BreadcrumbFactory <EOL> def breadcrumb ( self , request , context , key ) : <EOL> type_slug , count = self . conf . nearby [ key ] <EOL> et = EntityType . objects . get ( slug = type_slug ) <EOL> return Breadcrumb ( <EOL> self . conf . local_name , <EOL> None , <EOL> capfirst ( et . verbose_name_plural ) , <EOL> lazy_reverse ( '<STR_LIT>' % self . conf . local_name , <EOL> kwargs = { '<STR_LIT:key>' : key } ) , <EOL> ) <EOL> def initial_context ( self , request , key ) : <EOL> context = super ( PublicTransportView , self ) . initial_context ( request ) <EOL> location = context [ '<STR_LIT:location>' ] <EOL> selected_routes = request . GET . getlist ( '<STR_LIT>' ) <EOL> if key not in getattr ( self . conf , '<STR_LIT>' , { } ) : <EOL> raise Http404 ( ) <EOL> if hasattr ( self . conf , '<STR_LIT>' % key ) : <EOL> provider = getattr ( self . conf , '<STR_LIT>' % key ) <EOL> context [ '<STR_LIT>' ] = provider . get_status ( ) <EOL> type_slug , count = self . conf . nearby [ key ] <EOL> et = EntityType . objects . get ( slug = type_slug ) <EOL> favourites = filter ( <EOL> lambda e : e is not None and et in e . all_types_completion . all ( ) , <EOL> [ f . metadata . get ( '<STR_LIT>' ) for f in get_favourites ( request ) ] ) <EOL> if selected_routes : <EOL> if location : <EOL> es = et . entities_completion . filter ( <EOL> location__isnull = False , route__service_id__in = selected_routes ) <EOL> es = es . distinct ( ) . distance ( location ) . order_by ( '<STR_LIT>' ) [ : count ] <EOL> else : <EOL> es = [ ] <EOL> else : <EOL> if location : <EOL> es = et . entities_completion . filter ( location__isnull = False ) <EOL> es = es . distance ( location ) . order_by ( '<STR_LIT>' ) [ : count ] <EOL> else : <EOL> es = [ ] <EOL> for e in ( e for e in es if hasattr ( e , '<STR_LIT>' ) ) : <EOL> distance , e . bearing = e . get_distance_and_bearing_from ( location ) <EOL> self . augment_metadata ( es , routes = selected_routes ) <EOL> for e in ( e for e in favourites if hasattr ( e , '<STR_LIT>' ) ) : <EOL> distance , e . bearing = e . get_distance_and_bearing_from ( location ) <EOL> self . augment_metadata ( favourites ) <EOL> context . update ( { <EOL> '<STR_LIT>' : key , <EOL> '<STR_LIT:type>' : et , <EOL> '<STR_LIT>' : es , <EOL> '<STR_LIT>' : favourites <EOL> } ) <EOL> routes = Route . objects . filter ( stoponroute__entity__all_types_completion = et ) . distinct ( ) <EOL> route_ids = routes . values_list ( '<STR_LIT>' ) . distinct ( ) <EOL> context [ '<STR_LIT>' ] = sorted ( map ( itemgetter ( <NUM_LIT:0> ) , route_ids ) , key = bus_route_sorter ) <EOL> context [ '<STR_LIT>' ] = selected_routes <EOL> return context <EOL> def handle_GET ( self , request , context , key ) : <EOL> return self . render ( request , context , '<STR_LIT>' ) <EOL> class RoutesView ( TransportView ) : <EOL> @ BreadcrumbFactory <EOL> def breadcrumb ( self , request , context , key ) : <EOL> return Breadcrumb ( <EOL> self . conf . local_name , <EOL> lazy_parent ( '<STR_LIT>' , key = key ) , <EOL> _ ( '<STR_LIT>' ) , <EOL> lazy_reverse ( '<STR_LIT>' % self . conf . local_name ) , <EOL> ) <EOL> def initial_context ( self , request , key ) : <EOL> context = super ( RoutesView , self ) . initial_context ( request ) <EOL> type_slug , count = self . conf . nearby [ key ] <EOL> et = EntityType . objects . get ( slug = type_slug ) <EOL> if key not in getattr ( self . conf , '<STR_LIT>' , { } ) : <EOL> raise Http404 ( ) <EOL> location = context [ '<STR_LIT:location>' ] <EOL> routes = Route . objects . filter ( stoponroute__entity__all_types_completion = et ) . distinct ( ) <EOL> if location : <EOL> routes = list ( routes ) <EOL> for route in routes : <EOL> route . nearest = Entity . objects . filter ( route = route ) . distance ( location ) . order_by ( '<STR_LIT>' ) [ <NUM_LIT:0> ] <EOL> route . nearest_distance , route . nearest_bearing = route . nearest . get_distance_and_bearing_from ( location ) <EOL> context [ '<STR_LIT>' ] = sorted ( routes , key = lambda x : bus_route_sorter ( x . service_id ) ) <EOL> return context <EOL> def handle_GET ( self , request , context , key ) : <EOL> return self . render ( request , context , '<STR_LIT>' ) </s>
