from django.core.paginator import Paginator


def parse_items_per_page(raw_value):
    try:
        parsed = int(raw_value)
    except (TypeError, ValueError):
        return 10
    return parsed if parsed > 0 else 10


def paginate_queryset(queryset, page_number, per_page):
    paginator = Paginator(queryset, per_page)
    page = paginator.get_page(page_number)
    return paginator, page


def recent_from_queryset(queryset, limit=4):
    return queryset[:limit]
