def handle(self, *args, **options): file_ = options['csv_file'] with open(file_) as fp: reader = csv.DictReader(fp) unique = [] for dict_ in reader: if dict_['Unique Squirrel ID'] in unique: continue else: obj = Squirrel() obj.Latitude = dict_['X'] obj.Longitude = dict_['Y'] obj.Unique_Squirrel_ID = dict_['Unique Squirrel ID'] obj.Shift = dict_['Shift'] obj.Date = dict_['Date'][4:] + '-' + dict_[ 'Date'][:2] + '-' + dict_['Date'][2:4] obj.Age = dict_['Age'] obj.Primary_Fur_Color = dict_['Primary Fur Color'] obj.Location = dict_['Location'] obj.Specific_Location = dict_['Specific Location'] obj.Running = True if dict_['Running'].lower( ) == 'true' else False obj.Chasing = True if dict_['Chasing'].lower( ) == 'true' else False obj.Climbing = True if dict_['Climbing'].lower( ) == 'true' else False obj.Eating = True if dict_['Eating'].lower( ) == 'true' else False obj.Foraging = True if dict_['Foraging'].lower( ) == 'true' else False obj.Other_Activities = dict_['Other Activities'] obj.Kuks = True if dict_['Kuks'].lower( ) == 'true' else False obj.Quaas = True if dict_['Quaas'].lower( ) == 'true' else False obj.Moans = True if dict_['Moans'].lower( ) == 'true' else False obj.Tail_Flags = True if dict_['Tail flags'].lower( ) == 'true' else False obj.Tail_Twitches = True if dict_['Tail twitches'].lower( ) == 'true' else False obj.Approaches = True if dict_['Approaches'].lower( ) == 'true' else False obj.Indifferent = True if dict_['Indifferent'].lower( ) == 'true' else False obj.Runs_From = True if dict_['Runs from'].lower( ) == 'true' else False obj.save() unique.append(dict_['Unique Squirrel ID'])
def handle(self, *args, **options): file_ = options['path'] with open(file_) as fp: reader = csv.DictReader(fp) data = list(reader) dup_test = [] for item in data: if item['Unique Squirrel ID'] not in dup_test: obj = Squirrel() obj.Longitude = item['X'] obj.Latitude = item['Y'] obj.Unique_Squirrel_ID = item['Unique Squirrel ID'] obj.Shift = item['Shift'] obj.Date = datetime.datetime.strptime( item['Date'], '%m%d%Y') obj.Age = item['Age'] obj.Primary_Fur_Color = item['Primary Fur Color'] obj.Location = item['Location'] obj.Specific_Location = item['Specific Location'] obj.Running = True if item['Running'].lower( ) == 'true' else False obj.Chasing = True if item['Chasing'].lower( ) == 'true' else False obj.Climbing = True if item['Climbing'].lower( ) == 'true' else False obj.Eating = True if item['Eating'].lower( ) == 'true' else False obj.Foraging = True if item['Foraging'].lower( ) == 'true' else False obj.Other_Activities = item['Other Activities'] obj.Kuks = True if item['Kuks'].lower( ) == 'true' else False obj.Quaas = True if item['Quaas'].lower( ) == 'true' else False obj.Moans = True if item['Moans'].lower( ) == 'true' else False obj.Tail_Flags = True if item['Tail flags'].lower( ) == 'true' else False obj.Tail_Twitches = True if item['Tail twitches'].lower( ) == 'true' else False obj.Approaches = True if item['Approaches'].lower( ) == 'true' else False obj.Indifferent = True if item['Indifferent'].lower( ) == 'true' else False obj.Runs_From = True if item['Runs from'].lower( ) == 'true' else False obj.save() dup_test.append(item['Unique Squirrel ID'])
def handle(self,*args,**options): file_ = options['squirrel_file'] with open(file_) as fp: reader = csv.DictReader(fp) id_list = list() for item in reader: if item['Unique Squirrel ID']not in id_list: squirrel = Squirrel() squirrel.Latitude = float(item['X']), squirrel.Longitude = float(item['Y']), squirrel.Unique_Squirrel_ID = item['Unique Squirrel ID'] squirrel.Shift = item['Shift'] squirrel.Date = item['Date'] squirrel.Age = item['Age'] id_list.append(item['Unique Squirrel ID']) squirrel.save() else: continue msg = f'Importing from {file_}' self.stdout.write(self.style.SUCCESS(msg))
def handle(self, *args, **kwargs): path = kwargs['file'] if not os.path.exists(path): raise CommandError("%s does not exist." % path) with open (path, 'r') as f: reader = csv.DictReader (f) data = list(reader) for item in data: try: obj = Squirrel() obj.Latitude = float(item['Y']) obj.Longitude = float(item['X']) obj.Unique_Squirrel_ID = item['Unique Squirrel ID'] obj.Shift = item['Shift'] date = item['Date'][4:] + '-' + item['Date'][:2] + '-' + item['Date'][2:4] obj.Date = 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') or (item['Running'] == 'True') obj.Chasing = (item['Chasing'] == 'true') or (item['Chasing'] == 'True') obj.Climbing = (item['Climbing'] == 'true') or (item['Climbing'] == 'True') obj.Eating = (item['Eating'] == 'true') or (item['Eating'] == 'True') obj.Foraging = (item['Foraging'] == 'true') or (item['Foraging'] == 'True') obj.Other_Activities = item['Other Activities'] obj.Kuks = (item['Kuks'] == 'true') or (item['Kuks'] == 'True') obj.Quaas = (item['Quaas'] == 'true') or (item['Quaas'] == 'True') obj.Moans = (item['Moans'] == 'true') or (item['Moans'] == 'True') obj.Tail_flags = (item['Tail flags'] == 'true') or (item['Tail flags'] == 'True') obj.Tail_twitches = (item['Tail twitches'] == 'true') or (item['Tail twitches'] == 'True') obj.Approaches = (item['Approaches'] == 'true') or (item['Approaches'] == 'True') obj.Indifferent = (item['Indifferent'] == 'true') or (item['Indifferent'] == 'True') obj.Runs_from = (item['Runs from'] == 'true') or (item['Runs from'] == 'True') obj.save () except Exception as e: raise CommandError(e) self.stdout.write(self.style.SUCCESS('Successfully read file "%s"' % path))
def handle(self, *args, **options): file_ = options['squirrel_file'] msg = f'You are importing from {file_}' self.stdout.write(self.style.SUCCESS(msg)) with open(file_) as fp: reader = csv.DictReader(fp) for i in reader: s = Squirrel() s.X = i['X'] s.Y = i['Y'] s.Unique_Squirrel_ID = i['Unique Squirrel ID'] s.Shift = i['Shift'] s.Date = datetime.date(int(i['Date'][-4:]), int(i['Date'][:2]), int(i['Date'][2:4])) s.Age = i['Age'] s.Primary_Fur_Color = i['Primary Fur Color'] s.Location = i['Location'] s.Specific_Location = i['Specific Location'] s.Running = i['Running'] s.Chasing = i['Chasing'] s.Climbing = i['Climbing'] s.Eating = i['Eating'] s.Foraging = i['Foraging'] s.Other_Activities = i['Other Activities'] s.Kuks = i['Kuks'] s.Quaas = i['Quaas'] s.Moans = i['Moans'] s.Tail_flags = i['Tail flags'] s.Tail_twitches = i['Tail twitches'] s.Approaches = i['Approaches'] s.Indifferent = i['Indifferent'] s.Runs_from = i['Runs from'] s.save()