コード例 #1
0
    def handle(self, *args, **kwargs):
        path = kwargs['path']

        try:
            with open(path, encoding='utf-8') as fn:
                reader = csv.DictReader(fn)
                for r in reader:
                    sight = Squirrel(
                        lat=r['Y'],
                        lon=r['X'],
                        unique_id=r['Unique Squirrel ID'],
                        shift=r['Shift'],
                        date=datetime.strptime(r['Date'], r'%m%d%Y'),
                        age=r['Age'],
                        fur_color=r['Primary Fur Color'],
                        location=r['Location'],
                        specific_loc=r['Specific Location'],
                        running=(r['Running'] == 'true'),
                        chasing=(r['Chasing'] == 'true'),
                        climbing=(r['Climbing'] == 'true'),
                        eating=(r['Eating'] == 'true'),
                        foraging=(r['Foraging'] == 'true'),
                        other_activities=r['Other Activities'],
                        kuks=(r['Kuks'] == 'true'),
                        quaas=(r['Quaas'] == 'true'),
                        moans=(r['Moans'] == 'true'),
                        tail_flags=(r['Tail flags'] == 'true'),
                        tail_twitches=(r['Tail twitches'] == 'true'),
                        approaches=(r['Approaches'] == 'true'),
                        #indifferent=r['Indifferent'].upper(),
                        runs_from=(r['Runs from'] == 'true'),
                    )
                    sight.save()
        except csv.Error as e:
            print(f'Error with {reader.line_num}')
コード例 #2
0
 def handle(self, *args, **options):
     with open(options['csv_file']) as fp:
         reader = csv.DictReader(fp)
         data = list(reader)
         for item in data:
             squirrel = Squirrel(
                 longitude=item['X'],
                 latitude=item['Y'],
                 unique_squirrel_id=item['Unique Squirrel ID'],
                 shift=item['Shift'],
                 date=item['Date'][4:] + '-' + item['Date'][:2] + '-' +
                 item['Date'][2:4],
                 age=item['Age'],
                 primary_fur_color=item['Primary Fur Color'],
                 location=item['Location'],
                 specific_location=item['Specific Location'],
                 running=item['Running'].lower().capitalize(),
                 chasing=item['Chasing'].lower().capitalize(),
                 climbing=item['Climbing'].lower().capitalize(),
                 eating=item['Eating'].lower().capitalize(),
                 foraging=item['Foraging'].lower().capitalize(),
                 other_activities=item['Other Activities'],
                 kuks=item['Kuks'].lower().capitalize(),
                 quaas=item['Quaas'].lower().capitalize(),
                 moans=item['Moans'].lower().capitalize(),
                 tail_flags=item['Tail flags'].lower().capitalize(),
                 tail_twitches=item['Tail twitches'].lower().capitalize(),
                 approaches=item['Approaches'].lower().capitalize(),
                 indifferent=item['Indifferent'].lower().capitalize(),
                 runs_from=item['Runs from'].lower().capitalize())
             squirrel.save()
コード例 #3
0
 def handle(self, *args, **options):
     df = pd.read_csv(options['csv_file'])
     for item in df.iterrows():
         item = item[1]
         s = Squirrel(
             latitude=item['Y'],
             longitude=item['X'],
             squirrel_id=item['Unique Squirrel ID'],
             shift=item['Shift'],
             date=timezone.datetime(int(str(item['Date'])[-4:]),
                                    int(str(item['Date'])[:2]),
                                    int(str(item['Date'])[2:4])).date(),
             age=item['Age'],
             color=item['Primary Fur Color'],
             location=item['Location'],
             specific_location=item['Specific Location'],
             running=item['Running'],
             chasing=item['Chasing'],
             climbing=item['Climbing'],
             eating=item['Eating'],
             foraging=item['Foraging'],
             other_activities=item['Other Activities'],
             kuks=item['Kuks'],
             quaas=item['Quaas'],
             moans=item['Moans'],
             tail_flag=item['Tail flags'],
             tail_twitches=item['Tail twitches'],
             approaches=item['Approaches'],
             indifferent=item['Indifferent'],
             runs_from=item['Runs from'],
         )
         s.save()
コード例 #4
0
 def handle(self, *args, **options):
     path = options['path']
     df = pd.read_csv(path)
     for item in df.iterrows():
         item = item[1]
         s = Squirrel(
             Y=item['Y'],
             X=item['X'],
             USID=item['Unique Squirrel ID'],
             Shift=item['Shift'],
             Date=timezone.datetime(int(str(item['Date'])[-4:]),
                                    int(str(item['Date'])[:2]),
                                    int(str(item['Date'])[2:4])).date(),
             Age=item['Age'],
             PFC=item['Primary Fur Color'],
             Location=item['Location'],
             SL=item['Specific Location'],
             Running=item['Running'],
             Chasing=item['Chasing'],
             Climbing=item['Climbing'],
             Eating=item['Eating'],
             Foraging=item['Foraging'],
             OA=item['Other Activities'],
             Kuks=item['Kuks'],
             Quaas=item['Quaas'],
             Moans=item['Moans'],
             TF=item['Tail flags'],
             TT=item['Tail twitches'],
             Approaches=item['Approaches'],
             Indifferent=item['Indifferent'],
             RF=item['Runs from'],
         )
         s.save()
