def get(self, country_code="IND"): cache_key = "timezones_for_country_code_%s" % country_code timezones = memcache.get(cache_key) if timezones is None: from pytz.gae import pytz alpha2_code = COUNTRY_ISO_ALPHA_TABLE.get(country_code, "IN") timezones = pytz.country_timezones(alpha2_code) timezones.sort() memcache.set(cache_key, timezones) self.set_header("Content-Type", "application/json") self.write(json.dumps(timezones))
def get(self): from models import ( DEFAULT_COUNTRY_CODE_CHOICE, GENDER_TYPES_TUPLE_MAP, T_SHIRT_SIZES_TUPLE_MAP, RAILWAY_LINES_TUPLE_MAP, DEFAULT_RAILWAY_LINE_CHOICE, DEFAULT_PHONE_TYPE_CHOICE, PHONE_TYPES_TUPLE_MAP, ) from pytz.gae import pytz from services import country_code_from_ip_address user = users.get_current_user() federated_identity = user.federated_identity() email = user.email() logging.info("Email: " + email) logging.info("Federated identity: " + str(federated_identity)) determined_country_code = country_code_from_ip_address(self.request.remote_ip) if determined_country_code.lower() == "zz": determined_country_code = "IN" country_code = COUNTRY_ISO_ALPHA_TABLE.get(determined_country_code) timezones = pytz.country_timezones(determined_country_code) railway_line_choice = DEFAULT_RAILWAY_LINE_CHOICE city = "Mumbai" state_or_province = "Maharashtra" if country_code != "IND": railway_line_choice = "other" city = "" state_or_province = "" self.render( "register.html", email=email, gender_choices=GENDER_TYPES_TUPLE_MAP, t_shirt_sizes=T_SHIRT_SIZES_TUPLE_MAP, railway_lines=RAILWAY_LINES_TUPLE_MAP, countries_list=COUNTRIES_LIST, phone_types=PHONE_TYPES_TUPLE_MAP, timezones=timezones, state_or_province=state_or_province, city=city, default_phone_type=DEFAULT_PHONE_TYPE_CHOICE, default_country_code=country_code, default_railway_line_choice=railway_line_choice, federated_identity=federated_identity, logout_url=users.create_logout_url("/"), )
def ip_address_to_country_code(ip_address, default_country_code='ZZZ'): ''' Uses an external service to map an IP address to its country. ''' url = 'http://abusebutler.appspot.com/loc/%s' % ip_address result_country_code = memcache.get('geoiplocator:' + url) if not result_country_code: result_country_code = default_country_code try: result = urlfetch.fetch(url) if result and result.status_code == 200: alpha_2_iso_country_code = result.content.strip() if alpha_2_iso_country_code: #logging.info(alpha_2_iso_country_code) result_country_code = COUNTRY_ISO_ALPHA_TABLE.get(alpha_2_iso_country_code, default_country_code) #logging.info(result_country_code) memcache.set(url, result_country_code) else: result_country_code = default_country_code except urlfetch.DownloadError: result_country_code = default_country_code return result_country_code