示例#1
0
def sandbox_viewer(request):
    user = request.user.id
    request.session['curate_edit'] = False

    template_id = request.GET.get("id", None)
    if template_id is None:
        message = "The template doesn't exist anymore."
        add_message(request, WARNING, message)
        return redirect(reverse("xsd_viewer"), locals())
    request.session['currentTemplateID'] = template_id
    request.session.modified = True
    request.session['xmlDocTree'] = etree.tostring(
        etree.parse(
            BytesIO(
                Template.objects.get(pk=template_id).content.encode('utf-8'))))

    list_names = FormData.objects.values_list("name")

    form_name = "form_sandbox" + str(ObjectId())
    while form_name in list_names:
        form_name = "form_sandbox" + str(ObjectId())
    new_form = NewForm(request.POST)
    form_data = FormData(user=str(user), template=template_id, name=form_name)
    form_data.save()
    form_data_sb = FormDataSandbox()
    form_data_sb.form_data = form_data
    form_data_sb.save()
    request.session['curateFormData'] = str(form_data.id)

    context = {
        'edit': True,
        'sandbox': True,
        'curate_form': ['new'],
        'document_name': [form_name]
    }

    if 'form_id' in request.session:
        del request.session['form_id']

    template = loader.get_template('curate/curate_enter_data.html')
    return HttpResponse(template.render(context))
示例#2
0
def start_curate(request):
    if 'currentTemplateID' not in request.session and 'template' not in request.GET:
        return redirect('/curate/select-template')
    else:
        if request.method == 'POST':
            # parameters to build FormData object in db
            user = request.user.id
            template_id = request.session['currentTemplateID']

            form_data = None

            selected_option = request.POST['curate_form']
            if selected_option == "new":
                request.session['curate_edit'] = False
                new_form = NewForm(request.POST)
                try:
                    form_data = FormData(user=str(user),
                                         template=template_id,
                                         name=new_form.data['document_name'])
                    form_data.save()
                except:
                    return HttpResponseBadRequest(
                        'Unable to create the form. A form with the same name may already exist.'
                    )
            elif selected_option == "open":
                request.session['curate_edit'] = True
                open_form = OpenForm(request.POST)
                form_data = FormData.objects.get(
                    pk=ObjectId(open_form.data['forms']))
            elif selected_option == "upload":
                request.session['curate_edit'] = True
                upload_form = UploadForm(request.POST, request.FILES)
                xml_file = request.FILES['file']
                # put the cursor at the beginning of the file
                xml_file.seek(0)
                # read the content of the file
                xml_data = xml_file.read()
                # check XML data or not?
                try:
                    etree.fromstring(xml_data)
                except XMLSyntaxError:
                    return HttpResponseBadRequest(
                        'Uploaded File is not well formed XML.')
                try:
                    form_data = FormData(user=str(user),
                                         template=template_id,
                                         name=xml_file.name,
                                         xml_data=xml_data).save()
                except:
                    return HttpResponseBadRequest(
                        'Unable to create the form. A form with the same name may already exist.'
                    )

            # parameters that will be used during curation
            request.session['curateFormData'] = str(form_data.id)

            return HttpResponse('ok')
        else:
            try:
                ajaxCall = False
                context_params = dict()
                if 'template' in request.GET:
                    schema_name = request.GET['template']
                    context_params['template_name'] = schema_name
                    try:
                        templates = Template.objects(title=schema_name)
                    except:
                        raise MDCSError(
                            "The template you are looking for doesn't exist.")

                    # if the schemas are all versions of the same schema
                    if len(set(templates.values_list('templateVersion'))) == 1:
                        template_id = TemplateVersion.objects().get(
                            pk=templates[0].templateVersion).current
                        request.session['currentTemplateID'] = template_id
                    else:
                        raise MDCSError(
                            "The selection of template by name can't be used if the MDCS contain more than "
                            "one template with the same name.")

                    template = loader.get_template(
                        'curate/curate_full_start.html')

                    if 'xmlDocTree' in request.session:
                        del request.session['xmlDocTree']

                else:
                    ajaxCall = True
                    template = loader.get_template('curate/curate_start.html')

                open_form = OpenForm(forms=FormData.objects(
                    user=str(request.user.id),
                    template=request.session['currentTemplateID'],
                    xml_data_id__exists=False))
                new_form = NewForm()
                upload_form = UploadForm()

                context_params['new_form'] = new_form
                context_params['open_form'] = open_form
                context_params['upload_form'] = upload_form

                context = RequestContext(
                    request,
                    context_params)  # , 'options_form': options_form})

                if ajaxCall:
                    return HttpResponse(json.dumps(
                        {'template': template.render(context)}),
                                        content_type='application/javascript')
                else:
                    return HttpResponse(template.render(context))

            except MDCSError, e:
                template = loader.get_template('curate/errors.html')
                context = RequestContext(request, {
                    'errors': e.message,
                })
                return HttpResponse(template.render(context))