コード例 #5
0
    def handle(self, *args, **options):
        with open(options['csv_file']) as fp:
            reader = csv.DictReader(fp)
            data = list(reader)
        for item in data:
            if item['Shift'] == 'AM':
                shift_ = Squirrel.AM
            else:
                shift_ = Squirrel.PM
            if item['Age'] == 'Adult':
                age_ = Squirrel.ADULT
            elif item['Age'] == 'Juvenile':
                age_ = Squirrel.JUVENILE
            else:
                age_ = Squirrel.UNKNOWN

            if item['Primary Fur Color'] == 'Gray':
                color_ = Squirrel.GRAY
            elif item['Primary Fur Color'] == 'Cinnamon':
                color_ = Squirrel.CINNAMON
            elif item['Primary Fur Color'] == 'Black':
                color_ = Squirrel.BLACK
            else:
                color_ = Squirrel.UNKNOWN
            if item['Location'] == 'Ground Plane':
                location_ = Squirrel.GROUND_PLANE
            elif item['Location'] == 'Above Ground':
                location_ = Squirrel.ABOVE_GROUND
            else:
                location_ = Squirrel.UNKNOWN

            p = Squirrel(
                latitude=float(item['Y']),
                longitude=float(item['X']),
                squirrel_id=item['Unique Squirrel ID'],
                shift=shift_,
                date=datetime.date(int(item['Date'][-4:]), int(item['Date'][:2]), int(item['Date'][2:4])),
                age=age_,
                fur_color=color_,
                location=location_,
                specific_location=item['Specific Location'],
                running=self.booler(item['Running']),
                chasing=self.booler(item['Chasing']),
                climbing=self.booler(item['Climbing']),
                eating=self.booler(item['Eating']),
                foraging=self.booler(item['Foraging']),
                other_activities=item['Other Activities'],
                kuks=self.booler(item['Kuks']),
                quaas=self.booler(item['Quaas']),
                moans=self.booler(item['Moans']),
                tail_flags=self.booler(item['Tail flags']),
                tail_twitches=self.booler(item['Tail twitches']),
                approaches=self.booler(item['Approaches']),
                indifferent=self.booler(item['Indifferent']),
                runs_from=self.booler(item['Runs from']),
            )
            p.save()
コード例 #6
0
 def save_data(cnt: int):
     s = Squirrel(
         Latitude=float(df_new[cnt][1]),
         Longitude=float(df_new[cnt][0]),
         Unique_Squirrel_ID=df_new[cnt][2],
         Shift=df_new[cnt][3],
         Date=int(df_new[cnt][4]),
         Age=df_new[cnt][5],
         Primary_Fur_Color=df_new[cnt][6],
         Location=df_new[cnt][7],
         Specific_Location=df_new[cnt][8],
         Running=df_new[cnt][9],
         Chasing=df_new[cnt][10],
         Climbing=df_new[cnt][11],
         Eating=df_new[cnt][12],
         Foraging=df_new[cnt][13],
         Other_Activities=df_new[cnt][14],
         Kuks=df_new[cnt][15],
         Quaas=df_new[cnt][16],
         Moans=df_new[cnt][17],
         Tail_flags=df_new[cnt][18],
         Tail_twitches=df_new[cnt][19],
         Approaches=df_new[cnt][20],
         Indifferent=df_new[cnt][21],
         Runs_from=df_new[cnt][22],
     )
     return s
コード例 #7
0
    def handle(self, *args, **options):
        with open(options['csv_file']) as fp:
            reader = csv.DictReader(fp)
            data = list(reader)

        sightings = []
        for dict_ in data:
            sightings.append(
                Squirrel(
                    latitude=float(dict_['Y']),
                    longitude=float(dict_['X']),
                    unique_squirrel_id=dict_['Unique Squirrel ID'],
                    shift=dict_['Shift'].lower(),
                    date=timezone.datetime.strptime(dict_['Date'],
                                                    '%m%d%Y').date(),
                    age=dict_['Age'],
                    primary_fur_color=dict_['Primary Fur Color'],
                    specific_location=dict_['Specific Location'],
                    running=dict_['Running'].lower() == 'true',
                    chasing=dict_['Chasing'].lower() == 'true',
                    climbing=dict_['Climbing'].lower() == 'true',
                    eating=dict_['Eating'].lower() == 'true',
                    foraging=dict_['Foraging'].lower() == 'true',
                    other_activities=dict_['Other Activities'],
                    kuks=dict_['Kuks'].lower() == 'true',
                    quaas=dict_['Quaas'].lower() == 'true',
                    moans=dict_['Moans'].lower() == 'true',
                    tail_flags=dict_['Tail flags'].lower() == 'true',
                    tail_twitches=dict_['Tail twitches'].lower() == 'true',
                    approaches=dict_['Approaches'].lower() == 'true',
                    indifferent=dict_['Indifferent'].lower() == 'true',
                    runs_from=dict_['Runs from'].lower() == 'true',
                ))

        Squirrel.objects.bulk_create(sightings)
