def test_update_publish_draft(self): status = Status.ACTIVE new_xml = "<Resource localid='' status='"+status+"'><identity>" \ "<title>My new software</title></identity><curation><publisher>PF</publisher><contact><name></name>" \ "</contact></curation><content><description>This is a new record</description><subject></subject>" \ "<referenceURL></referenceURL></content></Resource>" id = self.createXMLData(ispublished=True) xmlData = XMLdata.get(id) self.assertNotEquals(new_xml, XMLdata.unparse(xmlData['content'])) adminId = self.getAdmin().id template = self.createTemplate() elements = SchemaElement.objects().all() self.assertEqual(len(elements), 0) elementsForm = FormData.objects().all() self.assertEqual(len(elementsForm), 0) formData = self.createFormData(user=adminId, name='name', template=str(template.id), xml_data=new_xml, xml_data_id=str(id)) url = '/dashboard/update_publish_draft' data = {'draft_id': str(formData.id)} r = self.doRequestGetAdminClientLogged(url=url, data=data) xmlDataInDatabase = XMLdata.get(id) elements = SchemaElement.objects().all() self.assertEqual(len(elements), 0) elementsForm = FormData.objects().all() self.assertEqual(len(elementsForm), 0) self.assertEquals(etree.XML(new_xml).text, etree.XML(str(XMLdata.unparse(xmlDataInDatabase['content']))).text) self.assertEquals(True, xmlDataInDatabase.get('ispublished')) self.assertEquals(str(adminId), xmlDataInDatabase.get('iduser')) self.assertNotEquals(xmlData.get('lastmodificationdate'), xmlDataInDatabase.get('lastmodificationdate')) self.assertNotEquals(xmlData.get('publicationdate'), xmlDataInDatabase.get('publicationdate')) self.assertEquals(status, xmlDataInDatabase.get('status'))
def dashboard_my_drafts(request): template = loader.get_template('dashboard/my_dashboard_my_forms.html') forms = FormData.objects(user=str(request.user.id), xml_data_id__exists=False, xml_data__exists=True).order_by('template') # xml_data_id False if document not curated detailed_forms = [] for form in forms: detailed_forms.append({'form': form, 'template_name': Template.objects().get(pk=form.template).title, 'user': form.user}) user_form = UserForm(request.user) context = RequestContext(request, {'forms': detailed_forms, 'user_form': user_form }) #If the user is an admin, we get forms for other users if request.user.is_staff: #Get user name for admin usernames = dict((str(x.id), x.username) for x in User.objects.all()) other_users_detailed_forms = [] otherUsersForms = FormData.objects(user__ne=str(request.user.id), xml_data_id__exists=False, xml_data__exists=True).order_by('template') for form in otherUsersForms: other_users_detailed_forms.append({'form': form, 'template_name': Template.objects().get(pk=form.template).title, 'user': form.user}) context.update({'otherUsersForms': other_users_detailed_forms, 'usernames': usernames}) return HttpResponse(template.render(context))
def dashboard_my_forms(request): template = loader.get_template('dashboard/my_dashboard_my_forms.html') # xml_data_id False if document not curated forms = FormData.objects(user=str(request.user.id), xml_data_id__exists=False, xml_data__exists=True).order_by('template') detailed_forms = [] for form in forms: detailed_forms.append({'form': form, 'template_name': Template.objects().get(pk=form.template).title, 'user': form.user}) user_form = UserForm(request.user) context = RequestContext(request, {'forms': detailed_forms, 'user_form': user_form }) #If the user is an admin, we get forms for other users if request.user.is_staff: #Get user name for admin usernames = dict((str(x.id), x.username) for x in User.objects.all()) other_users_detailed_forms = [] otherUsersForms = FormData.objects(user__ne=str(request.user.id), xml_data_id__exists=False, xml_data__exists=True).order_by('template') for form in otherUsersForms: other_users_detailed_forms.append({'form': form, 'template_name': Template.objects().get(pk=form.template).title, 'user': form.user}) context.update({'otherUsersForms': other_users_detailed_forms, 'usernames': usernames}) return HttpResponse(template.render(context))
def createFormData(self, user='', template='', name='', xml_data=None, xml_data_id=None): countFormData = len(FormData.objects()) formData = FormData(user=str(user), template=template, name=name, xml_data=xml_data, xml_data_id=xml_data_id).save() self.assertEquals(len(FormData.objects()), countFormData + 1) return formData
def __init__(self, *args, **kwargs): if 'forms' in kwargs: qs = kwargs.pop('forms') else: qs = FormData.objects().none() super(OpenForm, self).__init__(*args, **kwargs) self.fields['forms'].queryset = qs
def reload_form(request): """ Reload the form string from latest version saved :param request: :return: """ try: form_data_id = request.session['curateFormData'] form_data = FormData.objects().get(pk=form_data_id) xml_data = form_data.xml_data template_id = request.session['currentTemplateID'] template_object = Template.objects.get(pk=template_id) xsd_doc_data = template_object.content # the form has been saved already if xml_data is not None and xml_data != '': request.session['curate_edit'] = True root_element_id = generate_form(request, xsd_doc_data, xml_data, config=load_config()) # the form has never been saved else: root_element_id = generate_form(request, xsd_doc_data, config=load_config()) root_element = SchemaElement.objects.get(pk=root_element_id) renderer = ListRenderer(root_element, request) html_form = renderer.render() request.session['form_id'] = str(root_element_id) return HttpResponse(json.dumps({'xsdForm': html_form}), content_type='application/javascript') except Exception, e: return HttpResponse({}, status=400)
def curate_edit_data(request): try: if 'useForm' in request.GET and request.GET['useForm'] == 'true': pass else: 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: # TODO: check if need to delete all SchemaElements 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 ) form_data.save() request.session['curateFormData'] = str(form_data.id) if 'form_id' in request.session: del request.session['form_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.")
def reload_form(request): """ Reload the form string from latest version saved :param request: :return: """ try: form_data_id = request.session['curateFormData'] form_data = FormData.objects().get(pk=form_data_id) xml_data = form_data.xml_data template_id = request.session['currentTemplateID'] template_object = Template.objects.get(pk=template_id) xsd_doc_data = template_object.content # the form has been saved already if xml_data is not None and xml_data != '': request.session['curate_edit'] = True request.session['xmlDocTree'] = xml_data root_element_id = parser.generate_form(request, xsd_doc_data, xml_data) # the form has never been saved else: root_element_id = parser.generate_form(request, xsd_doc_data) root_element = SchemaElement.objects.get(pk=root_element_id) renderer = ListRenderer(root_element, request) html_form = renderer.render() request.session['form_id'] = str(root_element_id) return HttpResponse(json.dumps({'xsdForm': html_form}), content_type='application/javascript') except Exception, e: return HttpResponse({}, status=400)
def curate_edit_data(request): try: xml_data_id = request.GET['id'] xml_data = XMLdata.get(xml_data_id) xml_content = xml_data['xml_file'] 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: if previous_form.schema_element_root is not None: delete_branch_from_db(previous_form.schema_element_root.pk) 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) form_data.save() request.session['curateFormData'] = str(form_data.pk) if 'form_id' in request.session: del request.session['form_id'] if 'xmlDocTree' in request.session: del request.session['xmlDocTree'] except: raise MDCSError("The document you are looking for doesn't exist.")
def dashboard_my_drafts(request): forms = FormData.objects(user=str(request.user.id), xml_data_id__exists=False, xml_data__exists=True).order_by('template') # xml_data_id False if document not curated detailed_forms = [] for form in forms: detailed_forms.append({'form': form, 'template_name': Template.objects().get(pk=form.template).title}) user_form = UserForm(request.user) return render(request, 'dashboard/my_dashboard_my_forms.html', {'forms':detailed_forms, 'user_form': user_form})
def test_getListNameFromDependencies_FormData(self): FormData(template='test', name='testFormData', xml_data='testest', user=str(1)).save() self.assertEquals( 'testFormData', getListNameFromDependencies( list(FormData.objects(name='testFormData'))))
def delete_form(request): if 'id' in request.GET: form_data_id = request.GET['id'] try: form_data = FormData.objects().get(pk=form_data_id) # TODO: check if need to delete all SchemaElements form_data.delete() messages.add_message(request, messages.INFO, 'Form deleted with success.') except Exception, e: return HttpResponse({},status=400)
def cancel_form(request): try: form_data_id = request.session['curateFormData'] form_data = FormData.objects().get(pk=form_data_id) # TODO: check if need to delete all schema elements form_data.delete() messages.add_message(request, messages.INFO, 'Form deleted with success.') return HttpResponse({},status=204) except Exception, e: return HttpResponse({},status=400)
def dashboard_resources(request): template = loader.get_template('dashboard/my_dashboard_my_records.html') query = {} context = RequestContext(request, {}) ispublished = request.GET.get('ispublished', None) template_name = request.GET.get('template', None) query['iduser'] = str(request.user.id) #If ispublished not None, check if we want publish or unpublish records if ispublished: ispublished = ispublished == 'true' query['ispublished'] = ispublished if template_name: context.update({'template': template_name}) if template_name == 'datacollection': templateNamesQuery = list(chain(Template.objects.filter(title=template_name).values_list('id'), Template.objects.filter(title='repository').values_list('id'), Template.objects.filter(title='database').values_list('id'), Template.objects.filter(title='projectarchive').values_list('id'))) else : templateNamesQuery = Template.objects.filter(title=template_name).values_list('id') templateNames = [] for templateQuery in templateNamesQuery: templateNames.append(str(templateQuery)) query['schema'] = {"$in" : templateNames} userXmlData = sorted(XMLdata.find(query), key=lambda data: data['lastmodificationdate'], reverse=True) #Add user_form for change owner user_form = UserForm(request.user) context.update({'XMLdatas': userXmlData, 'ispublished': ispublished, 'user_form': user_form}) #If the user is an admin, we get records for other users if request.user.is_staff: #Get user name for admin usernames = dict((str(x.id), x.username) for x in User.objects.all()) query['iduser'] = {"$ne": str(request.user.id)} otherUsersXmlData = sorted(XMLdata.find(query), key=lambda data: data['lastmodificationdate'], reverse=True) context.update({'OtherUsersXMLdatas': otherUsersXmlData, 'usernames': usernames}) #Get new version of records listIds = [str(x['_id']) for x in userXmlData] if request.user.is_staff: listIdsOtherUsers = [str(x['_id']) for x in otherUsersXmlData] listIds = list(set(listIds).union(set(listIdsOtherUsers))) drafts = FormData.objects(xml_data_id__in=listIds, isNewVersionOfRecord=True).all() XMLdatasDrafts = dict() for draft in drafts: XMLdatasDrafts[draft.xml_data_id] = draft.id context.update({'XMLdatasDrafts': XMLdatasDrafts}) #Add Status enum context.update({'Status': Status}) return HttpResponse(template.render(context))
def change_owner_form(request): if 'formId' and 'userID' in request.POST: form_data_id = request.POST['formID'] user_id = request.POST['userID'] try: form_data = FormData.objects().get(pk=form_data_id) form_data.user = user_id form_data.save() messages.add_message(request, messages.INFO, 'Form Owner changed with success !') except Exception, e: return HttpResponse({}, status=400)
def change_owner_form(request): if 'formId' and 'userID' in request.POST: form_data_id = request.POST['formID'] user_id = request.POST['userID'] try: form_data = FormData.objects().get(pk=form_data_id) form_data.user = user_id form_data.save() messages.add_message(request, messages.INFO, 'Form Owner changed with success !') except Exception, e: return HttpResponse({}, status=400)
def curate_edit_data(request): try: if 'useForm' in request.GET and request.GET['useForm'] == 'true': pass else: xml_data_id = request.GET['id'] request.session['curate_edit'] = True # remove previously created forms when editing a new one previous_forms = FormData.objects(user=str(request.user.id), xml_data_id__exists=True, isNewVersionOfRecord=False) for previous_form in previous_forms: if previous_form.schema_element_root is not None: delete_branch_from_db(previous_form.schema_element_root.pk) previous_form.delete() #Check if a form_data already exists for this record form_data = FormData.objects(xml_data_id=xml_data_id).all().first() if not form_data: xml_data = XMLdata.get(xml_data_id) json_content = xml_data['content'] xml_content = XMLdata.unparse(json_content) 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, isNewVersionOfRecord=xml_data.get( 'ispublished', False)) form_data.save() request.session['currentTemplateID'] = form_data.template request.session['curate_edit_data'] = form_data.xml_data request.session['curateFormData'] = str(form_data.pk) if 'form_id' in request.session: del request.session['form_id'] if 'xmlDocTree' in request.session: del request.session['xmlDocTree'] except: raise MDCSError("The document you are looking for doesn't exist.")
def _get_element(self, xpath): if self.xpath != xpath: # get data about the current form form_data = FormData.objects().get(pk=self._form_data_id) # get all elements from the current form form_elements = FormElement.objects.filter(id__in=form_data.elements.values()) # check if the provided xpath is one of an element of the current form if xpath in form_elements.values_list("xml_xpath"): return form_elements.get(xml_xpath=xpath) else: raise XPathAccessorError("No element found for the given xpath.") else: raise XPathAccessorError("Xpath provided is the same as reference element.")
class OpenForm(forms.Form): """ Form to open an existing form """ forms = FormDataModelChoiceField(label='', queryset=FormData.objects().none()) def __init__(self, *args, **kwargs): if 'forms' in kwargs: qs = kwargs.pop('forms') else: qs = FormData.objects().none() super(OpenForm, self).__init__(*args, **kwargs) self.fields['forms'].queryset = qs
def test_update_publish_draft(self): status = Status.ACTIVE new_xml = "<Resource localid='' status='"+status+"'><identity>" \ "<title>My new software</title></identity><curation><publisher>PF</publisher><contact><name></name>" \ "</contact></curation><content><description>This is a new record</description><subject></subject>" \ "<referenceURL></referenceURL></content></Resource>" id = self.createXMLData(ispublished=True) xmlData = XMLdata.get(id) self.assertNotEquals(new_xml, XMLdata.unparse(xmlData['content'])) adminId = self.getAdmin().id template = self.createTemplate() elements = SchemaElement.objects().all() self.assertEqual(len(elements), 0) elementsForm = FormData.objects().all() self.assertEqual(len(elementsForm), 0) formData = self.createFormData(user=adminId, name='name', template=str(template.id), xml_data=new_xml, xml_data_id=str(id)) url = '/dashboard/update_publish_draft' data = {'draft_id': str(formData.id)} r = self.doRequestGetAdminClientLogged(url=url, data=data) xmlDataInDatabase = XMLdata.get(id) elements = SchemaElement.objects().all() self.assertEqual(len(elements), 0) elementsForm = FormData.objects().all() self.assertEqual(len(elementsForm), 0) self.assertEquals( etree.XML(new_xml).text, etree.XML(str(XMLdata.unparse(xmlDataInDatabase['content']))).text) self.assertEquals(True, xmlDataInDatabase.get('ispublished')) self.assertEquals(str(adminId), xmlDataInDatabase.get('iduser')) self.assertNotEquals(xmlData.get('lastmodificationdate'), xmlDataInDatabase.get('lastmodificationdate')) self.assertNotEquals(xmlData.get('publicationdate'), xmlDataInDatabase.get('publicationdate')) self.assertEquals(status, xmlDataInDatabase.get('status'))
def my_profile_my_forms(request): forms = FormData.objects( user=str(request.user.id), xml_data_id__exists=False).order_by( 'template') # xml_data_id False if document not curated detailed_forms = [] for form in forms: detailed_forms.append({ 'form': form, 'template_name': Template.objects().get(pk=form.template).title }) return render(request, 'profile/my_profile_my_forms.html', {'forms': detailed_forms})
def _get_element(self, xpath): if self.xpath != xpath: # get data about the current form form_data = FormData.objects().get(pk=self._form_data_id) # get all elements from the current form form_elements = FormElement.objects.filter( id__in=form_data.elements.values()) # check if the provided xpath is one of an element of the current form if xpath in form_elements.values_list('xml_xpath'): return form_elements.get(xml_xpath=xpath) else: raise XPathAccessorError( 'No element found for the given xpath.') else: raise XPathAccessorError( 'Xpath provided is the same as reference element.')
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)
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." )
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.' )
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.")
def getListTemplateDependenciesFormData(object_id): from mgi.models import FormData return list( FormData.objects(template=object_id, xml_data_id__exists=False, xml_data__exists=True))
def getListTemplateDependenciesFormData(object_id): from mgi.models import FormData return list(FormData.objects(template=object_id, xml_data_id__exists=False, xml_data__exists=True))
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'], xml_data=None).save() except: return HttpResponseBadRequest( 'Unable to create the form. A form with the same name may already exists.') 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'])) request.session['curate_edit_data'] = form_data.xml_data 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 'formString' in request.session: del request.session['formString'] 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))
def my_profile_my_forms(request): forms = FormData.objects(user=str(request.user.id), xml_data_id__exists=False).order_by('template') # xml_data_id False if document not curated detailed_forms = [] for form in forms: detailed_forms.append({'form': form, 'template_name': Template.objects().get(pk=form.template).title}) return render(request, 'profile/my_profile_my_forms.html', {'forms':detailed_forms})
def dashboard_resources(request): template = loader.get_template('dashboard/my_dashboard_my_records.html') query = {} context = RequestContext(request, {}) ispublished = request.GET.get('ispublished', None) template_name = request.GET.get('template', None) query['iduser'] = str(request.user.id) #If ispublished not None, check if we want publish or unpublish records if ispublished: ispublished = ispublished == 'true' query['ispublished'] = ispublished if template_name: context.update({'template': template_name}) if template_name == 'datacollection': templateNamesQuery = list( chain( Template.objects.filter( title=template_name).values_list('id'), Template.objects.filter( title='repository').values_list('id'), Template.objects.filter( title='database').values_list('id'), Template.objects.filter( title='projectarchive').values_list('id'))) else: templateNamesQuery = Template.objects.filter( title=template_name).values_list('id') templateNames = [] for templateQuery in templateNamesQuery: templateNames.append(str(templateQuery)) query['schema'] = {"$in": templateNames} userXmlData = sorted(XMLdata.find(query), key=lambda data: data['lastmodificationdate'], reverse=True) #Add user_form for change owner user_form = UserForm(request.user) context.update({ 'XMLdatas': userXmlData, 'ispublished': ispublished, 'user_form': user_form }) #If the user is an admin, we get records for other users if request.user.is_staff: #Get user name for admin usernames = dict((str(x.id), x.username) for x in User.objects.all()) query['iduser'] = {"$ne": str(request.user.id)} otherUsersXmlData = sorted( XMLdata.find(query), key=lambda data: data['lastmodificationdate'], reverse=True) context.update({ 'OtherUsersXMLdatas': otherUsersXmlData, 'usernames': usernames }) #Get new version of records listIds = [str(x['_id']) for x in userXmlData] if request.user.is_staff: listIdsOtherUsers = [str(x['_id']) for x in otherUsersXmlData] listIds = list(set(listIds).union(set(listIdsOtherUsers))) drafts = FormData.objects(xml_data_id__in=listIds, isNewVersionOfRecord=True).all() XMLdatasDrafts = dict() for draft in drafts: XMLdatasDrafts[draft.xml_data_id] = draft.id context.update({'XMLdatasDrafts': XMLdatasDrafts}) #Add Status enum context.update({'Status': Status}) return HttpResponse(template.render(context))
def dashboard_resources(request): template = loader.get_template('dashboard/my_dashboard_my_records.html') query = {} context = RequestContext(request, {}) ispublished = request.GET.get('ispublished', None) role_name_list = request.GET.getlist('role', ['all']) role_name = '' for role in role_name_list: role_name += role + ',' context.update({'roles': role_name[:-1]}) query['iduser'] = str(request.user.id) if ispublished: ispublished = ispublished == 'true' query['ispublished'] = ispublished if not (len(role_name_list) == 1 and role_name_list[0] == 'all'): query['$or'] = [{ "content.Resource.role.@xsi:type": role } for role in role_name_list] userXmlData = sorted(XMLdata.find(query), key=lambda data: data['lastmodificationdate'], reverse=True) #Add user_form for change owner user_form = UserForm(request.user) context.update({ 'XMLdatas': userXmlData, 'ispublished': ispublished, 'user_form': user_form }) #If the user is an admin, we get records for other users if request.user.is_staff: #Get user name for admin usernames = dict((str(x.id), x.username) for x in User.objects.all()) query['iduser'] = {"$ne": str(request.user.id)} otherUsersXmlData = sorted( XMLdata.find(query), key=lambda data: data['lastmodificationdate'], reverse=True) context.update({ 'OtherUsersXMLdatas': otherUsersXmlData, 'usernames': usernames }) #Get new version of records listIds = [str(x['_id']) for x in userXmlData] if request.user.is_staff: listIdsOtherUsers = [str(x['_id']) for x in otherUsersXmlData] listIds = list(set(listIds).union(set(listIdsOtherUsers))) drafts = FormData.objects(xml_data_id__in=listIds, isNewVersionOfRecord=True).all() XMLdatasDrafts = dict() for draft in drafts: XMLdatasDrafts[draft.xml_data_id] = draft.id context.update({'XMLdatasDrafts': XMLdatasDrafts}) #Add Status enum context.update({'Status': Status}) return HttpResponse(template.render(context))
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))