#NEWLINE# def refresh_price_history( quote ) : #NEWLINE# #INDENT# start_date = ( quote . last_trade + timedelta ( days = ( 0 - PRICE_HISTORY_LIMIT_IN_DAYS ) ) ) #NEWLINE# end_date = ( quote . last_trade + timedelta ( days = 1 ) ) #NEWLINE# csv_columns = 'date,open,high,low,close,volume,adj_close' #NEWLINE# csv_url = ( 'http://ichart.finance.yahoo.com/table.csv?s=%s&a=%.2d&b=%.2d&c=%.4d&d=%.2d&e=%.2d&f=%.4d&g=d&ignore=.csv' % ( quote . symbol , ( start_date . month - 1 ) , start_date . day , start_date . year , ( end_date . month - 1 ) , end_date . day , end_date . year ) ) #NEWLINE# to_remove = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# for history in quote . pricehistory_set . filter ( as_of_date__gte = start_date . date ( ) ) : #NEWLINE# #INDENT# to_remove [ history . as_of_date . date ( ) ] = to_remove #NEWLINE# #UNINDENT# to_save = [ ] #NEWLINE# for row in _yql_csv_to_json ( csv_url , csv_columns , PRICE_HISTORY_LIMIT_IN_DAYS , 2 ) : #NEWLINE# #INDENT# as_of_date = datetime . strptime ( row [ 'date' ] , '%Y-%m-%d' ) #NEWLINE# price = float ( row [ 'adj_close' ] ) #NEWLINE# history = to_remove . get ( as_of_date . date ( ) ) #NEWLINE# if ( history == None ) : #NEWLINE# #INDENT# history = PriceHistory ( ) #NEWLINE# history . quote = quote #NEWLINE# history . as_of_date = as_of_date #NEWLINE# history . price = price #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# del to_remove [ as_of_date . date ( ) ] #NEWLINE# if ( abs ( ( history . price - price ) ) > 0.0001 ) : #NEWLINE# #INDENT# history . price = price #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# continue #NEWLINE# #UNINDENT# #UNINDENT# to_save . append ( history ) #NEWLINE# #UNINDENT# if ( len ( to_remove ) > 0 ) : #NEWLINE# #INDENT# ids = [ h . id for h in to_remove . values ( ) ] #NEWLINE# PriceHistory . objects . filter ( id__in = ids ) . delete ( ) #NEWLINE# #UNINDENT# if ( len ( to_save ) > 0 ) : #NEWLINE# #INDENT# for history in to_save : #NEWLINE# #INDENT# history . save ( ) #NEWLINE# #UNINDENT# #UNINDENT# quote . history_date = datetime . now ( ) #NEWLINE# quote . save ( ) #NEWLINE# return quote