コード例 #8
0
    def handle(self, *args, **kwargs):
        path = kwargs['path']

        try:
            with open(path, encoding='utf-8') as f:
                reader = csv.DictReader(f)
                data = list(reader)

            def convert(string):
                if string in ['false', 'FALSE', 'False']:
                    return False
                elif string in ['true', 'TRUE', 'True']:
                    return True
                else:
                    return None

            for row in data:
                s = Squirrel(
                    Latitude=row['Y'],
                    Longitude=row['X'],
                    Squirrel_ID=row['Unique Squirrel ID'],
                    Shift=row['Shift'],
                    Date=datetime.datetime.strptime(row['Date'], '%m%d%Y'),
                    Age=row['Age'],
                    Primary_Fur_Color=row['Primary Fur Color'],
                    Location=row['Location'],
                    Specific_Location=row['Specific Location'],
                    Running=convert(row['Running']),
                    Chasing=convert(row['Chasing']),
                    Climbing=convert(row['Climbing']),
                    Eating=convert(row['Eating']),
                    Foraging=convert(row['Foraging']),
                    Other_Activities=row['Other Activities'],
                    Kuks=convert(row['Kuks']),
                    Quaas=convert(row['Quaas']),
                    Moans=convert(row['Moans']),
                    Tail_Flags=convert(row['Tail flags']),
                    Tail_Twitches=convert(row['Tail twitches']),
                    Approaches=convert(row['Approaches']),
                    Indifferent=convert(row['Indifferent']),
                    Runs_From=convert(row['Runs from']),
                )
                s.save()
            print('successfully saved csv')
        except:
            print('something wrong')
コード例 #9
0
 def handle(self, *args, **options):
     with open(options['csv_file']) as fp:
         reader = csv.DictReader(fp)
         data = list(reader)
         pattern = re.compile(r'(\d{2})(\d{2})(\d{4})')
         for item in data:
             i, j, k = pattern.match(item['Date']).groups()
             s = Squirrel(
                 X=item['X'],
                 Y=item['Y'],
                 Unique_Squirrel_ID=item['Unique Squirrel ID'],
                 #Hectare=item['Hectare'],
                 Shift=item['Shift'],
                 Date=date(int(k), int(i), int(j)),
                 #Hectare_Squirrel_Number=item['Hectare Squirrel Number'],
                 Age=item['Age'],
                 Primary_Fur_Color=item['Primary Fur Color'],
                 #Highlight_Fur_Color=item['Highlight Fur Color'],
                 #Combination_Fur=item['Combination of Primary and Highlight Color'],
                 #Color_Notes=item['Color Notes'],
                 Location=item['Location'],
                 Specific_Location=item['Specific Location'],
                 Running=item['Running'],
                 Chasing=item['Chasing'],
                 Climbing=item['Climbing'],
                 Eating=item['Eating'],
                 Foraging=item['Foraging'],
                 Other_Activities=item['Other Activities'],
                 Kuks=item['Kuks'],
                 Quaas=item['Quaas'],
                 Moans=item['Moans'],
                 Tail_Flags=item['Tail flags'],
                 Tail_Twitches=item['Tail twitches'],
                 Approaches=item['Approaches'],
                 Indifferent=item['Indifferent'],
                 Runs_From=item['Runs from'],
                 #Other_Interactions=item['Other Interactions'],
                 #Lat_Long=item['Lat/Long'],
                 #Zip_Codes=item['Zip Codes'],
                 #Community_Districts=item['Community Districts'],
                 #Borough_Boundaries=item['Borough Boundaries'],
                 #City_Council_Districts=item['City Council Districts'],
                 #Police_Precincts=item['Police Precincts'],
             )
             s.save()
