Пример #1
0
def get_random_articles(quantity, repo: AbstractRepository):
    article_count = repo.get_number_of_articles()

    if quantity >= article_count:
        # Reduce the quantity of ids to generate if the repository has an insufficient number of articles.
        quantity = article_count - 1

    # Pick distinct and random articles.
    random_ids = random.sample(range(1, article_count), quantity)
    articles = repo.get_articles_by_id(random_ids)

    return articles_to_dict(articles)
Пример #2
0
def add_user(username: str, password: str, repo: AbstractRepository):
    # Check that the given username is available.
    user = repo.get_user(username)
    if user is not None:
        raise NameNotUniqueException

    # Encrypt password so that the database doesn't store passwords 'in the clear'.
    password_hash = generate_password_hash(password)

    # Create and store the new User, with password encrypted.
    user = User(username, password_hash)
    repo.add_user(user)
Пример #3
0
def get_comments_for_article(article_id, repo: AbstractRepository):
    article = repo.get_article(article_id)

    if article is None:
        raise NonExistentMoviesException

    return comments_to_dict(article.comments)
Пример #4
0
def get_articles_by_id(id_list, repo: AbstractRepository):
    articles = repo.get_articles_by_id(id_list)

    # Convert Articles to dictionary form.
    articles_as_dict = articles_to_dict(articles)

    return articles_as_dict
Пример #5
0
def get_articles_by_date(date, repo: AbstractRepository):
    # Returns articles for the target date (empty if no matches), the date of the previous article (might be null), the date of the next article (might be null)

    articles = repo.get_articles_by_date(target_date=date)

    articles_dto = list()
    prev_date = next_date = None

    if len(articles) > 0:
        prev_date = repo.get_date_of_previous_article(articles[0])
        next_date = repo.get_date_of_next_article(articles[0])

        # Convert Articles to dictionary form.
        articles_dto = articles_to_dict(articles)

    return articles_dto, prev_date, next_date
Пример #6
0
def get_article(article_id: int, repo: AbstractRepository):
    article = repo.get_article(article_id)

    if article is None:
        raise NonExistentMoviesException

    return article_to_dict(article)
Пример #7
0
def add_comment(article_id: int, comment_text: str, username: str,
                repo: AbstractRepository):
    # Check that the article exists.
    article = repo.get_article(article_id)
    if article is None:
        raise NonExistentMoviesException

    user = repo.get_user(username)
    if user is None:
        raise UnknownUserException

    # Create comment.
    comment = make_comment(comment_text, user, article)

    # Update the repository.
    repo.add_comment(comment)
Пример #8
0
def authenticate_user(username: str, password: str, repo: AbstractRepository):
    authenticated = False

    user = repo.get_user(username)
    if user is not None:
        authenticated = check_password_hash(user.password, password)
    if not authenticated:
        raise AuthenticationException
Пример #9
0
def get_article_ids_for_tag(tag_name, repo: AbstractRepository):
    article_ids = repo.get_article_ids_for_tag(tag_name)

    return article_ids
Пример #10
0
def get_last_article(repo: AbstractRepository):

    article = repo.get_last_article()
    return article_to_dict(article)
Пример #11
0
def get_tag_names(repo: AbstractRepository):
    tags = repo.get_tags()
    tag_names = [tag.tag_name for tag in tags]

    return tag_names
Пример #12
0
def get_user(username: str, repo: AbstractRepository):
    user = repo.get_user(username)
    if user is None:
        raise UnknownUserException

    return user_to_dict(user)