Esempio n. 1
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')
 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)
Esempio n. 3
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)
Esempio n. 4
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'))))
Esempio n. 5
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
Esempio n. 6
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))
Esempio n. 7
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))
 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()))
Esempio n. 9
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()))
Esempio n. 10
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))
Esempio n. 11
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')
Esempio n. 12
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_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')
 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())
Esempio n. 15
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())
Esempio n. 16
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')
Esempio n. 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)
Esempio n. 18
0
    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")
Esempio n. 19
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')
Esempio n. 20
0
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')
Esempio n. 21
0
File: views.py Progetto: 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')
Esempio n. 22
0
def save_version(request):
    print 'BEGIN def saveVersion(request, objectType)'

    versionFilename = None
    versionContent = None
    objectVersionID = None
    objectType = None

    if ('uploadVersionValid' in request.session
            and request.session['uploadVersionValid'] == True
            and 'uploadVersionID' in request.session
            and request.session['uploadVersionID'] is not None
            and 'uploadVersionType' in request.session
            and request.session['uploadVersionType'] is not None
            and 'uploadVersionFilename' in request.session
            and request.session['uploadVersionFilename'] is not None
            and 'uploadVersionContent' in request.session
            and request.session['uploadVersionContent'] is not None):
        versionFilename = request.session['uploadVersionFilename']
        versionContent = request.session['uploadVersionContent']
        objectVersionID = request.session['uploadVersionID']
        objectType = request.session['uploadVersionType']
        if 'uploadVersionFlat' in request.session and request.session[
                'uploadVersionFlat'] is not None:
            versionFlat = request.session['uploadVersionFlat']
        else:
            versionFlat = None
        if 'uploadVersionAPIurl' in request.session and request.session[
                'uploadVersionAPIurl'] is not None:
            versionApiurl = request.session['uploadVersionAPIurl']
        else:
            versionApiurl = None
        if 'uploadDependencies' in request.session and request.session[
                'uploadDependencies'] is not None:
            dependencies = request.session['uploadDependencies']
        else:
            dependencies = None

        hash = XSDhash.get_hash(versionContent)
        # save the object
        if objectType == "Template":
            objectVersions = TemplateVersion.objects.get(pk=objectVersionID)
            objectVersions.nbVersions += 1
            object = Template.objects.get(pk=objectVersions.current)
            newObject = Template(title=object.title,
                                 filename=versionFilename,
                                 content=versionContent,
                                 version=objectVersions.nbVersions,
                                 templateVersion=objectVersionID,
                                 hash=hash).save()
        elif objectType == "Type":
            objectVersions = TypeVersion.objects.get(pk=objectVersionID)
            objectVersions.nbVersions += 1
            object = Type.objects.get(pk=objectVersions.current)
            newObject = Type(title=object.title,
                             filename=versionFilename,
                             content=versionContent,
                             version=objectVersions.nbVersions,
                             typeVersion=objectVersionID,
                             hash=hash).save()

        objectVersions.versions.append(str(newObject.id))
        objectVersions.save()

        if versionFlat is not None and versionApiurl is not None and dependencies is not None:
            MetaSchema(schemaId=str(newObject.id),
                       flat_content=versionFlat,
                       api_content=versionApiurl).save()
            object.dependencies = dependencies
            object.save()

        clearVersion(request)
    else:
        response_dict = {'errors': 'True'}
        return HttpResponse(json.dumps(response_dict),
                            content_type='application/javascript')

    return HttpResponse(json.dumps({}), content_type='application/javascript')
Esempio n. 23
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 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)
Esempio n. 25
0
    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')
Esempio n. 26
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)
        # 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')
Esempio n. 27
0
    flattener = XSDFlattenerMDCS(content)
    flatStr = flattener.get_flat()
    flatTree = etree.fromstring(flatStr)

    try:
        # is it a valid XML schema ?
        xmlSchema = etree.XMLSchema(flatTree)
    except Exception, e:
        dajax.script("""
            $("#new-type-error").html("<font color='red'>Not a valid XML schema.</font><br/>"""
                     + e.message.replace("'", "") + """ ");
        """)
        return dajax.json()

    type = Type(title=typeName,
                filename=typeName,
                content=content,
                user=request.user.id)
    type.save()
    MetaSchema(schemaId=str(type.id),
               flat_content=flatStr,
               api_content=content).save()

    dajax.script("""
        saveTemplateCallback();
        $("#dialog-save-type").dialog("close");
    """)
    return dajax.json()


################################################################################
#
Esempio n. 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)
Esempio n. 29
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
Esempio n. 30
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')
Esempio n. 31
0
def getListTypeDependenciesType(object_id):
    from mgi.models import Type
    return list(Type.objects(dependencies=object_id))
Esempio n. 32
0
        xmlSchema = etree.XMLSchema(flatTree)
    except Exception, e:
        response_dict['errors'] = "Not a valid XML document."
        response_dict['message'] = 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)
    type = Type(title=type_name,
                filename=type_name,
                content=content,
                user=str(request.user.id),
                hash=hash,
                dependencies=dependencies)
    type.save()
    MetaSchema(schemaId=str(type.id),
               flat_content=flatStr,
               api_content=content).save()

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


################################################################################
#
# Function Name: get_occurrences(request)
# Inputs:        request - HTTP request
def getListTypeDependenciesType(object_id):
    from mgi.models import Type
    return list(Type.objects(dependencies=object_id))