コード例 #10
0
    def handle(self, *args, **options):
        with open(options['SquirrelData'], 'r') as fp:
            reader = csv.DictReader(fp)
            data = list(reader)
            for row in data:

                def StrChange(str_):
                    if str.lower(str_) == 'true':
                        return True
                    else:
                        return False

                if re.search(r'(\d{2})(\d{2})(\d{4})', row['Date']):
                    BoolMatch = re.search(r'(\d{2})(\d{2})(\d{4})',
                                          row['Date'])

                p = Squirrel(Latitude=row['Y'],
                             Longitude=row['X'],
                             Unique_squirrel_id=row['Unique Squirrel ID'],
                             Shift=row['Shift'],
                             Date='-'.join([
                                 BoolMatch.group(3),
                                 BoolMatch.group(1),
                                 BoolMatch.group(2)
                             ]),
                             Age=row['Age'],
                             Primary_fur_color=row['Primary Fur Color'],
                             Location=row['Location'],
                             Specific_location=row['Specific Location'],
                             Running=StrChange(row['Running']),
                             Chasing=StrChange(row['Chasing']),
                             Climbing=StrChange(row['Climbing']),
                             Eating=StrChange(row['Eating']),
                             Foraging=StrChange(row['Foraging']),
                             Other_activities=row['Other Activities'],
                             Kuks=StrChange(row['Kuks']),
                             Quaas=StrChange(row['Quaas']),
                             Moans=StrChange(row['Moans']),
                             Tail_flags=StrChange(row['Tail flags']),
                             Tail_twitches=StrChange(row['Tail twitches']),
                             Approaches=StrChange(row['Approaches']),
                             Indifferent=StrChange(row['Indifferent']),
                             Runs_from=StrChange(row['Runs from']))

                p.save()
コード例 #11
0
    def handle(self, *args, **options):

        path = options['path']

        with open(path, 'r') as f:
            reader = csv.reader(f)
            next(reader)
            idlist = list()
            for row in reader:
                if row[2] in idlist:
                    pass
                else:
                    idlist.append(row[2])
                    date = row[5]
                    m = date[:2]
                    d = date[2:4]
                    y = date[4:]
                    new_date = y + '-' + m + '-' + d
                    p = Squirrel(
                        longitude=float(row[0]),
                        latitude=float(row[1]),
                        squirrel_id=row[2],
                        shift=row[4],
                        date=new_date,
                        age=row[7],
                        color=row[8],
                        location=row[12],
                        specific_location=row[14],
                        running=self.str_to_bool(row[15]),
                        chasing=self.str_to_bool(row[16]),
                        climbing=self.str_to_bool(row[17]),
                        eating=self.str_to_bool(row[18]),
                        foraging=self.str_to_bool(row[19]),
                        other_activities=row[20],
                        kuks=self.str_to_bool(row[21]),
                        quaas=self.str_to_bool(row[22]),
                        moans=self.str_to_bool(row[23]),
                        tail_flags=self.str_to_bool(row[24]),
                        tail_twitches=self.str_to_bool(row[25]),
                        approaches=self.str_to_bool(row[26]),
                        indifferent=self.str_to_bool(row[27]),
                        runs_from=self.str_to_bool(row[28]),
                    )
                    p.save()
コード例 #12
0
    def handle(self, *args, **options):
        file_ = options['squirrel_file']

        with open(file_, encoding='utf-8') as fp:
            reader = csv.DictReader(fp)

            for item in reader:
                obj = Squirrel()
                obj.X = item['X']
                obj.Y = item['Y']
                obj.unique_id = item['Unique Squirrel ID']
                obj.shift = item['Shift']
                obj.date = datetime.date(int(item['Date'][-4:]),
                                         int(item['Date'][:2]),
                                         int(item['Date'][2:4]))
                obj.age = item['Age']
                obj.save()
        msg = f'You are importing from {file_}'
        self.stdout.write(self.style.SUCCESS(msg))
コード例 #13
0
    def handle(self, *args, **options):
        Squirrel.objects.all().delete()
        file_ = options['file_']
        with open(file_) as fp:
            reader = csv.DictReader(fp)
            data = list(reader)

            def toBoolean(string):
                if str(string).lower() == 'true':
                    return True
                elif str(string).lower() == 'false':
                    return False

            for obs in data:
                inst = Squirrel(
                    X=obs['X'],
                    Y=obs['Y'],
                    Unique_Squirrel_ID=obs['Unique Squirrel ID'],
                    Shift=obs['Shift'],
                    Date=obs['Date'],
                    Age=obs['Age'],
                    Primary_Fur_Color=obs['Primary Fur Color'],
                    Location=obs['Location'],
                    Specific_Location=obs['Specific Location'],
                    Running=toBoolean(obs['Running']),
                    Chasing=toBoolean(obs['Chasing']),
                    Climbing=toBoolean(obs['Climbing']),
                    Eating=toBoolean(obs['Eating']),
                    Foraging=toBoolean(obs['Foraging']),
                    Other_Activities=obs['Other Activities'],
                    Kuks=toBoolean(obs['Kuks']),
                    Quaas=toBoolean(obs['Quaas']),
                    Moans=toBoolean(obs['Moans']),
                    Tail_flags=toBoolean(obs['Tail flags']),
                    Tail_twitches=toBoolean(obs['Tail twitches']),
                    Approaches=toBoolean(obs['Approaches']),
                    Indifferent=toBoolean(obs['Indifferent']),
                    Runs_from=toBoolean(obs['Runs from']),
                )
                inst.save()
            msg = f'You are importing from {file_}'
            self.stdout.write(self.style.SUCCESS(msg))
