Example #1
0
def format_atom(request, results):
	if len(results)==0:	#FIXME
		return

	className = entityFactory.getClassName_fromInstance(results[0], False)
	classFullname = entityFactory.getClassName_fromInstance(results[0], True)

	atom_document = atom.data.Feed(
		title = atom.data.Title(className, type='text'),
		id = atom.data.Id(urllib.unquote(request.url)),
		link = [atom.data.Link(rel='self', title=className, href=className)]
		)
#        atom_document.updated = atom.data.Updated(atom.data.Date(text=datetime.datetime.now().isoformat()))

# the following 2 lines will help reduce the size of the response
	atom_document.attributes['{%s}dummy_atribute'%core.EDMX_METADATA_NAMESPACE] = 'x'
	atom_document.attributes['{%s}dummy_atribute'%core.ODATA_SERVICES_NAMESPACE] = 'x'

	for o in results:
		atom_document.entry.append(core.build_atom_for_entity(o, request.application_url))

	return atom_document
Example #2
0
def format_atom(request, results):
    if len(results) == 0:  #FIXME
        return

    className = entityFactory.getClassName_fromInstance(results[0], False)
    classFullname = entityFactory.getClassName_fromInstance(results[0], True)

    atom_document = atom.data.Feed(
        title=atom.data.Title(className, type='text'),
        id=atom.data.Id(urllib.unquote(request.url)),
        link=[atom.data.Link(rel='self', title=className, href=className)])
    #        atom_document.updated = atom.data.Updated(atom.data.Date(text=datetime.datetime.now().isoformat()))

    # the following 2 lines will help reduce the size of the response
    atom_document.attributes['{%s}dummy_atribute' %
                             core.EDMX_METADATA_NAMESPACE] = 'x'
    atom_document.attributes['{%s}dummy_atribute' %
                             core.ODATA_SERVICES_NAMESPACE] = 'x'

    for o in results:
        atom_document.entry.append(
            core.build_atom_for_entity(o, request.application_url))

    return atom_document
Example #3
0
def build_atom_for_entity(o, application_url):
    class_name = entityFactory.getClassName_fromInstance(o, False)
    class_fullname = entityFactory.getClassName_fromInstance(o, True)

    entry_key = o.key()

    properties = atom.data.ExtensionElement(tag="properties", namespace=EDMX_METADATA_NAMESPACE)

    # add field key
    prop = atom.core.XmlElement(text=str(entry_key))
    prop.tag = "key"
    prop.namespace = ODATA_SERVICES_NAMESPACE
    prop.attributes["{%s}type" % EDMX_METADATA_NAMESPACE] = TYPE_MAPPING[db.StringProperty]
    properties.extension_elements.append(prop)

    # add field key_name
    prop = atom.core.XmlElement(text=str(entry_key.name()))
    prop.tag = "key_name"
    prop.namespace = ODATA_SERVICES_NAMESPACE
    prop.attributes["{%s}type" % EDMX_METADATA_NAMESPACE] = TYPE_MAPPING[db.StringProperty]
    properties.extension_elements.append(prop)

    entry = atom.data.Entry(
        id=atom.data.Id(text="%s/%s/%s('%s')" % (application_url, BASE_SVC_URL, class_name, entry_key)),
        link=[atom.data.Link(rel="edit", title=class_name, href="%s('%s')" % (class_name, o.key()))],
        category=[
            atom.data.Category(
                term=class_fullname, scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme"
            )
        ],
        content=atom.data.Content(type="application/xml"),
    )

    # add other fields
    for field in o.fields():
        value = getattr(o, field)
        prop = atom.core.XmlElement()
        prop.tag = field
        prop.namespace = ODATA_SERVICES_NAMESPACE
        if value == None:
            prop.attributes[
                "{%s}null" % EDMX_METADATA_NAMESPACE
            ] = "true"  # atom.core.XmlAttribute(EDMX_METADATA_NAMESPACE, 'null'))
        else:
            field_class = o.fields()[field].__class__
            if field_class == db.ReferenceProperty:
                ref_class = o.fields()[field].reference_class.__name__
                entry.link.append(
                    atom.data.Link(
                        rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/%s" % ref_class,
                        type="application/atom+xml;type=entry",
                        title=ref_class,
                        href="%s('%s')" % (ref_class, value.key()),
                    )
                )

                field_class = db.StringProperty
                value = str(value.key())
                prop.tag = prop.tag + "__key__"

            elif field_class == db.StringListProperty:  # FIXME
                value = ",".join(value)

            prop.attributes["{%s}type" % EDMX_METADATA_NAMESPACE] = TYPE_MAPPING[field_class]
            prop.text = TYPE_TRANSFORM_FUNCTIONS[value.__class__](value)  # unicode(value)

        properties.extension_elements.append(prop)

    entry.content.extension_elements = [properties]

    return entry
