Blame progit/urls.py

Pierre-Yves Chibon ac8023
#-*- coding: utf-8 -*-
Pierre-Yves Chibon ac8023
Pierre-Yves Chibon ac8023
"""
Pierre-Yves Chibon ac8023
 (c) 2014 - Copyright Red Hat Inc
Pierre-Yves Chibon ac8023
Pierre-Yves Chibon ac8023
 Authors:
Pierre-Yves Chibon ac8023
   Pierre-Yves Chibon <pingou@pingoured.fr></pingou@pingoured.fr>
Pierre-Yves Chibon ac8023
Pierre-Yves Chibon ac8023
"""
Pierre-Yves Chibon ac8023
Pierre-Yves Chibon ac8023
import flask
Pierre-Yves Chibon ac8023
import os
Pierre-Yves Chibon ac8023
from math import ceil
Pierre-Yves Chibon ac8023
Pierre-Yves Chibon ac8023
import pygit2
Pierre-Yves Chibon ac8023
from sqlalchemy.exc import SQLAlchemyError
Pierre-Yves Chibon ac8023
from pygments import highlight
Pierre-Yves Chibon ac8023
from pygments.lexers import guess_lexer
Pierre-Yves Chibon ac8023
from pygments.lexers.text import DiffLexer
Pierre-Yves Chibon ac8023
from pygments.formatters import HtmlFormatter
Pierre-Yves Chibon ac8023
Pierre-Yves Chibon ac8023
Pierre-Yves Chibon ac8023
import progit.app
Pierre-Yves Chibon ac8023
import progit.exceptions
Pierre-Yves Chibon ac8023
import progit.lib
Pierre-Yves Chibon ac8023
import progit.forms
Pierre-Yves Chibon ac8023
from progit import APP, SESSION, LOG, __get_file_in_tree
Pierre-Yves Chibon ac8023
Pierre-Yves Chibon ac8023
Pierre-Yves Chibon ac8023
### Application
Pierre-Yves Chibon ac8023
@APP.route('/')
Pierre-Yves Chibon ac8023
def index():
Pierre-Yves Chibon ac8023
    """ Front page of the application.
Pierre-Yves Chibon ac8023
    """
Pierre-Yves Chibon ac8023
    page = flask.request.args.get('page', 1)
Pierre-Yves Chibon ac8023
    try:
Pierre-Yves Chibon ac8023
        page = int(page)
Pierre-Yves Chibon ac8023
    except ValueError:
Pierre-Yves Chibon ac8023
        page = 1
Pierre-Yves Chibon ac8023
Pierre-Yves Chibon ac8023
    limit = APP.config['ITEM_PER_PAGE']
Pierre-Yves Chibon ac8023
    start = limit * (page - 1)
Pierre-Yves Chibon ac8023
Pierre-Yves Chibon ac8023
    repos = progit.lib.list_projects(SESSION, fork=False, start=start, limit=limit)
Pierre-Yves Chibon ac8023
    num_repos = progit.lib.list_projects(SESSION, fork=False, count=True)
Pierre-Yves Chibon ac8023
Pierre-Yves Chibon ac8023
    total_page = int(ceil(num_repos / float(limit)))
Pierre-Yves Chibon ac8023
Pierre-Yves Chibon ac8023
    return flask.render_template(
Pierre-Yves Chibon ac8023
        'index.html',
Pierre-Yves Chibon ac8023
        repos=repos,
Pierre-Yves Chibon ac8023
        total_page=total_page,
Pierre-Yves Chibon ac8023
        page=page,
Pierre-Yves Chibon ac8023
    )
Pierre-Yves Chibon ac8023
Pierre-Yves Chibon ac8023
Pierre-Yves Chibon ac8023
@APP.route('/users/')
Pierre-Yves Chibon ac8023
def view_users():
Pierre-Yves Chibon ac8023
    """ Present the list of users.
Pierre-Yves Chibon ac8023
    """
Pierre-Yves Chibon ac8023
    page = flask.request.args.get('page', 1)
Pierre-Yves Chibon ac8023
    try:
Pierre-Yves Chibon ac8023
        page = int(page)
Pierre-Yves Chibon ac8023
    except ValueError:
