Skip to content

Instantly share code, notes, and snippets.

@ebrehault
Created March 5, 2013 08:45
Show Gist options
  • Save ebrehault/5088864 to your computer and use it in GitHub Desktop.
Save ebrehault/5088864 to your computer and use it in GitHub Desktop.
db = context.getParentDatabase()
file_path = db.getDocument("annonces_path").configValues.encode('ascii')
photos_path = db.getDocument("photos_path").configValues.encode('ascii')
annonces_file = open_url("file://"+file_path, asFile=True)
csv_data = csv_to_array(annonces_file, delimiter="#")
csv_data = csv_data[1:]
mapping_main = {
'annonce_reference': 0,
'annonce_type': 1,
'annonce_nature': 2,
'annonce_exclu': 3,
'annonce_pieces': 4,
'annonce_chambres': 5,
'annonce_code_postal': 6,
'annonce_ville': 7,
'annonce_lat': 8,
'annonce_lon': 9,
'annonce_superficie': 10,
'annonce_superficie_terrain': 11,
'annonce_prix_simax': 12,
'annonce_titre': 13,
'annonce_libelle': 14,
'annonce_descriptif': 15,
'annonce_bce_valeur': 16,
'annonce_bce': 17,
'annonce_ges_valeur': 18,
'annonce_ges': 19,
'conseiller_sms' : 20,
'conseiller_mail' : 21,
'conseiller_nom' : 22,
'conseiller_prenom' : 23,
}
all_fields = db.getForm("frmAnnonce").getFormFields()
fields = {}
for f in all_fields:
fields[f.id] = f
old_ids = [doc.getPath().split("/")[-1] for doc in db.getView('allfrmAnnonce').getAllDocuments()]
db.deleteDocuments(ids=old_ids, massive=False)
for row in csv_data:
doc = db.createDocument()
doc.setItem('Form', "frmAnnonce")
doc.setItem('doctype', "Annonce")
# set field values
for f in mapping_main.items():
field_id = f[0]
field_value = row[f[1]]
field = fields[field_id]
if field_value:
if field.FieldType == "NUMBER":
field_value = field_value.replace(" ","")
if field.getSettings().type == "FLOAT":
field_value = float(field_value.replace(",","."))
else:
field_value = int(field_value)
else:
field_value = asUnicode(field_value)
doc.setItem(field_id, field_value)
doc.setItem("annonce_departement", row[mapping_main['annonce_code_postal']][:2])
doc.save(creation=True)
# import images
photos = {}
reference = doc.getItem("annonce_reference")
for suffix in ['A', 'B', 'C', 'D']:
#context.plone_log("file://"+photos_path+"/"+reference+suffix+".jpg")
try:
photo = open_url("file://"+photos_path+"/"+reference+suffix+".jpg", asFile=True)
except:
#no photo provided
photo = None
if photo:
(new_file, contenttype) = doc.setfile(photo, filename=doc.id+"-"+suffix+".jpg")
photos[new_file] = contenttype
if photos:
doc.setItem('annonce_photos', photos)
find_location = open_url("http://ws.geonames.org/searchJSON?name=%s&q=%s&featureClass=P&country=FR" % (doc.getItem('annonce_ville'), doc.getItem('annonce_code_postal')))
if '"totalResultsCount":0' in find_location:
find_location = open_url("http://ws.geonames.org/searchJSON?name=%s&q=%s&featureClass=P&country=FR" % (doc.getItem('annonce_ville'), str(doc.getItem('annonce_code_postal'))[:2]))
if '"totalResultsCount":0' in find_location:
doc.setItem('annonce_ville', doc.annonce_ville+" NOT FOUND")
continue
if not doc.getItem('annonce_lat'):
before_lat = find_location[find_location.index('"lat":')+6:]
str_lat = before_lat[:before_lat.index(',')]
doc.setItem('annonce_lat', float(str_lat))
before_lng = find_location[find_location.index('"lng":')+6:]
str_lng = before_lng[:before_lng.index(',')]
doc.setItem('annonce_lon', float(str_lng))
before_name = find_location[find_location.index('"name":"')+8:]
str_name = before_name[:before_name.index('",')]
doc.setItem('annonce_ville', str_name.decode('utf-8'))
doc.refresh()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment