def test_delete_type_and_version(self):
     self.assertEquals(len(Type.objects()), 0)
     self.assertEquals(len(TypeVersion.objects()), 0)
     type = self.createType()
     delete_type_and_version(str(type.id))
     self.assertEquals(len(Type.objects()), 0)
     self.assertEquals(len(TypeVersion.objects()), 0)
예제 #2
0
 def test_delete_type_and_version(self):
     self.assertEquals(len(Type.objects()), 0)
     self.assertEquals(len(TypeVersion.objects()), 0)
     type = self.createType()
     delete_type_and_version(str(type.id))
     self.assertEquals(len(Type.objects()), 0)
     self.assertEquals(len(TypeVersion.objects()), 0)
예제 #3
0
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')
예제 #4
0
 def createType(self, title='test', filename='test', user=None):
     countType = len(Type.objects())
     hash = XSDhash.get_hash('<test>test xmldata</test>')
     objectVersions = self.createTypeVersion()
     type = Type(title=title,
                 filename=filename,
                 content='<test>test xmldata</test>',
                 version=1,
                 typeVersion=str(objectVersions.id),
                 hash=hash,
                 user=str(user)).save()
     self.assertEquals(len(Type.objects()), countType + 1)
     return type
예제 #5
0
def dashboard_types(request):
    template = loader.get_template('dashboard/my_dashboard_my_templates_types.html')
    objects = Type.objects(user=str(request.user.id))
    context = RequestContext(request, {
                'objects': objects,
                'objectType': "Type"
            })
    #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 = Type.objects(user__not__in={str(request.user.id), None})
        context.update({'otherUsersObjects': otherUsersObjects, 'usernames': usernames})

    return HttpResponse(template.render(context))
예제 #6
0
def compose_build_template(request):
    template = loader.get_template('compose/compose_build_template.html')
    # 1) user types: list of ids
    userTypes = []
    for user_type in Type.objects(user=str(request.user.id)):
        userTypes.append(user_type)
    # 2) buckets: label -> list of type that are not deleted
    # 3) nobuckets: list of types that are not assigned to a specific bucket
    bucketsTypes = dict()
    nobucketsTypes = []

    buckets = Bucket.objects

    for type_version in TypeVersion.objects():
        if type_version.isDeleted == False:
            hasBucket = False
            for bucket in buckets:
                if str(type_version.id) in bucket.types:
                    if bucket not in bucketsTypes.keys():
                        bucketsTypes[bucket] = []
                    bucketsTypes[bucket].append(
                        Type.objects.get(pk=type_version.current))
                    hasBucket = True
            if hasBucket == False:
                nobucketsTypes.append(
                    Type.objects.get(pk=type_version.current))

    context = RequestContext(
        request, {
            'bucketsTypes': bucketsTypes,
            'nobucketsTypes': nobucketsTypes,
            'userTypes': userTypes,
        })

    return HttpResponse(template.render(context))
예제 #7
0
파일: views.py 프로젝트: hzhao1230/nanomine
def compose_build_template(request):
    template = loader.get_template('compose/compose_build_template.html')
    # 1) user types: list of ids
    userTypes = []
    for user_type in Type.objects(user=str(request.user.id)):
        userTypes.append(user_type)
    # 2) buckets: label -> list of type that are not deleted
    # 3) nobuckets: list of types that are not assigned to a specific bucket
    bucketsTypes = dict()
    nobucketsTypes = []

    buckets = Bucket.objects

    for type_version in TypeVersion.objects():
        if type_version.isDeleted == False:
            hasBucket = False
            for bucket in buckets:
                if str(type_version.id) in bucket.types:
                    if bucket not in bucketsTypes.keys():
                        bucketsTypes[bucket] = []
                    bucketsTypes[bucket].append(Type.objects.get(pk=type_version.current))
                    hasBucket = True
            if hasBucket == False:
                nobucketsTypes.append(Type.objects.get(pk=type_version.current))

    context = RequestContext(request, {
       'bucketsTypes': bucketsTypes,
       'nobucketsTypes': nobucketsTypes,
       'userTypes': userTypes,
    })

    return HttpResponse(template.render(context))
