Esempio n. 1
0
def post(slug, year=None, month=None, day=None):
    posts, total_posts, total_pages = helpers.get_formatted_posts(slugs=[slug])

    if not posts:
        flask.abort(404)

    post = posts[0]

    topics = api.get_topics(post_id=post['id'])

    if topics:
        post['topic'] = topics[0]

    tags = api.get_tags(post_id=post['id'])
    related_posts, total_posts, total_pages = helpers.get_formatted_posts(
        tag_ids=[tag['id'] for tag in tags],
        per_page=3
    )

    return flask.render_template(
        'post.html',
        post=post,
        tags=tags,
        related_posts=related_posts,
    )
Esempio n. 2
0
def post(slug, year, month, day=None):
    posts, total_posts, total_pages = helpers.get_formatted_posts(slugs=[slug])

    if not day and posts:
        pubdate = dateutil.parser.parse(posts[0]['date_gmt'])
        day = pubdate.strftime('%d')
        return flask.redirect(
            '/{year}/{month}/{day}/{slug}'.format(**locals())
        )

    if not posts:
        flask.abort(404)

    post = posts[0]

    topics = api.get_topics(post_id=post['id'])

    if topics:
        post['topic'] = topics[0]

    tags = api.get_tags(post_id=post['id'])
    related_posts, total_posts, total_pages = helpers.get_formatted_posts(
        tag_ids=[tag['id'] for tag in tags],
        per_page=3,
        exclude=post['id']
    )

    return flask.render_template(
        'post.html',
        post=post,
        tags=tags,
        related_posts=related_posts,
    )
Esempio n. 3
0
def archives():
    page = helpers.to_int(flask.request.args.get('page'), default=1)
    year = helpers.to_int(flask.request.args.get('year'))
    month = helpers.to_int(flask.request.args.get('month'))
    group_slug = flask.request.args.get('group')
    category_slug = flask.request.args.get('category')

    if month and month > 12:
        month = None

    friendly_date = None
    group = None
    after = None
    before = None

    if year:
        if month:
            after = datetime(year=year, month=month, day=1)
            before = after + relativedelta(months=1)
            friendly_date = after.strftime('%B %Y')
        if not month:
            after = datetime(year=year, month=1, day=1)
            before = after + relativedelta(years=1)
            friendly_date = after.strftime('%Y')

    if group_slug:
        groups = api.get_groups(slugs=[group_slug])

        if groups:
            group = groups[0]

    if category_slug:
        categories = api.get_categories(slugs=[category_slug])
        category_ids = [category['id'] for category in categories]
    else:
        categories = []
        category_ids = []

    posts, total_posts, total_pages = helpers.get_formatted_posts(
        page=page,
        after=after,
        before=before,
        group_ids=[group['id']] if group else [],
        category_ids=category_ids if category_ids else [],
    )

    return flask.render_template(
        'archives.html',
        categories=categories,
        category_ids=category_ids,
        category_slug=category_slug if category_slug else None,
        current_page=page,
        friendly_date=friendly_date,
        group=group,
        now=datetime.now(),
        posts=posts,
        total_pages=total_pages,
        total_posts=total_posts,
    )
Esempio n. 4
0
def post(slug, year=None, month=None, day=None):
    posts, total_posts, total_pages = helpers.get_formatted_posts(slugs=[slug])

    if not posts:
        flask.abort(404)

    if not (day and month and year):
        pubdate = dateutil.parser.parse(posts[0]["date_gmt"])
        day = pubdate.strftime("%d")
        month = pubdate.strftime("%m")
        year = pubdate.strftime("%Y")

        return flask.redirect(
            "/{year}/{month}/{day}/{slug}".format(**locals())
        )

    post = posts[0]

    topics = api.get_topics(post_id=post["id"])

    if topics:
        post["topic"] = topics[0]

    tags = api.get_tags(post_id=post["id"])
    related_posts, total_posts, total_pages = helpers.get_formatted_posts(
        tag_ids=[tag["id"] for tag in tags], per_page=3, exclude=post["id"]
    )

    # Even though we're filtering tags below, we need to know the snapcraft.io
    # tag, specifically to add the canonical meta tag
    snapcraft_io_tag = list(filter(lambda tag: tag["id"] == 2996, tags))
    if snapcraft_io_tag:
        canonical_link = "https://snapcraft.io/blog/" + slug
    else:
        canonical_link = None

    display_tags = helpers.filter_tags_for_display(tags)

    return flask.render_template(
        "post.html",
        post=post,
        tags=display_tags,
        related_posts=related_posts,
        canonical_link=canonical_link,
    )
Esempio n. 5
0
def user(slug):
    authors = api.get_users(slugs=[slug])

    if not authors:
        flask.abort(404)

    author = authors[0]

    recent_posts, total_posts, total_pages = helpers.get_formatted_posts(
        author_ids=[author['id']], per_page=5)

    return flask.render_template('author.html',
                                 author=author,
                                 recent_posts=recent_posts)
Esempio n. 6
0
def post(slug, year, month, day=None):
    posts, total_posts, total_pages = helpers.get_formatted_posts(slugs=[slug])

    if not day and posts:
        pubdate = dateutil.parser.parse(posts[0]['date_gmt'])
        day = pubdate.strftime('%d')
        return flask.redirect(
            '/{year}/{month}/{day}/{slug}'.format(**locals()))

    if not posts:
        flask.abort(404)

    post = posts[0]

    topics = api.get_topics(post_id=post['id'])

    if topics:
        post['topic'] = topics[0]

    tags = api.get_tags(post_id=post['id'])
    related_posts, total_posts, total_pages = helpers.get_formatted_posts(
        tag_ids=[tag['id'] for tag in tags], per_page=3, exclude=post['id'])

    snapcraft_io_tag = list(filter(lambda tag: tag['id'] == 2996, tags))

    if snapcraft_io_tag:
        canonical_link = 'https://snapcraft.io/blog/' + slug
    else:
        canonical_link = None

    return flask.render_template(
        'post.html',
        post=post,
        tags=tags,
        related_posts=related_posts,
        canonical_link=canonical_link,
    )
Esempio n. 7
0
def search():
    query = flask.request.args.get('q') or ''
    page = helpers.to_int(flask.request.args.get('page'), default=1)
    posts = []
    total_pages = None
    total_posts = None

    if query:
        posts, total_posts, total_pages = helpers.get_formatted_posts(
            query=query, page=page)

    return flask.render_template(
        'search.html',
        posts=posts,
        query=query,
        current_page=page,
        total_posts=total_posts,
        total_pages=total_pages,
    )