コード例 #14
0
 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))
コード例 #15
0
    def handle(self, *args, **kwargs):
        path = kwargs['path']

        try:
            with open(path, encoding='utf-8') as f:
                reader = csv.DictReader(f)
                for x in reader:
                    squirrel = Squirrel(
                        X=x['X'],
                        Y=x['Y'],
                        Squirrel_ID=x['Unique Squirrel ID'],
                        Shift=x['Shift'],
                        Date=datetime.date(int(x['Date'][-4:]),
                                           int(x['Date'][:2]),
                                           int(x['Date'][2:4])),
                        Age=x['Age'],
                        Primary_Fur_Color=x['Primary Fur Color'],
                        Location=x['Location'],
                        Specific_Location=x['Specific Location'],
                        Running=True if x['Running'] == 'True' else False,
                        Chasing=True if x['Chasing'] == 'True' else False,
                        Climbing=True if x['Climbing'] == 'True' else False,
                        Eating=True if x['Eating'] == 'True' else False,
                        Foraging=True if x['Foraging'] == 'True' else False,
                        Other_Activities=x['Other Activities'],
                        Kuks=True if x['Kuks'] == 'True' else False,
                        Quaas=True if x['Quaas'] == 'True' else False,
                        Moans=True if x['Moans'] == 'True' else False,
                        Tail_Flags=True
                        if x['Tail flags'] == 'True' else False,
                        Tail_Twitches=True
                        if x['Tail twitches'] == 'True' else False,
                        Approaches=True
                        if x['Approaches'] == 'True' else False,
                        Indifferent=True
                        if x['Indifferent'] == 'True' else False,
                        Runs_From=True if x['Runs from'] == 'True' else False,
                    )
                    squirrel.save()
        except csv.Error as e:
            pring(f'An error has occured at {reader.line_num}')
コード例 #16
0
    def handle(self, *args, **options):
        with open(options['csv_file']) as fp:
            reader = csv.DictReader(fp)
            data = list(reader)

        def clean_data(data):
            if (data == 'true') or (data == 'True') or (data == 'TRUE'):
                return True
            elif (data == 'false') or (data == 'False') or (data == 'FALSE'):
                return False
            else:
                return data

        for item in data:
            s = Squirrel(
                latitude=item['Y'],
                longitude=item['X'],
                unique_squirrel_id=item['Unique Squirrel ID'],
                shift=item['Shift'],
                date=datetime.datetime.strptime(item['Date'], '%m%d%Y'),
                age=item['Age'],
                primary_fur_color=item['Primary Fur Color'],
                location=item['Location'],
                specific_location=item['Specific Location'],
                running=clean_data(item['Running']),
                chasing=clean_data(item['Chasing']),
                climbing=clean_data(item['Climbing']),
                eating=clean_data(item['Eating']),
                foraging=clean_data(item['Foraging']),
                other_activities=item['Other Activities'],
                kuks=clean_data(item['Kuks']),
                quaas=clean_data(item['Quaas']),
                moans=clean_data(item['Moans']),
                tail_flags=clean_data(item['Tail flags']),
                tail_twitches=clean_data(item['Tail twitches']),
                approaches=clean_data(item['Approaches']),
                indifferent=clean_data(item['Indifferent']),
                runs_from=clean_data(item['Runs from']),
            )

            s.save()
コード例 #17
0
 def handle(self, *args, **options):
     with open(options['csv_file']) as fp:
         reader = csv.DictReader(fp)
         next(reader)
         data = list()
         for item in reader:
             if item['Unique Squirrel ID'] in data:
                 pass
             else:
                 data.append(item['Unique Squirrel ID'])
                 date = item['Date']
                 y = date[4:]
                 m = date[:2]
                 d = date[2:4]
                 formal_date = y + '-' + m + '-' + d
                 s = Squirrel(
                     Latitude = item['Y'],
                     Longitude = item['X'],
                     Unique_Squirrel_ID = item['Unique Squirrel ID'],
                     Shift = item['Shift'],
                     Date = formal_date,
                     Age = item['Age'],
                     Primary_Fur_Color = item['Primary Fur Color'],
                     Location = item['Location'],
                     Specific_Location = item['Specific Location'],
                     Running = self.str_to_bool(item['Running']),
                     Chasing = self.str_to_bool(item['Chasing']),
                     Climbing = self.str_to_bool(item['Climbing']),
                     Eating = self.str_to_bool(item['Eating']),
                     Foraging = self.str_to_bool(item['Foraging']),
                     Other_Activities = item['Other Activities'],
                     Kuks = self.str_to_bool(item['Kuks']),
                     Quaas = self.str_to_bool(item['Quaas']),
                     Moans = self.str_to_bool(item['Moans']),
                     Tail_flags = self.str_to_bool(item['Tail flags']),
                     Tail_twitches = self.str_to_bool(item['Tail twitches']),
                     Approaches = self.str_to_bool(item['Approaches']),
                     Indifferent = self.str_to_bool(item['Indifferent']),
                     Runs_from = self.str_to_bool(item['Runs from']),
                 )
                 s.save()
