<s> from datetime import timedelta <EOL> from django . core . paginator import Paginator <EOL> from django . http import Http404 <EOL> from django . utils . translation import ugettext as _ <EOL> from molly . utils import haversine <EOL> from molly . utils . views import BaseView , ZoomableView <EOL> from molly . utils . breadcrumbs import * <EOL> from molly . maps import Map <EOL> from molly . apps . library . forms import SearchForm <EOL> from molly . apps . library . models import LibrarySearchQuery , LibrarySearchError <EOL> class IndexView ( BaseView ) : <EOL> """<STR_LIT>""" <EOL> def get_metadata ( self , request ) : <EOL> return { <EOL> '<STR_LIT:title>' : _ ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : _ ( "<STR_LIT>" ) , <EOL> } <EOL> def initial_context ( self , request ) : <EOL> return { <EOL> '<STR_LIT>' : SearchForm ( ) <EOL> } <EOL> @ BreadcrumbFactory <EOL> def breadcrumb ( self , request , context ) : <EOL> return Breadcrumb ( self . conf . local_name , None , _ ( '<STR_LIT>' ) , lazy_reverse ( '<STR_LIT:index>' ) ) <EOL> def handle_GET ( self , request , context ) : <EOL> return self . render ( request , context , '<STR_LIT>' , <EOL> expires = timedelta ( days = <NUM_LIT> ) ) <EOL> class SearchDetailView ( BaseView ) : <EOL> """<STR_LIT>""" <EOL> def get_metadata ( self , request ) : <EOL> return { <EOL> '<STR_LIT>' : False , <EOL> } <EOL> def initial_context ( self , request ) : <EOL> return { <EOL> '<STR_LIT>' : SearchForm ( request . GET ) , <EOL> } <EOL> @ BreadcrumbFactory <EOL> def breadcrumb ( self , request , context ) : <EOL> if '<STR_LIT>' in context or context [ '<STR_LIT>' ] . is_valid ( ) : <EOL> title = _ ( '<STR_LIT>' ) <EOL> else : <EOL> title = _ ( '<STR_LIT>' ) <EOL> return Breadcrumb ( <EOL> self . conf . local_name , <EOL> lazy_parent ( '<STR_LIT:index>' ) , <EOL> title , <EOL> lazy_reverse ( '<STR_LIT>' ) , <EOL> ) <EOL> def handle_GET ( self , request , context ) : <EOL> search_form = context [ '<STR_LIT>' ] <EOL> if not ( request . GET and search_form . is_valid ( ) ) : <EOL> return self . render ( request , context , '<STR_LIT>' ) <EOL> try : <EOL> query = LibrarySearchQuery ( <EOL> search_form . cleaned_data [ '<STR_LIT:title>' ] , <EOL> search_form . cleaned_data [ '<STR_LIT>' ] , <EOL> search_form . cleaned_data [ '<STR_LIT>' ] <EOL> ) <EOL> except LibrarySearchQuery . InconsistentQuery , e : <EOL> return self . handle_error ( request , context , e . msg ) <EOL> try : <EOL> results = self . conf . provider . library_search ( query ) <EOL> except LibrarySearchError as e : <EOL> return self . handle_error ( request , context , e . message ) <EOL> paginator = Paginator ( results , <NUM_LIT:10> ) <EOL> try : <EOL> page_index = int ( request . GET [ '<STR_LIT>' ] ) <EOL> except ( ValueError , KeyError ) : <EOL> page_index = <NUM_LIT:1> <EOL> else : <EOL> page_index = min ( max ( <NUM_LIT:1> , page_index ) , paginator . num_pages ) <EOL> page = paginator . page ( page_index ) <EOL> if hasattr ( self . conf , '<STR_LIT>' ) : <EOL> self . conf . additional_metadata_provider . annotate ( page . object_list ) <EOL> page . object_list = list ( page . object_list ) <EOL> context . update ( { <EOL> '<STR_LIT>' : query . removed , <EOL> '<STR_LIT>' : paginator , <EOL> '<STR_LIT>' : page , <EOL> } ) <EOL> return self . render ( request , context , '<STR_LIT>' , <EOL> expires = timedelta ( hours = <NUM_LIT:1> ) ) <EOL> def handle_error ( self , request , context , message ) : <EOL> context [ '<STR_LIT>' ] = message <EOL> return self . render ( request , context , '<STR_LIT>' ) <EOL> AVAIL_COLORS = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> class ItemDetailView ( ZoomableView ) : <EOL> """<STR_LIT>""" <EOL> def initial_context ( self , request , control_number ) : <EOL> context = super ( ItemDetailView , self ) . initial_context ( request ) <EOL> item = self . conf . provider . control_number_search ( control_number ) <EOL> if item is None : <EOL> raise Http404 ( ) <EOL> context . update ( { <EOL> '<STR_LIT>' : item , <EOL> '<STR_LIT>' : control_number , <EOL> } ) <EOL> return context <EOL> @ BreadcrumbFactory <EOL> def breadcrumb ( self , request , context , control_number ) : <EOL> return Breadcrumb ( <EOL> self . conf . local_name , <EOL> lazy_parent ( '<STR_LIT>' ) , <EOL> _ ( '<STR_LIT>' ) , <EOL> lazy_reverse ( '<STR_LIT>' , args = [ control_number ] ) , <EOL> ) <EOL> def handle_GET ( self , request , context , control_number ) : <EOL> user_location = request . session . get ( '<STR_LIT>' ) <EOL> points = [ ] <EOL> point_libraries = [ ] <EOL> lbs = context [ '<STR_LIT>' ] . libraries . items ( ) <EOL> for library , books in lbs : <EOL> library . entity = library . get_entity ( ) <EOL> if user_location : <EOL> lbs = sorted ( lbs , key = lambda ( l , b ) : ( haversine ( user_location , l . entity . location ) <EOL> if l . entity and l . entity . location else float ( '<STR_LIT>' ) ) ) <EOL> for library , books in lbs : <EOL> if library . entity != None and library . entity . location != None : <EOL> colour = AVAIL_COLORS [ max ( b [ '<STR_LIT>' ] for b in books ) ] <EOL> points . append ( ( library . entity . location [ <NUM_LIT:0> ] , <EOL> library . entity . location [ <NUM_LIT:1> ] , <EOL> colour , <EOL> library . entity . title ) ) <EOL> point_libraries . append ( library ) <EOL> if len ( points ) > <NUM_LIT:0> : <EOL> context [ '<STR_LIT>' ] = Map ( <EOL> centre_point = ( user_location [ <NUM_LIT:0> ] , user_location [ <NUM_LIT:1> ] , '<STR_LIT>' , '<STR_LIT>' ) <EOL> if user_location != None else None , <EOL> points = points , <EOL> min_points = <NUM_LIT:0> if context [ '<STR_LIT>' ] else len ( points ) , <EOL> zoom = context [ '<STR_LIT>' ] , <EOL> width = request . map_width , <EOL> height = request . map_height , <EOL> ) <EOL> lib_iter = iter ( point_libraries ) <EOL> for i , ( a , b ) in enumerate ( context [ '<STR_LIT>' ] . points ) : <EOL> for j in range ( len ( b ) ) : <EOL> lib_iter . next ( ) . marker_number = i + <NUM_LIT:1> <EOL> context . update ( { <EOL> '<STR_LIT>' : lbs , <EOL> } ) <EOL> return self . render ( request , context , '<STR_LIT>' ) <EOL> class ItemHoldingsView ( ZoomableView ) : <EOL> """<STR_LIT>""" <EOL> def initial_context ( self , request , control_number , sublocation ) : <EOL> context = super ( ItemHoldingsView , self ) . initial_context ( request ) <EOL> item = self . conf . provider . control_number_search ( control_number ) <EOL> if item is None : <EOL> raise Http404 <EOL> library = None <EOL> for item_library in item . libraries : <EOL> if item_library . location == tuple ( sublocation . split ( '<STR_LIT:/>' ) ) : <EOL> library = item_library <EOL> if library is None : <EOL> raise Http404 <EOL> context . update ( { <EOL> '<STR_LIT>' : item , <EOL> '<STR_LIT>' : library , <EOL> '<STR_LIT>' : control_number , <EOL> '<STR_LIT>' : item . libraries [ library ] , <EOL> } ) <EOL> return context <EOL> def get_metadata ( self , request , control_number , sublocation ) : <EOL> return { <EOL> '<STR_LIT>' : False , <EOL> } <EOL> @ BreadcrumbFactory <EOL> def breadcrumb ( self , request , context , control_number , sublocation ) : <EOL> return Breadcrumb ( <EOL> self . conf . local_name , <EOL> lazy_parent ( '<STR_LIT>' , control_number = control_number ) , <EOL> _ ( '<STR_LIT>' ) , <EOL> lazy_reverse ( '<STR_LIT>' , args = [ control_number , sublocation ] ) , <EOL> ) <EOL> def handle_GET ( self , request , context , control_number , sublocation ) : <EOL> return self . render ( request , context , '<STR_LIT>' ) </s>