예제 #8
0
 def test_create_type_same_name(self):
     with open(join(RESOURCES_PATH, 'bType.xsd'), 'r') as data_file:
         data_content = data_file.read()
         create_type(data_content, "b", "bType.xsd")
         self.assertEquals(len(Type.objects()), 1)
         with self.assertRaises(Exception):
             create_type(data_content, "b", "bType.xsd")
 def test_delete_object_type(self):
     type = self.createType()
     url = '/dashboard/delete_object'
     data = {'objectID': type.id, 'objectType': 'Type'}
     r = self.doRequestPostAdminClientLogged(url=url, data=data)
     self.assertEquals(0, len(Type.objects()))
     self.assertEquals(0, len(TypeVersion.objects()))
예제 #10
0
 def test_delete_object_type(self):
     type = self.createType()
     url = '/dashboard/delete_object'
     data = {'objectID': type.id, 'objectType': 'Type'}
     r = self.doRequestPostAdminClientLogged(url=url, data=data)
     self.assertEquals(0, len(Type.objects()))
     self.assertEquals(0, len(TypeVersion.objects()))
 def test_delete_type_with_dependencies(self):
     type = self.createType()
     Type(title='testType', filename='filename', content='content', hash='hash', dependencies=[str(type.id)]).save()
     Template(title='testTemplate', filename='filename', content='content', hash='hash', dependencies=[str(type.id)]).save()
     listDependencies = delete_type(str(type.id))
     self.assertEquals(len(Type.objects()), 2)
     self.assertEquals(len(TypeVersion.objects()), 1)
     self.assertEquals(listDependencies, 'testType, testTemplate')
예제 #12
0
 def test_getListNameFromDependencies_Type(self):
     Type(title='testType',
          filename='filename',
          content='content',
          hash='hash').save()
     self.assertEquals(
         'testType',
         getListNameFromDependencies(list(Type.objects(title='testType'))))
예제 #13
0
 def test_delete_object_type_with_dependencie(self):
     type = self.createType()
     otherType = self.createType()
     otherType.dependencies = [str(type.id)]
     otherType.save()
     url = '/dashboard/delete_object'
     data = {'objectID': type.id, 'objectType': 'Type'}
     r = self.doRequestPostAdminClientLogged(url=url, data=data)
     self.assertIsNotNone(Type.objects(pk=type.id).get())
 def test_delete_object_type_with_dependencie(self):
     type = self.createType()
     otherType = self.createType()
     otherType.dependencies = [str(type.id)]
     otherType.save()
     url = '/dashboard/delete_object'
     data = {'objectID': type.id, 'objectType': 'Type'}
     r = self.doRequestPostAdminClientLogged(url=url, data=data)
     self.assertIsNotNone(Type.objects(pk=type.id).get())
예제 #15
0
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)
        objectVersions = TemplateVersion.objects.get(pk=object.templateVersion)
    else:
        object = Type.objects.get(pk=object_id)
        objectVersions = TypeVersion.objects.get(pk=object.typeVersion)
        # check if a type with the same name already exists
        testFilenameObjects = Type.objects(filename=new_filename)
        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 = {'errors': 'True'}
                return HttpResponse(json.dumps(response_dict),
                                    content_type='application/javascript')

    # change the name of every version but only the filename of the current
    for version in objectVersions.versions:
        if object_type == "Template":
            obj = Template.objects.get(pk=version)
        else:
            obj = Type.objects.get(pk=version)
        obj.title = new_name
        if version == object_id:
            obj.filename = new_filename
        obj.save()

    if object_type == "Type":
        new_buckets = request.POST.getlist('newBuckets[]')
        # update the buckets
        allBuckets = Bucket.objects
        for bucket in allBuckets:
            if str(bucket.id) in new_buckets:
                if str(objectVersions.id) not in bucket.types:
                    bucket.types.append(str(objectVersions.id))

            else:
                if str(objectVersions.id) in bucket.types:
                    bucket.types.remove(str(objectVersions.id))

            bucket.save()

    return HttpResponse(json.dumps({}), content_type='application/javascript')
예제 #16
0
 def test_delete_type_with_dependencies(self):
     type = self.createType()
     Type(title='testType',
          filename='filename',
          content='content',
          hash='hash',
          dependencies=[str(type.id)]).save()
     Template(title='testTemplate',
              filename='filename',
              content='content',
              hash='hash',
              dependencies=[str(type.id)]).save()
     listDependencies = delete_type(str(type.id))
     self.assertEquals(len(Type.objects()), 2)
     self.assertEquals(len(TypeVersion.objects()), 1)
     self.assertEquals(listDependencies, 'testType, testTemplate')