コード例 #18
0
 def handle(self, *args, **options):
     with open(options['path']) as fp:
         reader = csv.DictReader(fp)
         data = list(reader)
     squirrels = []
     a = []
     for dict_ in data:
         if dict_['Unique Squirrel ID'] in a:
             continue
         else:
             squirrels.append(
                 Squirrel(
                     latitude=dict_['X'],
                     longitude=dict_['Y'],
                     unique_squirrel_id=dict_['Unique Squirrel ID'],
                     shift=dict_['Shift'],
                     date=dict_['Date'][4:8] + '-' + dict_['Date'][0:2] +
                     '-' + dict_['Date'][2:4],
                     age=dict_['Age'],
                     primary_fur_color=dict_['Primary Fur Color'],
                     location=dict_['Location'],
                     specific_location=dict_['Specific Location'],
                     running=True
                     if dict_['Running'].lower() == 'true' else False,
                     chasing=True
                     if dict_['Chasing'].lower() == 'true' else False,
                     climbing=True
                     if dict_['Climbing'].lower() == 'true' else False,
                     eating=True
                     if dict_['Eating'].lower() == 'true' else False,
                     foraging=True
                     if dict_['Foraging'].lower() == 'true' else False,
                     other_activities=dict_['Other Activities'],
                     kuks=True
                     if dict_['Kuks'].lower() == 'true' else False,
                     quaas=True
                     if dict_['Quaas'].lower() == 'true' else False,
                     moans=True
                     if dict_['Moans'].lower() == 'true' else False,
                     tail_flags=True
                     if dict_['Tail flags'].lower() == 'true' else False,
                     tail_twitches=True
                     if dict_['Tail twitches'].lower() == 'true' else False,
                     approaches=True
                     if dict_['Approaches'].lower() == 'true' else False,
                     indifferent=True
                     if dict_['Indifferent'].lower() == 'true' else False,
                     runs_from=True
                     if dict_['Runs from'].lower() == 'true' else False,
                 ))
             a.append(dict_['Unique Squirrel ID'])
     Squirrel.objects.bulk_create(squirrels)
コード例 #19
0
    def handle(self, *args, **options):

        csvpath = args[0]

        fields_name = []
        with open(csvpath) as csvfile:
            reader = csv.reader(csvfile)
            header = csvfile.readline().strip().split(',')
            fields_name = [
                x.lower().replace(' ', '_').replace('/', '_') for x in header
            ]
            for row in reader:
                obj = Squirrel()
                for i, field in enumerate(row):
                    if field == 'true':
                        field = True
                    if field == 'false':
                        field = False

                    setattr(obj, fields_name[i], field)

                obj.save()
コード例 #20
0
    def handle(self, *arg, **options):
        import csv
        import datetime
        path = options['csv_file']
        with open(path) as f:
            reader = csv.DictReader(f)
            data = list(reader)

            for line in data:
                line['Date'] = line['Date'][4:] + '-' + line['Date'][
                    0:2] + '-' + line['Date'][2:4]
                sighting = Squirrel(
                    latitude=line['Y'],
                    longitude=line['X'],
                    squirrel_id=line['Unique Squirrel ID'],
                    shift=line['Shift'],
                    date=line['Date'],
                    age=line['Age'],
                    fur_color=line['Primary Fur Color'],
                    location=line['Location'],
                    specific_location=line['Specific Location'],
                    running=strtobool(line['Running']),
                    chasing=strtobool(line['Chasing']),
                    climbing=strtobool(line['Climbing']),
                    eating=strtobool(line['Eating']),
                    foraging=strtobool(line['Foraging']),
                    other_activities=line['Other Activities'],
                    kuks=strtobool(line['Kuks']),
                    quaas=strtobool(line['Quaas']),
                    moans=strtobool(line['Moans']),
                    tail_flags=strtobool(line['Tail flags']),
                    tail_twitches=strtobool(line['Tail twitches']),
                    approaches=strtobool(line['Approaches']),
                    indifferent=strtobool(line['Indifferent']),
                    runs_from=strtobool(line['Runs from']),
                )

                sighting.save()
