Пример #1
0
def post(request, lang, code):
    post_obj = post_bizlogic.get_post_obj_by_code(code)
    if not post_obj:
        return page_not_found(request)

    # If viewer is allowed to see the unpublished post.
    if post_bizlogic.is_available_post_obj(post_obj):
        pass
    else:
        if not basic_auth.basic_auth(request):
            return basic_auth.http401()

    formatted_post = post_bizlogic.format_post(post_obj,
                                               lang,
                                               require_body=True)
    related_formatted_posts, next_formatted_post, previous_formatted_post = post_bizlogic.get_related_formatted_posts(
        lang, post_obj)

    data = common_bizlogic.get_base_data(lang, request)
    data['page_title'] = f'{formatted_post["title"]} | {data["page_title"]}'
    data['post'] = formatted_post
    data['mainimage_fullpath'] = image_utils.get_mediafile_full_url(
        request, formatted_post['thumbnail'])
    data['linked_from'] = link_bizlogic.get_posts_having_linkto(post_obj, lang)
    data['comments'] = comment_bizlogic.get_comments_for_post(lang, post_obj)
    data['related_posts'] = related_formatted_posts
    data['next_post'] = next_formatted_post
    data['previous_post'] = previous_formatted_post
    return render(request, 'app/post.tpl', data)
Пример #2
0
def get_posts_having_linkto(post_obj, lang):
    """Select posts which have links to assigned post."""

    return [
        post_bizlogic.format_post(link.parent_post, lang, require_body=False)
        for link in Link.objects.filter(
            linked_post=post_obj,
            # No idea why but timezone can be used without import.
            parent_post__publish_at__lte=timezone.now())
    ]
Пример #3
0
def get_pickup_post(lang):
    """Randomly pick one post up, from newer posts."""
    count = Post.available().count()
    if not count:
        return
    # -(-num//num2) means ceil of division, obstructing it becomes less than 1.
    # math.ceil looks so simple but runs slowly. Besides it needs import math.
    picknum = random.randint(1, -(-count//2))
    return post_bizlogic.format_post(
        Post.available().order_by('publish_at').reverse()[picknum:picknum+1].first(),
        lang,
        require_body=False,
    )
Пример #4
0
def get_recommended_posts(lang):
    """Get recommended posts set in DB."""

    # Recommended post codes.
    codes = (
        config_obj.value
        for config_obj
        in Config.objects.filter(key__startswith=consts.ConfigKeys.RECOMMENDED_POST_CODE)
    )

    # As want to pass not to append None, don't use comprehension(内包表記).
    posts = []
    for code in codes:
        post = Post.available().filter(code=code).first()
        if post:
            posts.append(post_bizlogic.format_post(post, lang, require_body=False))
    return posts
Пример #5
0
def latest(request, lang):
    post_obj = post_bizlogic.get_latest_post_obj()
    if not post_obj:
        return page_not_found(request)
    formatted_post = post_bizlogic.format_post(post_obj,
                                               lang,
                                               require_body=True)
    related_formatted_posts, next_formatted_post, previous_formatted_post = post_bizlogic.get_related_formatted_posts(
        lang, post_obj)

    data = common_bizlogic.get_base_data(lang, request)
    data['is_latest_page'] = True
    data['page_title'] = f'{formatted_post["title"]} | {data["page_title"]}'
    data['post'] = formatted_post
    data['mainimage_fullpath'] = image_utils.get_mediafile_full_url(
        request, formatted_post['thumbnail']),
    data['comments'] = comment_bizlogic.get_comments_for_post(lang, post_obj)
    data['related_posts'] = related_formatted_posts
    data['next_post'] = next_formatted_post
    data['previous_post'] = previous_formatted_post
    return render(request, 'app/post.tpl', data)
Пример #6
0
def get_recently_updated_posts(lang):
    """Get 5 recently updated posts."""
    return (
        post_bizlogic.format_post(post_obj, lang, require_body=False)
        for post_obj in Post.available().order_by('updated_at').reverse()[:5]
    )
Пример #7
0
def get_latest_posts(lang):
    """Get 5 latest posts."""
    return [
        post_bizlogic.format_post(post_obj, lang, require_body=False)
        for post_obj in Post.available().order_by('publish_at').reverse()[:5]
    ]