<s> import geopy <EOL> import time <EOL> import logging <EOL> from django . db import transaction <EOL> from schools . models import County , District , School <EOL> from . api . cde import ( <EOL> MODE_PUBLIC , <EOL> MODE_PRIVATE , <EOL> CdeSchoolSearchList , <EOL> CdeSchoolDetail <EOL> ) <EOL> from . serializers import CountySerializer , DistrictSerializer , SchoolSerializer <EOL> DEFAULT_GEOCODER_TIMEOUT = <NUM_LIT:10> <EOL> DEFAULT_GEOCODER_RETRIES = <NUM_LIT:3> <EOL> DEFAULT_GEOCODER_INTERVAL_BETWEEN_RETRIES = <NUM_LIT:5> <EOL> logger = logging . getLogger ( __name__ ) <EOL> class DBLoader ( object ) : <EOL> geocoder = geopy . geocoders . GoogleV3 ( timeout = DEFAULT_GEOCODER_TIMEOUT ) <EOL> def geocode ( self , address ) : <EOL> for itr in xrange ( DEFAULT_GEOCODER_RETRIES ) : <EOL> try : <EOL> loc = self . geocoder . geocode ( address ) if address else None <EOL> return ( <EOL> getattr ( loc , '<STR_LIT>' , None ) , <EOL> getattr ( loc , '<STR_LIT>' , None ) <EOL> ) <EOL> except Exception , e : <EOL> logger . warning ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' . format ( <EOL> address , <EOL> DEFAULT_GEOCODER_INTERVAL_BETWEEN_RETRIES <EOL> ) <EOL> ) <EOL> time . sleep ( DEFAULT_GEOCODER_INTERVAL_BETWEEN_RETRIES ) <EOL> continue <EOL> else : <EOL> raise geopy . exc . GeocoderServiceError <EOL> def add_or_update_geo_coords_for_school ( self , school ) : <EOL> if school . updated : <EOL> logger . info ( <EOL> '<STR_LIT>' . format ( school . name , <EOL> school . code ) <EOL> ) <EOL> school . lat , school . lng = self . geocode ( school . address ) <EOL> school . save ( ) <EOL> def serialize_to_objects ( self , info ) : <EOL> county_serializer = CountySerializer ( data = info ) <EOL> county_serializer . is_valid ( raise_exception = True ) <EOL> county , _ = County . objects . get_or_create ( <EOL> ** county_serializer . validated_data <EOL> ) <EOL> district_serializer = DistrictSerializer ( data = info ) <EOL> district_serializer . is_valid ( raise_exception = True ) <EOL> district , _ = District . objects . get_or_create ( <EOL> county = county , <EOL> ** district_serializer . validated_data <EOL> ) <EOL> school_serializer = SchoolSerializer ( data = info ) <EOL> school_serializer . is_valid ( raise_exception = True ) <EOL> school , _ = School . objects . update_or_create ( <EOL> defaults = school_serializer . validated_data , <EOL> code = school_serializer . validated_data [ '<STR_LIT:code>' ] , <EOL> county = county , <EOL> district = district <EOL> ) <EOL> self . add_or_update_geo_coords_for_school ( school ) <EOL> def source ( self ) : <EOL> School . objects . update ( deprecated = True ) <EOL> for mode in ( MODE_PUBLIC , MODE_PRIVATE ) : <EOL> logger . info ( '<STR_LIT>' ) <EOL> school_endpoints = CdeSchoolSearchList ( mode = mode ) . schools <EOL> logger . info ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' . format ( len ( school_endpoints ) ) <EOL> ) <EOL> for endpoint in school_endpoints : <EOL> info = CdeSchoolDetail ( endpoint ) . to_dict ( ) <EOL> info [ '<STR_LIT>' ] = mode == MODE_PUBLIC <EOL> self . serialize_to_objects ( info ) <EOL> def update_db ( self ) : <EOL> try : <EOL> logger . info ( '<STR_LIT>' ) <EOL> with transaction . atomic ( ) : <EOL> self . source ( ) <EOL> logger . info ( '<STR_LIT>' ) <EOL> except Exception , e : <EOL> logger . exception ( e ) </s>