コード例 #21
0
    def handle(self, *args, **options):
        with open(options['csv_file']) as fp:
                reader = csv.DictReader(fp)
                data = list(reader)
                
        def boolstr(obj):
            if str(obj).lower == 'true':
                return True
            else:
                return False

        sqs = []
        for dict_ in data:
            s=Squirrel(
                Latitude=float(dict_['Y']), #Notice the sequence for exportation
                Longitude=float(dict_['X']),
                Unique_Squirrel_ID=dict_['Unique Squirrel ID'],
                Shift=dict_['Shift'],
                Date='-'.join([str(dict_['Date'])[4:8], str(dict_['Date'])[0:2], str(dict_['Date'])[2:4]]),
                Age=dict_['Age'] if str(dict_['Age'])!='nan' else 'Unknown',
                Primary_Fur_Color = dict_['Primary Fur Color'] if str(dict_['Primary Fur Color'])!='nan' else 'Unknown',
                Location = dict_['Location'] if str(dict_['Location'])!='nan' else 'Unknown',
                Specific_Location = str(dict_['Specific Location']),
                Running = boolstr(dict_['Running']),
                Chasing = boolstr(dict_['Chasing']),
                Climbing = boolstr(dict_['Climbing']),
                Eating = boolstr(dict_['Eating']),
                Foraging = boolstr(dict_['Foraging']),
                Other_Activities = str(dict_['Other Activities']),
                Kuks = boolstr(dict_['Kuks']),
                Quaas = boolstr(dict_['Quaas']),
                Moans = boolstr(dict_['Moans']),
                Tail_flags = boolstr(dict_['Tail flags']),
                Tail_twitches = boolstr(dict_['Tail twitches']),
                Approaches = boolstr(dict_['Approaches']),
                Indifferent = boolstr(dict_['Indifferent']),
                Runs_from = boolstr(dict_['Runs from']))
            s.save()
コード例 #22
0
    def handle(self, *args, **options):
        with open(options['path']) as fp:
            reader = csv.DictReader(fp)
            data = list(reader)

        for item in data:
            for i in ('Running','Chasing','Climbing','Eating','Foraging','Kuks','Quaas','Moans','Tail flags','Tail twitches','Approaches','Indifferent','Runs from'):
                if item[i] == 'false':
                    item[i] = False
                else:
                    item[i] = True
            squirrel = Squirrel(
                lon = item['X'],
                lat = item['Y'],
                squirrel_id = item['Unique Squirrel ID'],
                shift = item['Shift'],
                date = datetime.datetime.strptime(item['Date'].strip(),'%m%d%Y').date(),
                age = item['Age'],
                pri_fur_color = item['Primary Fur Color'],
                location = item['Location'],
                specific_location = item['Specific Location'],
                running = item['Running'],
                chasing = item['Chasing'],
                climbing = item['Climbing'],
                eating = item['Eating'],
                foraging = item['Foraging'],
                other_activities = item['Other Activities'],
                kuks = item['Kuks'],
                quaas = item['Quaas'],
                moans = item['Moans'],
                tail_flags = item['Tail flags'],
                tail_twitches = item['Tail twitches'],
                approaches = item['Approaches'],
                indifferent = item['Indifferent'],
                runs_from = item['Runs from'],       
            )

            squirrel.save()
コード例 #23
0
 def handle(self, *args, **kwargs):
     path = args[0]
     insertlist = []
     with open(path, "r", encoding="utf-8") as f:
         csv_reader = csv.reader(f)
         for row in csv_reader:
             for i in range(0, len(row)):
                 if row[i] == "false":
                     row[i] = False
                 if row[i] == "true":
                     row[i] = True
                 date = row[5]
                 newdate = date[-4:] + "-" + date[0:2] + "-" + date[2:4]
             try:
                 squirrel = Squirrel(
                     x=Decimal(row[0]),
                     y=Decimal(row[1]),
                     unique_squirrel_id=row[2],
                     hectare=row[3],
                     shift=row[4],
                     date=newdate,
                     hectare_squirrel_number=row[6],
                     age=row[7],
                     primary_fur_color=row[8],
                     highlight_fur_color=row[9],
                     combination_of_primary_and_highlight_color=row[10],
                     color_notes=row[11],
                     location=row[12],
                     above_ground_sighter_measurement=row[13],
                     specific_location=row[14],
                     running=row[15],
                     chasing=row[16],
                     climbing=row[17],
                     eating=row[18],
                     foraging=row[19],
                     other_activities=row[20],
                     kuks=row[21],
                     quaas=row[22],
                     moans=row[23],
                     tail_flags=row[24],
                     tail_twitches=row[25],
                     approaches=row[26],
                     indifferent=row[27],
                     runs_from=row[28],
                     other_interactions=row[29],
                     lat_long=row[30])
                 insertlist.append(squirrel)
             except:
                 continue
         Squirrel.objects.bulk_create(insertlist)
