<s> import logging <EOL> import math <EOL> from google . appengine . ext import db <EOL> from google . appengine . ext . db import djangoforms <EOL> import geobox <EOL> RADIUS = <NUM_LIT> <EOL> GEOBOX_CONFIGS = ( <EOL> ( <NUM_LIT:4> , <NUM_LIT:5> , True ) , <EOL> ( <NUM_LIT:3> , <NUM_LIT:2> , True ) , <EOL> ( <NUM_LIT:3> , <NUM_LIT:8> , False ) , <EOL> ( <NUM_LIT:3> , <NUM_LIT:16> , False ) , <EOL> ( <NUM_LIT:2> , <NUM_LIT:5> , False ) , <EOL> ) <EOL> _DAY_DICTIONARY = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> def _make_hours ( store_hours ) : <EOL> """<STR_LIT>""" <EOL> week_hrs = { } <EOL> for dow in store_hours . keys ( ) : <EOL> dow_hours = [ ] <EOL> for hour_set in store_hours [ dow ] : <EOL> if len ( hour_set ) < <NUM_LIT:2> : <EOL> open_hr = <NUM_LIT:0.0> <EOL> close_hr = <NUM_LIT> <EOL> else : <EOL> open_hr = float ( hour_set [ <NUM_LIT:0> ] ) <EOL> close_hr = float ( hour_set [ <NUM_LIT:1> ] ) <EOL> if close_hr < open_hr : <EOL> tmp = close_hr <EOL> close_hr = open_hr <EOL> open_hr = tmp <EOL> current_hr_it = open_hr <EOL> while ( ( close_hr - current_hr_it ) >= <NUM_LIT> ) : <EOL> dow_hours . append ( current_hr_it ) <EOL> current_hr_it += <NUM_LIT> <EOL> week_hrs [ dow ] = dow_hours <EOL> return week_hrs <EOL> def _earth_distance ( lat1 , lon1 , lat2 , lon2 ) : <EOL> lat1 , lon1 = math . radians ( float ( lat1 ) ) , math . radians ( float ( lon1 ) ) <EOL> lat2 , lon2 = math . radians ( float ( lat2 ) ) , math . radians ( float ( lon2 ) ) <EOL> return RADIUS * math . acos ( math . sin ( lat1 ) * math . sin ( lat2 ) + <EOL> math . cos ( lat1 ) * math . cos ( lat2 ) * math . cos ( lon2 - lon1 ) ) <EOL> class Store ( db . Model ) : <EOL> name = db . StringProperty ( ) <EOL> pretty_address = db . TextProperty ( ) <EOL> pretty_description = db . TextProperty ( ) <EOL> pretty_hours = db . TextProperty ( ) <EOL> location = db . GeoPtProperty ( ) <EOL> geoboxes = db . StringListProperty ( ) <EOL> hr_mon = db . ListProperty ( float ) <EOL> hr_tues = db . ListProperty ( float ) <EOL> hr_weds = db . ListProperty ( float ) <EOL> hr_thurs = db . ListProperty ( float ) <EOL> hr_fri = db . ListProperty ( float ) <EOL> hr_sat = db . ListProperty ( float ) <EOL> hr_sun = db . ListProperty ( float ) <EOL> holidays = db . StringListProperty ( ) <EOL> categories = db . StringListProperty ( ) <EOL> phone_numbers = db . StringProperty ( ) <EOL> @ classmethod <EOL> def add ( self , ** kwargs ) : <EOL> lat = kwargs . pop ( '<STR_LIT>' ) <EOL> lon = kwargs . pop ( '<STR_LIT>' ) <EOL> location = db . GeoPt ( lat , lon ) <EOL> name = kwargs [ '<STR_LIT:name>' ] <EOL> new_store = Store ( name = name , location = location ) <EOL> all_boxes = [ ] <EOL> new_store . pretty_address = kwargs [ '<STR_LIT:address>' ] <EOL> for ( resolution , slice , use_set ) in GEOBOX_CONFIGS : <EOL> if use_set : <EOL> all_boxes . extend ( geobox . compute_set ( lat , lon , resolution , slice ) ) <EOL> else : <EOL> all_boxes . append ( geobox . compute ( lat , lon , resolution , slice ) ) <EOL> new_store . geoboxes = all_boxes <EOL> store_hour_dict = _make_hours ( kwargs [ '<STR_LIT>' ] ) <EOL> for day , prop in _DAY_DICTIONARY . iteritems ( ) : <EOL> setattr ( new_store , prop , store_hour_dict [ day ] ) <EOL> new_store . categories = kwargs [ '<STR_LIT>' ] <EOL> new_store . pretty_description = kwargs [ '<STR_LIT:description>' ] <EOL> new_store . put ( ) <EOL> @ classmethod <EOL> def query ( self , time , dow , lat , lon , max_results , min_params ) : <EOL> """<STR_LIT>""" <EOL> found_stores = { } <EOL> dow_query_string = _DAY_DICTIONARY [ dow ] + '<STR_LIT>' <EOL> for params in GEOBOX_CONFIGS : <EOL> if len ( found_stores ) >= max_results : <EOL> break <EOL> if params < min_params : <EOL> break <EOL> resolution , slice , unused = params <EOL> box = geobox . compute ( lat , lon , resolution , slice ) <EOL> logging . info ( "<STR_LIT>" , <EOL> box , resolution , slice ) <EOL> query = self . all ( ) . filter ( "<STR_LIT>" , box ) . filter ( dow_query_string , time ) <EOL> results = query . fetch ( <NUM_LIT:50> ) <EOL> logging . info ( "<STR_LIT>" , len ( results ) ) <EOL> for result in results : <EOL> if result . name not in found_stores : <EOL> found_stores [ result . name ] = result <EOL> stores_by_distance = [ ] <EOL> for store in found_stores . itervalues ( ) : <EOL> distance = _earth_distance ( lat , lon , store . location . lat , store . location . lon ) <EOL> stores_by_distance . append ( ( distance , store ) ) <EOL> stores_by_distance . sort ( ) <EOL> return stores_by_distance <EOL> class UserProfile ( db . Model ) : <EOL> user = db . UserProperty ( required = True ) <EOL> class CommentIndex ( db . Model ) : <EOL> max_index = db . IntegerProperty ( default = <NUM_LIT:0> , required = True ) <EOL> class Comment ( db . Model ) : <EOL> index = db . IntegerProperty ( required = True ) <EOL> reviewer = db . ReferenceProperty ( UserProfile , required = True ) <EOL> store = db . ReferenceProperty ( Store , required = True ) <EOL> review = db . TextProperty ( required = True ) <EOL> rating = db . IntegerProperty ( choices = set ( [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:5> ] ) ) <EOL> posted_on = db . DateTimeProperty ( auto_now_add = True ) <EOL> disabled = db . BooleanProperty ( default = False ) </s>
