def test_gets_doc(app, client: TApp, admin, user, get_headers, by): """ corret doc getting """ md = "# GOO GOO" with app.app_context(): repo = InstructionDocumentRepository() doc = InstructionDocument(name=f'coolish doc' + ' ' + by if by else 'None', description='. . .', created_by=admin) managed_doc = InstructionDocumentManager(document=doc) repo.save(doc) page = InstructionDocumentPage(document_id=doc.id, md=md) managed_doc.add_page(page, admin.id) doc_id = doc.id doc_slug = doc.slug get_doc_data = {"uid": str(uuid.uuid4())} if by: get_doc_data[by] = int(doc_id) if by == 'document_id' else str( doc_slug) print('get_doc_data', get_doc_data) response = client.post_json('/api/instruction-documents/get-doc', get_doc_data, headers=get_headers('nouser')) assert response.json['status'] == 'OK' assert response.json['uid'] == get_doc_data['uid'] assert len(response.json['data']['pages']) == 1 assert response.json['data']['pages'][0]['page_num'] == 1 assert response.json['data']['pages'][0]['document_id'] == doc_id assert response.json['data']['pages'][0]['md'] == md
def __init__(self, document_id: int = None, document: InstructionDocument = None): self.__page_repo = InstructionDocumentPageRepository() self.__doc_repo = InstructionDocumentRepository() self.__document = self.__doc_repo.get( document_id) if document_id else document if not self.__document: raise ValueError('Document not provided!')
def test_adds_doc_page(app, client: TApp, admin, user, get_headers): """ corret doc page addition """ create_doc_data = {"name": "doc with pages", "description": "..."} created_doc_id = client.post_json( '/api/instruction-documents/add-doc', create_doc_data, headers=get_headers('admin')).json['data']['id'] assert created_doc_id is not None doc_pages_data = { "uid": str(uuid.uuid4()), "document_id": created_doc_id, "md": "# page one ;)" } response = client.post_json('/api/instruction-documents/add-page', doc_pages_data, headers=get_headers('admin')) assert response.json['status'] == 'OK' assert response.json['uid'] == doc_pages_data['uid'] assert response.json['data']['id'] is not None assert response.json['data']['document_id'] == created_doc_id assert response.json['data']['md'] == doc_pages_data['md'] assert response.json['data'][ 'html'] == '<div class="text-h1">page one ;)</div>' with app.app_context(): doc: InstructionDocument = InstructionDocumentRepository().get( created_doc_id) assert doc.updated_by_user_id == admin.id assert doc.updated is not None magaged_doc = InstructionDocumentManager(document=doc) assert magaged_doc.page_count() == 1
def check_input(cls, values: dict): doc_slug = values.get('document_slug', None) doc_id = values.get('document_id', None) print('validation doc id', doc_id) print('validation doc slug', doc_slug) if doc_slug is None and doc_id is None: raise ValueError('document_slug or document_id is required') if doc_slug is not None and doc_id is not None: raise ValueError('provide only document_slug or only document_id') if doc_slug is not None: must_exist_by(InstructionDocumentRepository(), by='slug', value=doc_slug) if doc_id is not None: must_exist_by_pk(InstructionDocumentRepository(), doc_id) return values
def get_response(self) -> JsonResponse: user: User = UserRepository().get(get_jwt_identity()['id']) doc = InstructionDocument(created_by=user, name=self.request.json['name'].strip(), description=self.request.json.get( 'description', None)) InstructionDocumentRepository().save(doc) return ok_response(InstructionDocumentEntityModel.from_orm(doc))
def test_lists_docs(app, client: TApp, admin, user, get_headers): """ corret doc listing """ with app.app_context(): repo = InstructionDocumentRepository() for i in range(1, 10): repo.save(InstructionDocument(f'[{i}]test doc', 'foo', admin)) list_docs_data = {"uid": str(uuid.uuid4()), "page": 2, "limit": 3} response = client.post_json('/api/instruction-documents/list-docs', list_docs_data, headers=get_headers('nouser')) assert response.json['status'] == 'OK' assert response.json['uid'] == list_docs_data['uid'] assert response.json['data']['page'] == list_docs_data['page'] assert response.json['data']['prev_num'] == list_docs_data['page'] - 1 assert response.json['data']['next_num'] == list_docs_data['page'] + 1 assert len(response.json['data']['results']) == 3
def test_deletes_doc_page(app, client: TApp, admin, user, get_headers): """ corret doc page deletion """ create_doc_data = { "name": "doc with pages to delete", "description": "..." } created_doc_id = client.post_json( '/api/instruction-documents/add-doc', create_doc_data, headers=get_headers('admin')).json['data']['id'] assert created_doc_id is not None doc_pages_data = { "document_id": created_doc_id, "md": "# page one ;) \n* foo \n* bar" } page_id = client.post_json('/api/instruction-documents/add-page', doc_pages_data, headers=get_headers('admin')).json['data']['id'] with app.app_context(): doc: InstructionDocument = InstructionDocumentRepository().get( created_doc_id) assert doc.updated_by_user_id == admin.id assert doc.updated is not None magaged_doc = InstructionDocumentManager(document=doc) assert magaged_doc.page_count() == 1 deletion_data = {"uid": str(uuid.uuid4()), "page_id": page_id} response = client.post_json('/api/instruction-documents/delete-page', deletion_data, headers=get_headers('admin')) assert response.json['uid'] == deletion_data['uid'] assert response.json['status'] == 'OK' assert response.json['data'] is None with app.app_context(): doc: InstructionDocument = InstructionDocumentRepository().get( created_doc_id) assert doc.updated_by_user_id == admin.id assert doc.updated is not None magaged_doc = InstructionDocumentManager(document=doc) assert magaged_doc.page_count() == 0
def get_response(self) -> JsonResponse: docs_paginated = InstructionDocumentRepository() \ .all_paginated(page=self.request.json['page'], limit=self.request.json['limit'], order='id desc') rdata = ListInstructionDocumentEventResponseDataModel( total=docs_paginated.total, page=docs_paginated.page, next_num=docs_paginated.next_num, prev_num=docs_paginated.prev_num, results=docs_paginated.items) return ok_response(rdata)
def test_search_docs(app, client: TApp, admin, user, get_headers): """ corret doc searching """ added_count = 0 with app.app_context(): repo = InstructionDocumentRepository() for i in range(1, 10): repo.save(InstructionDocument(f'[{i}] searched doc', 'foo', admin)) added_count += 1 for i in range(1, 6): repo.save( InstructionDocument(f'[{i}] ...', f'xyz {i} searched doc', admin)) added_count += 1 search_docs_data = { "uid": str(uuid.uuid4()), "search": "SearChed dOc", "page": 1, "limit": 100 } response = client.post_json('/api/instruction-documents/search-docs', search_docs_data, headers=get_headers('nouser')) assert response.json['status'] == 'OK' assert response.json['uid'] == search_docs_data['uid'] assert response.json['data']['page'] == 1 assert len(response.json['data']['results']) == added_count pass
class InstructionDocumentManager: def __init__(self, document_id: int = None, document: InstructionDocument = None): self.__page_repo = InstructionDocumentPageRepository() self.__doc_repo = InstructionDocumentRepository() self.__document = self.__doc_repo.get( document_id) if document_id else document if not self.__document: raise ValueError('Document not provided!') @property def document(self): return self.__document def update(self, user_id: int, **kwargs): valid_kwargs = ('name', 'description') for k, v in kwargs.items(): if k in valid_kwargs: setattr(self.__document, k, v) self.__document.slug = get_slug(self.__document.name) self.__document.updated_by_user_id = user_id self.__document.updated = datetime.utcnow() self.__doc_repo.save(self.__document) def pages(self) -> List[InstructionDocumentPage]: return self.__page_repo.filter( InstructionDocumentPage.doc(self.__document.id)) def page_count(self) -> int: return len(self.pages()) def add_page(self, page: InstructionDocumentPage, user_id: int) -> InstructionDocumentPage: page.page_num = self.page_count() + 1 self.__page_repo.save(page) self.__document.updated_by_user_id = user_id self.__document.updated = datetime.utcnow() self.__doc_repo.save(self.__document) return page def delete_page(self, user_id: int, page_num: int): page = self.__page_repo.get_by(document_id=self.__document.id, page_num=page_num) self.__page_repo.delete(page.id) for page in self.pages(): if page.page_num > page_num: page.page_num = page.page_num - 1 self.__page_repo.save(page) self.__document.updated_by_user_id = user_id self.__document.updated = datetime.utcnow() self.__doc_repo.save(self.__document)
def get_response(self) -> JsonResponse: rmodel: SearchInstructionDocumentEventRequestModel = self.request_model search = rmodel.search.lower().strip() docs_paginated = InstructionDocumentRepository() \ .filter_paginated(f=or_(InstructionDocument.name.ilike(f'%{search}%'), InstructionDocument.description.ilike(f'%{search}%')), page=rmodel.page, limit=rmodel.limit, order='id desc') rdata = ListInstructionDocumentEventResponseDataModel( total=docs_paginated.total, page=docs_paginated.page, next_num=docs_paginated.next_num, prev_num=docs_paginated.prev_num, results=docs_paginated.items) return ok_response(rdata)
def get_response(self) -> JsonResponse: rmodel: GetInstructionDocumentEventRequestModel = self.request_model by, val = ( 'slug', rmodel.document_slug) if rmodel.document_slug is not None else ( 'id', rmodel.document_id) doc = InstructionDocumentRepository().get_by(**{by: val}) doc_entity = InstructionDocumentEntityModel.from_orm(doc) pages = InstructionDocumentPageRepository().filter( InstructionDocumentPage.document_id == doc.id) pages_entities = [ InstructionDocumentPageEntityModel.from_orm(p) for p in pages ] data_dict = doc_entity.dict() data_dict['pages'] = pages_entities rdata = GetInstructionDocumentEventResponsedataModel(**data_dict) return ok_response(rdata)
def test_updates_doc_page(app, client: TApp, admin, get_headers): """ corret doc page edittion """ create_doc_data = { "name": "doc with pages to update", "description": "..." } created_doc_id = client.post_json( '/api/instruction-documents/add-doc', create_doc_data, headers=get_headers('admin')).json['data']['id'] assert created_doc_id is not None doc_pages_data = { "document_id": created_doc_id, "md": "# page one ;) \n* foo \n* bar" } page_id = client.post_json('/api/instruction-documents/add-page', doc_pages_data, headers=get_headers('admin')).json['data']['id'] update_page_data = { "uid": str(uuid.uuid4()), "page_id": page_id, "md": "# updated page one ;)" } response = client.post_json('/api/instruction-documents/update-page', update_page_data, headers=get_headers('admin')) assert response.json['uid'] == update_page_data['uid'] assert response.json['status'] == 'OK' assert response.json['data']['md'] == update_page_data['md'] assert response.json['data'][ 'html'] == '<div class="text-h1">updated page one ;)</div>' with app.app_context(): doc: InstructionDocument = InstructionDocumentRepository().get( created_doc_id) assert doc.updated_by_user_id == admin.id assert doc.updated is not None
def doc_must_exist(cls, v: int): must_exist_by_pk(InstructionDocumentRepository(), v) return v
def get_response(self) -> JsonResponse: InstructionDocumentRepository().delete( self.request.json['document_id']) return ok_response()