Pierre-Yves Chibon ac8023
        page = 1
Pierre-Yves Chibon ac8023
Pierre-Yves Chibon ac8023
    ## TODO: retrieve this from the DB
Pierre-Yves Chibon ac8023
    users = ['pingou']
Pierre-Yves Chibon ac8023
Pierre-Yves Chibon ac8023
    limit = APP.config['ITEM_PER_PAGE']
Pierre-Yves Chibon ac8023
    start = limit * (page - 1)
Pierre-Yves Chibon ac8023
    end = limit * page
Pierre-Yves Chibon ac8023
    users_length = len(users)
Pierre-Yves Chibon ac8023
    users = users[start:end]
Pierre-Yves Chibon ac8023
Pierre-Yves Chibon ac8023
    total_page = int(ceil(users_length / float(limit)))
Pierre-Yves Chibon ac8023
Pierre-Yves Chibon ac8023
    return flask.render_template(
Pierre-Yves Chibon ac8023
        'user_list.html',
Pierre-Yves Chibon ac8023
        users=users,
Pierre-Yves Chibon ac8023
        total_page=total_page,
Pierre-Yves Chibon ac8023
        page=page,
Pierre-Yves Chibon ac8023
    )
Pierre-Yves Chibon ac8023
Pierre-Yves Chibon ac8023
Pierre-Yves Chibon ac8023
@APP.route('/user/<username>')</username>
Pierre-Yves Chibon ac8023
def view_user(username):
Pierre-Yves Chibon ac8023
    """ Front page of a specific user.
Pierre-Yves Chibon ac8023
    """
Pierre-Yves Chibon ac8023
Pierre-Yves Chibon ac8023
    repopage = flask.request.args.get('repopage', 1)
Pierre-Yves Chibon ac8023
    try:
Pierre-Yves Chibon ac8023
        repopage = int(repopage)
Pierre-Yves Chibon ac8023
    except ValueError:
Pierre-Yves Chibon ac8023
        repopage = 1
Pierre-Yves Chibon ac8023
Pierre-Yves Chibon ac8023
    forkpage = flask.request.args.get('forkpage', 1)
Pierre-Yves Chibon ac8023
    try:
Pierre-Yves Chibon ac8023
        forkpage = int(forkpage)
Pierre-Yves Chibon ac8023
    except ValueError:
Pierre-Yves Chibon ac8023
        forkpage = 1
Pierre-Yves Chibon ac8023
Pierre-Yves Chibon ac8023
    limit = APP.config['ITEM_PER_PAGE']
Pierre-Yves Chibon ac8023
    repo_start = limit * (repopage - 1)
Pierre-Yves Chibon ac8023
    fork_start = limit * (forkpage - 1)
Pierre-Yves Chibon ac8023
Pierre-Yves Chibon ac8023
    repos = progit.lib.list_projects(
Pierre-Yves Chibon ac8023
        SESSION,
Pierre-Yves Chibon ac8023
        username=flask.g.fas_user.username,
Pierre-Yves Chibon ac8023
        fork=False,
Pierre-Yves Chibon ac8023
        start=repo_start,
Pierre-Yves Chibon ac8023
        limit=limit)
Pierre-Yves Chibon ac8023
    repos_length = progit.lib.list_projects(
Pierre-Yves Chibon ac8023
        SESSION,
Pierre-Yves Chibon ac8023
        username=flask.g.fas_user.username,
Pierre-Yves Chibon ac8023
        fork=False,
Pierre-Yves Chibon ac8023
        count=True)
Pierre-Yves Chibon ac8023
Pierre-Yves Chibon ac8023
    forks = progit.lib.list_projects(
Pierre-Yves Chibon ac8023
        SESSION,
Pierre-Yves Chibon ac8023
        username=flask.g.fas_user.username,
Pierre-Yves Chibon ac8023
        fork=True,
Pierre-Yves Chibon ac8023
        start=fork_start,
Pierre-Yves Chibon ac8023
        limit=limit)