コード例 #24
0
    def handle(self, *args, **options):
        with open(options['csv_file']) as fp:
            reader = csv.DictReader(fp)
            data = list(reader)

        sightings = []
        for dict_ in data:
            sightings.append(Squirrel(
                latitude=float(dict_['Y']),
                longitude=float(dict_['X']),
                unique_Squirrel_ID=dict_['Unique Squirrel ID'],
                shift=dict_['Shift'].lower(),
                date=timezone.datetime.strptime(dict_['Date'], '%m%d%Y').date(),
                age=dict_['Age'],
            ))

        Squirrel.objects.bulk_create(sightings)
コード例 #25
0
 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'])
コード例 #26
0
    def handle(self, *args, **options):
        path = options['path']
        if not os.path.exists(path):
            raise CommandError(f'Path:{path}does not exist.')

        with open(options['path']) as fp:
            reader = csv.DictReader(fp)
            data = list(reader)
            
            for item in data:
                latitude = item['Y']
                longitude = item['X']
                unique_squirrel_id = item['Unique Squirrel ID']

                if (item['Shift'].lower() in ['am', 'pm']):
                    shift = item['Shift'].upper()
                else:
                    shift = ''
    
                date = item['Date']

                if (item['Age'].lower() in ['adult', 'juvenile']):
                    age = item['Age'].lower().capitalize()
                else:
                    age = ''

                if (item['Primary Fur Color'].lower() in ['black', 'gray', 'cinnamon']):
                    primary_fur_color = item['Primary Fur Color'].lower().capitalize()
                else:
                    primary_fur_color = ''

                if (item['Location'].lower() in ['ground plane', 'above ground']):
                    location = item['Location'].split()[0].capitalize() + ' ' + item['Location'].split()[1].capitalize()
                else:
                    location = ''

                specific_location = item['Specific Location']

                running = True if "true" in item['Running'].lower() else False
                chasing = True if "true" in item['Chasing'].lower() else False
                climbing = True if "true" in item['Climbing'].lower() else False
                eating = True if "true" in item['Eating'].lower() else False
                foraging = True if "true" in item['Foraging'].lower() else False

                other_activities = item['Other Activities']

                kuks = True if "true" in item['Kuks'].lower() else False
                quaas = True if "true" in item['Quaas'].lower() else False
                moans = True if "true" in item['Moans'].lower() else False
                tail_flags = True if "true" in item['Tail flags'].lower() else False
                tail_twitches = True if "true" in item['Tail twitches'].lower() else False
                approaches = True if "true" in item['Approaches'].lower() else False
                indifferent = True if "true" in item['Indifferent'].lower() else False
                runs_from = True if "true" in item['Runs from'].lower() else False
    
                squirrel = Squirrel(
                    latitude = latitude,
                    longitude = longitude,
                    unique_squirrel_id = unique_squirrel_id,
                    shift = shift,
                    date = date,
                    age = age,
                    primary_fur_color = primary_fur_color,
                    location = location,
                    specific_location = specific_location,
                    running = running,
                    chasing = chasing,
                    climbing = climbing,
                    eating = eating,
                    foraging = foraging,
                    other_activities = other_activities,
                    kuks = kuks,
                    quaas = quaas,
                    moans = moans,
                    tail_flags = tail_flags,
                    tail_twitches = tail_twitches,
                    approaches = approaches,
                    indifferent = indifferent,
                    runs_from = runs_from,
                )
                squirrel.save()
コード例 #27
0
    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'])
コード例 #28
0
    def handle(self, *args, **options):
        file_ = options['squirrels_file']

        with open(file_) as fp:
            reader = csv.DictReader(fp)

            for item in reader:
                obj = Squirrel()
                obj.latitude = item['Y']
                obj.longitude = item['X']
                obj.unique_squirrel_id = item['Unique Squirrel ID']
                obj.hectare = item['Hectare']
                obj.shift = item['Shift']
                obj.date = datetime.strptime(item['Date'], '%m%d%Y').date()
                obj.hectare_squirrel_number = item['Hectare Squirrel Number']
                obj.age = item['Age']
                obj.primary_fur_color = item['Primary Fur Color']
                obj.highlight_fur_color = item['Highlight Fur Color']
                obj.color_notes = item['Color notes']
                obj.location = item['Location']
                obj.specific_location = item['Specific Location']
                obj.running = item['Running']
                obj.chasing = item['Chasing']
                obj.climbing = item['Climbing']
                obj.eating = item['Eating']
                obj.foraging = item['Foraging']
                obj.other_activities = item['Other Activities']
                obj.kuks = item['Kuks']
                obj.quaas = item['Quaas']
                obj.moans = item['Moans']
                obj.tail_flags = item['Tail flags']
                obj.tail_twitch = item['Tail twitches']
                obj.approaches = item['Approaches']
                obj.indifferent = item['Indifferent']
                obj.runs_from = item['Runs from']
                obj.other_interactions = item['Other Interactions']

                obj.save()

        msg = f'You are importing from {file_}'
        self.stdout.write(self.style.SUCCESS(msg))
コード例 #29
0
    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()
コード例 #30
0
	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))