예제 #1
0
    def test_read_write_hyperlinked_collection_property(self):
        request = Request(make_dummy_request())
        request._user = User(is_superuser=True)

        context = {'request': request}
        serializer = ProjectSerializer(self.project, context=context)

        field = serializer.get_fields()['notes']
        property_serializer = HydraPropertySerializer(field,
                                                      'notes',
                                                      'projectns:',
                                                      self.project,
                                                      context=context)

        supported_operations = property_serializer.data\
            .get('property')\
            .get('hydra:supportedOperation')

        self.assertEqual(len(supported_operations), 2)

        create_operation, = filter(lambda op: op.get('hydra:method') == 'POST',
                                   supported_operations)

        self.assertDictEqual(
            dict(create_operation), {
                '@id': '_:project_note_create',
                '@type': 'hydra:CreateResourceOperation',
                'label': 'Create a note for this project.',
                'description': None,
                'hydra:method': 'POST',
                'hydra:expects': 'projectns:Note',
                'hydra:returns': 'projectns:Note',
                'hydra:possibleStatus': []
            })
예제 #2
0
    def test_read_write_hyperlinked_collection_property(self):
        request = Request(make_dummy_request())
        request._user = User(username='******', is_superuser=True)

        context = {'request': request}
        serializer = ProjectSerializer(self.project, context=context)

        field = serializer.get_fields()['notes']
        property_serializer = HydraPropertySerializer(
            field, 'notes',
            'projectns:',
            self.project,
            context=context
        )

        supported_operations = property_serializer.data\
            .get('property')\
            .get('hydra:supportedOperation')

        self.assertEqual(len(supported_operations), 2)

        create_operation, = filter(lambda op: op.get('hydra:method') == 'POST',
                                   supported_operations)

        self.assertDictEqual(dict(create_operation), {
            '@id': '_:project_note_create',
            '@type': 'hydra:CreateResourceOperation',
            'label': 'Create a note for this project.',
            'description': None,
            'hydra:method': 'POST',
            'hydra:expects': 'projectns:Note',
            'hydra:returns': 'projectns:Note',
            'hydra:possibleStatus': []
        })
예제 #3
0
    def test_read_write_hyperlinked_collection_property(self):
        request = Request(make_dummy_request())
        request._user = User(is_superuser=True)

        context = {"request": request}
        serializer = ProjectSerializer(self.project, context=context)

        field = serializer.get_fields()["notes"]
        property_serializer = HydraPropertySerializer(field, "notes", "projectns:", self.project, context=context)

        supported_operations = property_serializer.data.get("property").get("hydra:supportedOperation")

        self.assertEqual(len(supported_operations), 2)

        create_operation, = [op for op in supported_operations if op.get("hydra:method") == "POST"]

        self.assertDictEqual(
            dict(create_operation),
            {
                "@id": "_:project_note_create",
                "@type": "hydra:CreateResourceOperation",
                "label": "Create a note for this project.",
                "description": None,
                "hydra:method": "POST",
                "hydra:expects": "projectns:Note",
                "hydra:returns": "projectns:Note",
                "hydra:possibleStatus": [],
            },
        )
예제 #4
0
    def test_read_only_hyperlinked_collection_property(self):
        request = Request(make_dummy_request())
        context = {'request': request}
        serializer = ProjectSerializer(self.project, context=context)
        field = serializer.get_fields()['notes']

        property_serializer = HydraPropertySerializer(field,
                                                      'notes',
                                                      'projectns:',
                                                      self.project,
                                                      context=context)

        serialized_property = dict(property_serializer.data.items())
        hydra_property = dict(serialized_property.pop('property').items())

        self.assertDictEqual(
            hydra_property, {
                '@id':
                'projectns:Project/notes',
                '@type':
                'hydra:Link',
                'label':
                'notes',
                'hydra:description':
                'Notes for this project.',
                'domain':
                'projectns:Project',
                'range':
                'hydra:Collection',
                'hydra:supportedOperation': [{
                    '@id': '_:project_notes_retrieve',
                    '@type': 'hydra:Operation',
                    'label': 'Retrieve all notes for this project.',
                    'description': None,
                    'hydra:method': 'GET',
                    'hydra:expects': None,
                    'hydra:returns': 'hydra:Collection',
                    'hydra:possibleStatus': []
                }]
            })

        self.assertDictEqual(
            serialized_property, {
                'hydra:description': 'Notes for this project.',
                'hydra:title': 'notes',
                'hydra:required': False,
                'hydra:readonly': True,
                'hydra:writeonly': False
            })
