def create_sighting(values): # try: s = Sighting() s.sighting_id = int(values[0]) # Ugh... python hates times in 24:00 format... data contains it. if " 24:" in values[1]: values[1] = values[1].replace(" 24:", " 23:", 1) dt = datetime.strptime(values[1], "%m/%d/%Y %H:%M") dt = dt + timedelta(hours=1) else: dt = datetime.strptime(values[1], "%m/%d/%Y %H:%M") s.occurred_at = pytz.utc.localize(dt) s.city = values[2] s.state = values[3] s.country = values[4] s.shape = values[5] s.duration_seconds = float(values[6]) s.duration_text = values[7].strip() s.description = values[8] s.reported_on = datetime.strptime(values[9], '%m/%d/%Y').date() s.latitude = float(values[10]) s.longitude = float(values[11]) # print(f"s is {s.__dict__}") # if s.sighting_id == 4: # try: # s.save() # except Exception as e: # import ipdb; ipdb.set_trace() # else: s.save() return True
def handle(self, *args, **options): with open(options['squirrel_csv']) as fp: reader = csv.DictReader(fp) noduplicates = list() for item in reader: if item['Unique Squirrel ID'] in noduplicates: continue else: obj = Sighting() obj.longitude = float(item['X']), obj.latitude = float(item['Y']), obj.unique_squirrel_id = item['Unique Squirrel ID'], obj.shift = item['Shift'], obj.date = timezone.datetime.strptime(str(item['Date']), '%m%d%Y').date(), obj.age = item['Age'], obj.primary_fur_color = item['Primary Fur Color'], obj.location = item['Location'], obj.specific_location = item['Specific Location'], obj.running = item['Running'] == 'TRUE', obj.chasing = item['Chasing'] == 'TRUE', obj.climbing = item['Climbing'] == 'TRUE', obj.eating = item['Eating'] == 'TRUE', obj.foraging = item['Foraging'] == 'TRUE', obj.other_activities = item['Other Activities'], obj.kuks = item['Kuks'] == 'TRUE', obj.quaas = item['Quaas'] == 'TRUE', obj.moans = item['Moans'] == 'TRUE', obj.tail_flags = item['Tail flags'] == 'TRUE', obj.tail_twitches = item['Tail twitches'] == 'TRUE', obj.approaches = item['Approaches'] == 'TRUE', obj.indifferent = item['Indifferent'] == 'TRUE', obj.runs_from = item['Runs from'] == 'TRUE', obj.save() noduplicates.append(item['Unique Squirrel ID'])