Source

buglink / web / views.py

Full commit

import models
from pagination import Pagination

from flask import Blueprint, render_template, abort, request
from flask import flash, make_response

import sqlalchemy

views = Blueprint('views', __name__)

PER_PAGE = 20

@views.route('/')
def index():
    return render_template('index.html')

@views.route('/issues/', defaults={'page': 1})
@views.route('/issues/page/<int:page>')
def listissues(page):
    query = request.dbsession.query(models.Issue). \
                              order_by(models.Issue.ref.desc())
    items = query.limit(PER_PAGE).offset((page - 1) * PER_PAGE).all()
    if not items and page != 1:
        abort(404)
    pgnation = Pagination(page, PER_PAGE, query.count())
    return render_template('issues.html',
                           pagination=pgnation,
                           issues=items,
                           )

@views.route('/issue/<ref>')
def issue(ref):
    issue = request.dbsession.query(models.Issue).filter_by(ref=ref).first()
    if issue is None:
        abort(404)
    return render_template('issue.html', issue=issue)

@views.route('/builds/', defaults={'page': 1})
@views.route('/builds/page/<int:page>')
def listbuilds(page):
    query = request.dbsession.query(models.Build) \
                             .order_by(models.Build.number.desc())
    items = query.limit(PER_PAGE).offset((page - 1) * PER_PAGE).all()
    if not items and page != 1:
        abort(404)
    pgnation = Pagination(page, PER_PAGE, query.count())
    return render_template('builds.html',
                           pagination=pgnation,
                           builds=items,
                           )

@views.route('/build/<int:number>')
def build(number):
    try:
        build = request.dbsession.query(models.Build).filter_by(number=number).one()
    except sqlalchemy.orm.exc.NoResultFound:
        abort(404)
    return render_template('build.html', build=build)


@views.route('/releases/', defaults={'page': 1})
@views.route('/releases/page/<int:page>')
def listreleases(page):
    query = request.dbsession.query(models.Release) \
                             .order_by(models.Release.ref.desc()) \
                             .order_by(models.Release.rc.desc())
    items = query.limit(PER_PAGE).offset((page - 1) * PER_PAGE).all()
    if not items and page != 1:
        abort(404)
    pgnation = Pagination(page, PER_PAGE, query.count())
    return render_template('releases.html',
                           pagination=pgnation,
                           releases=items)

@views.route('/release/<ref>/<int:rc>')
@views.route('/release/<ref>', defaults={'rc': -1})
def release(ref, rc):
    query = request.dbsession.query(models.Release) \
                             .filter_by(ref=ref)
    if rc == -1:
        query = query.filter_by(final=True)
    else:
        query = query.filter_by(rc=rc)

    try:
        release = query.one()
    except sqlalchemy.orm.exc.NoResultFound:
        abort(404)
    return render_template('release.html', release=release)

@views.route('/binaries/', defaults={'page': 1})
@views.route('/binaries/page/<int:page>')
def listbinaries(page):
    query = request.dbsession.query(models.Binary) \
                             .order_by(models.Binary.id.desc())
    items = query.limit(PER_PAGE).offset((page - 1) * PER_PAGE).all()
    if not items and page != 1:
        abort(404)
    pgnation = Pagination(page, PER_PAGE, query.count())
    return render_template('binaries.html',
                           pagination=pgnation,
                           binaries=items,
                           )


@views.route('/binary/<name>/<int:build>')
def binary(name, build):
    query = request.dbsession.query(models.Binary) \
                             .join(models.Module) \
                             .join(models.Build) \
                             .filter(models.Module.name==name) \
                             .filter(models.Build.number==build)
    try:
        binary = query.one()
    except sqlalchemy.orm.exc.NoResultFound:
        abort(404)
    return render_template('binary.html', binary=binary)

@views.route('/repositories/', defaults={'page': 1})
@views.route('/repositories/page/<int:page>')
def listrepositories(page):
    query = request.dbsession.query(models.Repository) \
                             .order_by(models.Repository.path)
    items = query.limit(PER_PAGE).offset((page - 1) * PER_PAGE).all()
    if not items and page != 1:
        abort(404)
    pgnation = Pagination(page, PER_PAGE, query.count())
    filling = []
    for repository in items:
        count = request.dbsession.query(models.Changeset) \
                                 .filter_by(repository=repository) \
                                 .filter(models.Changeset.issues.any()) \
                                 .count()
        total = len(repository.changesets)
        if total == 0:
            filling.append(100.)
        else:
            filling.append((count * 100.) / total)
    return render_template('repositories.html',
                           pagination=pgnation,
                           repositories=items,
                           filling=filling,
                           )

@views.route('/modules/', defaults={'page': 1})
@views.route('/modules/page/<int:page>')
def listmodules(page):
    query = request.dbsession.query(models.Module) \
                             .order_by(models.Module.name)
    items = query.limit(PER_PAGE).offset((page - 1) * PER_PAGE).all()
    if not items and page != 1:
        abort(404)
    pgnation = Pagination(page, PER_PAGE, query.count())
    return render_template('modules.html',
                           pagination=pgnation,
                           modules=items,
                           )

@views.route('/module/<name>')
def module(name):
    query = request.dbsession.query(models.Module) \
                             .filter_by(name=name)
    try:
        module = query.one()
    except sqlalchemy.orm.exc.NoResultFound:
        abort(404)
    return render_template('module.html', module=module)

def text_response(data, code=None):
    if code is None:
        rv = make_response(str(data))
    else:
        rv = make_response(str(data), code)
    rv.mimetype='text/plain'
    return rv
                           
@views.route('/newbuild')
def newbuild():
    query = request.dbsession.query(models.Build) \
                             .order_by(models.Build.number.desc())
    last = query.first() # desc ordering

    number = 1
    if last is not None:
        number += last.number

    newbuild = models.Build(number = number)
    request.dbsession.add(newbuild)
    request.dbsession.commit()
    flash("Created new build %d" % number)
    return text_response(newbuild.number)