Ejemplo n.º 1
0
def update_keyword(
        *,
        _: bool = Depends(is_authenticated),
        repository: KeywordRepository = Depends(get_keyword_repository),
        id: int,
        keyword_update: KeywordUpdate):
    db_keyword: DBKeyword = or_404(repository.get(id))
    updated: DBKeyword = repository.update(
        db_keyword, keyword_update.dict(skip_defaults=True))
    return updated
Ejemplo n.º 2
0
def update_keyword(
    *,
    _: bool = Depends(is_authenticated),
    repository: KeywordRepository = Depends(get_keyword_repository),
    id: int,
    keyword_update: KeywordUpdate,
):
    db_keyword: DBKeyword = or_404(repository.get(id))
    updated: DBKeyword = repository.update(
        db_keyword, keyword_update.dict(exclude_unset=True))
    return updated.to_model()
Ejemplo n.º 3
0
    def setUp(self) -> None:
        db_session.rollback()
        db_session.query(Keyword).delete()
        db_session.query(Collection).delete()
        self.collection_repo = CollectionRepository(db_session)
        self.keyword_repo = KeywordRepository(db_session)
        self.keywords = [
            Keyword(
                name="Test setup",
                doc="Prepare test environment, use teardown after this one",
            ),
            Keyword(name="Login keyword", doc="Perform some check"),
            Keyword(name="Teardown", doc="Clean up environment"),
        ]
        self.app_keyword = Keyword(name="Login to Application")

        self.collections = [
            Collection(name="First collection",
                       type="robot",
                       keywords=self.keywords),
            Collection(name="Second collection",
                       type="Robot",
                       keywords=[self.app_keyword]),
            Collection(name="Third", type="Library"),
        ]
        self.sorted_keywords = sorted(self.keywords + [self.app_keyword],
                                      key=lambda k: k.name)
        db_session.add_all(self.collections)
        db_session.commit()
        for item in self.collections:
            db_session.refresh(item)
Ejemplo n.º 4
0
def delete_keyword(
        *,
        _: bool = Depends(is_authenticated),
        repository: KeywordRepository = Depends(get_keyword_repository),
        id: int):
    deleted: int = repository.delete(id)
    if deleted:
        return Response(status_code=204)
    else:
        raise HTTPException(status_code=404)
Ejemplo n.º 5
0
def search_keywords(
        *,
        repository: KeywordRepository = Depends(get_keyword_repository),
        params: SearchParams = Depends(),
        skip: int = 0,
        limit: int = 100):
    return repository.get_all(pattern=params.pattern,
                              collection_name=params.collection_name,
                              use_doc=params.use_doc,
                              skip=skip,
                              limit=limit)
Ejemplo n.º 6
0
def get_keywords(
        repository: KeywordRepository = Depends(get_keyword_repository),
        skip: int = 0,
        limit: int = 100,
        pattern: str = None,
        use_doc: bool = True):
    keywords: List[DBKeyword] = repository.get_all(skip=skip,
                                                   limit=limit,
                                                   pattern=pattern,
                                                   use_doc=use_doc)
    return keywords
Ejemplo n.º 7
0
def create_keyword(
        *,
        _: bool = Depends(is_authenticated),
        repository: KeywordRepository = Depends(get_keyword_repository),
        collection_repository: CollectionRepository = Depends(
            get_collection_repository),
        keyword: KeywordCreate):
    collection: Optional[DBCollection] = collection_repository.get(
        keyword.collection_id)
    if not collection:
        raise HTTPException(status_code=400,
                            detail='Collection does not exist')
    db_keyword: DBKeyword = DBKeyword(**keyword.dict())
    return repository.add(db_keyword)
Ejemplo n.º 8
0
def search_keywords(
        *,
        repository: KeywordRepository = Depends(get_keyword_repository),
        params: SearchParams = Depends(),
        skip: int = 0,
        limit: int = 100,
        ordering: List[OrderingItem] = Depends(get_ordering),
):
    return repository.get_all(
        pattern=params.pattern,
        collection_name=params.collection_name,
        use_doc=params.use_doc,
        use_tags=params.use_tags,
        skip=skip,
        limit=limit,
        ordering=ordering,
    )
Ejemplo n.º 9
0
def get_keywords_with_stats(
        repository: KeywordRepository = Depends(get_keyword_repository),
        skip: int = 0,
        limit: int = 100,
        pattern: str = None,
        use_doc: bool = True,
        collection_id: Optional[int] = None,
        ordering: List[OrderingItem] = Depends(get_ordering),
):
    return repository.get_all_with_stats(
        skip=skip,
        limit=limit,
        pattern=pattern,
        collection_id=collection_id,
        use_doc=use_doc,
        ordering=ordering,
    )
Ejemplo n.º 10
0
async def search(
    request: Request,
    params: SearchParams = Depends(),
    repository: KeywordRepository = Depends(get_keyword_repository)):
    """
    Search endpoint accepts query parameter 'pattern' which can optionally indicate that pattern should be searched for
    only in keyword names with 'name:query'. It can also optionally indicate query used for filtering collections
    of found keywords.

    Example pattern values:
    search?pattern=keyword
    search?pattern=name: keyword
    search?pattern=keyword in: some collection
    """
    keywords = repository.get_all(pattern=params.pattern,
                                  collection_name=params.collection_name,
                                  use_doc=params.use_doc)
    context = {
        "request": request,
        "keywords": keywords,
        "pattern": params.raw_pattern,
        "version": version
    }
    return templates.TemplateResponse("search.html", context)
Ejemplo n.º 11
0
def get_keyword(
        *,
        repository: KeywordRepository = Depends(get_keyword_repository),
        id: int):
    keyword: Optional[DBKeyword] = repository.get(id)
    return or_404(keyword)
Ejemplo n.º 12
0
def get_keyword_repository(request: Request) -> KeywordRepository:
    return KeywordRepository(request.state.db)
Ejemplo n.º 13
0
def get_keyword_with_stats(
        *,
        repository: KeywordRepository = Depends(get_keyword_repository),
        id: int):
    keyword: Optional[KeywordWithStats] = repository.get_with_stats(id)
    return or_404(keyword)