예제 #17
0
    def test_create_template_dependency(self):
        with open(join(RESOURCES_PATH, 'bType.xsd'), 'r') as data_file:
            data_content = data_file.read()
            dependency = create_type(data_content, "b", "bType.xsd")
            self.assertEquals(len(Type.objects()), 1)

        with open(join(RESOURCES_PATH, 'a.xsd'), 'r') as data_file:
            data_content = data_file.read()
            xml_tree = etree.parse(BytesIO(data_content.encode('utf-8')))
            update_dependencies(xml_tree, {'bType.xsd': str(dependency.id)})
            data_content = etree.tostring(xml_tree)
            create_template(data_content,
                            "a",
                            "a.xsd",
                            dependencies=[str(dependency.id)])
            self.assertEquals(len(Template.objects()), 1)
예제 #18
0
파일: discover.py 프로젝트: RayPlante/RDA
    def scan_static_resources():
        """
        Dynamically load templates/types in resource folder
        :return:
        """
        try:
            # if templates are already present, initialization already happened
            existing_templates = Template.objects()
            existing_types = Type.objects()

            if len(existing_templates) == 0 and len(existing_types) == 0:
                templates_dir = os.path.join(STATIC_DIR, 'templates')
                if os.path.exists(templates_dir):
                    for filename in os.listdir(templates_dir):
                        if filename.endswith(".xsd"):
                            file_path = os.path.join(templates_dir, filename)
                            file = open(file_path, 'r')
                            file_content = file.read()
                            try:
                                name_no_extension = filename.split(".xsd")[0]
                                create_template(file_content,
                                                name_no_extension,
                                                name_no_extension)
                            except Exception, e:
                                print "ERROR: Unable to load {0} ({1})".format(
                                    filename, e.message)
                        else:
                            print "WARNING: {0} not loaded because extension is not {1}".format(
                                filename, ".xsd")

                types_dir = os.path.join(STATIC_DIR, 'types')
                if os.path.exists(types_dir):
                    for filename in os.listdir(types_dir):
                        if filename.endswith(".xsd"):
                            file_path = os.path.join(types_dir, filename)
                            file = open(file_path, 'r')
                            file_content = file.read()
                            try:
                                name_no_extension = filename.split(".xsd")[0]
                                create_type(file_content, name_no_extension,
                                            name_no_extension)
                            except Exception, e:
                                print "ERROR: Unable to load {0} ({1})".format(
                                    filename, e.message)
                        else:
                            print "WARNING: {0} not loaded because extension is not {1}".format(
                                filename, ".xsd")
예제 #19
0
파일: ajax.py 프로젝트: hzhao1230/nanomine
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)
        objectVersions = TemplateVersion.objects.get(pk=object.templateVersion)
    else:        
        object = Type.objects.get(pk=object_id)
        objectVersions = TypeVersion.objects.get(pk=object.typeVersion)
        # check if a type with the same name already exists
        testFilenameObjects = Type.objects(filename=new_filename)    
        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 = {'errors': 'True'}
                return HttpResponse(json.dumps(response_dict), content_type='application/javascript')
    
    # change the name of every version but only the filename of the current
    for version in objectVersions.versions:
        if object_type == "Template":
            obj = Template.objects.get(pk=version)
        else:
            obj = Type.objects.get(pk=version)
        obj.title = new_name
        if version == object_id:
            obj.filename = new_filename
        obj.save()
    
    if object_type == "Type":
        new_buckets = request.POST.getlist('newBuckets[]')
        # update the buckets
        allBuckets = Bucket.objects
        for bucket in allBuckets:
            if str(bucket.id) in new_buckets:
                if str(objectVersions.id) not in bucket.types:
                    bucket.types.append(str(objectVersions.id))
            
            else:   
                if str(objectVersions.id) in bucket.types:
                    bucket.types.remove(str(objectVersions.id))
            
            bucket.save()
    
    return HttpResponse(json.dumps({}), content_type='application/javascript')
