@@ -21,10 +21,8 @@ def expired(record_date):
2121def _get_info_from_source (cep ):
2222 tracker = CepTracker ()
2323 info = tracker .track (cep )
24-
2524 if len (info ) == 0 :
2625 raise ValueError ()
27-
2826 return info
2927
3028
@@ -33,27 +31,25 @@ def _get_info_from_source(cep):
3331def verifica_cep (cep ):
3432 cep = cep .replace ('-' ,'' )
3533 db = Database ()
36-
3734 response .headers ['Access-Control-Allow-Origin' ] = '*'
3835
39- try :
40- result = db .get_one (cep , fields = { '_id' : False })
41-
42- if not result or not result .has_key ('v_date' ) or expired (result ):
43- try :
44- for item in _get_info_from_source (cep ):
45- db .insert_or_update (item )
46-
47- except ConnectionError :
48- response .status = '503 Servico Temporariamente Indisponivel'
49-
36+ result = db .get_one (cep , fields = { '_id' : False })
37+ if result and result .has_key ('v_date' ) and not expired (result ):
38+ result .pop ('v_date' )
39+ else :
40+ try :
41+ info = _get_info_from_source (cep )
42+ except ValueError :
43+ response .status = '404 O CEP %s informado nao pode ser localizado' % cep
44+ return
45+ except ConnectionError :
46+ response .status = '503 Servico Temporariamente Indisponivel'
47+ return
48+ for item in info :
49+ db .insert_or_update (item )
5050 result = db .get_one (cep , fields = { '_id' : False , 'v_date' : False })
5151
52- response .headers ['Cache-Control' ] = 'public, max-age=2592000'
53-
54- except ValueError :
55- response .status = '404 O CEP %s informado nao pode ser localizado' % cep
56-
52+ response .headers ['Cache-Control' ] = 'public, max-age=2592000'
5753 return result
5854
5955
0 commit comments