コード例 #1
0
def save_xml_data_to_db(request):
    xmlString = request.session['xmlString']
    templateID = request.session['currentTemplateID']

    form = SaveDataForm(request.POST)

    if form.is_valid():
        if xmlString != "":
            try:
                # get form data from the database
                form_data_id = request.session['curateFormData']
                form_data = FormData.objects.get(pk=ObjectId(form_data_id))
                if not form.data['title'].lower().endswith('.xml'):
                    form.data['title'] += ".xml"
                # update data if id is present
                if form_data.xml_data_id is not None:
                    XMLdata.update_content(form_data.xml_data_id, xmlString, title=form.data['title'])
                else:
                    #create new data otherwise
                    newJSONData = XMLdata(schemaID=templateID, xml=xmlString, title=form.data['title'], iduser=str(request.user.id))
                    newJSONData.save()
                # delete form data
                try:
                    form_data = FormData.objects().get(pk=form_data_id)
                    # cascade delete references
                    for form_element_id in form_data.elements.values():
                        try:
                            form_element = FormElement.objects().get(pk=form_element_id)
                            if form_element.xml_element is not None:
                                try:
                                    xml_element = XMLElement.objects().get(pk=str(form_element.xml_element.id))
                                    xml_element.delete()
                                except:
                                    # raise an exception when element not found
                                    pass
                            form_element.delete()
                        except:
                            # raise an exception when element not found
                            pass
                    form_data.delete()
                except Exception, e:
                    return HttpResponseBadRequest('Unable to save data.')
                return HttpResponse('ok')
            except Exception, e:
                message = e.message.replace('"', '\'')
                return HttpResponseBadRequest(message)
コード例 #2
0
ファイル: xpathaccessor.py プロジェクト: hzhao1230/nanomine
 def __init__(self, request):
     try:
         # get id of form data
         self._form_data_id = request.session["curateFormData"]
         # get the form data
         form_data = FormData.objects().get(pk=self._form_data_id)
         # get the html id of the module
         html_id = request.POST["htmlId"]
         # get the id (mongo) of the form element at this id (html)
         form_element_id = form_data.elements[html_id]
         # get the form element from db
         form_element = FormElement.objects().get(pk=form_element_id)
         # get xml xpath of the element
         self.xpath = form_element.xml_xpath
         self.values = {}
         self.set_XpathAccessor(request)
     except:
         raise XPathAccessorError(
             "Unable to get form data information. Please check session is still valid and that HTTP request is correctly sent to the Siblings Accessor system."
         )
コード例 #3
0
ファイル: xpathaccessor.py プロジェクト: hzhao1230/NanoMine
 def __init__(self, request):
     try:
         # get id of form data
         self._form_data_id = request.session['curateFormData']
         # get the form data
         form_data = FormData.objects().get(pk=self._form_data_id)
         # get the html id of the module
         html_id = request.POST['htmlId']
         # get the id (mongo) of the form element at this id (html)
         form_element_id = form_data.elements[html_id]
         # get the form element from db
         form_element = FormElement.objects().get(pk=form_element_id)
         # get xml xpath of the element
         self.xpath = form_element.xml_xpath
         self.values = {}
         self.set_XpathAccessor(request)
     except:
         raise XPathAccessorError(
             'Unable to get form data information. Please check session is still valid and that HTTP request is correctly sent to the Siblings Accessor system.'
         )
コード例 #4
0
def curate_edit_data(request):
    try:
        xml_data_id = request.GET['id']
        xml_data = XMLdata.get(xml_data_id)
        json_content = xml_data['content']
        xml_content = xmltodict.unparse(json_content)
        request.session['curate_edit_data'] = xml_content
        request.session['curate_edit'] = True
        request.session['currentTemplateID'] = xml_data['schema']
        # remove previously created forms when editing a new one
        previous_forms = FormData.objects(user=str(request.user.id), xml_data_id__exists=True)
        for previous_form in previous_forms:
            # cascade delete references
            for form_element_id in previous_form.elements.values():
                try:
                    form_element = FormElement.objects().get(pk=form_element_id)
                    if form_element.xml_element is not None:
                        try:
                            xml_element = XMLElement.objects().get(pk=str(form_element.xml_element.id))
                            xml_element.delete()
                        except:
                            # raise an exception when element not found
                            pass
                    form_element.delete()
                except:
                    # raise an exception when element not found
                    pass
            previous_form.delete()
        form_data = FormData(user=str(request.user.id), template=xml_data['schema'], name=xml_data['title'], xml_data=xml_content, xml_data_id=xml_data_id).save()
        request.session['curateFormData'] = str(form_data.id)
        if 'formString' in request.session:
            del request.session['formString']
        if 'xmlDocTree' in request.session:
            del request.session['xmlDocTree']
    except:
        raise MDCSError("The document you are looking for doesn't exist.")