예제 #20
0
파일: views.py 프로젝트: ztrautt/MDCS-1.5
def compose_build_template(request):
    template = loader.get_template('compose_build_template.html')
    request.session['currentYear'] = currentYear()
    if request.user.is_authenticated():

        # 1) user types: list of ids
        userTypes = []
        for user_type in Type.objects(user=request.user.id):
            userTypes.append(user_type)

        # 2) buckets: label -> list of type that are not deleted
        # 3) nobuckets: list of types that are not assigned to a specific bucket
        bucketsTypes = dict()
        nobucketsTypes = []

        buckets = Bucket.objects

        for type_version in TypeVersion.objects():
            if type_version.isDeleted == False:
                hasBucket = False
                for bucket in buckets:
                    if str(type_version.id) in bucket.types:
                        if bucket not in bucketsTypes.keys():
                            bucketsTypes[bucket] = []
                        bucketsTypes[bucket].append(
                            Type.objects.get(pk=type_version.current))
                        hasBucket = True
                if hasBucket == False:
                    nobucketsTypes.append(
                        Type.objects.get(pk=type_version.current))

        context = RequestContext(
            request, {
                'bucketsTypes': bucketsTypes,
                'nobucketsTypes': nobucketsTypes,
                'userTypes': userTypes,
            })

        return HttpResponse(template.render(context))
    else:
        if 'loggedOut' in request.session:
            del request.session['loggedOut']
        request.session['next'] = '/compose/build-template'
        return redirect('/login')
예제 #21
0
파일: views.py 프로젝트: WardLT/MDCS
def compose_build_template(request):
    template = loader.get_template('compose_build_template.html')
    request.session['currentYear'] = currentYear()
    if request.user.is_authenticated():
               
        # 1) user types: list of ids
        userTypes = []
        for user_type in Type.objects(user=request.user.id):
            userTypes.append(user_type)
                       
        # 2) buckets: label -> list of type that are not deleted
        # 3) nobuckets: list of types that are not assigned to a specific bucket
        bucketsTypes = dict()        
        nobucketsTypes = []
        
        buckets = Bucket.objects
        
        for type_version in TypeVersion.objects():
            if type_version.isDeleted == False:
                hasBucket = False
                for bucket in buckets:
                    if str(type_version.id) in bucket.types:
                        if bucket not in bucketsTypes.keys():
                            bucketsTypes[bucket] = []
                        bucketsTypes[bucket].append(Type.objects.get(pk=type_version.current))
                        hasBucket = True
                if hasBucket == False:
                    nobucketsTypes.append(Type.objects.get(pk=type_version.current))
        
        context = RequestContext(request, {
           'bucketsTypes': bucketsTypes,
           'nobucketsTypes': nobucketsTypes,
           'userTypes': userTypes,
        })
        
        return HttpResponse(template.render(context))
    else:
        if 'loggedOut' in request.session:
            del request.session['loggedOut']
        request.session['next'] = '/compose/build-template'
        return redirect('/login')
def getListTypeDependenciesType(object_id):
    from mgi.models import Type
    return list(Type.objects(dependencies=object_id))
 def test_delete_type_no_dependencies(self):
     type = self.createType()
     delete_type(str(type.id))
     self.assertEquals(len(Type.objects()), 0)
     self.assertEquals(len(TypeVersion.objects()), 0)
예제 #24
0
    # validate the schema
    error = validate_xml_schema(xml_tree)

    if error is not None:
        response_dict['errors'] = 'This is not a valid XML schema.' + error.replace("'","")
        return HttpResponse(json.dumps(response_dict), content_type='application/javascript')

    dependencies = []

    for uri in request.session["includedTypesCompose"]:
        try:
            url = urlparse(uri)
            id = url.query.split("=")[1]
            # add dependency if it matches a type id
            Type.objects().get(pk=id)
            dependencies.append(id)
        except:
            pass

    create_template(content, template_name, template_name, dependencies, user=str(request.user.id))

    return HttpResponse(json.dumps(response_dict), content_type='application/javascript')


