def delete_comment(self, context, request: TracimRequest, hapic_data=None): """ Delete comment """ app_config = request.registry.settings['CFG'] api = ContentApi( current_user=request.current_user, session=request.dbsession, config=app_config, ) wapi = WorkspaceApi( current_user=request.current_user, session=request.dbsession, config=app_config, ) workspace = wapi.get_one(hapic_data.path.workspace_id) parent = api.get_one(hapic_data.path.content_id, content_type=ContentType.Any, workspace=workspace) comment = api.get_one( hapic_data.path.comment_id, content_type=ContentType.Comment, workspace=workspace, parent=parent, ) with new_revision(session=request.dbsession, tm=transaction.manager, content=comment): api.delete(comment) return
def update_file_info(self, context, request: TracimRequest, hapic_data=None) -> ContentInContext: # nopep8 """ update thread """ app_config = request.registry.settings['CFG'] api = ContentApi( current_user=request.current_user, session=request.dbsession, config=app_config, ) content = api.get_one(hapic_data.path.content_id, content_type=ContentType.Any) with new_revision(session=request.dbsession, tm=transaction.manager, content=content): api.update_content( item=content, new_label=hapic_data.body.label, new_content=hapic_data.body.raw_content, ) api.save(content) return api.get_content_in_context(content)
def sized_preview_jpg_revision(self, context, request: TracimRequest, hapic_data=None): # nopep8 """ Obtain resized jpg preview of a specific revision of content. """ app_config = request.registry.settings['CFG'] api = ContentApi( current_user=request.current_user, session=request.dbsession, config=app_config, ) content = api.get_one(hapic_data.path.content_id, content_type=ContentType.Any) revision = api.get_one_revision( revision_id=hapic_data.path.revision_id, content=content) jpg_preview_path = api.get_jpg_preview_path( content_id=content.content_id, revision_id=revision.revision_id, page=hapic_data.query.page, height=hapic_data.path.height, width=hapic_data.path.width, ) return FileResponse(jpg_preview_path)
def unarchive_content( self, context, request: TracimRequest, hapic_data=None, ) -> None: """ unarchive a content """ app_config = request.registry.settings['CFG'] path_data = hapic_data.path api = ContentApi( current_user=request.current_user, session=request.dbsession, config=app_config, show_archived=True, ) content = api.get_one( path_data.content_id, content_type=ContentType.Any ) with new_revision( session=request.dbsession, tm=transaction.manager, content=content ): api.unarchive(content) return
def create_generic_empty_content( self, context, request: TracimRequest, hapic_data=None, ) -> ContentInContext: """ create a generic empty content """ app_config = request.registry.settings['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=ContentType.Any) # nopep8 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=creation_data.content_type, workspace=request.current_workspace, parent=parent, ) api.save(content, ActionDescription.CREATION) content = api.get_content_in_context(content) return content
def set_html_document_status( self, context, request: TracimRequest, hapic_data=None ) -> None: """ set html_document status """ app_config = request.registry.settings['CFG'] api = ContentApi( current_user=request.current_user, session=request.dbsession, config=app_config, ) content = api.get_one( hapic_data.path.content_id, content_type=ContentType.Any ) with new_revision( session=request.dbsession, tm=transaction.manager, content=content ): api.set_status( content, hapic_data.body.status, ) api.save(content) return
def _get_current_content(self, user: User, workspace: Workspace, request: 'TracimRequest') -> Content: """ Get current content from request :param user: User who want to check the workspace :param workspace: Workspace of the content :param request: pyramid request :return: current content """ content_id = '' try: if 'content_id' in request.matchdict: content_id_str = request.matchdict['content_id'] if not isinstance( content_id_str, str) or not content_id_str.isdecimal(): # nopep8 raise InvalidContentId( 'content_id is not a correct integer') # nopep8 content_id = int(request.matchdict['content_id']) if not content_id: raise ContentNotFoundInTracimRequest( 'No content_id property found in request') # nopep8 api = ContentApi(current_user=user, session=request.dbsession, config=request.registry.settings['CFG']) content = api.get_one(content_id=content_id, workspace=workspace, content_type=ContentType.Any) # nopep8 except NoResultFound as exc: raise ContentNotFound( 'Content {} does not exist ' 'or is not visible for this user'.format(content_id)) from exc return content
def upload_file(self, context, request: TracimRequest, hapic_data=None): """ Upload a new version of raw file of content. This will create a new revision. """ app_config = request.registry.settings['CFG'] api = ContentApi( current_user=request.current_user, session=request.dbsession, config=app_config, ) content = api.get_one(hapic_data.path.content_id, content_type=ContentType.Any) file = request.POST['files'] 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
def move_content( self, context, request: TracimRequest, hapic_data=None, ) -> ContentInContext: """ move a content """ app_config = request.registry.settings['CFG'] path_data = hapic_data.path move_data = hapic_data.body api = ContentApi( current_user=request.current_user, session=request.dbsession, config=app_config, ) content = api.get_one( path_data.content_id, content_type=ContentType.Any ) new_parent = api.get_one( move_data.new_parent_id, content_type=ContentType.Any ) new_workspace = request.candidate_workspace with new_revision( session=request.dbsession, tm=transaction.manager, content=content ): api.move( content, new_parent=new_parent, new_workspace=new_workspace, must_stay_in_same_workspace=False, ) updated_content = api.get_one( path_data.content_id, content_type=ContentType.Any ) return api.get_content_in_context(updated_content)
def get_html_document(self, context, request: TracimRequest, hapic_data=None) -> ContentInContext: # nopep8 """ Get html document content """ app_config = request.registry.settings['CFG'] api = ContentApi( current_user=request.current_user, session=request.dbsession, config=app_config, ) content = api.get_one( hapic_data.path.content_id, content_type=ContentType.Any ) return api.get_content_in_context(content)
def preview_pdf(self, context, request: TracimRequest, hapic_data=None): """ Obtain a specific page pdf preview of last revision of content. """ app_config = request.registry.settings['CFG'] api = ContentApi( current_user=request.current_user, session=request.dbsession, config=app_config, ) content = api.get_one(hapic_data.path.content_id, content_type=ContentType.Any) pdf_preview_path = api.get_pdf_preview_path(content.content_id, content.revision_id, page=hapic_data.query.page) return FileResponse(pdf_preview_path)
def download_file(self, context, request: TracimRequest, hapic_data=None): """ Download raw file of last revision of content. """ app_config = request.registry.settings['CFG'] api = ContentApi( current_user=request.current_user, session=request.dbsession, config=app_config, ) content = api.get_one(hapic_data.path.content_id, content_type=ContentType.Any) file = DepotManager.get().get(content.depot_file) response = request.response response.content_type = file.content_type response.app_iter = FileIter(file) return response
def preview_pdf_full(self, context, request: TracimRequest, hapic_data=None): # nopep8 """ Obtain a full pdf preview (all page) of last revision of content. """ app_config = request.registry.settings['CFG'] api = ContentApi( current_user=request.current_user, session=request.dbsession, config=app_config, ) content = api.get_one(hapic_data.path.content_id, content_type=ContentType.Any) pdf_preview_path = api.get_full_pdf_preview_path(content.revision_id) return FileResponse(pdf_preview_path)
def get_file_revisions(self, context, request: TracimRequest, hapic_data=None) -> typing.List[RevisionInContext]: """ get file revisions """ app_config = request.registry.settings['CFG'] api = ContentApi( current_user=request.current_user, session=request.dbsession, config=app_config, ) content = api.get_one(hapic_data.path.content_id, content_type=ContentType.Any) revisions = content.revisions return [ api.get_revision_in_context(revision) for revision in revisions ]
def add_comment(self, context, request: TracimRequest, hapic_data=None): """ Add new comment """ # login = hapic_data.body app_config = request.registry.settings['CFG'] api = ContentApi( current_user=request.current_user, session=request.dbsession, config=app_config, ) content = api.get_one(hapic_data.path.content_id, content_type=ContentType.Any) comment = api.create_comment( content.workspace, content, hapic_data.body.raw_content, do_save=True, ) return api.get_content_in_context(comment)
def content_comments(self, context, request: TracimRequest, hapic_data=None): """ Get all comments related to a content in asc order (first is the oldest) """ # login = hapic_data.body app_config = request.registry.settings['CFG'] api = ContentApi( current_user=request.current_user, session=request.dbsession, config=app_config, ) content = api.get_one(hapic_data.path.content_id, content_type=ContentType.Any) comments = content.get_comments() comments.sort(key=lambda comment: comment.created) return [api.get_content_in_context(comment) for comment in comments]
def preview_jpg(self, context, request: TracimRequest, hapic_data=None): """ Obtain normally sied jpg preview of last revision of content. """ app_config = request.registry.settings['CFG'] api = ContentApi( current_user=request.current_user, session=request.dbsession, config=app_config, ) content = api.get_one(hapic_data.path.content_id, content_type=ContentType.Any) allowed_dim = api.get_jpg_preview_allowed_dim() jpg_preview_path = api.get_jpg_preview_path( content_id=content.content_id, revision_id=content.revision_id, page=hapic_data.query.page, width=allowed_dim.dimensions[0].width, height=allowed_dim.dimensions[0].height, ) return FileResponse(jpg_preview_path)
def test_query(self): content1 = self.test_create() with new_revision( session=self.session, tm=transaction.manager, content=content1, ): content1.description = 'TEST_CONTENT_DESCRIPTION_1_UPDATED' self.session.flush() content2 = self.test_create(key='2') with new_revision( session=self.session, tm=transaction.manager, content=content2, ): content2.description = 'TEST_CONTENT_DESCRIPTION_2_UPDATED' self.session.flush() workspace1 = self.session.query(Workspace)\ .filter(Workspace.label == 'TEST_WORKSPACE_1').one() workspace2 = self.session.query(Workspace)\ .filter(Workspace.label == 'TEST_WORKSPACE_2').one() # To get Content in database # we have to join Content and ContentRevisionRO # with particular condition: # Join have to be on most recent revision join_sub_query = self.session.query(ContentRevisionRO.revision_id)\ .filter(ContentRevisionRO.content_id == Content.id)\ .order_by(ContentRevisionRO.revision_id.desc())\ .limit(1)\ .correlate(Content) base_query = self.session.query(Content).join( ContentRevisionRO, and_(Content.id == ContentRevisionRO.content_id, ContentRevisionRO.revision_id == join_sub_query)) pattern = 'TEST_CONTENT_DESCRIPTION_%_UPDATED' eq_(2, base_query.filter(Content.description.like(pattern)).count()) eq_(1, base_query.filter(Content.workspace == workspace1).count()) eq_(1, base_query.filter(Content.workspace == workspace2).count()) content1_from_query = base_query\ .filter(Content.workspace == workspace1).one() eq_(content1.id, content1_from_query.id) eq_('TEST_CONTENT_DESCRIPTION_1_UPDATED', content1_from_query.description) user_admin = self.session.query(User)\ .filter(User.email == '*****@*****.**').one() api = ContentApi( current_user=None, session=self.session, config=self.app_config, ) content1_from_api = api.get_one(content1.id, ContentType.Page, workspace1)
def get_content_from_revision(self, revision: ContentRevisionRO, api: ContentApi) -> Content: try: return api.get_one(revision.content_id, ContentType.Any) except NoResultFound: return None