<s> module = request . controller <EOL> resourcename = request . function <EOL> if not settings . has_module ( module ) : <EOL> raise HTTP ( <NUM_LIT> , body = "<STR_LIT>" % module ) <EOL> ClimateDataPortal = local_import ( "<STR_LIT>" ) <EOL> SampleTable = ClimateDataPortal . SampleTable <EOL> DSL = local_import ( "<STR_LIT>" ) <EOL> def _map_plugin ( ** client_config ) : <EOL> return ClimateDataPortal . MapPlugin ( <EOL> env = Storage ( globals ( ) ) , <EOL> year_max = <NUM_LIT> , <EOL> year_min = <NUM_LIT> , <EOL> place_table = climate_place , <EOL> client_config = client_config <EOL> ) <EOL> def index ( ) : <EOL> try : <EOL> module_name = settings . modules [ module ] . name_nice <EOL> except : <EOL> module_name = T ( "<STR_LIT>" ) <EOL> config = gis . get_config ( ) <EOL> if config . wmsbrowser_url : <EOL> wms_browser = { <EOL> "<STR_LIT:name>" : config . wmsbrowser_name , <EOL> "<STR_LIT:url>" : config . wmsbrowser_url <EOL> } <EOL> else : <EOL> wms_browser = None <EOL> print_service = settings . get_gis_print_service ( ) <EOL> if print_service : <EOL> print_tool = { "<STR_LIT:url>" : print_service } <EOL> else : <EOL> print_tool = { } <EOL> vars = request . vars <EOL> if vars . get ( "<STR_LIT>" , None ) is not None : <EOL> zoom = int ( vars [ "<STR_LIT>" ] ) <EOL> else : <EOL> zoom = <NUM_LIT:7> <EOL> if vars . get ( "<STR_LIT>" , None ) is not None : <EOL> lon , lat = map ( float , vars [ "<STR_LIT>" ] . split ( "<STR_LIT:U+002C>" ) ) <EOL> else : <EOL> lon = <NUM_LIT> <EOL> lat = <NUM_LIT> <EOL> gis_map = gis . show_map ( <EOL> lon = lon , <EOL> lat = lat , <EOL> zoom = zoom , <EOL> toolbar = vars . get ( "<STR_LIT>" , None ) != "<STR_LIT>" , <EOL> googleEarth = True , <EOL> wms_browser = wms_browser , <EOL> plugins = [ <EOL> _map_plugin ( <EOL> ** vars <EOL> ) <EOL> ] <EOL> ) <EOL> response . title = module_name <EOL> return dict ( <EOL> module_name = module_name , <EOL> map = gis_map <EOL> ) <EOL> def climate_overlay_data ( ) : <EOL> kwargs = dict ( request . vars ) <EOL> arguments = { } <EOL> errors = [ ] <EOL> for kwarg_name , converter in dict ( <EOL> query_expression = str , <EOL> ) . iteritems ( ) : <EOL> try : <EOL> value = kwargs . pop ( kwarg_name ) <EOL> except KeyError : <EOL> errors . append ( "<STR_LIT>" % kwarg_name ) <EOL> else : <EOL> try : <EOL> arguments [ kwarg_name ] = converter ( value ) <EOL> except TypeError : <EOL> errors . append ( "<STR_LIT>" % kwarg_name ) <EOL> except AssertionError , assertion_error : <EOL> errors . append ( "<STR_LIT>" % ( kwarg_name , assertion_error ) ) <EOL> if kwargs : <EOL> errors . append ( "<STR_LIT>" % kwargs . keys ( ) ) <EOL> if errors : <EOL> raise HTTP ( <NUM_LIT> , "<STR_LIT>" . join ( errors ) ) <EOL> else : <EOL> try : <EOL> data_path = _map_plugin ( ) . get_overlay_data ( ** arguments ) <EOL> except DSL . DSLSyntaxError , syntax_error : <EOL> raise HTTP ( <NUM_LIT> , json . dumps ( { <EOL> "<STR_LIT:error>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : syntax_error . lineno , <EOL> "<STR_LIT>" : syntax_error . offset , <EOL> "<STR_LIT>" : syntax_error . understood_expression <EOL> } ) ) <EOL> except ( <EOL> DSL . MeaninglessUnitsException , <EOL> DSL . DimensionError , <EOL> DSL . DSLTypeError <EOL> ) , exception : <EOL> raise HTTP ( <NUM_LIT> , json . dumps ( { <EOL> "<STR_LIT:error>" : exception . __class__ . __name__ , <EOL> "<STR_LIT>" : str ( exception ) <EOL> } ) ) <EOL> else : <EOL> return response . stream ( <EOL> open ( data_path , "<STR_LIT:rb>" ) , <EOL> chunk_size = <NUM_LIT> <EOL> ) <EOL> def climate_csv_location_data ( ) : <EOL> kwargs = dict ( request . vars ) <EOL> arguments = { } <EOL> errors = [ ] <EOL> for kwarg_name , converter in dict ( <EOL> query_expression = str , <EOL> ) . iteritems ( ) : <EOL> try : <EOL> value = kwargs . pop ( kwarg_name ) <EOL> except KeyError : <EOL> errors . append ( "<STR_LIT>" % kwarg_name ) <EOL> else : <EOL> try : <EOL> arguments [ kwarg_name ] = converter ( value ) <EOL> except TypeError : <EOL> errors . append ( "<STR_LIT>" % kwarg_name ) <EOL> except AssertionError , assertion_error : <EOL> errors . append ( "<STR_LIT>" % ( kwarg_name , assertion_error ) ) <EOL> if kwargs : <EOL> errors . append ( "<STR_LIT>" % kwargs . keys ( ) ) <EOL> if errors : <EOL> raise HTTP ( <NUM_LIT> , "<STR_LIT>" . join ( errors ) ) <EOL> else : <EOL> data_path = _map_plugin ( ) . get_csv_location_data ( ** arguments ) <EOL> return response . stream ( <EOL> open ( data_path , "<STR_LIT:rb>" ) , <EOL> chunk_size = <NUM_LIT> <EOL> ) <EOL> def climate_chart ( ) : <EOL> import gluon . contenttype <EOL> data_image_file_path = _climate_chart ( gluon . contenttype . contenttype ( "<STR_LIT>" ) ) <EOL> return response . stream ( <EOL> open ( data_image_file_path , "<STR_LIT:rb>" ) , <EOL> chunk_size = <NUM_LIT> <EOL> ) <EOL> def _climate_chart ( content_type ) : <EOL> """<STR_LIT:U+0020>""" <EOL> kwargs = dict ( request . vars ) <EOL> specs = json . loads ( kwargs . pop ( "<STR_LIT>" ) ) <EOL> def list_of ( converter ) : <EOL> def convert_list ( choices ) : <EOL> return map ( converter , choices ) <EOL> return convert_list <EOL> checked_specs = [ ] <EOL> for label , spec in specs . iteritems ( ) : <EOL> arguments = { } <EOL> errors = [ ] <EOL> for name , converter in dict ( <EOL> query_expression = str , <EOL> place_ids = list_of ( int ) <EOL> ) . iteritems ( ) : <EOL> try : <EOL> value = spec . pop ( name ) <EOL> except KeyError : <EOL> errors . append ( "<STR_LIT>" % name ) <EOL> else : <EOL> try : <EOL> arguments [ name ] = converter ( value ) <EOL> except TypeError : <EOL> errors . append ( "<STR_LIT>" % name ) <EOL> except AssertionError , assertion_error : <EOL> errors . append ( "<STR_LIT>" % ( name , assertion_error ) ) <EOL> if spec : <EOL> errors . append ( "<STR_LIT>" % spec . keys ( ) ) <EOL> checked_specs . append ( ( label , arguments ) ) <EOL> if errors : <EOL> raise HTTP ( <NUM_LIT> , "<STR_LIT>" . join ( errors ) ) <EOL> else : <EOL> response . headers [ "<STR_LIT:Content-Type>" ] = content_type <EOL> data_image_file_path = _map_plugin ( ) . render_plots ( <EOL> specs = checked_specs , <EOL> width = int ( kwargs . pop ( "<STR_LIT:width>" ) ) , <EOL> height = int ( kwargs . pop ( "<STR_LIT>" ) ) <EOL> ) <EOL> return data_image_file_path <EOL> def climate_chart_download ( ) : <EOL> data_image_file_path = _climate_chart ( "<STR_LIT>" ) <EOL> import os <EOL> response . headers [ "<STR_LIT>" ] = ( <EOL> "<STR_LIT>" + <EOL> os . path . basename ( data_image_file_path ) <EOL> ) <EOL> return response . stream ( <EOL> open ( data_image_file_path , "<STR_LIT:rb>" ) , <EOL> chunk_size = <NUM_LIT> <EOL> ) <EOL> def chart_popup ( ) : <EOL> """<STR_LIT>""" <EOL> return { } <EOL> def buy_data ( ) : <EOL> """<STR_LIT>""" <EOL> return { } <EOL> def stations ( ) : <EOL> """<STR_LIT>""" <EOL> stations_strings = [ ] <EOL> append = stations_strings . append <EOL> table = s3db . climate_place <EOL> etable = s3db . climate_place_elevation <EOL> itable = s3db . climate_place_station_id <EOL> ntable = s3db . climate_place_station_name <EOL> query = ( table . id == etable . id ) & ( table . id == itable . id ) & ( table . id == ntable . id ) <EOL> rows = db ( query ) . select ( table . id , <EOL> table . longitude , <EOL> table . latitude , <EOL> etable . elevation_metres , <EOL> itable . station_id , <EOL> ntable . name ) <EOL> for place_row in rows : <EOL> append ( <EOL> "<STR_LIT>" . join ( ( <EOL> "<STR_LIT:(>" , str ( place_row . climate_place . id ) , "<STR_LIT>" , <EOL> '<STR_LIT>' , str ( place_row . climate_place . longitude ) , <EOL> '<STR_LIT>' , str ( place_row . climate_place . latitude ) , <EOL> "<STR_LIT:}>" <EOL> ) ) <EOL> ) <EOL> return "<STR_LIT>" % "<STR_LIT:U+002C>" . join ( stations_strings ) <EOL> def places ( ) : <EOL> """<STR_LIT:U+0020>""" <EOL> from datetime import datetime , timedelta <EOL> response . headers [ "<STR_LIT>" ] = ( <EOL> datetime . now ( ) + timedelta ( days = <NUM_LIT:7> ) <EOL> ) . strftime ( "<STR_LIT>" ) <EOL> return response . stream ( <EOL> open ( _map_plugin ( ) . place_data ( ) , "<STR_LIT:rb>" ) , <EOL> chunk_size = <NUM_LIT> <EOL> ) <EOL> def sample_table_spec ( ) : <EOL> return s3_rest_controller ( ) <EOL> def station_parameter ( ) : <EOL> return s3_rest_controller ( ) <EOL> def purchase ( ) : <EOL> """<STR_LIT:U+0020>""" <EOL> table = s3db . climate_purchase <EOL> if not auth . is_logged_in ( ) : <EOL> redirect ( URL ( c = "<STR_LIT:default>" , <EOL> f = "<STR_LIT:user>" , <EOL> args = [ "<STR_LIT>" ] , <EOL> vars = { "<STR_LIT>" : URL ( c = "<STR_LIT>" , <EOL> f = "<STR_LIT>" ) } ) ) <EOL> if not s3_has_role ( ADMIN ) : <EOL> table . paid . writable = False <EOL> table . price . writable = False <EOL> s3 . filter = ( table . created_by == auth . user . id ) <EOL> def prep ( r ) : <EOL> if not s3_has_role ( ADMIN ) and r . record and r . record . paid : <EOL> for f in table . fields : <EOL> table [ f ] . writable = False <EOL> if r . method == "<STR_LIT>" : <EOL> s3 . rfooter = DIV ( <EOL> T ( "<STR_LIT>" ) , <EOL> TABLE ( <EOL> TR ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> TR ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> TR ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> TR ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> TR ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> TR ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> TR ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> ) <EOL> ) <EOL> return True <EOL> def rheader ( r ) : <EOL> record = r . record <EOL> if record and record . paid : <EOL> return A ( "<STR_LIT>" , <EOL> _href = URL ( c = "<STR_LIT>" , <EOL> vars = { "<STR_LIT>" : record . id } ) , <EOL> _style = "<STR_LIT>" <EOL> ) <EOL> else : <EOL> return None <EOL> s3 . prep = prep <EOL> output = s3_rest_controller ( rheader = rheader ) <EOL> output [ "<STR_LIT>" ] = T ( "<STR_LIT>" ) <EOL> return output <EOL> def prices ( ) : <EOL> """<STR_LIT:U+0020>""" <EOL> if not auth . is_logged_in ( ) : <EOL> redirect ( URL ( c = "<STR_LIT:default>" , <EOL> f = "<STR_LIT:user>" , <EOL> args = [ "<STR_LIT>" ] , <EOL> vars = { "<STR_LIT>" : URL ( c = "<STR_LIT>" , <EOL> f = "<STR_LIT>" ) } <EOL> ) <EOL> ) <EOL> else : <EOL> if s3_has_role ( ADMIN ) : <EOL> return s3_rest_controller ( ) <EOL> def save_query ( ) : <EOL> output = s3_rest_controller ( ) <EOL> return output <EOL> def request_image ( ) : <EOL> """<STR_LIT:U+0020>""" <EOL> from datetime import datetime , timedelta <EOL> response . headers [ "<STR_LIT>" ] = ( <EOL> datetime . now ( ) + timedelta ( days = <NUM_LIT:7> ) <EOL> ) . strftime ( "<STR_LIT>" ) <EOL> response . headers [ "<STR_LIT:Content-Type>" ] = "<STR_LIT>" <EOL> response . headers [ "<STR_LIT>" ] = ( <EOL> "<STR_LIT>" <EOL> ) <EOL> vars = request . vars <EOL> return response . stream ( <EOL> open ( <EOL> _map_plugin ( ) . printable_map_image_file ( <EOL> command = ( <EOL> request . env . applications_parent + "<STR_LIT>" + <EOL> "<STR_LIT>" % appname <EOL> ) , <EOL> url_prefix = ( <EOL> "<STR_LIT>" <EOL> ) % dict ( <EOL> http_host = request . env . http_host , <EOL> appname = appname , <EOL> controller = request . controller , <EOL> ) , <EOL> query_string = request . env . query_string , <EOL> width = int ( vars [ "<STR_LIT:width>" ] ) , <EOL> height = int ( vars [ "<STR_LIT>" ] ) <EOL> ) , <EOL> "<STR_LIT:rb>" <EOL> ) , <EOL> chunk_size = <NUM_LIT> <EOL> ) <EOL> def download_purchased_data ( ) : <EOL> """<STR_LIT:U+0020>""" <EOL> purchase_id = request . vars [ "<STR_LIT>" ] <EOL> query = ( s3db . climate_purchase . id == purchase_id ) <EOL> climate_purchase = db ( query ) . select ( limitby = ( <NUM_LIT:0> , <NUM_LIT:1> ) ) . first ( ) <EOL> if climate_purchase is not None and climate_purchase . paid : <EOL> sample_table = SampleTable . with_id ( climate_purchase . parameter_id ) <EOL> parameter_name = repr ( sample_table ) <EOL> place_id = climate_purchase . station_id <EOL> itable = s3db . climate_place_station_id <EOL> ntable = s3db . climate_place_station_name <EOL> query = ( itable . id == place_id ) & ( ntable . id == place_id ) <EOL> station = db ( query ) . select ( ntable . name , <EOL> itable . station_id , <EOL> limitby = ( <NUM_LIT:0> , <NUM_LIT:1> ) ) . first ( ) <EOL> station_id = station . climate_place_station_id . station_id <EOL> station_name = station . climate_place_station_name . name <EOL> date_from = climate_purchase . date_from <EOL> date_to = climate_purchase . date_to <EOL> csv_data = sample_table . csv_data ( <EOL> place_id = place_id , <EOL> date_from = date_from , <EOL> date_to = date_to <EOL> ) <EOL> response . headers [ "<STR_LIT:Content-Type>" ] = "<STR_LIT>" <EOL> response . headers [ "<STR_LIT>" ] = ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % locals ( ) <EOL> ) <EOL> return csv_data <EOL> else : <EOL> return <EOL> def get_years ( ) : <EOL> """<STR_LIT:U+0020>""" <EOL> from datetime import datetime , timedelta <EOL> response . headers [ "<STR_LIT>" ] = ( <EOL> datetime . now ( ) + timedelta ( days = <NUM_LIT:7> ) <EOL> ) . strftime ( "<STR_LIT>" ) <EOL> return response . stream ( <EOL> open ( _map_plugin ( ) . get_available_years ( request . vars [ "<STR_LIT>" ] ) , "<STR_LIT:rb>" ) , <EOL> chunk_size = <NUM_LIT> <EOL> ) </s>