Example #4
0
def build_atom_for_entity(o, application_url):
    class_name = entityFactory.getClassName_fromInstance(o, False)
    class_fullname = entityFactory.getClassName_fromInstance(o, True)

    entry_key = o.key()

    properties = atom.data.ExtensionElement(tag='properties',
                                            namespace=EDMX_METADATA_NAMESPACE)

    # add field key
    prop = atom.core.XmlElement(text=str(entry_key))
    prop.tag = 'key'
    prop.namespace = ODATA_SERVICES_NAMESPACE
    prop.attributes['{%s}type' %
                    EDMX_METADATA_NAMESPACE] = TYPE_MAPPING[db.StringProperty]
    properties.extension_elements.append(prop)

    # add field key_name
    prop = atom.core.XmlElement(text=str(entry_key.name()))
    prop.tag = 'key_name'
    prop.namespace = ODATA_SERVICES_NAMESPACE
    prop.attributes['{%s}type' %
                    EDMX_METADATA_NAMESPACE] = TYPE_MAPPING[db.StringProperty]
    properties.extension_elements.append(prop)

    entry = atom.data.Entry(
        id=atom.data.Id(
            text="%s/%s/%s('%s')" %
            (application_url, BASE_SVC_URL, class_name, entry_key)),
        link=[
            atom.data.Link(rel='edit',
                           title=class_name,
                           href="%s('%s')" % (class_name, o.key()))
        ],
        category=[
            atom.data.Category(
                term=class_fullname,
                scheme=
                "http://schemas.microsoft.com/ado/2007/08/dataservices/scheme")
        ],
        content=atom.data.Content(type='application/xml'))

    # add other fields
    for field in o.fields():
        value = getattr(o, field)
        prop = atom.core.XmlElement()
        prop.tag = field
        prop.namespace = ODATA_SERVICES_NAMESPACE
        if value == None:
            prop.attributes[
                '{%s}null' %
                EDMX_METADATA_NAMESPACE] = 'true'  #atom.core.XmlAttribute(EDMX_METADATA_NAMESPACE, 'null'))
        else:
            field_class = o.fields()[field].__class__
            if field_class == db.ReferenceProperty:
                ref_class = o.fields()[field].reference_class.__name__
                entry.link.append(
                    atom.data.Link(
                        rel=
                        'http://schemas.microsoft.com/ado/2007/08/dataservices/related/%s'
                        % ref_class,
                        type='application/atom+xml;type=entry',
                        title=ref_class,
                        href="%s('%s')" % (ref_class, value.key())))

                field_class = db.StringProperty
                value = str(value.key())
                prop.tag = prop.tag + '__key__'

            elif field_class == db.StringListProperty:  #FIXME
                value = ','.join(value)

            prop.attributes[
                '{%s}type' %
                EDMX_METADATA_NAMESPACE] = TYPE_MAPPING[field_class]
            prop.text = TYPE_TRANSFORM_FUNCTIONS[value.__class__](
                value)  #unicode(value)

        properties.extension_elements.append(prop)

    entry.content.extension_elements = [properties]

    return entry