Pierre-Yves Chibon ac8023
    forks_length = progit.lib.list_projects(
Pierre-Yves Chibon ac8023
        SESSION,
Pierre-Yves Chibon ac8023
        username=flask.g.fas_user.username,
Pierre-Yves Chibon ac8023
        fork=True,
Pierre-Yves Chibon ac8023
        count=True)
Pierre-Yves Chibon ac8023
Pierre-Yves Chibon ac8023
    total_page_repos = int(ceil(repos_length / float(limit)))
Pierre-Yves Chibon ac8023
    total_page_forks = int(ceil(forks_length / float(limit)))
Pierre-Yves Chibon ac8023
Pierre-Yves Chibon ac8023
    repos_obj = [
Pierre-Yves Chibon ac8023
        pygit2.Repository(
Pierre-Yves Chibon ac8023
            os.path.join(APP.config['GIT_FOLDER'], repo.path))
Pierre-Yves Chibon ac8023
        for repo in repos]
Pierre-Yves Chibon ac8023
Pierre-Yves Chibon ac8023
    forks_obj = [
Pierre-Yves Chibon ac8023
        pygit2.Repository(
Pierre-Yves Chibon ac8023
            os.path.join(APP.config['FORK_FOLDER'], repo.path))
Pierre-Yves Chibon ac8023
        for repo in forks]
Pierre-Yves Chibon ac8023
Pierre-Yves Chibon ac8023
    return flask.render_template(
Pierre-Yves Chibon ac8023
        'user_info.html',
Pierre-Yves Chibon ac8023
        username=username,
Pierre-Yves Chibon ac8023
        repos=repos,
Pierre-Yves Chibon ac8023
        repos_obj=repos_obj,
Pierre-Yves Chibon ac8023
        total_page_repos=total_page_repos,
Pierre-Yves Chibon ac8023
        forks=forks,
Pierre-Yves Chibon ac8023
        forks_obj=forks_obj,
Pierre-Yves Chibon ac8023
        total_page_forks=total_page_forks,
Pierre-Yves Chibon ac8023
        repopage=repopage,
Pierre-Yves Chibon ac8023
        forkpage=forkpage,
Pierre-Yves Chibon ac8023
    )
Pierre-Yves Chibon ac8023
Pierre-Yves Chibon ac8023
Pierre-Yves Chibon ac8023
@APP.route('/new/', methods=('GET', 'POST'))
Pierre-Yves Chibon ac8023
def new_project():
Pierre-Yves Chibon ac8023
    """ Form to create a new project.
Pierre-Yves Chibon ac8023
    """
Pierre-Yves Chibon ac8023
    form = progit.forms.ProjectForm()
Pierre-Yves Chibon ac8023
    if form.validate_on_submit():
Pierre-Yves Chibon ac8023
        name = form.name.data
Pierre-Yves Chibon ac8023
        description = form.description.data
Pierre-Yves Chibon ac8023
Pierre-Yves Chibon ac8023
        try:
Pierre-Yves Chibon ac8023
            message = progit.lib.new_project(
Pierre-Yves Chibon ac8023
                SESSION,
Pierre-Yves Chibon ac8023
                name=name,
Pierre-Yves Chibon ac8023
                description=description,
Pierre-Yves Chibon ac8023
                user=flask.g.fas_user.username,
Pierre-Yves Chibon ac8023
                folder=APP.config['GIT_FOLDER'],
Pierre-Yves Chibon ac8023
            )
Pierre-Yves Chibon ac8023
            SESSION.commit()
Pierre-Yves Chibon ac8023
            flask.flash(message)
Pierre-Yves Chibon ac8023
            return flask.redirect(flask.url_for('view_repo', repo=name))
Pierre-Yves Chibon ac8023
        except progit.exceptions.ProgitException, err:
Pierre-Yves Chibon ac8023
            flask.flash(str(err), 'error')
Pierre-Yves Chibon ac8023
        except SQLAlchemyError, err:  # pragma: no cover
Pierre-Yves Chibon ac8023
            SESSION.rollback()
Pierre-Yves Chibon ac8023
            flask.flash(str(err), 'error')
Pierre-Yves Chibon ac8023
Pierre-Yves Chibon ac8023
    return flask.render_template(
Pierre-Yves Chibon ac8023
        'new_project.html',
Pierre-Yves Chibon ac8023
        form=form,
Pierre-Yves Chibon ac8023
    )
