Exemple #1
0
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')
Exemple #2
0
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',
            'document': 'Document.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)
Exemple #3
0
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 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)