def test_delete_template_and_version(self): numberTemplate = len(Template.objects()) numberTemplateVersion = len(TemplateVersion.objects()) template = self.createTemplate() delete_template_and_version(str(template.id)) self.assertEquals(len(Template.objects()), numberTemplate) self.assertEquals(len(TemplateVersion.objects()), numberTemplateVersion)
def edit_information(request): object_id = request.POST['objectID'] object_type = request.POST['objectType'] new_name = request.POST['newName'] new_filename = request.POST['newFilename'] if object_type == "Template": object = Template.objects.get(pk=object_id) testFilenameObjects = Template.objects(filename=new_filename.strip()) testNameObjects = Template.objects(title=new_name.strip()) else: object = Type.objects.get(pk=object_id) testFilenameObjects = Type.objects(filename=new_filename.strip()) testNameObjects = Type.objects(title=new_name.strip()) if len(testNameObjects) == 1: # 0 is ok, more than 1 can't happen #check that the type with the same filename is the current one if testNameObjects[0].id != object.id: response_dict = {'name': 'True'} return HttpResponse(json.dumps(response_dict), content_type='application/javascript') if len(testFilenameObjects) == 1: # 0 is ok, more than 1 can't happen #check that the type with the same filename is the current one if testFilenameObjects[0].id != object.id: response_dict = {'filename': 'True'} return HttpResponse(json.dumps(response_dict), content_type='application/javascript') object.title = new_name.strip() object.filename = new_filename.strip() object.save() return HttpResponse(json.dumps({}), content_type='application/javascript')
def load_xslt(): # Add OAI Xslt xsltFullName = 'full_demo-oai_pmh' xsltFullPath = 'nmrr-full_demo-oai_pmh.xsl' xsltDetailName = 'detail_demo-oai_pmh' sltDetailPath = 'nmrr-detail_demo-oai_pmh.xsl' objFull = ResultXslt.objects(filename=xsltFullPath) if not objFull: file = open( os.path.join(SITE_ROOT, 'oai_pmh', 'resources', 'xsl', xsltFullPath), 'r') fileContent = file.read() objFull = ResultXslt(name=xsltFullName, filename=xsltFullPath, content=fileContent).save() Template.objects().update(set__ResultXsltList=objFull, upsert=True) objDetail = ResultXslt.objects(filename=sltDetailPath) if not objDetail: file = open( os.path.join(SITE_ROOT, 'oai_pmh', 'resources', 'xsl', sltDetailPath), 'r') fileContent = file.read() objDetail = ResultXslt(name=xsltDetailName, filename=sltDetailPath, content=fileContent).save() Template.objects().update(set__ResultXsltDetailed=objDetail, upsert=True)
def manage_xslt(request, id=None): if request.method == 'POST': upload_form = UploadXSLTForm(request.POST, request.FILES) name = upload_form['name'].value() name = name.strip(' \t\n\r') available = upload_form['available_for_all'].value() xml_file = upload_form['xslt_file'].value() # 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.') #No exceptions, we can add it in DB try: xslt = ExporterXslt(name=name, filename=xml_file.name, content=xml_data, available_for_all=available).save() #IF it's available for all templates, we add the reference for all templates using the XSLT exporter if available: xslt_exporter = None try: xslt_exporter = Exporter.objects.get(name='XSLT') except: None if xslt_exporter != None: Template.objects(exporters__all=[xslt_exporter]).update(push__XSLTFiles=xslt) except NotUniqueError, e: return HttpResponseBadRequest('This XSLT name already exists. Please enter an other name.') messages.add_message(request, messages.INFO, 'XSLT saved with success.') return HttpResponse('ok')
def __init__(self, userId=""): self.SCHEMAS_OPTIONS = [] self.SCHEMAS_USER_OPTIONS = [] #We retrieve all common template + user template currentSchemas = TemplateVersion.objects(isDeleted=False).distinct( field="current") schemas = Template.objects(pk__in=currentSchemas, user=None).distinct(field="title") userSchemas = Template.objects(user=str(userId)).distinct( field="title") for schema in schemas: #We add them self.SCHEMAS_OPTIONS.append((schema, schema)) for schema in userSchemas: #We add them self.SCHEMAS_USER_OPTIONS.append((schema, schema)) super(KeywordForm, self).__init__() #Init KeywordFormOAIPMH self.form_oai_pmh = KeywordFormOAIPMH() self.fields['my_schemas'].choices = [] self.fields['my_schemas'].choices = self.SCHEMAS_OPTIONS self.fields['my_user_schemas'].choices = [] self.fields['my_user_schemas'].choices = self.SCHEMAS_USER_OPTIONS self.my_schemas_nb = len(self.SCHEMAS_OPTIONS) self.my_user_schemas_nb = len(self.SCHEMAS_USER_OPTIONS) if self.my_schemas_nb + self.my_user_schemas_nb == 1: self.fields['my_schemas'].widget.attrs['disabled'] = True self.fields['my_user_schemas'].widget.attrs['disabled'] = True
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 __init__(self, userId=""): self.SCHEMAS_OPTIONS = [] self.SCHEMAS_USER_OPTIONS = [] #We retrieve all common template + user template currentSchemas = TemplateVersion.objects(isDeleted=False).distinct(field="current") schemas = Template.objects(pk__in=currentSchemas, user=None).distinct(field="title") userSchemas = Template.objects(user=str(userId)).distinct(field="title") for schema in schemas: #We add them self.SCHEMAS_OPTIONS.append((schema, schema)) for schema in userSchemas: #We add them self.SCHEMAS_USER_OPTIONS.append((schema, schema)) super(KeywordForm, self).__init__() self.fields['my_schemas'].choices = [] self.fields['my_schemas'].choices = self.SCHEMAS_OPTIONS self.fields['my_user_schemas'].choices = [] self.fields['my_user_schemas'].choices = self.SCHEMAS_USER_OPTIONS self.my_schemas_nb = len(self.SCHEMAS_OPTIONS) self.my_user_schemas_nb = len(self.SCHEMAS_USER_OPTIONS) if self.my_schemas_nb + self.my_user_schemas_nb == 1: self.fields['my_schemas'].widget.attrs['disabled'] = True self.fields['my_user_schemas'].widget.attrs['disabled'] = True
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 _get_metadata_formats_id(schemas, user_schemas, registries): # We get all template versions for the given schemas # First, we take care of user defined schema templates_id_user = Template.objects(title__in=user_schemas).distinct( field="id") templates_id_user = [str(x) for x in templates_id_user] # Take care of the rest, with versions templates_versions = Template.objects(title__in=schemas).distinct( field="templateVersion") # We get all templates ID, for all versions all_templates_id_common = TemplateVersion.objects(pk__in=templates_versions, isDeleted=False)\ .distinct(field="versions") # We remove the removed version all_templates_id_common_removed = TemplateVersion.objects(pk__in=templates_versions, isDeleted=False)\ .distinct( field="deletedVersions") templates_id_common = list( set(all_templates_id_common) - set(all_templates_id_common_removed)) templates_id = templates_id_user + templates_id_common if len(registries) == 0: # We retrieve deactivated registries so as not to get their metadata formats deactivatedRegistries = [ str(x.id) for x in OaiRegistry.objects(isDeactivated=True).order_by('id') ] metadataFormatsID = OaiMetadataFormat.objects( template__in=templates_id, registry__not__in=deactivatedRegistries).distinct(field="id") else: # We retrieve registries from the refinement metadataFormatsID = OaiMetadataFormat.objects( template__in=templates_id, registry__in=registries).distinct(field="id") return metadataFormatsID
def discover_exporter(): patterns = __flatten_patterns_tree__(urls.urlpatterns) list_add_exporters = [] list_update_exporters = [] try: for pattern in patterns: try: #We try to instanciate the exporter to be sure that it will worked. If not, just print an error in the python console instance = get_exporter(pattern['view']) currentExporter = None try: currentExporter = Exporter.objects.get(name=pattern['name']) except Exception, e: pass update = Exporter.objects.filter(name=pattern['name']).update(set__url=pattern['view'], set__name=pattern['name'], set__available_for_all = pattern['available_for_all'], upsert=True, full_result=True) if update['updatedExisting'] == False: list_add_exporters.append(pattern['name']) #Check if this new exporter is available for all. If yes, we add this exporter by default for all templates if pattern['available_for_all'] == True: Template.objects.all().update(push__exporters=update[u'upserted']) else: if pattern['available_for_all'] == True and currentExporter.available_for_all == False: Template.objects(exporters__nin=[currentExporter]).update(push__exporters=currentExporter) list_update_exporters.append(pattern['name']) except Exception, e: print('ERROR : Impossible to load the following exporter, class not found : ' + pattern['view'])
def test_getListNameFromDependencies_Template(self): Template(title='testTemplate', filename='filename', content='content', hash='hash').save() self.assertEquals( 'testTemplate', getListNameFromDependencies( list(Template.objects(title='testTemplate'))))
def test_delete_object_template_with_dependencie(self): self.assertEquals(0, len(Template.objects())) template = self.createTemplate() self.assertEquals(1, len(Template.objects())) self.createXMLData(schemaID=template.id) url = '/dashboard/delete_object' data = {'objectID': template.id, 'objectType': 'Template'} r = self.doRequestPostAdminClientLogged(url=url, data=data) self.assertEquals(1, len(Template.objects())) self.assertEquals(1, len(TemplateVersion.objects()))
def _get_flatten_schema_and_namespaces(template_name): schemas = Template.objects(title=template_name) schema_id = TemplateVersion.objects().get(pk=schemas[0].templateVersion).current schema = Template.objects().get(pk=schema_id) flattener = XSDFlattenerURL(schema.content.encode('utf-8')) ref_xml_schema_content = flattener.get_flat() # find the namespaces namespaces = common.get_namespaces(BytesIO(schema.content.encode('utf-8'))) return ref_xml_schema_content, namespaces
def createTemplate(self, title='test', filename='test', user=None): countTemplate = len(Template.objects()) hash = XSDhash.get_hash('<test>test xmldata</test>') objectVersions = self.createTemplateVersion() template = Template(title=title, filename=filename, content='<test>test xmldata</test>', version=1, templateVersion=str(objectVersions.id), hash=hash, user=str(user)).save() self.assertEquals(len(Template.objects()), countTemplate + 1) return template
def dashboard_templates(request): template = loader.get_template('dashboard/my_dashboard_my_templates_types.html') objects = Template.objects(user=str(request.user.id)) context = RequestContext(request, { 'objects': objects, 'objectType': "Template" }) #If the user is an admin, we get templates 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()) otherUsersObjects = Template.objects(user__not__in={str(request.user.id), None}) context.update({'otherUsersObjects': otherUsersObjects, 'usernames': usernames}) return HttpResponse(template.render(context))
def test_delete_object_template(self): template = self.createTemplate() url = '/dashboard/delete_object' data = {'objectID': template.id, 'objectType': 'Template'} r = self.doRequestPostAdminClientLogged(url=url, data=data) self.assertEquals(0, len(Template.objects())) self.assertEquals(0, len(TemplateVersion.objects()))
def index(request): template = loader.get_template('explore.html') request.session['currentYear'] = currentYear() if request.user.is_authenticated(): currentTemplateVersions = [] for tpl_version in TemplateVersion.objects(): currentTemplateVersions.append(tpl_version.current) currentTemplates = dict() for tpl_version in currentTemplateVersions: tpl = Template.objects.get(pk=tpl_version) templateVersions = TemplateVersion.objects.get( pk=tpl.templateVersion) currentTemplates[tpl] = templateVersions.isDeleted context = RequestContext( request, { 'templates': currentTemplates, 'userTemplates': Template.objects(user=str(request.user.id)), }) return HttpResponse(template.render(context)) else: if 'loggedOut' in request.session: del request.session['loggedOut'] request.session['next'] = '/explore' return redirect('/login')
def test_create_template_same_name(self): with open(join(RESOURCES_PATH, 'a.xsd'), 'r') as data_file: data_content = data_file.read() create_template(data_content, "a", "a.xsd") self.assertEquals(len(Template.objects()), 1) with self.assertRaises(Exception): create_template(data_content, "a", "a.xsd")
def __init__(self, templateIds=[]): self.EXPORT_OPTIONS = [] self.EXPORT_OPTIONS_DISABLED = [] dictExporters = [] #We retrieve all XSLTFiles available for those templates templates = Template.objects(pk__in=templateIds) for template in templates: dictExporters.append(set(template.XSLTFiles)) xslts = [] if len(dictExporters) > 0: xslts = set.intersection(*dictExporters) diffXslts = [] if len(dictExporters) > 1: diffXslts = set.union(*dictExporters) - xslts for xslt in xslts: #We add them self.EXPORT_OPTIONS.append((xslt.id, xslt.name)) self.my_xslts_disabled = ", ".join(x.name for x in diffXslts) super(UploadXSLTForm, self).__init__() self.fields['my_xslts'].choices = [] self.fields['my_xslts'].choices = self.EXPORT_OPTIONS
def __init__(self, templateIds=[]): self.EXPORT_OPTIONS = [] self.EXPORT_OPTIONS_DISABLED = [] dictExporters = [] #We retrieve exporters for those templates templates = Template.objects(pk__in=templateIds) for template in templates: dictExporters.append(set(template.exporters)) mutualExporters = [] diffExporters = [] if len(dictExporters) > 0: mutualExporters = set.intersection(*dictExporters) if len(dictExporters) > 1: diffExporters = set.union(*dictExporters) - mutualExporters for exporter in mutualExporters: if exporter.name != 'XSLT': #We add them self.EXPORT_OPTIONS.append((exporter.url, exporter.name)) self.my_exporters_disabled = ", ".join(x.name for x in diffExporters if x.name != 'XSLT') super(ExportForm, self).__init__() self.fields['my_exporters'].choices = [] self.fields['my_exporters'].choices = self.EXPORT_OPTIONS
def browse_all(request): template = loader.get_template('browse-all.html') context = RequestContext(request, { 'templates': Template.objects(user=None).order_by('title') }) return HttpResponse(template.render(context))
def curate_enter_data(request): print "BEGIN curate_enter_data(request)" try: context = RequestContext(request, {}) if 'id' in request.GET: xml_data_id = request.GET['id'] xml_data = XMLdata.get(xml_data_id) template = Template.objects().get(pk=ObjectId(xml_data['schema'])) context = RequestContext(request, {'edit': True, 'template_name': template.title}) curate_edit_data(request) elif 'template' in request.GET: context = RequestContext(request, {'template_name': request.GET['template']}) curate_from_schema(request) elif 'templateid' in request.GET: pass template = loader.get_template('curate/curate_enter_data.html') 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 curate_from_schema(request): try: schema_name = request.GET['template'] templates = Template.objects(title=schema_name) if 'curate_edit' in request.session and request.session['curate_edit'] == False: # 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 # # form_data = FormData(user=str(request.user.id), template=template_id, name=schema_name) # form_data.save() # # request.session['curateFormData'] = str(form_data.pk) # request.session['curate_edit'] = False if 'useForm' in request.GET and request.GET['useForm'] == 'true': pass else: if 'formString' in request.session: del request.session['formString'] # if 'xmlDocTree' in request.session: # del request.session['xmlDocTree'] else: error_message = "The selection of template by name can't be used if the MDCS contain more than one " error_message += "template with the same name." raise MDCSError(error_message) except: raise MDCSError("The template you are looking for doesn't exist.")
def index(request): template = loader.get_template('curate.html') request.session['currentYear'] = currentYear() if request.user.is_authenticated(): currentTemplateVersions = [] for tpl_version in TemplateVersion.objects(): currentTemplateVersions.append(tpl_version.current) currentTemplates = dict() for tpl_version in currentTemplateVersions: tpl = Template.objects.get(pk=tpl_version) templateVersions = TemplateVersion.objects.get(pk=tpl.templateVersion) currentTemplates[tpl] = templateVersions.isDeleted context = RequestContext(request, { 'templates':currentTemplates, 'userTemplates': Template.objects(user=str(request.user.id)), }) return HttpResponse(template.render(context)) else: if 'loggedOut' in request.session: del request.session['loggedOut'] request.session['next'] = '/curate' return redirect('/login')
def curate_from_schema(request): try: schema_name = request.GET['template'] templates = Template.objects(title=schema_name) # 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 form_data = FormData(user=str(request.user.id), template=template_id, name=schema_name) form_data.save() request.session['curateFormData'] = str(form_data.pk) request.session['curate_edit'] = False if 'xmlDocTree' in request.session: del request.session['xmlDocTree'] else: error_message = "The selection of template by name can't be used if the MDCS contain more than one " error_message += "template with the same name." raise MDCSError(error_message) except: raise MDCSError("The template you are looking for doesn't exist.")
def home(request): template = loader.get_template('index.html') context = RequestContext(request, { 'templates': Template.objects(user=None).order_by('-id')[:7] }) return HttpResponse(template.render(context))
def __init__(self, templateIds=[]): self.EXPORT_OPTIONS = [] self.EXPORT_OPTIONS_DISABLED = [] dictExporters = [] #We retrieve exporters for those templates templates = Template.objects(pk__in=templateIds) for template in templates: dictExporters.append(set(template.exporters)) mutualExporters = [] diffExporters = [] if len(dictExporters) > 0: mutualExporters = set.intersection(*dictExporters) if len(dictExporters) > 1: diffExporters = set.union(*dictExporters) - mutualExporters for exporter in mutualExporters: if exporter.name != 'XSLT': #We add them self.EXPORT_OPTIONS.append((exporter.url,exporter.name)) self.my_exporters_disabled = ", ".join(x.name for x in diffExporters if x.name !='XSLT') super(ExportForm, self).__init__() self.fields['my_exporters'].choices = [] self.fields['my_exporters'].choices = self.EXPORT_OPTIONS
def test_get_record_deleted(self): self.dump_oai_templ_mf_xslt() self.dump_oai_my_metadata_format() self.dump_oai_my_set() self.dump_xmldata() template = Template.objects(filename='Software.xsd').get() dataSoft = XMLdata.find({ 'schema': str(template.id), 'status': { '$ne': Status.DELETED } }) if len(dataSoft) > 0: xmlDataId = dataSoft[0]['_id'] identifier = '%s:%s:id/%s' % (OAI_SCHEME, OAI_REPO_IDENTIFIER, xmlDataId) data = { 'verb': 'GetRecord', 'identifier': identifier, 'metadataPrefix': 'oai_soft' } r = self.doRequestServer(data=data) self.isStatusOK(r.status_code) #Check attribute status='deleted' of header doesn't exist self.checkTagExist(r.text, 'GetRecord') self.checkTagExist(r.text, 'record') #Delete one record XMLdata.delete(xmlDataId) r = self.doRequestServer(data=data) self.isStatusOK(r.status_code) #Check attribute status='deleted' of header does exist self.checkTagExist(r.text, 'GetRecord')
def __init__(self, *args, **kwargs): templatesVersionID = Template.objects.distinct(field="templateVersion") templatesID = TemplateVersion.objects(pk__in=templatesVersionID, isDeleted=False).distinct(field="current") templates = Template.objects(pk__in=templatesID).all() super(MyTemplateMetadataFormatForm, self).__init__(*args, **kwargs) self.fields['template'].queryset = [] self.fields['template'].queryset = templates
def curate_enter_data(request): print "BEGIN curate_enter_data(request)" try: context = RequestContext(request, {}) if 'id' in request.GET: xml_data_id = request.GET['id'] xml_data = XMLdata.get(xml_data_id) template = Template.objects().get(pk=ObjectId(xml_data['schema'])) context = RequestContext(request, { 'edit': True, 'template_name': template.title }) curate_edit_data(request) elif 'template' in request.GET: context = RequestContext( request, {'template_name': request.GET['template']}) curate_from_schema(request) elif 'templateid' in request.GET: pass template = loader.get_template('curate/curate_enter_data.html') 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 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_delete_template_with_dependencies(self): template = self.createTemplate() XMLdata(schemaID=str(template.id), title='testRecord', xml='<test>test xmldata</test>').save() FormData(template=str(template.id), name='testFormData', xml_data='testest', user=str(1)).save() listDependencies = delete_template(str(template.id)) self.assertEquals(len(Template.objects()), 1) self.assertEquals(len(TemplateVersion.objects()), 1) self.assertEquals(listDependencies, 'testFormData, testRecord')
def createTemplateWithTemplateVersion(self, templateVersionId): hash = XSDhash.get_hash('<test>test xmldata</test>') return Template(title='test', filename='test', content='<test>test xmldata</test>', version=1, templateVersion=templateVersionId, hash=hash).save()
def createTemplate(self): hash = XSDhash.get_hash('<test>test xmldata</test>') objectVersions = self.createTemplateVersion() return Template(title='test', filename='test', content='<test>test xmldata</test>', version=1, templateVersion=str(objectVersions.id), hash=hash).save()
def dashboard_records(request): template = loader.get_template('dashboard/my_dashboard_my_records.html') query = {} # ispublished = request.GET.get('ispublished', None) #If ispublished not None, check if we want publish or unpublish records # if ispublished: # ispublished = ispublished == 'true' # query['ispublished'] = ispublished query['iduser'] = str(request.user.id) userXmlData = sorted(XMLdata.find(query), key=lambda data: data['lastmodificationdate'], reverse=True) # Get all the templates #templates_used = Template.find() templates_used = sorted(Template.find(query), key=lambda data: data['content'], reverse=True) #Add user_form for change owner user_form = UserForm(request.user) otherUsers = User.objects.all() #otherUXMLdatas = sorted(XMLdata.objects(user=str(userId)).distinct(field='title'), key=lambda data: data['lastmodificationdate'], reverse=True) idotherUsers = User.objects.only('_id') otherUXMLdatas = [] # sorted(XMLdata.find(query), key=lambda data: data['lastmodificationdate'], reverse=True) otherUsersTemplates = [] otherUXMLd =[] usernames = [] # for idotherUser in idotherUsers: # otherUXMLdata = Template.objects(user = str(idotherUser)).distinct(field ='title') # otherUXMLdatas.append(otherUXMLdata) #otherXMLdatas = XMLdata.objects(user = (str())) context = RequestContext(request, {'XMLdatas': userXmlData, # 'ispublished': ispublished, 'user_form': user_form, 'Templates': templates_used, 'OtherUsersXMLdatas': otherUXMLdatas, 'OtherUsers': otherUsers, 'IdotherUsers':idotherUsers, 'OtherUXMLd' : otherUXMLd, 'usernames' : usernames, 'totaldocs_user' : len(userXmlData), 'totaldocs_other_users' : len(otherUXMLdatas) }) #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, 'totaldocs_other_users': len(otherUsersXmlData)}) i = None for elem in context['OtherUsersXMLdatas']: i = context['OtherUsersXMLdatas'].index(elem) j = 0 for k,v in context['usernames'].items(): if j == i: other_users.append((k,v)) return HttpResponse(template.render(context))
def load_xslt(): # Add OAI Xslt xsltFullName = 'full-oai_pmh' xsltFullPath = 'nmrr-full-oai_pmh.xsl' xsltDetailName = 'detail-oai_pmh' sltDetailPath = 'nmrr-detail-oai_pmh.xsl' objFull = ResultXslt.objects(filename='nmrr-full-oai_pmh.xsl') if not objFull: file = open(os.path.join(SITE_ROOT, 'oai_pmh', 'resources', 'xsl', xsltFullPath),'r') fileContent = file.read() objFull = ResultXslt(name=xsltFullName, filename=xsltFullPath, content=fileContent).save() Template.objects().update(set__ResultXsltList=str(objFull.id), upsert=True) objDetail = ResultXslt.objects(filename='nmrr-detail-oai_pmh.xsl') if not objDetail: file = open(os.path.join(SITE_ROOT, 'oai_pmh', 'resources', 'xsl', sltDetailPath),'r') fileContent = file.read() objDetail = ResultXslt(name=xsltDetailName, filename=sltDetailPath, content=fileContent).save() Template.objects().update(set__ResultXsltDetailed=str(objDetail.id), upsert=True)
def createTemplateWithTemplateVersionValidContent(self, templateVersionId): hash = XSDhash.get_hash(TEMPLATE_VALID_CONTENT) template = Template(title='test', filename='test', content=TEMPLATE_VALID_CONTENT, version=1, templateVersion=templateVersionId, hash=hash).save() TemplateVersion.objects.get(pk=templateVersionId).update( set__current=template.id) return template
def save_exporters(request): template_id = request.session['moduleTemplateID'] listIdOn = request.POST.getlist('listIdOn[]') listIdOnXslt = request.POST.getlist('listIdOnXslt[]') #We retrieve the exporter template = Template.objects.get(pk=template_id) #We reinitialise exporters and XSLT template.exporters = None template.XSLTFiles = None template.save() #We add exporters for exp in listIdOn: exp = Exporter.objects.get(pk=exp) Template.objects(id=template_id).update_one(push__exporters=exp) #We add XSLT files for xslt in listIdOnXslt: xslt = ExporterXslt.objects.get(pk=xslt) Template.objects(id=template_id).update_one(push__XSLTFiles=xslt) template.save() return HttpResponse(json.dumps({}), content_type='application/javascript')
def get_xsd(request, schema): #TODO Available if publication ok and no user template #We retrieve the schema filename in the schema attribute #Get the templateVersion ID try: templatesVersionID = Template.objects(filename=schema).distinct(field="templateVersion") templateID = TemplateVersion.objects(pk__in=templatesVersionID, isDeleted=False).distinct(field="current") templates = Template.objects.get(pk__in=templateID) #Get the XML schema contentEncoded = templates.content.encode('utf-8') fileObj = StringIO(contentEncoded) return HttpResponse(fileObj, content_type='text/xml') except MONGO_ERRORS.DoesNotExist, e: return HttpResponseNotFound('Impossible to retrieve the schema with the given name.')
def index(request): template = loader.get_template('compose/compose.html') currentTemplateVersions = [] for tpl_version in TemplateVersion.objects(): currentTemplateVersions.append(tpl_version.current) currentTemplates = dict() for tpl_version in currentTemplateVersions: tpl = Template.objects.get(pk=tpl_version) templateVersions = TemplateVersion.objects.get(pk=tpl.templateVersion) currentTemplates[tpl] = templateVersions.isDeleted context = RequestContext(request, { 'templates':currentTemplates, 'userTemplates': Template.objects(user=str(request.user.id)), }) return HttpResponse(template.render(context))
def home(request): template = loader.get_template('index-new.html') context = RequestContext(request, { 'templates': Template.objects(user=None).order_by('-id')[:7] }) if request.method == "POST": if request.POST[u'email']: if SignUp.objects.filter(email=request.POST[u'email']).exists(): messages.success(request, 'Thank you AGAIN, ' + request.POST[u'email'] + '!') return HttpResponseRedirect('/') else: new_signup = SignUp(email=request.POST[u'email']) new_signup.save() messages.success(request, 'Thank you, ' + request.POST[u'email'] + '!') return HttpResponseRedirect('/') return HttpResponse(template.render(context))
def list_test_deleted(self, verb): self.dump_oai_templ_mf_xslt() self.dump_oai_my_metadata_format() self.dump_oai_my_set() self.dump_xmldata() data = {'verb': verb, 'metadataPrefix': 'oai_soft'} r = self.doRequestServer(data=data) self.isStatusOK(r.status_code) #Check attribute status='deleted' of header doesn't exist self.checkTagExist(r.text, verb) #Delete one record template = Template.objects(filename='Software.xsd').get() dataSoft = XMLdata.find({'schema': str(template.id), 'status': {'$ne': Status.DELETED}}) if len(dataSoft) > 0: XMLdata.update(dataSoft[0]['_id'], {'status': Status.DELETED}) r = self.doRequestServer(data=data) self.isStatusOK(r.status_code) self.checkTagExist(r.text, verb) #Check attribute status='deleted' of header does exist self.checkTagWithParamExist(r.text, 'header', 'status="deleted"')
def test_get_record_deleted(self): self.dump_oai_templ_mf_xslt() self.dump_oai_my_metadata_format() self.dump_oai_my_set() self.dump_xmldata() template = Template.objects(filename='Software.xsd').get() dataSoft = XMLdata.find({'schema': str(template.id), 'status': {'$ne': Status.DELETED}}) if len(dataSoft) > 0: xmlDataId = dataSoft[0]['_id'] identifier = '%s:%s:id/%s' % (OAI_SCHEME, OAI_REPO_IDENTIFIER, xmlDataId) data = {'verb': 'GetRecord', 'identifier': identifier, 'metadataPrefix': 'oai_soft'} r = self.doRequestServer(data=data) self.isStatusOK(r.status_code) #Check attribute status='deleted' of header doesn't exist self.checkTagExist(r.text, 'GetRecord') self.checkTagExist(r.text, 'record') #Delete one record XMLdata.update(xmlDataId, {'status': Status.DELETED}) r = self.doRequestServer(data=data) self.isStatusOK(r.status_code) #Check attribute status='deleted' of header does exist self.checkTagExist(r.text, 'GetRecord') # Only for NMRR self.checkTagWithParamExist(r.text, 'header', 'status="deleted"')
def save_object(request): print 'BEGIN def save_object(request)' objectName = None objectFilename = None objectContent = None objectType = None objectFlat = None objectApiurl = None if ('uploadObjectValid' in request.session and request.session['uploadObjectValid'] == True and 'uploadObjectName' in request.session and request.session['uploadObjectName'] is not None and 'uploadObjectFilename' in request.session and request.session['uploadObjectFilename'] is not None and 'uploadObjectContent' in request.session and request.session['uploadObjectContent'] is not None and 'uploadObjectType' in request.session and request.session['uploadObjectType'] is not None): objectName = request.session['uploadObjectName'] objectFilename = request.session['uploadObjectFilename'] objectContent = request.session['uploadObjectContent'] objectType = request.session['uploadObjectType'] if 'uploadObjectFlat' in request.session and request.session['uploadObjectFlat'] is not None: objectFlat = request.session['uploadObjectFlat'] else: objectFlat = None if 'uploadObjectAPIurl' in request.session and request.session['uploadObjectAPIurl'] is not None: objectApiurl = request.session['uploadObjectAPIurl'] else: objectApiurl = None if 'uploadDependencies' in request.session and request.session['uploadDependencies'] is not None: dependencies = request.session['uploadDependencies'] else: dependencies = None hash = XSDhash.get_hash(objectContent) # save the object if objectType == "Template": objectVersions = TemplateVersion(nbVersions=1, isDeleted=False).save() object = Template(title=objectName, filename=objectFilename, content=objectContent, version=1, templateVersion=str(objectVersions.id), hash=hash).save() #We add default exporters try: exporters = Exporter.objects.filter(available_for_all=True) object.exporters = exporters except: pass elif objectType == "Type": objectVersions = TypeVersion(nbVersions=1, isDeleted=False).save() object = Type(title=objectName, filename=objectFilename, content=objectContent, version=1, typeVersion=str(objectVersions.id), hash=hash).save() buckets = request.POST.getlist('buckets[]') for bucket_id in buckets: bucket = Bucket.objects.get(pk=bucket_id) bucket.types.append(str(objectVersions.id)) bucket.save() objectVersions.versions = [str(object.id)] objectVersions.current = str(object.id) objectVersions.save() object.save() if objectFlat is not None and objectApiurl is not None and dependencies is not None: MetaSchema(schemaId=str(object.id), flat_content=objectFlat, api_content=objectApiurl).save() object.dependencies = dependencies object.save() clear_object(request) else: response_dict = {'errors': 'True'} return HttpResponse(json.dumps(response_dict), content_type='application/javascript') return HttpResponse(json.dumps({}), content_type='application/javascript')
def get_results_by_instance_keyword(request): print 'BEGIN def getResultsKeyword(request)' resultsByKeyword = [] results = [] resultString = "" #Instance json_instances = [] if 'HTTPS' in request.META['SERVER_PROTOCOL']: protocol = "https" else: protocol = "http" instance = Instance(name="Local", protocol=protocol, address=request.META['REMOTE_ADDR'], port=request.META['SERVER_PORT'], access_token="token", refresh_token="token") json_instances.append(instance.to_json()) request.session['instancesExplore'] = json_instances sessionName = "resultsExploreOaiPMh" + instance['name'] try: keyword = request.GET['keyword'] schemas = request.GET.getlist('schemas[]') userSchemas = request.GET.getlist('userSchemas[]') refinements = refinements_to_mongo(request.GET.getlist('refinements[]')) if 'onlySuggestions' in request.GET: onlySuggestions = json.loads(request.GET['onlySuggestions']) else: onlySuggestions = False except: keyword = '' schemas = [] userSchemas = [] refinements = {} onlySuggestions = True #We get all template versions for the given schemas #First, we take care of user defined schema templatesIDUser = Template.objects(title__in=userSchemas).distinct(field="id") templatesIDUser = [str(x) for x in templatesIDUser] #Take care of the rest, with versions templatesVersions = Template.objects(title__in=schemas).distinct(field="templateVersion") #We get all templates ID, for all versions allTemplatesIDCommon = TemplateVersion.objects(pk__in=templatesVersions, isDeleted=False).distinct(field="versions") #We remove the removed version allTemplatesIDCommonRemoved = TemplateVersion.objects(pk__in=templatesVersions, isDeleted=False).distinct(field="deletedVersions") templatesIDCommon = list(set(allTemplatesIDCommon) - set(allTemplatesIDCommonRemoved)) templatesID = templatesIDUser + templatesIDCommon #We retrieve deactivated registries so as not to get their metadata formats deactivatedRegistries = [str(x.id) for x in OaiRegistry.objects(isDeactivated=True).order_by('id')] metadataFormatsID = OaiMetadataFormat.objects(template__in=templatesID, registry__not__in=deactivatedRegistries).distinct(field="id") instanceResults = OaiRecord.executeFullTextQuery(keyword, metadataFormatsID, refinements) if len(instanceResults) > 0: if not onlySuggestions: xsltPath = os.path.join(settings.SITE_ROOT, 'static/resources/xsl/xml2html.xsl') xslt = etree.parse(xsltPath) transform = etree.XSLT(xslt) template = loader.get_template('oai_pmh/explore/explore_result_keyword.html') #Retrieve schema and registries. Avoid to retrieve the information for each result registriesName = {} objMetadataFormats = {} listRegistriesID = set([x['registry'] for x in instanceResults]) for registryId in listRegistriesID: obj = OaiRegistry.objects(pk=registryId).get() registriesName[str(registryId)] = obj.name listSchemaId = set([x['metadataformat'] for x in instanceResults]) for schemaId in listSchemaId: obj = OaiMetadataFormat.objects(pk=schemaId).get() objMetadataFormats[str(schemaId)] = obj listItems = [] xmltodictunparse = xmltodict.unparse appendResult = results.append toXML = etree.XML parse = etree.parse XSLT = etree.XSLT if not onlySuggestions: for instanceResult in instanceResults: custom_xslt = False appendResult({'title':instanceResult['identifier'], 'content':xmltodictunparse(instanceResult['metadata']),'id':str(instanceResult['_id'])}) dom = toXML(str(xmltodictunparse(instanceResult['metadata']).encode('utf-8'))) #Check if a custom list result XSLT has to be used try: metadataFormat = objMetadataFormats[str(instanceResult['metadataformat'])] if metadataFormat.template.ResultXsltList: listXslt = parse(BytesIO(metadataFormat.template.ResultXsltList.content.encode('utf-8'))) listTransform = XSLT(listXslt) newdom = listTransform(dom) custom_xslt = True else: newdom = transform(dom) except Exception, e: #We use the default one newdom = transform(dom) custom_xslt = False context = RequestContext(request, {'id':str(instanceResult['_id']), 'xml': str(newdom), 'title': instanceResult['identifier'], 'custom_xslt': custom_xslt, 'schema_name': metadataFormat.metadataPrefix, 'registry_name': registriesName[instanceResult['registry']], 'oai_pmh': True}) resultString+= template.render(context) else: for instanceResult in instanceResults[:20]: wordList = re.sub("[^\w]", " ", keyword).split() wordList = [x + "|" + x +"\w+" for x in wordList] wordList = '|'.join(wordList) listWholeKeywords = re.findall("\\b("+ wordList +")\\b", xmltodict.unparse(instanceResult['metadata']).encode('utf-8'), flags=re.IGNORECASE) labels = list(set(listWholeKeywords)) for label in labels: label = label.lower() result_json = {} result_json['label'] = label result_json['value'] = label if not result_json in resultsByKeyword: resultsByKeyword.append(result_json)
flatTree = etree.fromstring(flatStr) try: # is it a valid XML schema ? xmlSchema = etree.XMLSchema(flatTree) except Exception, e: response_dict['errors'] = e.message.replace("'","") return HttpResponse(json.dumps(response_dict), content_type='application/javascript') hash = XSDhash.get_hash(content) dependencies = [] for uri in request.session["includedTypesCompose"]: url = urlparse(uri) id = url.query.split("=")[1] dependencies.append(id) template = Template(title=template_name, filename=template_name, content=content, hash=hash, user=str(request.user.id), dependencies=dependencies) #We add default exporters try: exporters = Exporter.objects.filter(available_for_all=True) template.exporters = exporters except: pass template.save() MetaSchema(schemaId=str(template.id), flat_content=flatStr, api_content=content).save() return HttpResponse(json.dumps(response_dict), content_type='application/javascript') ################################################################################
try: # is it a valid XML schema ? xmlSchema = etree.XMLSchema(flatTree) except Exception, e: dajax.script( """ $("#new-template-error").html("<font color='red'>Not a valid XML schema.</font><br/>""" + e.message.replace("'", "") + """ "); """ ) return dajax.json() hash = XSDhash.get_hash(content) template = Template(title=templateName, filename=templateName, content=content, hash=hash, user=request.user.id) template.save() MetaSchema(schemaId=str(template.id), flat_content=flatStr, api_content=content).save() dajax.script( """ saveTemplateCallback(); $("#dialog-save-template").dialog("close"); """ ) return dajax.json() ################################################################################
def load_templates(): """ Loads templates/xslt for NMRR the first time """ # if templates are already present, initialization already happened existing_templates = Template.objects() if len(existing_templates) == 0: templates = { 'all':'AllResources.xsd', 'organization': 'Organization.xsd', 'datacollection': 'DataCollection.xsd', 'repository': 'Repository.xsd', 'projectarchive': 'ProjectArchive.xsd', 'database': 'Database.xsd', 'dataset': 'Dataset.xsd', 'service': 'Service.xsd', 'informational': 'Informational.xsd', 'software': 'Software.xsd', } template_ids = [] template_results = { 'full': 'nmrr-full.xsl', 'detail': 'nmrr-detail.xsl', } template_results_id = { 'full': None, 'detail': None, } # connect to mongo client = MongoClient(MONGODB_URI) # connect to the db 'mgi' db = client[MGI_DB] # Add the templates for template_name, template_path in templates.iteritems(): file = open(os.path.join(SITE_ROOT, 'static', 'resources', 'xsd', template_path),'r') templateContent = file.read() hash = XSDhash.get_hash(templateContent) #create template/ template version objectVersions = TemplateVersion(nbVersions=1, isDeleted=False).save() object = Template(title=template_name, filename=template_path, content=templateContent, version=1, templateVersion=str(objectVersions.id), hash=hash).save() objectVersions.versions = [str(object.id)] objectVersions.current = str(object.id) objectVersions.save() object.save() # save template id template_ids.append(str(object.id)) # Add xslt xsl_col = db['result_xslt'] for xsl_name, xsl_path in template_results.iteritems(): file = open(os.path.join(SITE_ROOT, 'static', 'resources', 'xsl', xsl_path),'r') fileContent = file.read() xsl = {} xsl['name'] = xsl_name xsl['filename'] = xsl_path xsl['content'] = fileContent xsl_id = xsl_col.insert(xsl) template_results_id[xsl_name] = str(xsl_id) templates = db['template'] results_xslt = {'ResultXsltList': template_results_id['full'], 'ResultXsltDetailed': template_results_id['detail']} templates.update({}, {"$set":results_xslt}, upsert=False, multi=True)
def dump_template(self): self.assertEquals(len(Template.objects()), 0) self.dump_template_version() self.restoreDump(join(DUMP_TEST_PATH, 'template.bson'), 'template') self.assertTrue(len(Template.objects()) > 0)