Exemple #1
0
 def create_generic_empty_content(self,
                                  context,
                                  request: TracimRequest,
                                  hapic_data=None) -> ContentInContext:
     """
     Creates a generic empty content. The minimum viable content has a label and a content type.
     Creating a content generally starts with a request to this endpoint.
     For specific contents like files, it is recommended to use the dedicated endpoint.
     This feature is accessible to contributors and higher role only.
     """
     app_config = request.registry.settings["CFG"]  # type: CFG
     creation_data = hapic_data.body
     api = ContentApi(current_user=request.current_user,
                      session=request.dbsession,
                      config=app_config)
     parent = None
     if creation_data.parent_id:
         try:
             parent = api.get_one(content_id=creation_data.parent_id,
                                  content_type=content_type_list.Any_SLUG)
         except ContentNotFound as exc:
             raise ParentNotFound(
                 "Parent with content_id {} not found".format(
                     creation_data.parent_id)) from exc
     content = api.create(
         label=creation_data.label,
         content_type_slug=creation_data.content_type,
         workspace=request.current_workspace,
         parent=parent,
     )
     api.save(content, ActionDescription.CREATION)
     api.execute_created_content_actions(content)
     content = api.get_content_in_context(content)
     return content
Exemple #2
0
 def create_file(self, context, request: TracimRequest, hapic_data=None):
     """
     Create a file .This will create 2 new revision.
     """
     # INFO - G.M - 2019-09-03 - check validation of file here, because marshmallow
     # required doesn't work correctly with cgi_fieldstorage.
     # check is done with None because cgi_fieldstorage cannot be converted to bool
     if hapic_data.files.files is None:
         raise NoFileValidationError(
             'No file "files" given at input, validation failed.')
     app_config = request.registry.settings["CFG"]  # type: CFG
     api = ContentApi(
         show_archived=True,
         show_deleted=True,
         current_user=request.current_user,
         session=request.dbsession,
         config=app_config,
     )
     api.check_upload_size(request.content_length,
                           request.current_workspace)
     _file = hapic_data.files.files
     parent_id = hapic_data.forms.parent_id
     parent = None  # type: typing.Optional['Content']
     if parent_id:
         try:
             parent = api.get_one(content_id=parent_id,
                                  content_type=content_type_list.Any_SLUG)
         except ContentNotFound as exc:
             raise ParentNotFound(
                 "Parent with content_id {} not found".format(
                     parent_id)) from exc
     content = api.create(
         filename=_file.filename,
         content_type_slug=FILE_TYPE,
         workspace=request.current_workspace,
         parent=parent,
     )
     api.save(content, ActionDescription.CREATION)
     with new_revision(session=request.dbsession,
                       tm=transaction.manager,
                       content=content):
         api.update_file_data(
             content,
             new_filename=_file.filename,
             new_mimetype=_file.type,
             new_content=_file.file,
         )
     api.execute_created_content_actions(content)
     return api.get_content_in_context(content)
Exemple #3
0
    def create_file(self, context, request: TracimRequest, hapic_data=None):
        """
        Create a file .This will create 2 new revision.
        """
        app_config = request.registry.settings['CFG']
        api = ContentApi(
            show_archived=True,
            show_deleted=True,
            current_user=request.current_user,
            session=request.dbsession,
            config=app_config,
        )
        _file = hapic_data.files.files
        parent_id = hapic_data.forms.parent_id
        api = ContentApi(
            current_user=request.current_user,
            session=request.dbsession,
            config=app_config
        )

        parent = None  # type: typing.Optional['Content']
        if parent_id:
            try:
                parent = api.get_one(content_id=parent_id, content_type=content_type_list.Any_SLUG)  # nopep8
            except ContentNotFound as exc:
                raise ParentNotFound(
                    'Parent with content_id {} not found'.format(parent_id)
                ) from exc
        content = api.create(
            filename=_file.filename,
            content_type_slug=FILE_TYPE,
            workspace=request.current_workspace,
            parent=parent,
        )
        api.save(content, ActionDescription.CREATION)
        with new_revision(
                session=request.dbsession,
                tm=transaction.manager,
                content=content
        ):
            api.update_file_data(
                content,
                new_filename=_file.filename,
                new_mimetype=_file.type,
                new_content=_file.file,
            )

        return api.get_content_in_context(content)
Exemple #4
0
 def create_file_from_template(
         self,
         context: DefaultRootFactory,
         request: TracimRequest,
         hapic_data: HapicData = None) -> ContentInContext:
     app_config = request.registry.settings["CFG"]  # type: CFG
     api = ContentApi(current_user=request.current_user,
                      session=request.dbsession,
                      config=app_config)
     collaborative_document_edition_api = CollaborativeDocumentEditionFactory(
     ).get_lib(current_user=request.current_user,
               session=request.dbsession,
               config=app_config)
     collaborative_document_edition_api.check_template_available(
         hapic_data.body.template)
     parent = None  # type: typing.Optional['Content']
     if hapic_data.body.parent_id:
         try:
             parent = api.get_one(content_id=hapic_data.body.parent_id,
                                  content_type=content_type_list.Any_SLUG)
         except ContentNotFound as exc:
             raise ParentNotFound(
                 "Parent with content_id {} not found".format(
                     hapic_data.body.parent_id)) from exc
     content = api.create(
         filename=hapic_data.body.filename,
         content_type_slug=FILE_TYPE,
         workspace=request.current_workspace,
         parent=parent,
     )
     api.save(content, ActionDescription.CREATION)
     with new_revision(session=request.dbsession,
                       tm=transaction.manager,
                       content=content):
         collaborative_document_edition_api.update_content_from_template(
             content=content, template_filename=hapic_data.body.template)
     api.execute_created_content_actions(content)
     return api.get_content_in_context(content)