def run(): csv_file = open("%s/../fixtures/DataSet1.csv" % os.path.dirname(__file__)) contents = csv.reader(csv_file, dialect='excel', delimiter=',') header = contents.next() g = geocoders.Google('AIzaSyAZoNPSlRTETltbmJvgYYqol0SLAVBgKs') for row in contents: animal_id = row[3] if not Animal.objects.filter(animal_id=animal_id).exists(): location = row[1] location_found = True try: (place, point) = g.geocode(location) except: location_found = False if location_found: a = Animal() a.animal_id = animal_id intake_date = row[0] dt = datetime.strptime(intake_date.strip(), "%m/%d/%y") a.intake_date = date(year=dt.year, month=dt.month, day=dt.day) a.location = location a.intake_condition = row[2] a.animal_type = row[4] sex = {'UNKNOWN': 'U', 'MALE': 'M', 'FEMALE': 'F'} a.sex = sex[row[5]] a.spayed = True if row[6] == 'YES' else False a.name = row[7] a.age = int(float(row[8].replace(',', ''))) a.description = row[9] a.intake_total = 1 a.geometry = "POINT (%s %s)" % (point[1], point[0]) a.save() print a
def populate(row): animal_id = row[3] image_updated = True if row[13] == 'New Image Available' else False print "starting to process %s" % animal_id if not Animal.objects.filter(animal_id=animal_id).exists(): location = row[1] location_found = True try: (place, point) = g.geocode(location) except: location_found = False print 'location not found' if location_found: a = Animal() a.animal_id = animal_id intake_date = row[0] dt = datetime.strptime(intake_date.strip(), "%m/%d/%Y") a.intake_date = date(year=dt.year, month=dt.month, day=dt.day) a.location = location a.intake_condition = row[2] a.animal_type = row[4] sex = {'UNKNOWN': 'U', 'MALE': 'M', 'FEMALE': 'F'} a.sex = sex[row[5]] a.spayed = True if row[6] == 'YES' else False a.name = row[7] a.age = int(float(row[8].replace(',', ''))) a.description = row[9] a.intake_total = 1 a.outcome_type = row[10] outcome_date = row[11] if outcome_date: odt = datetime.strptime(intake_date.strip(), "%m/%d/%Y") a.outcome_date = date(year=odt.year, month=odt.month, day=odt.day) a.transferred_to = row[12] a.geometry = "POINT (%s %s)" % (point[1], point[0]) a.photo = '' a.save() print a elif image_updated: print 'marking %s as having photo updated' % animal_id a = Animal.objects.get(animal_id=animal_id) a.photo_updated = image_updated a.save() return 'finished processing %s' % (animal_id)
def populate(row): animal_id = row[3] image_updated = True if row[13] == 'New Image Available' else False print "starting to process %s" % animal_id if not Animal.objects.filter(animal_id=animal_id).exists(): location = row[1] location_found = True try: address, (latitude, longitude) = g.geocode(location) except: location_found = False print 'location not found' if location_found: a = Animal() a.animal_id = animal_id intake_date = row[0] dt = datetime.strptime(intake_date.strip(), "%m/%d/%Y") a.intake_date = date(year=dt.year, month=dt.month, day=dt.day) a.location = location a.intake_condition = row[2] a.animal_type = row[4] sex = {'UNKNOWN': 'U', 'MALE': 'M', 'FEMALE': 'F'} a.sex = sex[row[5]] a.spayed = True if row[6] == 'YES' else False a.name = row[7] a.age = int(float(row[8].replace(',', ''))) a.description = row[9] a.intake_total = 1 a.outcome_type = row[10] outcome_date = row[11] if outcome_date: odt = datetime.strptime(intake_date.strip(), "%m/%d/%Y") a.outcome_date = date(year=odt.year, month=odt.month, day=odt.day) a.transferred_to = row[12] a.geometry = "POINT (%s %s)" % (longitude, latitude) a.photo = '' a.save() print a elif image_updated: print 'marking %s as having photo updated' % animal_id a = Animal.objects.get(animal_id=animal_id) a.photo_updated = image_updated a.save() return 'finished processing %s' % (animal_id)
def process_data(request): if request.method == 'POST': sender = request.POST.get('sender') recipient = request.POST.get('recipient') subject = request.POST.get('subject') for key in request.FILES: data_file = request.FILES[key] contents = unicode_csv_reader(data_file, dialect='excel', delimiter=',') header = contents.next() g = geocoders.Google('AIzaSyAZoNPSlRTETltbmJvgYYqol0SLAVBgKs') for row in contents: animal_id = row[3] if not Animal.objects.filter(animal_id=animal_id).exists(): location = row[1] location_found = True try: (place, point) = g.geocode(location) except: location_found = False if location_found: a = Animal() a.animal_id = animal_id intake_date = row[0] dt = datetime.strptime(intake_date.strip(), "%m/%d/%y") a.intake_date = date(year=dt.year, month=dt.month, day=dt.day) a.location = location a.intake_condition = row[2] a.animal_type = row[4] sex = {'UNKNOWN': 'U', 'MALE': 'M', 'FEMALE': 'F'} a.sex = sex[row[5]] a.spayed = True if row[6] == 'YES' else False a.name = row[7] a.age = int(float(row[8].replace(',', ''))) a.description = row[9] a.intake_total = 1 a.geometry = "POINT (%s %s)" % (point[1], point[0]) a.photo = '' a.save() return HttpResponse('cool')
def populate(row): animal_id = row[3] image_updated = True if row[13] == "New Image Available" else False print "starting to process %s" % animal_id if not Animal.objects.filter(animal_id=animal_id).exists(): location = row[1] location_found = True try: (place, point) = g.geocode(location) except: location_found = False print "location not found" if location_found: a = Animal() a.animal_id = animal_id intake_date = row[0] dt = datetime.strptime(intake_date.strip(), "%m/%d/%Y") a.intake_date = date(year=dt.year, month=dt.month, day=dt.day) a.location = location a.intake_condition = row[2] a.animal_type = row[4] sex = {"UNKNOWN": "U", "MALE": "M", "FEMALE": "F"} a.sex = sex[row[5]] a.spayed = True if row[6] == "YES" else False a.name = row[7] a.age = int(float(row[8].replace(",", ""))) a.description = row[9] a.intake_total = 1 a.outcome_type = row[10] outcome_date = row[11] if outcome_date: odt = datetime.strptime(intake_date.strip(), "%m/%d/%Y") a.outcome_date = date(year=odt.year, month=odt.month, day=odt.day) a.transferred_to = row[12] a.geometry = "POINT (%s %s)" % (point[1], point[0]) a.photo = "" a.save() print a elif image_updated: print "marking %s as having photo updated" % animal_id a = Animal.objects.get(animal_id=animal_id) a.photo_updated = image_updated a.save() return "finished processing %s" % (animal_id)
def handle(self, *args, **options): lines_count = 0 fname = options['filename'][0] xl = xlrd.open_workbook(fname) # Open the workbook # for sheet in (2,0): for sheet in (0,): print('Sheet #' + str(sheet)) xl_sheet = xl.sheet_by_index(sheet) # grab sheet num_cols = xl_sheet.ncols # Number of columns for row_idx in range(10, xl_sheet.nrows): # Iterate through rows print('.', end ="", flush=True) # print ('-'*40) # print ('Row: %s' % row_idx) # Print row number # for col_idx in range(0, num_cols): # Iterate through columns # cell_obj = xl_sheet.cell(row_idx, col_idx) # Get cell object by row, col # print ('Column: [%s] cell_obj: [%s]' % (col_idx, cell_obj)) a = Animal() # a.pk = int(xl_sheet.cell(row_idx, 0).value) a.entry_date = xldate_as_datetime(xl_sheet.cell(row_idx, 1).value, xl.datemode) a.database_id = str(xl_sheet.cell(row_idx, 2).value).strip() a.lab_id = str(xl_sheet.cell(row_idx, 3).value).strip() a.day_of_birth = xldate_as_datetime(xl_sheet.cell(row_idx, 4).value, xl.datemode) try: age = int(xl_sheet.cell(row_idx, 5).value) except ValueError: age = 0 a.line = str(xl_sheet.cell(row_idx, 6).value).strip() mutation_1 = str(xl_sheet.cell(row_idx, 7).value).strip() grade_1 = str(xl_sheet.cell(row_idx, 8).value).strip() mutation_2 = str(xl_sheet.cell(row_idx, 9).value).strip() grade_2 = str(xl_sheet.cell(row_idx, 10).value).strip() mutation_3 = str(xl_sheet.cell(row_idx, 11).value).strip() grade_3 = str(xl_sheet.cell(row_idx, 12).value).strip() mutation_4 = str(xl_sheet.cell(row_idx, 13).value).strip() grade_4 = str(xl_sheet.cell(row_idx, 14).value).strip() a.sex = str(xl_sheet.cell(row_idx, 15).value).strip().lower() or 'u' a.licence_number = str(xl_sheet.cell(row_idx, 17).value).strip() responsible_person = str(xl_sheet.cell(row_idx, 18).value).strip().strip() a.available_from = xldate_as_datetime(xl_sheet.cell(row_idx, 19).value, xl.datemode) a.available_to = xldate_as_datetime(xl_sheet.cell(row_idx, 20).value, xl.datemode) a.new_owner = str(xl_sheet.cell(row_idx, 21).value).strip() a.animal_type = 'mouse' # a.organ_type = 'whole animal' a.mutations = mutation_1 + ' ' + grade_1 + '\n' + \ mutation_2 + ' ' + grade_2 + '\n' + \ mutation_3 + ' ' + grade_3 + '\n' + \ mutation_4 + ' ' + grade_4 + '\n' location_name = str(xl_sheet.cell(row_idx, 16).value).strip() try: a.location = Location.objects.get(name=location_name) except Location.DoesNotExist: loc = Location(name=location_name) loc.save() a.location = loc a.responsible_person = Person.objects.get(Q(email=responsible_person.lower()) | Q(name__iexact=responsible_person.lower())) try: Animal.objects.get(entry_date=a.entry_date, day_of_birth=a.day_of_birth, available_from=a.available_from, available_to=a.available_to, line=a.line, database_id=a.database_id, lab_id=a.lab_id, location=a.location, sex=a.sex) except Animal.DoesNotExist: a.save() lines_count += 1 print() self.stdout.write(self.style.SUCCESS('Successfully imported %i lines.' % lines_count))
def handle(self, *args, **options): lines_count = 0 fname = options['filename'][0] xl = xlrd.open_workbook(fname) # Open the workbook # for sheet in (2,0): for sheet in (0, ): print('Sheet #' + str(sheet)) xl_sheet = xl.sheet_by_index(sheet) # grab sheet num_cols = xl_sheet.ncols # Number of columns for row_idx in range(1, xl_sheet.nrows): # Iterate through rows print('.', end="", flush=True) # print ('-'*40) # print ('Row: %s' % row_idx) # Print row number # for col_idx in range(0, num_cols): # Iterate through columns # cell_obj = xl_sheet.cell(row_idx, col_idx) # Get cell object by row, col # print ('Column: [%s] cell_obj: [%s]' % (col_idx, cell_obj)) a = Animal() # a.pk = int(xl_sheet.cell(row_idx, 0).value) #a.entry_date = xldate_as_datetime(xl_sheet.cell(row_idx, 1).value, xl.datemode) #a.available_from = xldate_as_datetime(xl_sheet.cell(row_idx, 0).value, xl.datemode) # angeboten am a.animal_type = 'fish' a.available_from = datetime.today().date() a.comment = str(xl_sheet.cell(row_idx, 0).value).strip() # Kommentar a.sex = str(xl_sheet.cell( row_idx, 1).value).strip().lower() or 'u' # Geschlecht a.line = str(xl_sheet.cell(row_idx, 2).value).strip() # Zuchtlinie a.lab_id = str(xl_sheet.cell(row_idx, 3).value).strip() # Käfig-ID a.database_id = str(xl_sheet.cell(row_idx, 4).value).strip() # Tier-ID a.day_of_birth = xldate_as_datetime( xl_sheet.cell(row_idx, 5).value, xl.datemode) #Geb. a.amount = int(xl_sheet.cell(row_idx, 7).value) # Anzahl a.licence_number = str(xl_sheet.cell( row_idx, 11).value).strip() # Aktenzeichen location_name = str(xl_sheet.cell(row_idx, 13).value).strip() # Raum responsible_person = str(xl_sheet.cell( row_idx, 16).value).strip().strip() # Verantwortliche Person #a.available_from = xldate_as_datetime(xl_sheet.cell(row_idx, 19).value, xl.datemode) a.available_to = xldate_as_datetime( xl_sheet.cell(row_idx, 22).value, xl.datemode) #a.new_owner = str(xl_sheet.cell(row_idx, 21).value).strip() # a.organ_type = 'whole animal' #a.mutations = try: a.location = Location.objects.get(name=location_name) except Location.DoesNotExist: loc = Location(name=location_name) loc.save() a.location = loc a.responsible_person = Person.objects.get( Q(email=responsible_person.lower()) | Q(name__iexact=responsible_person.lower())) try: Animal.objects.get(entry_date=a.entry_date, day_of_birth=a.day_of_birth, available_from=a.available_from, available_to=a.available_to, line=a.line, database_id=a.database_id, lab_id=a.lab_id, location=a.location, sex=a.sex) except Animal.DoesNotExist: a.save() lines_count += 1 print() self.stdout.write( self.style.SUCCESS('Successfully imported %i lines.' % lines_count))