예제 #1
0
 def test_data_get_all_api_without_sorting_param_use_default_data_sorting_setting(self):
     # Arrange
     mock_user = _create_user('1', is_superuser=True)
     # Act
     data = data_api.get_all(mock_user)
     # Assert
     self.assertListEqual(list(data), self.fixture.data_collection)
예제 #2
0
    def get(self, request, *args, **kwargs):

        # Get records
        if self.administration:
            try:
                loaded_data = data_api.get_all(request.user,
                                               '-last_modification_date')
            except AccessControlError, ace:
                loaded_data = []
예제 #3
0
 def test_get_all_as_user_raises_error(self):
     mock_user = _create_user('1')
     with self.assertRaises(AccessControlError):
         data_api.get_all(mock_user)
예제 #4
0
 def test_get_all_as_superuser_returns_all_data(self):
     mock_user = _create_user('1', is_superuser=True)
     data_list = data_api.get_all(mock_user)
     self.assertTrue(len(data_list) == len(self.fixture.data_collection))
예제 #5
0
    def get(self, request):
        """Get all Data

        Url Parameters:

            user: user_id
            workspace: workspace_id
            template: template_id
            title: document_title

        Examples:

            ../data/
            ../data?user=[user_id]
            ../data?workspace=[workspace_id]
            ../data?template=[template_id]
            ../data?title=[document_title]
            ../data?template=[template_id]&title=[document_title]

        Args:

            request: HTTP request

        Returns:

            - code: 200
              content: List of data
            - code: 500
              content: Internal server error
        """
        if not request.user.is_superuser:
            content = {"message": "Only a superuser can use this feature."}
            return Response(content, status=status.HTTP_403_FORBIDDEN)
        try:
            # Get object
            data_object_list = data_api.get_all(request.user)

            # Apply filters
            user = self.request.query_params.get("user", None)
            if user is not None:
                data_object_list = data_object_list.filter(user_id=user)

            workspace = self.request.query_params.get("workspace", None)
            if workspace is not None:
                data_object_list = data_object_list.filter(workspace=workspace)

            template = self.request.query_params.get("template", None)
            if template is not None:
                data_object_list = data_object_list.filter(template=template)

            title = self.request.query_params.get("title", None)
            if title is not None:
                data_object_list = data_object_list.filter(title=title)

            # Serialize object
            data_serializer = self.serializer(data_object_list, many=True)

            # Return response
            return Response(data_serializer.data, status=status.HTTP_200_OK)
        except Exception as api_exception:
            content = {"message": str(api_exception)}
            return Response(content,
                            status=status.HTTP_500_INTERNAL_SERVER_ERROR)
예제 #6
0
    def get(self, request, *args, **kwargs):

        # Get records
        if self.administration:
            try:
                loaded_data = data_api.get_all(request.user)

            except AccessControlError as ace:
                loaded_data = data_api.get_none()

        else:
            loaded_data = data_api.get_all_by_user(request.user)

        # Paginator
        page = request.GET.get("page", 1)
        results_paginator = ResultsPaginator.get_results(
            loaded_data, page, settings.RECORD_PER_PAGE_PAGINATION
        )

        # Data context
        try:
            results_paginator.object_list = self._format_data_context(
                results_paginator.object_list
            )
        except:
            results_paginator.object_list = []

        # Add user_form for change owner
        user_form = UserForm(request.user)
        context = {
            "number_total": loaded_data.count(),
            "user_data": results_paginator,
            "user_form": user_form,
            "document": self.document,
            "template": self.data_template,
            "action_form": ActionForm(
                [
                    ("1", "Delete selected records"),
                    ("2", "Change owner of selected records"),
                ]
            ),
            "menu": self.administration,
            "administration": self.administration,
            "share_pid_button": "core_linked_records_app" in settings.INSTALLED_APPS,
        }

        if self.administration:
            context.update(
                {
                    "username_list": get_id_username_dict(user_api.get_all_users()),
                }
            )

        modals = [
            "core_main_app/user/workspaces/list/modals/assign_workspace.html",
            dashboard_constants.MODALS_COMMON_DELETE,
            dashboard_constants.MODALS_COMMON_CHANGE_OWNER,
        ]

        assets = self._get_assets()

        if context["share_pid_button"]:
            modals.append("core_linked_records_app/user/sharing/data_detail/modal.html")

            assets["js"] += [
                {
                    "path": "core_main_app/user/js/sharing_modal.js",
                    "is_raw": False,
                },
                {
                    "path": "core_linked_records_app/user/js/sharing/common_list.js",
                    "is_raw": False,
                },
                {
                    "path": "core_linked_records_app/user/js/sharing/data_list.js",
                    "is_raw": False,
                },
            ]

        return self.common_render(
            request, self.template, context=context, assets=assets, modals=modals
        )