# Path: brigitte/repositories/decorators.py
# def repository_view(can_admin=False):
#     def inner_repository_view(f):
#         def wrapped(request, user, slug, *args, **kwargs):
#             qs = Repository.objects.none()
# 
#             if request.user.is_authenticated():
#                 if can_admin:
#                     qs = Repository.objects.manageable_repositories(request.user)
#                 else:
#                     qs = Repository.objects.available_repositories(request.user)
#             else:
#                 if not can_admin:
#                     qs = Repository.objects.public_repositories()
# 
#             repository = get_object_or_404(qs, user__username=user, slug=slug)
# 
#             return f(request, repository, *args, **kwargs)
#         return wraps(f)(wrapped)
#     return inner_repository_view


from django.http import Http404
from django.shortcuts import render
from brigitte.repositories.decorators import repository_view
# -*- coding: utf-8 -*-



@repository_view()
def commits(request, repo, branchtag):
    count = 10

    try:
        page = int(request.GET.get('page', 1))
    except ValueError:
        page = 1

    skip = (page * count) - count
    if skip < 0:
        skip = 0
    commits = repo.get_commits(count=count + 1, skip=skip, head=branchtag)
    if not commits:
        raise Http404

    return render(request, 'commits/commits.html', {
        'repository': repo,
        'commits': commits[:count],
        'branchtag': branchtag,
        'next_page': page + 1 if len(commits) > count else None,
        'prev_page': page - 1 if page > 1 else None,
    })



# Path: brigitte/repositories/decorators.py
# def repository_view(can_admin=False):
#     def inner_repository_view(f):
#         def wrapped(request, user, slug, *args, **kwargs):
#             qs = Repository.objects.none()
# 
#             if request.user.is_authenticated():
#                 if can_admin:
#                     qs = Repository.objects.manageable_repositories(request.user)
#                 else:
#                     qs = Repository.objects.available_repositories(request.user)
#             else:
#                 if not can_admin:
#                     qs = Repository.objects.public_repositories()
# 
#             repository = get_object_or_404(qs, user__username=user, slug=slug)
# 
#             return f(request, repository, *args, **kwargs)
#         return wraps(f)(wrapped)
#     return inner_repository_view


# Path: brigitte/commits/views.py
from django.http import Http404
from django.shortcuts import render
from brigitte.repositories.decorators import repository_view

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



@repository_view()
def commits(request, repo, branchtag):
    count = 10

    try:
        page = int(request.GET.get('page', 1))
    except ValueError:
        page = 1

    skip = (page * count) - count
    if skip < 0:
        skip = 0
    commits = repo.get_commits(count=count + 1, skip=skip, head=branchtag)
    if not commits:
        raise Http404

    return render(request, 'commits/commits.html', {
        'repository': repo,
        'commits': commits[:count],
        'branchtag': branchtag,
        'next_page': page + 1 if len(commits) > count else None,
        'prev_page': page - 1 if page > 1 else None,
    })



@repository_view()