コード例 #1
0
 def test_get_object_schema_for_image(self):
     self.portal.invokeFactory(
         'Image',
         id='image1',
         title='Image 1'
     )
     schema = [x[0] for x in get_object_schema(self.portal.image1)]
     self.assertEqual(
         set(schema),
         set([
             'image',
             'description',
             'title',
             'allow_discussion',
             'exclude_from_nav',
             'relatedItems',
             'meta_type',
             'isPrincipiaFolderish',
             'icon',
             'rights',
             'effective',
             'expires',
             'language',
             'subjects',
             'creators',
             'contributors',
             'rights',
         ])
     )
コード例 #2
0
 def test_get_object_schema_for_folder(self):
     self.portal.invokeFactory('Folder', id='folder1', title='Folder 1')
     schema = [x[0] for x in get_object_schema(self.portal.folder1)]
     self.assertEqual(
         set(schema),
         set([
             'title',
             'allow_discussion',
             'exclude_from_nav',
             'relatedItems',
             'nextPreviousEnabled',
             'isAnObjectManager',
             'meta_type',
             'meta_types',
             'isPrincipiaFolderish',
             'icon',
             'rights',
             'contributors',
             'effective',
             'expires',
             'language',
             'subjects',
             'creators',
             'description'
         ])
     )
コード例 #3
0
 def test_get_object_schema_for_document(self):
     self.portal.invokeFactory('Document', id='doc1', title='Doc 1')
     schema = [x[0] for x in get_object_schema(self.portal.doc1)]
     self.assertEqual(
         set(schema),
         set([
             'text',
             'title',
             'allow_discussion',
             'exclude_from_nav',
             'relatedItems',
             'table_of_contents',
             'meta_type',
             'isPrincipiaFolderish',
             'icon',
             'rights',
             'contributors',
             'effective',
             'expires',
             'language',
             'subjects',
             'creators',
             'description',
             'changeNote'
         ])
     )
コード例 #4
0
ファイル: adapter.py プロジェクト: AnneGilles/plone.restapi
def SerializeToJson(context):
    result = {
        '@context': 'http://www.w3.org/ns/hydra/context.jsonld',
        '@id': context.absolute_url(),
        '@type': context.portal_type,
        'parent': {
            '@id': aq_parent(aq_inner(context)).absolute_url(),
            'title': aq_parent(aq_inner(context)).title,
            'description': aq_parent(aq_inner(context)).description
        }
    }
    if IFolderish.providedBy(context):
        result['member'] = [
            {
                '@id': member.absolute_url(),
                'title': member.title,
                'description': member.description
            }
            for member_id, member in context.objectItems()
        ]
    if ICollection.providedBy(context):
        portal = getSite()
        result['member'] = [
            {
                '@id': '{0}/{1}'.format(
                    portal.absolute_url(),
                    '/'.join(member.getPhysicalPath())
                ),
                'title': member.title,
                'description': member.description
            }
            for member in context.results()
        ]
    for title, schema_object in get_object_schema(context):
        value = getattr(context, title, None)
        if value is not None:
            # RichText
            if isinstance(schema_object, RichText):
                result[title] = value.output
            # DateTime
            elif isinstance(schema_object, Datetime):
                # Return DateTime in ISO-8601 format. See
                # https://pypi.python.org/pypi/DateTime/3.0 and
                # http://www.w3.org/TR/NOTE-datetime for details.
                # XXX: We might want to change that in the future.
                result[title] = value().ISO8601()
            # Tuple
            elif isinstance(value, tuple):
                result[title] = list(value)
            # List
            elif isinstance(value, list):
                result[title] = value
            # String
            elif isinstance(value, str):
                result[title] = value
            # Unicode
            elif isinstance(value, unicode):
                result[title] = value
            # Image
            elif isinstance(value, NamedBlobImage):
                result[title] = '{0}/@@images/{1}'.format(
                    context.absolute_url(),
                    title
                )
            # File
            elif isinstance(value, NamedBlobFile):
                result[title] = '{0}/{1}'.format(
                    context.absolute_url(),
                    value.filename
                )
            else:
                result[title] = str(value)

    # Operations
    # result["operation"] = [
    #     {
    #         "@type": "CreateResourceOperation",
    #         "name": "Create Resource",
    #         "method": "POST",
    #         "expects": {
    #             "supportedProperty": [
    #                 {
    #                     "@type": "PropertyValueSpecification",
    #                     "hydra:property": "id",
    #                     "hydra:required": "true",
    #                     "readOnlyValue": "true"
    #                 },
    #                 {
    #                     "@type": "PropertyValueSpecification",
    #                     "hydra:property": "title",
    #                     "hydra:required": "true",
    #                     "readOnlyValue": "false"
    #                 },
    #             ],
    #         }
    #     },
    #     {
    #         "@type": "ReplaceResourceOperation",
    #         "name": "Update Resource",
    #         "method": "PUT",
    #     },
    #     {
    #         "@type": "DeleteResourceOperation",
    #         "name": "Delete Resource",
    #         "method": "DELETE",
    #     }
    # ]

    if getattr(context, 'request', False):
        if context.request.get('append_json_to_hyperlinks', False):
            result = append_json_to_links(result)
    return json.dumps(result, indent=2, sort_keys=True)