################################################################################
# 
# Function Name: save_type(request)
# Inputs:        request - HTTP request
# Outputs:       JSON 
# Exceptions:    None
예제 #25
0
파일: ajax.py 프로젝트: RayPlante/RDA
def insert_element_sequence(request):
    try:
        type_id = request.POST['typeID']
        client_type_name = request.POST['typeName']
        xpath = request.POST['xpath']

        xml_tree_str = request.session['newXmlTemplateCompose']

        # build the dom tree of the schema being built
        xsd_tree = etree.parse(BytesIO(xml_tree_str.encode('utf-8')))
        # get namespaces information for the schema
        namespaces = get_namespaces(BytesIO(str(xml_tree_str)))
        default_prefix = get_default_prefix(namespaces)
        # get target namespace information
        target_namespace, target_namespace_prefix = get_target_namespace(
            namespaces, xsd_tree)
        # build xpath to element
        xpath = xpath.replace(default_prefix + ":", LXML_SCHEMA_NAMESPACE)
        if type_id == 'built_in_type':
            type_name = default_prefix + ':' + client_type_name
            xsd_tree.find(xpath).append(
                etree.Element("{}element".format(LXML_SCHEMA_NAMESPACE),
                              attrib={
                                  'type': type_name,
                                  'name': client_type_name
                              }))
            # validate XML schema
            error = validate_xml_schema(xsd_tree)
            new_xsd_str = etree.tostring(xsd_tree)
        else:
            # get the type being included
            type_object = Type.objects().get(pk=type_id)
            type_xsd_tree = etree.parse(
                BytesIO(type_object.content.encode('utf-8')))
            # get namespaces information for the type
            type_namespaces = get_namespaces(BytesIO(str(type_object.content)))
            type_target_namespace, type_target_namespace_prefix = get_target_namespace(
                type_namespaces, type_xsd_tree)

            # get the type from the included/imported file
            # If there is a complex type
            element_type = type_xsd_tree.find(
                "{}complexType".format(LXML_SCHEMA_NAMESPACE))
            if element_type is None:
                # If there is a simple type
                element_type = type_xsd_tree.find(
                    "{}simpleType".format(LXML_SCHEMA_NAMESPACE))
            type_name = element_type.attrib["name"]

            if type_target_namespace is not None:
                ns_type_name = "{0}:{1}".format(type_target_namespace_prefix,
                                                type_name)
            else:
                if target_namespace is not None:
                    ns_type_name = "{0}:{1}".format(target_namespace_prefix,
                                                    type_name)
                else:
                    ns_type_name = '{}'.format(type_name)
            nsmap = {type_target_namespace_prefix: type_target_namespace}
            update_nsmap = False

            # get link to the type to include
            include_url = getSchemaLocation(str(type_id))

            # Schema without target namespace
            if target_namespace is None:
                # Type without target namespace
                if type_target_namespace is None:
                    # add include
                    xsd_tree.getroot().insert(
                        0,
                        etree.Element(
                            "{}include".format(LXML_SCHEMA_NAMESPACE),
                            attrib={'schemaLocation': include_url}))
                    # add element
                    xsd_tree.find(xpath).append(
                        etree.Element(
                            "{}element".format(LXML_SCHEMA_NAMESPACE),
                            attrib={
                                'type': type_name,
                                'name': client_type_name
                            }))
                # Type with target namespace
                else:
                    # add import
                    xsd_tree.getroot().insert(
                        0,
                        etree.Element("{}import".format(LXML_SCHEMA_NAMESPACE),
                                      attrib={
                                          'schemaLocation': include_url,
                                          'namespace': type_target_namespace
                                      }))
                    # create the element to add
                    element = etree.Element(
                        "{}element".format(LXML_SCHEMA_NAMESPACE),
                        attrib={
                            'name': client_type_name,
                            'type': ns_type_name
                        },
                    )
                    # add the element
                    xsd_tree.find(xpath).append(element)

                    update_nsmap = True

            # Schema with target namespace
            else:
                # Type without target namespace
                if type_target_namespace is None:
                    # add include
                    xsd_tree.getroot().insert(
                        0,
                        etree.Element(
                            "{}include".format(LXML_SCHEMA_NAMESPACE),
                            attrib={'schemaLocation': include_url}))
                    # add element
                    xsd_tree.find(xpath).append(
                        etree.Element(
                            "{}element".format(LXML_SCHEMA_NAMESPACE),
                            attrib={
                                'name': client_type_name,
                                'type': ns_type_name
                            }))
                # Type with target namespace
                else:
                    # Same target namespace as base template
                    if target_namespace == type_target_namespace:
                        # add include
                        xsd_tree.getroot().insert(
                            0,
                            etree.Element(
                                "{}include".format(LXML_SCHEMA_NAMESPACE),
                                attrib={'schemaLocation': include_url}))
                        # add element
                        xsd_tree.find(xpath).append(
                            etree.Element(
                                "{}element".format(LXML_SCHEMA_NAMESPACE),
                                attrib={
                                    'name': client_type_name,
                                    'type': ns_type_name
                                }))
                    # Different target namespace as base template
                    else:
                        # add import
                        xsd_tree.getroot().insert(
                            0,
                            etree.Element(
                                "{}import".format(LXML_SCHEMA_NAMESPACE),
                                attrib={
                                    'schemaLocation': include_url,
                                    'namespace': type_target_namespace
                                }))
                        # create the element to add
                        element = etree.Element(
                            "{}element".format(LXML_SCHEMA_NAMESPACE),
                            attrib={
                                'name': client_type_name,
                                'type': ns_type_name
                            },
                        )
                        # add the element
                        xsd_tree.find(xpath).append(element)

                        update_nsmap = True

            # add the id of the type if not already present
            if include_url not in request.session['includedTypesCompose']:
                request.session['includedTypesCompose'].append(include_url)

            if update_nsmap:
                root = xsd_tree.getroot()
                root_nsmap = root.nsmap

                if type_target_namespace_prefix in root_nsmap.keys() and\
                                root_nsmap[type_target_namespace_prefix] != type_target_namespace:
                    raise MDCSError(
                        'The namespace prefix is already declared for a different namespace.'
                    )
                else:
                    root_nsmap[
                        type_target_namespace_prefix] = type_target_namespace
                    new_root = etree.Element(root.tag, nsmap=root_nsmap)
                    new_root[:] = root[:]

                    # validate XML schema
                    error = validate_xml_schema(new_root)
                    new_xsd_str = etree.tostring(new_root)

            else:
                # validate XML schema
                error = validate_xml_schema(xsd_tree)
                new_xsd_str = etree.tostring(xsd_tree)

        if error is not None:
            raise MDCSError(error)

        # save the tree in the session
        request.session['newXmlTemplateCompose'] = new_xsd_str
    except Exception, e:
        return HttpResponseBadRequest(e.message,
                                      content_type='application/javascript')
