def create_questions(instance): name, ext = get_filename_ext(instance.quiz_file.name) quiz_file_path = os.path.join(settings.QUIZ_ROOT, instance.quiz_file.name) quiz_file_dir = os.path.join(settings.QUIZ_ROOT, name) label_file_path = os.path.join(settings.LABEL_ROOT, instance.label_file.name) os.mkdir(quiz_file_dir) if ext == '.csv': inner_dir_name = '%s' % instance.name quiz_file_dir = os.path.join(quiz_file_dir, inner_dir_name) os.mkdir(quiz_file_dir) quiz_reader = csv.DictReader(open(quiz_file_path, encoding='utf-8')) label_reader = csv.DictReader(open(label_file_path, encoding='utf-8')) for (quiz_row, label_row) in zip(quiz_reader, label_reader): if quiz_row['\ufeffid'] == label_row['\ufeffid']: file_name = '%s.csv' % quiz_row['\ufeffid'] final_quiz_file_path = os.path.join(quiz_file_dir, file_name) f = open(final_quiz_file_path, 'w', newline='', encoding='utf-8') writer = csv.DictWriter(f, dialect='excel', fieldnames=quiz_reader.fieldnames) writer.writeheader() writer.writerow(quiz_row) Question.objects.create( quiz=instance, file_path=final_quiz_file_path, label=label_row['label'], )
def create_tasks(instance): name, ext = get_filename_ext(instance.project_file.name) project_file_path = os.path.join(settings.MEDIA_ROOT, instance.project_file.name) project_file_dir = os.path.join(settings.MEDIA_ROOT, name) os.mkdir(project_file_dir) rr = instance.repetition_rate if ext == '.zip': zf = zipfile.ZipFile(project_file_path, 'r') zf.extractall(path=project_file_dir) inner_dir_name = os.listdir(project_file_dir)[0] project_file_path = os.path.join(project_file_dir, inner_dir_name) final_project_file_path = project_file_path.encode('cp437').decode( 'gbk') os.rename(project_file_path, final_project_file_path) file_name_list = os.listdir(final_project_file_path) for file_name in file_name_list: Task.objects.create( project=instance, file_path=os.path.join(final_project_file_path, file_name), copy=int(rr), ) elif ext == '.csv': inner_dir_name = '%s' % instance.name project_file_dir = os.path.join(project_file_dir, inner_dir_name) os.mkdir(project_file_dir) reader = csv.DictReader(open(project_file_path, encoding='utf-8')) for row in reader: file_name = '%s.csv' % row['\ufeffid'] final_project_file_path = os.path.join(project_file_dir, file_name) f = open(final_project_file_path, 'w', newline='', encoding='utf-8') writer = csv.DictWriter(f, dialect='excel', fieldnames=reader.fieldnames) writer.writeheader() writer.writerow(row) Task.objects.create( project=instance, file_path=final_project_file_path, copy=int(rr), ) if 1 < rr < 2: length = instance.task_set.count() num = int((rr - 1) * length) ids = instance.task_set.values_list("id", flat=True) rand_list = random.sample(set(ids), num) for i in rand_list: Task.objects.filter(id=i).update(copy=2) for task in Task.objects.filter(project=instance): for copy in range(task.copy): Contribution.objects.create(project=instance, task=task)
def upload_project_file_path(instance, filename): name, ext = get_filename_ext(filename) while True: new_filename = random_string_generator() if new_filename not in os.listdir(settings.MEDIA_ROOT): break final_filename = '{new_filename}{ext}'.format(new_filename=new_filename, ext=ext) return final_filename
def get_text_content(self, obj): path = obj.file_path name, ext = get_filename_ext(path) if ext == '.csv': reader = csv.DictReader(open(path, encoding='utf-8')) for i, row in enumerate(reader): if i == 0: content = row else: content = '' return content
def get_text_content(self, obj): path = obj.task.file_path name, ext = get_filename_ext(path) if ext == '.csv': reader = csv.DictReader(open(path, encoding='utf-8')) for i, row in enumerate(reader): if i == 0: content = row else: with open(path, 'r', encoding='utf-8') as file: content = file.read().strip() return content