Exemple #1
0
    def get(self, request):
        """ Get all user blobs

        /rest/blob/
        /rest/blob/?filename=<filename>

        Query Params:
            filename: filename

        Args:
            request:

        Returns:

        """
        try:
            # FIXME: remove?
            if request.user.is_superuser:
                blob_list = blob_api.get_all()
            else:
                blob_list = blob_api.get_all_by_user_id(user_id=request.user.id)

            # Apply filters
            filename = self.request.query_params.get('filename', None)
            if filename is not None:
                blob_list = blob_list.filter(filename=filename)

            # Serialize object
            serializer = BlobSerializer(blob_list, many=True, context={'request': request})

            # Return response
            return Response(serializer.data, status=status.HTTP_200_OK)
        except Exception as api_exception:
            content = {'message': api_exception.message}
            return Response(content, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
Exemple #2
0
    def _get_blobs(self, request):
        """ Retrieve blobs

        Args:
            request:

        Returns:

        """
        return blob_api.get_all(request.user)
Exemple #3
0
    def test_delete_deletes_one_blob_from_database(self):
        # Arrange
        user = create_mock_user("1")

        # Act
        response = RequestMock.do_request_delete(
            views.BlobDetail.as_view(), user, param={"pk": str(self.fixture.blob_1.id)}
        )

        # Assert
        self.assertEqual(len(blob_api.get_all()), 2)
Exemple #4
0
    def test_post_adds_an_entry_in_database(self):
        # Arrange
        user = create_mock_user("1")

        # Act
        response = RequestMock.do_request_post(
            views.BlobList.as_view(), user, data=self.data
        )

        # Assert
        self.assertEqual(len(blob_api.get_all()), 4)
Exemple #5
0
    def get(self, request):
        """ Get all user Blob

        Url Parameters:

            filename: document_filename

        Examples:

            ../blob/
            ../blob?filename=[filename]

        Args:

            request: HTTP request

        Returns:

            - code: 200
              content: List of blob
            - code: 500
              content: Internal server error
        """
        try:
            # FIXME: remove?
            if request.user.is_superuser:
                blob_list = blob_api.get_all()
            else:
                blob_list = blob_api.get_all_by_user_id(
                    user_id=request.user.id)

            # Apply filters
            filename = self.request.query_params.get('filename', None)
            if filename is not None:
                blob_list = blob_list.filter(filename=filename)

            # Serialize object
            serializer = BlobSerializer(blob_list,
                                        many=True,
                                        context={'request': request})

            # Return response
            return Response(serializer.data, status=status.HTTP_200_OK)
        except Exception as api_exception:
            content = {'message': api_exception.message}
            return Response(content,
                            status=status.HTTP_500_INTERNAL_SERVER_ERROR)
    def get(self, request, *args, **kwargs):
        """ Method GET

        Args:
            request:
            args:
            kwargs:

        Returns:
        """
        if self.administration:
            files = blob_api.get_all()
        else:
            files = blob_api.get_all_by_user_id(request.user.id)

        detailed_file = []
        for file in files:
            detailed_file.append({
                'user':
                user_api.get_user_by_id(file.user_id).username,
                'date':
                file.id.generation_time,
                'file':
                file,
                'url':
                blob_utils.get_blob_download_uri(file, request)
            })

        context = {
            'administration': self.administration,
            'number_total': len(files),
            'user_data': detailed_file,
            'document': dashboard_constants.FUNCTIONAL_OBJECT_ENUM.FILE,
            'template': dashboard_constants.DASHBOARD_FILES_TEMPLATE_TABLE,
            'menu': self.administration,
        }

        if self.administration:
            context.update(
                {'action_form': ActionForm([('1', 'Delete selected files')])})

        modals = [dashboard_constants.MODALS_COMMON_DELETE]

        assets = {
            "css":
            dashboard_constants.CSS_COMMON,
            "js": [{
                "path": 'core_dashboard_common_app/user/js/init.raw.js',
                "is_raw": True
            }, {
                "path": dashboard_constants.JS_COMMON_FUNCTION_DELETE,
                "is_raw": False
            }, {
                "path": dashboard_constants.JS_USER_SELECTED_ELEMENT,
                "is_raw": True
            }]
        }

        # Admin
        if self.administration:
            assets['js'].append({
                "path":
                'core_dashboard_common_app/common/js/init_pagination.js',
                "is_raw": False
            })
            assets['js'].append({
                "path": dashboard_constants.JS_ADMIN_ACTION_DASHBOARD,
                "is_raw": True
            })
            assets['js'].append({
                "path": dashboard_constants.JS_ADMIN_COUNT_CHECK,
                "is_raw": True
            })
            assets['js'].append({
                "path": dashboard_constants.JS_ADMIN_RESET_CHECKBOX,
                "is_raw": True
            })
            assets['js'].append({
                "path": dashboard_constants.JS_ADMIN_SELECT_ALL,
                "is_raw": True
            })
            assets['js'].append({
                "path": dashboard_constants.JS_ADMIN_SELETED_ELEMENT,
                "is_raw": False
            })
            assets['js'].append({
                "path": dashboard_constants.JS_ADMIN_INIT_MENU,
                "is_raw": False
            })

        return self.common_render(request,
                                  self.template,
                                  context=context,
                                  assets=assets,
                                  modals=modals)
Exemple #7
0
 def test_get_all_as_user_raises_error(self):
     mock_user = _create_user('1')
     with self.assertRaises(AccessControlError):
         blob_api.get_all(mock_user)
Exemple #8
0
 def test_get_all_as_superuser_returns_all_blob(self):
     mock_user = _create_user('1', is_superuser=True)
     data_list = blob_api.get_all(mock_user)
     self.assertTrue(len(data_list) == len(self.fixture.blob_collection))
    def get(self, request, *args, **kwargs):
        """Method GET

        Args:
            request:
            args:
            kwargs:

        Returns:
        """
        if self.administration:
            try:
                files = blob_api.get_all(request.user)

            except AccessControlError as ace:
                files = blob_api.get_none()
        else:
            files = blob_api.get_all_by_user(request.user)

        # Paginator
        page = request.GET.get("page", 1)
        results_paginator = ResultsPaginator.get_results(
            files, page, settings.FILE_PER_PAGE_PAGINATION
        )
        detailed_file = []
        for file in results_paginator:
            try:
                username = user_api.get_user_by_id(file.user_id).username
            except ObjectDoesNotExist:
                username = "******"
            detailed_file.append(
                {
                    "user": username,
                    "date": file.id.generation_time,
                    "file": file,
                    "url": blob_utils.get_blob_download_uri(file, request),
                    "can_change_workspace": check_if_workspace_can_be_changed(file),
                    "is_owner": True,
                }
            )

        # Add user_form for change owner
        user_form = UserForm(request.user)
        context = {
            "administration": self.administration,
            "number_total": files.count(),
            "user_data": detailed_file,
            "user_form": user_form,
            "document": dashboard_constants.FUNCTIONAL_OBJECT_ENUM.FILE.value,
            "template": dashboard_constants.DASHBOARD_FILES_TEMPLATE_TABLE,
            "menu": self.administration,
            "share_pid_button": "core_linked_records_app" in settings.INSTALLED_APPS,
            "pagination": _get_pagination_document(
                page,
                results_paginator,
                files.count(),
                settings.FILE_PER_PAGE_PAGINATION,
            ),
        }

        if self.administration:
            context.update(
                {
                    "action_form": ActionForm(
                        [
                            ("1", "Delete selected files"),
                            ("2", "Change owner of selected files"),
                        ]
                    )
                }
            )

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

        assets = {
            "css": dashboard_constants.CSS_COMMON,
            "js": [
                {
                    "path": "core_dashboard_common_app/user/js/init.raw.js",
                    "is_raw": True,
                },
                {
                    "path": dashboard_constants.JS_COMMON_FUNCTION_DELETE,
                    "is_raw": False,
                },
                {"path": dashboard_constants.JS_USER_SELECTED_ELEMENT, "is_raw": True},
                {
                    "path": "core_main_app/user/js/workspaces/list/modals/assign_workspace.js",
                    "is_raw": False,
                },
                {
                    "path": "core_main_app/user/js/workspaces/list/modals/assign_blob_workspace.raw.js",
                    "is_raw": True,
                },
                {
                    "path": dashboard_constants.JS_COMMON_FUNCTION_CHANGE_OWNER,
                    "is_raw": False,
                },
                {
                    "path": "core_dashboard_common_app/common/js/init_pagination.js",
                    "is_raw": False,
                },
            ],
        }

        if "core_file_preview_app" in INSTALLED_APPS:
            assets["js"].extend(
                [
                    {
                        "path": "core_file_preview_app/user/js/file_preview.js",
                        "is_raw": False,
                    }
                ]
            )
            assets["css"].append("core_file_preview_app/user/css/file_preview.css")
            modals.append("core_file_preview_app/user/file_preview_modal.html")

        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/blob_list.js",
                    "is_raw": False,
                },
            ]

        # Admin
        if self.administration:
            assets["js"].append(
                {
                    "path": "core_dashboard_common_app/common/js/init_pagination.js",
                    "is_raw": False,
                }
            )
            assets["js"].append(
                {"path": dashboard_constants.JS_ADMIN_ACTION_DASHBOARD, "is_raw": True}
            )
            assets["js"].append(
                {"path": dashboard_constants.JS_ADMIN_COUNT_CHECK, "is_raw": True}
            )
            assets["js"].append(
                {"path": dashboard_constants.JS_ADMIN_RESET_CHECKBOX, "is_raw": True}
            )
            assets["js"].append(
                {"path": dashboard_constants.JS_ADMIN_SELECT_ALL, "is_raw": True}
            )
            assets["js"].append(
                {"path": dashboard_constants.JS_ADMIN_SELETED_ELEMENT, "is_raw": False}
            )
            assets["js"].append(
                {"path": dashboard_constants.JS_ADMIN_INIT_MENU, "is_raw": False}
            )

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