<commit_before>import urllib.request

import pandas as pd

from django.core.exceptions import ObjectDoesNotExist
from django.core.files import File
from django.core.management.base import BaseCommand, CommandError

from fat.models import Claimed

CSV_TO_IMPORT = 'old_applications.csv'

class Command(BaseCommand):
    help = "Import CSV (old_applications.csv) with applications to claimedship to the database."

    # TODO Make use of args and options.
    def handle(self, *args, **options):
        data =  pd.read_csv(CSV_TO_IMPORT)
        for idx, line in data.iterrows():
            try:
                if line['Selected']=='Yes':
                    is_fellow=True
                else:
                    is_fellow=False

                if pd.notnull(line["Photo"]):
                    photo_name, photo_info = urllib.request.urlretrieve(line["Photo"])
                    photo = File(open(photo_name, "rb"))
                    photo.name = line["Photo"].split("/")[-1]

                if pd.notnull(line["Research classification"]):
                    jacs = "{}00".format(line["Research classification"][0:2])
                else:
                    jacs = "Y000"

                applicants_dict = {
                    "application_year": line["Inauguration year"] - 1,
                    "selected": is_fellow,
                    "forenames": line["Forename(s)"],<commit_msg>Fix bug that duplicate photos when load old data<commit_after>import urllib.request

import pandas as pd

from django.core.exceptions import ObjectDoesNotExist
from django.core.files import File
from django.core.management.base import BaseCommand, CommandError

from fat.models import Claimed

CSV_TO_IMPORT = 'old_applications.csv'

class Command(BaseCommand):
    help = "Import CSV (old_applications.csv) with applications to claimedship to the database."

    # TODO Make use of args and options.
    def handle(self, *args, **options):
        data =  pd.read_csv(CSV_TO_IMPORT)
        for idx, line in data.iterrows():
            try:
                if line['Selected']=='Yes':
                    is_fellow=True
                else:
                    is_fellow=False

                if pd.notnull(line["Photo"]):
                    photo_name, photo_info = urllib.request.urlretrieve(line["Photo"])
                    photo = File(open(photo_name, "rb"))
                    photo.name = line["Photo"].split("/")[-1]
                else:
                    photo = None

                if pd.notnull(line["Research classification"]):
                    jacs = "{}00".format(line["Research classification"][0:2])
                else:
                    jacs = "Y000"

                applicants_dict = {
                    "application_year": line["Inauguration year"] - 1,
                    "selected": is_fellow,
                    "forenames": line["Forename(s)"],
{'fraction_non_alphanumeric': 0.10573600552868002, 'fraction_numerical': 0.006219765031098825, 'mean_word_length': 1.9154078549848943, 'pattern_counts': {'":': 6, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '816772', 'n_tokens_mistral': 818, 'n_tokens_neox': 755, 'n_words': 190}