Esempio n. 1
0
def evalresult_csv_parse(filename):
    questions = {}

    with io.open(filename, 'r', encoding='utf-8') as csvfile:
        csvreader = unicode_csv_reader(csvfile, delimiter=',', quotechar='"')
        for row in csvreader:
            print ".",
            if row[0] == "evaluation":
                # Evaluation data, should be first line
                cureval = Evaluation(title=row[1], etype="PMC")  # , date=row[2], comments=row[3])
                cureval.save()

            elif row[0] == "comment":
                # Commentaires sur la EvalItem
                for col, quest in enumerate(row[4:]):
                    if col not in questions.keys():
                        questions[col] = EvalItem(evaluation=cureval, itype="")
                    questions[col].comment = quest
                    questions[col].save()

            elif row[0] == "question":
                # Description de la EvalItem
                for col, quest in enumerate(row[4:]):
                    if col not in questions.keys():
                        questions[col] = EvalItem(evaluation=cureval, itype="")
                    questions[col].content = quest
                    questions[col].save()

            elif row[0] in ["points", "bareme"]:
                # Barème
                for col, quest in enumerate(row[4:]):
                    bareme = float(quest.replace(",", "."))
                    if col not in questions.keys():
                        questions[col] = EvalItem(evaluation=cureval, itype="")
                        questions[col].save()
                    point = EvalItemPoint(item=questions[col], point=bareme, ptype="")
                    point.save()

            elif row[0] == "refitem":
                # Elément de référence
                for col, quest in enumerate(row[4:]):
                    if quest != "":
                        refitemid = int(quest)
                        refitem = EvalRefItem.objects.get(pk=refitemid)
                    if col not in questions.keys():
                        questions[col] = EvalItem(evaluation=cureval, itype="")
                        questions[col].save()
                    ref = EvalItemEvalref(item=questions[col], evalref=refitem, title="", rtype="")
                    ref.save()

            elif row[0] == "student":
                # Etudiant
                student = EUser(first_name=row[3], last_name=row[2])
                student.username = unique_username(student)
                student.save()
                student.student.is_student = True
                student.student.save()
                paper = EvalPaper(evaluation=cureval, ufor=student, uby=student)
                paper.save()
                for col, res in enumerate(row[4:]):
                    answer = EvalPaperAnswer(paper=paper, evalitem=questions[col])
                    answer.save()
                    corr = EvalPaperEvalitem(evalitem=questions[col], answer=answer, uby=student, status="COR")
                    corr.save()
                    if quest != "":
                        points = EvalPaperEvalitemPoint(
                            pevalitem=corr, pointref=questions[col].points.all()[0],
                            result=float(quest.replace(',', '.')), status="", ptype="")
                        points.save()