Pierre-Yves Chibon ac8023
Pierre-Yves Chibon ac8023
Pierre-Yves Chibon ac8023
@APP.route('/<repo>')</repo>
Pierre-Yves Chibon ac8023
def view_repo(repo):
Pierre-Yves Chibon ac8023
    """ Front page of a specific repo.
Pierre-Yves Chibon ac8023
    """
Pierre-Yves Chibon ac8023
    return progit.app.view_repo(repo=repo)
Pierre-Yves Chibon ac8023
Pierre-Yves Chibon ac8023
Pierre-Yves Chibon ac8023
@APP.route('/fork/<username>/<repo>')</repo></username>
Pierre-Yves Chibon ac8023
def view_fork_repo(username, repo):
Pierre-Yves Chibon ac8023
    """ Front page of a specific repo.
Pierre-Yves Chibon ac8023
    """
Pierre-Yves Chibon ac8023
    return progit.app.view_repo(repo=repo, username=username)
Pierre-Yves Chibon ac8023
Pierre-Yves Chibon ac8023
Pierre-Yves Chibon ac8023
@APP.route('/<repo>/branch/<branchname>')</branchname></repo>
Pierre-Yves Chibon ac8023
def view_repo_branch(repo, branchname):
Pierre-Yves Chibon ac8023
    return progit.app.view_repo_branch(repo, branchname)
Pierre-Yves Chibon ac8023
Pierre-Yves Chibon ac8023
Pierre-Yves Chibon ac8023
@APP.route('/fork/<username>/<repo>/branch/<branchname>')</branchname></repo></username>
Pierre-Yves Chibon ac8023
def view_fork_repo_branch(username, repo, branchname):
Pierre-Yves Chibon ac8023
    """ Displays the information about a specific branch.
Pierre-Yves Chibon ac8023
    """
Pierre-Yves Chibon ac8023
    return progit.app.view_repo_branch(repo, branchname, username=username)
Pierre-Yves Chibon ac8023
Pierre-Yves Chibon ac8023
Pierre-Yves Chibon ac8023
@APP.route('/<repo>/log')</repo>
Pierre-Yves Chibon ac8023
@APP.route('/<repo>/log/<branchname>')</branchname></repo>
Pierre-Yves Chibon ac8023
def view_log(repo, branchname=None):
Pierre-Yves Chibon ac8023
    """ Displays the logs of the specified repo.
Pierre-Yves Chibon ac8023
    """
Pierre-Yves Chibon ac8023
    return progit.app.view_log(repo, branchname)
Pierre-Yves Chibon ac8023
Pierre-Yves Chibon ac8023
Pierre-Yves Chibon ac8023
@APP.route('/fork/<username>/<repo>/log')</repo></username>
Pierre-Yves Chibon ac8023
@APP.route('/fork/<username>/<repo>/log/<branchname>')</branchname></repo></username>
Pierre-Yves Chibon ac8023
def view_fork_log(username, repo, branchname=None):
Pierre-Yves Chibon ac8023
    """ Displays the logs of the specified repo.
Pierre-Yves Chibon ac8023
    """
Pierre-Yves Chibon ac8023
    return progit.app.view_log(repo, branchname, username=username)
Pierre-Yves Chibon ac8023
Pierre-Yves Chibon ac8023
Pierre-Yves Chibon ac8023
@APP.route('/<repo>/blob/<identifier>/<path:filename>')</path:filename></identifier></repo>
Pierre-Yves Chibon ac8023
@APP.route('/<repo>/blob/<identifier>/<path:filename>')</path:filename></identifier></repo>
Pierre-Yves Chibon ac8023
def view_file(repo, identifier, filename):
Pierre-Yves Chibon ac8023
    """ Displays the content of a file or a tree for the specified repo.
Pierre-Yves Chibon ac8023
    """
Pierre-Yves Chibon ac8023
    return progit.app.view_file(repo, identifier, filename)