예제 #26
0
파일: ajax.py 프로젝트: RayPlante/RDA
    if error is not None:
        response_dict[
            'errors'] = 'This is not a valid XML schema.' + error.replace(
                "'", "")
        return HttpResponse(json.dumps(response_dict),
                            content_type='application/javascript')

    dependencies = []

    for uri in request.session["includedTypesCompose"]:
        try:
            url = urlparse(uri)
            id = url.query.split("=")[1]
            # add dependency if it matches a type id
            Type.objects().get(pk=id)
            dependencies.append(id)
        except:
            pass

    try:
        create_template(content,
                        template_name,
                        template_name,
                        dependencies,
                        user=str(request.user.id))
    except Exception, e:
        response_dict['errors'] = e.message.replace("'", "")
        return HttpResponse(json.dumps(response_dict),
                            content_type='application/javascript')
예제 #27
0
def insert_element_sequence(request):
    try:
        type_id = request.POST['typeID']
        client_type_name = request.POST['typeName']
        xpath = request.POST['xpath']

        xml_tree_str = request.session['newXmlTemplateCompose']

        # build the dom tree of the schema being built
        xsd_tree = etree.parse(BytesIO(xml_tree_str.encode('utf-8')))
        # get namespaces information for the schema
        namespaces = get_namespaces(BytesIO(str(xml_tree_str)))
        default_prefix = get_default_prefix(namespaces)
        target_namespace, target_namespace_prefix = get_target_namespace(namespaces, xsd_tree)

        # get the type being included
        type_object = Type.objects().get(pk=type_id)
        type_xsd_tree = etree.parse(BytesIO(type_object.content.encode('utf-8')))
        # get namespaces information for the type
        type_namespaces = get_namespaces(BytesIO(str(type_object.content)))
        type_target_namespace, type_target_namespace_prefix = get_target_namespace(type_namespaces, type_xsd_tree)

        # get the type from the included/imported file
        # If there is a complex type
        element_type = type_xsd_tree.find("{}complexType".format(LXML_SCHEMA_NAMESPACE))
        if element_type is None:
            # If there is a simple type
            element_type = type_xsd_tree.find("{}simpleType".format(LXML_SCHEMA_NAMESPACE))
        type_name = element_type.attrib["name"]

        if type_target_namespace is not None:
            ns_type_name = "{0}:{1}".format(type_target_namespace_prefix, type_name)
        else:
            if target_namespace is not None:
                ns_type_name = "{0}:{1}".format(target_namespace_prefix, type_name)
            else:
                ns_type_name = '{}'.format(type_name)
        nsmap = {type_target_namespace_prefix: type_target_namespace}
        update_nsmap = False

        # build xpath to element
        xpath = xpath.replace(default_prefix + ":", LXML_SCHEMA_NAMESPACE)

        # get link to the type to include
        include_url = getSchemaLocation(str(type_id))

        # Schema without target namespace
        if target_namespace is None:
            # Type without target namespace
            if type_target_namespace is None:
                # add include
                xsd_tree.getroot().insert(0, etree.Element("{}include".format(LXML_SCHEMA_NAMESPACE),
                                          attrib={'schemaLocation': include_url}))
                # add element
                xsd_tree.find(xpath).append(etree.Element("{}element".format(LXML_SCHEMA_NAMESPACE),
                                            attrib={'type': type_name,
                                                    'name': client_type_name}))
            # Type with target namespace
            else:
                # add import
                xsd_tree.getroot().insert(0, etree.Element("{}import".format(LXML_SCHEMA_NAMESPACE),
                                          attrib={'schemaLocation': include_url,
                                                  'namespace': type_target_namespace}))
                # create the element to add
                element = etree.Element("{}element".format(LXML_SCHEMA_NAMESPACE),
                                        attrib={'name': client_type_name,
                                                'type': ns_type_name},
                                        )
                # add the element
                xsd_tree.find(xpath).append(element)

                update_nsmap = True

        # Schema with target namespace
        else:
            # Type without target namespace
            if type_target_namespace is None:
                # add include
                xsd_tree.getroot().insert(0, etree.Element("{}include".format(LXML_SCHEMA_NAMESPACE),
                                          attrib={'schemaLocation': include_url}))
                # add element
                xsd_tree.find(xpath).append(etree.Element("{}element".format(LXML_SCHEMA_NAMESPACE),
                                            attrib={'name': client_type_name,
                                                    'type': ns_type_name}))
            # Type with target namespace
            else:
                # Same target namespace as base template
                if target_namespace == type_target_namespace:
                    # add include
                    xsd_tree.getroot().insert(0, etree.Element("{}include".format(LXML_SCHEMA_NAMESPACE),
                                              attrib={'schemaLocation': include_url}))
                    # add element
                    xsd_tree.find(xpath).append(etree.Element("{}element".format(LXML_SCHEMA_NAMESPACE),
                                                attrib={'name': client_type_name,
                                                        'type': ns_type_name}))
                # Different target namespace as base template
                else:
                    # add import
                    xsd_tree.getroot().insert(0, etree.Element("{}import".format(LXML_SCHEMA_NAMESPACE),
                                              attrib={'schemaLocation': include_url,
                                                      'namespace': type_target_namespace}))
                    # create the element to add
                    element = etree.Element("{}element".format(LXML_SCHEMA_NAMESPACE),
                                            attrib={'name': client_type_name,
                                                    'type': ns_type_name},
                                            )
                    # add the element
                    xsd_tree.find(xpath).append(element)

                    update_nsmap = True

        # add the id of the type if not already present
        if include_url not in request.session['includedTypesCompose']:
            request.session['includedTypesCompose'].append(include_url)

        if update_nsmap:
            root = xsd_tree.getroot()
            root_nsmap = root.nsmap

            if type_target_namespace_prefix in root_nsmap.keys() and\
                            root_nsmap[type_target_namespace_prefix] != type_target_namespace:
                raise MDCSError('The namespace prefix is already declared for a different namespace.')
            else:
                root_nsmap[type_target_namespace_prefix] = type_target_namespace
                new_root = etree.Element(root.tag, nsmap=root_nsmap)
                new_root[:] = root[:]

                # validate XML schema
                error = validate_xml_schema(new_root)

                new_xsd_str = etree.tostring(new_root)

        else:
            # validate XML schema
            error = validate_xml_schema(xsd_tree)
            new_xsd_str = etree.tostring(xsd_tree)

        if error is not None:
            raise MDCSError(error)

        # save the tree in the session
        request.session['newXmlTemplateCompose'] = new_xsd_str
    except Exception, e:
        return HttpResponseBadRequest(e.message, content_type='application/javascript')
예제 #28
0
 def test_delete_type_no_dependencies(self):
     type = self.createType()
     delete_type(str(type.id))
     self.assertEquals(len(Type.objects()), 0)
     self.assertEquals(len(TypeVersion.objects()), 0)
예제 #29
0
def getListTypeDependenciesType(object_id):
    from mgi.models import Type
    return list(Type.objects(dependencies=object_id))