def form_valid(self, form): exam_file = form.cleaned_data.get('file') content = exam_file.read().decode('utf-8') project = form.cleaned_data.get('project') ei = EditorItem(content=content, author=self.request.user, project=project) ei.locale = project.default_locale ei.save() ei.set_licence(project.default_licence) exam = NewExam() exam.editoritem = ei exam.save() exam_object = NumbasObject(source=content) groups = [] for group in exam_object.data['question_groups']: qs = [] for q in group['questions']: question_object = NumbasObject(data=q, version=exam_object.version) qei = EditorItem(content=str(question_object), author=ei.author) qei.set_licence(ei.licence) qei.project = ei.project qei.save() qo = NewQuestion() qo.editoritem = qei qo.save() extensions = Extension.objects.filter( location__in=exam_object.data['extensions']) qo.extensions.add(*extensions) qs.append(qo.pk) groups.append(qs) exam.set_question_groups(groups) self.exam = exam return HttpResponseRedirect(self.get_success_url())
def form_valid(self, form): exam_file = form.cleaned_data.get('file') content = exam_file.read().decode('utf-8') project = form.cleaned_data.get('project') ei = EditorItem(content=content,author=self.request.user,project=project) ei.locale = project.default_locale ei.save() ei.set_licence(project.default_licence) exam = NewExam() exam.editoritem = ei exam.save() exam_object = NumbasObject(source=content) groups = [] for group in exam_object.data['question_groups']: qs = [] for q in group['questions']: question_object = NumbasObject(data=q,version=exam_object.version) qei = EditorItem( content = str(question_object), author = ei.author ) qei.set_licence(ei.licence) qei.project = ei.project qei.save() qo = NewQuestion() qo.editoritem = qei qo.save() extensions = Extension.objects.filter(location__in=exam_object.data['extensions']) qo.extensions.add(*extensions) qs.append(qo.pk) groups.append(qs) exam.set_question_groups(groups) self.exam = exam return HttpResponseRedirect(self.get_success_url())
def form_valid(self, form): exam_file = form.cleaned_data.get('file') content = exam_file.read().decode('utf-8') project = form.cleaned_data.get('project') ei = EditorItem(content=content, author=self.request.user, project=project) ei.locale = project.default_locale ei.save() ei.set_licence(project.default_licence) obj = ei.get_parsed_content() def add_contributors(item,contributor_data): root = self.request.build_absolute_uri('/') for c in contributor_data: if c['profile_url'][:len(root)]==root: rest = c['profile_url'][len(root):] try: match = resolve(rest) if match.url_name != 'view_profile': raise Resolver404() pk = match.kwargs['pk'] user = User.objects.get(pk=pk) Contributor.objects.create(item=item,user=user) except (Resolver404,User.DoesNotExist): Contributor.objects.create(item=item,name=c['name'],profile_url=c['profile_url']) else: Contributor.objects.create(item=item,name=c['name'],profile_url=c['profile_url']) contributors = obj.data.get('contributors',[]) add_contributors(ei,contributors) exam = NewExam() exam.editoritem = ei exam.save() exam_object = NumbasObject(source=content) groups = [] for group in exam_object.data['question_groups']: qs = [] for q in group['questions']: question_object = NumbasObject(data=q, version=exam_object.version) qei = EditorItem( content=str(question_object), author=ei.author ) qei.set_licence(ei.licence) qei.project = ei.project qei.save() qo = NewQuestion() qo.editoritem = qei qo.save() extensions = Extension.objects.filter(location__in=exam_object.data['extensions']) qo.extensions.add(*extensions) qs.append(qo.pk) contributors = q.get('contributors',[]) add_contributors(qei,contributors) groups.append(qs) exam.set_question_groups(groups) self.exam = exam return HttpResponseRedirect(self.get_success_url())