# -*- coding: utf-8 -*-

"""
byceps.blueprints.news.views
~~~~~~~~~~~~~~~~~~~~~~~~~~~~

:Copyright: 2006-2016 Jochen Kupperschmidt
:License: Modified BSD, see LICENSE for details.
"""

from flask import abort, g

from ...services.news import service as news_service
from ...util.framework import create_blueprint
from ...util.templating import templated


blueprint = create_blueprint('news', __name__)


ITEMS_PER_PAGE = 4


@blueprint.route('/', defaults={'page': 1})
@blueprint.route('/pages/<int:page>')
@templated
def index(page):
    """Show a page of news items."""
    items = news_service.get_items_paginated(g.party.brand.id, page,
                                             ITEMS_PER_PAGE)

    return {
        'items': items,
        'page': page,
    }


@blueprint.route('/<slug>')
@templated
def view(slug):
    """Show a single news item."""
    item = news_service.find_item_by_slug(g.party.brand.id, slug)

    if item is None:
        abort(404)

    return {
        'item': item,
    }

# -*- coding: utf-8 -*-

"""
byceps.blueprints.news.views
~~~~~~~~~~~~~~~~~~~~~~~~~~~~

:Copyright: 2006-2016 Jochen Kupperschmidt
:License: Modified BSD, see LICENSE for details.
"""

from flask import abort, current_app, g

from ...services.news import service as news_service
from ...util.framework import create_blueprint
from ...util.templating import templated


blueprint = create_blueprint('news', __name__)


@blueprint.route('/', defaults={'page': 1})
@blueprint.route('/pages/<int:page>')
@templated
def index(page):
    """Show a page of news items."""
    items_per_page = _get_items_per_page_value()

    items = news_service.get_items_paginated(g.party.brand.id, page,
                                             items_per_page)

    return {
        'items': items,
        'page': page,
    }


@blueprint.route('/<slug>')
@templated
def view(slug):
    """Show a single news item."""
    item = news_service.find_item_by_slug(g.party.brand.id, slug)

    if item is None:
        abort(404)

    return {
        'item': item,
    }


def _get_items_per_page_value(default=4):
    return int(current_app.config.get('NEWS_ITEMS_PER_PAGE', default))

Allow configuration of the number of news items per page