Pierre-Yves Chibon ac8023
Pierre-Yves Chibon ac8023
Pierre-Yves Chibon ac8023
@APP.route('/fork/<username>/<repo>/blob/<identifier>/<path:filename>')</path:filename></identifier></repo></username>
Pierre-Yves Chibon ac8023
@APP.route('/fork/<username>/<repo>/blob/<identifier>/<path:filename>')</path:filename></identifier></repo></username>
Pierre-Yves Chibon ac8023
def view_fork_file(username, repo, identifier, filename):
Pierre-Yves Chibon ac8023
    """ Displays the content of a file or a tree for the specified repo.
Pierre-Yves Chibon ac8023
    """
Pierre-Yves Chibon ac8023
    return progit.app.view_file(repo, identifier, filename, username=username)
Pierre-Yves Chibon ac8023
Pierre-Yves Chibon ac8023
Pierre-Yves Chibon ac8023
@APP.route('/<repo>/<commitid>')</commitid></repo>
Pierre-Yves Chibon ac8023
def view_commit(repo, commitid):
Pierre-Yves Chibon ac8023
    """ Render a commit in a repo
Pierre-Yves Chibon ac8023
    """
Pierre-Yves Chibon ac8023
    return progit.app.view_commit(repo, commitid)
Pierre-Yves Chibon ac8023
Pierre-Yves Chibon ac8023
Pierre-Yves Chibon ac8023
@APP.route('/fork/<username>/<repo>/<commitid>')</commitid></repo></username>
Pierre-Yves Chibon ac8023
def view_fork_commit(username, repo, commitid):
Pierre-Yves Chibon ac8023
    """ Render a commit in a repo
Pierre-Yves Chibon ac8023
    """
Pierre-Yves Chibon ac8023
    return progit.app.view_commit(repo, commitid, username=username)
Pierre-Yves Chibon ac8023
Pierre-Yves Chibon ac8023
Pierre-Yves Chibon ac8023
@APP.route('/<repo>/tree/')</repo>
Pierre-Yves Chibon ac8023
@APP.route('/<repo>/tree/<identifier>')</identifier></repo>
Pierre-Yves Chibon ac8023
def view_tree(repo, identifier=None):
Pierre-Yves Chibon ac8023
    """ Render the tree of the repo
Pierre-Yves Chibon ac8023
    """
Pierre-Yves Chibon ac8023
    return progit.app.view_tree(repo, identifier=identifier)
Pierre-Yves Chibon ac8023
Pierre-Yves Chibon ac8023
Pierre-Yves Chibon ac8023
@APP.route('/fork/<username>/<repo>/tree/')</repo></username>
Pierre-Yves Chibon ac8023
@APP.route('/fork/<username>/<repo>/tree/<identifier>')</identifier></repo></username>
Pierre-Yves Chibon ac8023
def view_fork_tree(username, repo, identifier=None):
Pierre-Yves Chibon ac8023
    """ Render the tree of the repo
Pierre-Yves Chibon ac8023
    """
Pierre-Yves Chibon ac8023
    return progit.app.view_tree(repo, identifier=identifier, username=username)
Pierre-Yves Chibon ac8023
Pierre-Yves Chibon ac8023
Pierre-Yves Chibon ac8023
@APP.route('/<repo>/issues')</repo>
Pierre-Yves Chibon ac8023
def view_issues(repo):
Pierre-Yves Chibon ac8023
    """ List all issues associated to a repo
Pierre-Yves Chibon ac8023
    """
Pierre-Yves Chibon ac8023
    return progit.app.view_issues(repo)
Pierre-Yves Chibon ac8023
Pierre-Yves Chibon ac8023
Pierre-Yves Chibon ac8023
@APP.route('/fork/<username>/<repo>/issues')</repo></username>
Pierre-Yves Chibon ac8023
def view_fork_issues(repo, username):
Pierre-Yves Chibon ac8023
    """ List all issues associated to a repo
Pierre-Yves Chibon ac8023
    """
Pierre-Yves Chibon ac8023
    return progit.app.view_issues(repo, username=username)
Pierre-Yves Chibon ac8023
Pierre-Yves Chibon ac8023
Pierre-Yves Chibon ac8023
@APP.route('/<repo>/new_issue', methods=('GET', 'POST'))</repo>
Pierre-Yves Chibon ac8023
def new_issue(repo):
Pierre-Yves Chibon ac8023
    """ Create a new issue
Pierre-Yves Chibon ac8023
    """
