def add_article(title: str,
                description: str,
                content: str,
                category_id: int,
                uow: unit_of_work.AbstractUnitOfWork,
                tags: list = None) -> str:
    with uow:

        category = category_service.get_category(category_id)

        if not category:
            raise exceptions.CategoryNotFound('Category not found.')

        if title_is_duplicate(title, uow):
            raise exceptions.DuplicateTitle('Can\'t create article. Title '
                                            'duplicate.')

        _tags = tag_service.get_valid_tags_by_name(tags, uow) or []

        _article = article.Article(title, description, content, _tags,
                                   category)

        uow.articles.add(_article)
        uow.commit()

    return title
Exemple #2
0
def delete_credential(username: str, uow: unit_of_work.AbstractUnitOfWork):
    with uow:
        _credential = get_credential(username, uow)

        uow.credentials.delete(_credential)

        uow.commit()
Exemple #3
0
def update_credential(
    username: str,
    uow: unit_of_work.AbstractUnitOfWork,
    new_username: str = None,
    new_password: str = None,
) -> str:
    _credential = get_credential(username, uow)

    def update_username():

        if uow.credentials.get(new_username):
            raise exceptions.CredentialValueError('Username is already taken.')
        _credential.username = new_username

    def update_password():
        _credential.set_password(new_password)

    if new_username:
        update_username()

    if new_password:
        update_password()

    uow.commit()

    return f'Credential updated for user {username}'
def delete_article(article_url, uow: unit_of_work.AbstractUnitOfWork) -> str:
    with uow:
        _article = uow.articles.get(article_url)

        if not _article:
            raise exceptions.ArticleNotFound('Article not found.')

        uow.articles.delete(_article)

        uow.commit()

    return article_url
Exemple #5
0
def add_credential(username: str, password: str,
                   uow: unit_of_work.AbstractUnitOfWork) -> str:

    with uow:

        new_credential = credential.Credential(username=username, )

        new_credential.set_password(password)

        uow.credentials.add(new_credential)
        uow.commit()

        return f'Credential created for {username}'
Exemple #6
0
def add_tag(tag_name: str, uow: unit_of_work.AbstractUnitOfWork) -> str:
    with uow:

        tags = uow.tags.list()

        _tag = tag.Tag(tag_name)

        if _tag in tags:
            raise exceptions.DuplicateTag(f'Tag {tag_name} is duplicate')

        uow.tags.add(_tag)
        uow.commit()

    return tag_name
Exemple #7
0
def update_tag(tag_name: str, new_name: str,
               uow: unit_of_work.AbstractUnitOfWork) -> str:
    with uow:

        tag_to_update = uow.tags.get(tag_name)

        if tag_name is None:
            raise exceptions.TagNotFound('Tag not found')

        tag_to_update.name = new_name

        uow.commit()

    return f'Tag {tag_name} updated to {new_name}'
Exemple #8
0
def delete_tag(tag_name: str, uow: unit_of_work.AbstractUnitOfWork) -> str:

    with uow:

        tag_to_delete = uow.tags.get(tag_name)

        if tag_to_delete is None:
            raise exceptions.TagNotFound('Tag not found')

        uow.tags.delete(tag_to_delete)

        uow.commit()

    return f'Tag {tag_name} deleted'
def update_article(
    article_url: str,
    uow: unit_of_work.AbstractUnitOfWork,
    **kwargs,
):
    with uow:

        _article = uow.articles.get(value=article_url)

        if not _article:
            raise exceptions.ArticleNotFound('Article not found.')

        for attribute in kwargs:
            update_attribute(_article, attribute, kwargs, uow)

        uow.commit()

    return _article.title