예제 #5
0
    def test_read_only_hyperlinked_collection_property(self):
        request = Request(make_dummy_request())
        context = {"request": request}
        serializer = ProjectSerializer(self.project, context=context)
        field = serializer.get_fields()["notes"]

        property_serializer = HydraPropertySerializer(field, "notes", "projectns:", self.project, context=context)

        serialized_property = dict(list(property_serializer.data.items()))
        hydra_property = dict(list(serialized_property.pop("property").items()))

        self.assertDictEqual(
            hydra_property,
            {
                "@id": "projectns:Project/notes",
                "@type": "hydra:Link",
                "label": "notes",
                "hydra:description": "Notes for this project.",
                "domain": "projectns:Project",
                "range": "hydra:Collection",
                "hydra:supportedOperation": [
                    {
                        "@id": "_:project_notes_retrieve",
                        "@type": "hydra:Operation",
                        "label": "Retrieve all notes for this project.",
                        "description": None,
                        "hydra:method": "GET",
                        "hydra:expects": None,
                        "hydra:returns": "hydra:Collection",
                        "hydra:possibleStatus": [],
                    }
                ],
            },
        )

        self.assertDictEqual(
            serialized_property,
            {
                "hydra:description": "Notes for this project.",
                "hydra:title": "notes",
                "hydra:required": False,
                "hydra:readonly": True,
                "hydra:writeonly": False,
            },
        )
예제 #6
0
    def test_read_only_hyperlinked_collection_property(self):
        request = Request(make_dummy_request())
        context = {'request': request}
        serializer = ProjectSerializer(self.project, context=context)
        field = serializer.get_fields()['notes']

        property_serializer = HydraPropertySerializer(
            field, 'notes',
            'projectns:',
            self.project,
            context=context
        )

        serialized_property = dict(property_serializer.data.items())
        hydra_property = dict(serialized_property.pop('property').items())

        self.assertDictEqual(hydra_property, {
            '@id': 'projectns:Project/notes',
            '@type': 'hydra:Link',
            'label': 'notes',
            'hydra:description': 'Notes for this project.',
            'domain': 'projectns:Project',
            'range': 'hydra:Collection',
            'hydra:supportedOperation': [
                {
                    '@id': '_:project_notes_retrieve',
                    '@type': 'hydra:Operation',
                    'label': 'Retrieve all notes for this project.',
                    'description': None,
                    'hydra:method': 'GET',
                    'hydra:expects': None,
                    'hydra:returns': 'hydra:Collection',
                    'hydra:possibleStatus': []
                }
            ]
        })

        self.assertDictEqual(serialized_property, {
            'hydra:description': 'Notes for this project.',
            'hydra:title': 'notes',
            'hydra:required': False,
            'hydra:readonly': True,
            'hydra:writeonly': False
        })
예제 #7
0
    def filter_search(self, request, search, view):
        params = request.query_params

        if not (hasattr(request, 'project') or 'project' in params):
            return search

        request_project = getattr(request, 'project', None)

        # If in debug mode, always use a request based off of the base URL that
        # ES uses to index things, since it might be different from the passed
        # request URL. In production, requests should always be proxied and get
        # the same host.
        if settings.DEBUG:
            request = make_dummy_request()

        project_url = (request.build_absolute_uri(
            request_project.get_absolute_url())
                       if request_project else params['project'])

        return search.filter('term', **{'serialized.project': project_url})
예제 #8
0
    def filter_search(self, request, search, view):
        params = request.query_params

        if not (hasattr(request, 'project') or 'project' in params):
            return search

        request_project = getattr(request, 'project', None)

        # If in debug mode, always use a request based off of the base URL that
        # ES uses to index things, since it might be different from the passed
        # request URL. In production, requests should always be proxied and get
        # the same host.
        if settings.DEBUG:
            request = make_dummy_request()

        project_url = (
            request.build_absolute_uri(request_project.get_absolute_url())
            if request_project
            else params['project']
        )

        return search.filter('term', **{'serialized.project': project_url})