Pierre-Yves Chibon ac8023
    return progit.app.new_issue(repo)
Pierre-Yves Chibon ac8023
Pierre-Yves Chibon ac8023
Pierre-Yves Chibon ac8023
@APP.route('/fork/<username>/<repo>/new_issue', methods=('GET', 'POST'))</repo></username>
Pierre-Yves Chibon ac8023
def fork_new_issue(username, repo):
Pierre-Yves Chibon ac8023
    """ Create a new issue
Pierre-Yves Chibon ac8023
    """
Pierre-Yves Chibon ac8023
    return progit.app.new_issue(repo, username=username)
Pierre-Yves Chibon ac8023
Pierre-Yves Chibon ac8023
Pierre-Yves Chibon ac8023
@APP.route('/<repo>/issue/<issueid>')</issueid></repo>
Pierre-Yves Chibon ac8023
def view_issue(repo, issueid):
Pierre-Yves Chibon ac8023
    """ List all issues associated to a repo
Pierre-Yves Chibon ac8023
    """
Pierre-Yves Chibon ac8023
    return progit.app.view_issue(repo, issueid)
Pierre-Yves Chibon ac8023
Pierre-Yves Chibon ac8023
Pierre-Yves Chibon ac8023
@APP.route('/fork/<username>/<repo>/issue/<issueid>')</issueid></repo></username>
Pierre-Yves Chibon ac8023
def view_fork_issue(username, repo, issueid):
Pierre-Yves Chibon ac8023
    """ List all issues associated to a repo
Pierre-Yves Chibon ac8023
    """
Pierre-Yves Chibon ac8023
    return progit.app.view_issue(repo, issueid, username=username)
Pierre-Yves Chibon ac8023
Pierre-Yves Chibon ac8023
Pierre-Yves Chibon ac8023
@APP.route('/<repo>/issue/<issueid>/edit', methods=('GET', 'POST'))</issueid></repo>
Pierre-Yves Chibon ac8023
def edit_issue(repo, issueid):
Pierre-Yves Chibon ac8023
    """ Edit the specified issue
Pierre-Yves Chibon ac8023
    """
Pierre-Yves Chibon ac8023
    return progit.app.edit_issue(repo, issueid)
Pierre-Yves Chibon ac8023
Pierre-Yves Chibon ac8023
Pierre-Yves Chibon ac8023
@APP.route('/fork/<username>/<repo>/issue/<issueid>/edit',</issueid></repo></username>
Pierre-Yves Chibon ac8023
           methods=('GET', 'POST'))
Pierre-Yves Chibon ac8023
def fork_edit_issue(username, repo, issueid):
Pierre-Yves Chibon ac8023
    """ Edit the specified issue opened against a fork
Pierre-Yves Chibon ac8023
    """
Pierre-Yves Chibon ac8023
    return progit.app.edit_issue(repo, issueid, username=username)
Pierre-Yves Chibon ac8023
Pierre-Yves Chibon ac8023
Pierre-Yves Chibon ac8023
@APP.route('/<repo>/request-pull/<requestid>')</requestid></repo>
Pierre-Yves Chibon 40f54b
def request_pull(repo, requestid):
Pierre-Yves Chibon ac8023
    """ Request pulling the changes from the fork into the project.
Pierre-Yves Chibon ac8023
    """
Pierre-Yves Chibon ac8023
    return progit.app.request_pull(repo, requestid=requestid)
Pierre-Yves Chibon ac8023
Pierre-Yves Chibon ac8023
Pierre-Yves Chibon ac8023
@APP.route('/fork/<username>/<repo>/request-pull/<requestid>')</requestid></repo></username>
Pierre-Yves Chibon 40f54b
def fork_request_pull(username, repo, requestid):
Pierre-Yves Chibon ac8023
    """ Request pulling the changes from the fork into the project.
Pierre-Yves Chibon ac8023
    """
Pierre-Yves Chibon ac8023
    return progit.app.request_pull(repo, requestid=requestid, username=username)