summaryrefslogtreecommitdiffstats
path: root/israwhidebroken/controllers.py
blob: 08832166a23d4458834fb90a6cc7ce2e7d35f4a5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import turbogears as tg
import fedora
from turbogears import controllers, expose, flash
from israwhidebroken.model import Test, Tree, TestResult, hub
from sqlobject import AND
from turbogears import identity, redirect
from cherrypy import request, response
# from israwhidebroken import json
# import logging
# log = logging.getLogger("israwhidebroken.controllers")

def to_int(fstr):
    '''Returns fstr converted to int, or None if fstr is None'''
    return fstr and int(float(fstr))

class Root(controllers.RootController):
    # Main index page
    @expose(template="israwhidebroken.templates.index")
    def index(self, c=0):
        if not c:
            newest_tree = Tree.select(orderBy='-compose_id')[0]
            c = newest_tree.compose_id
        return dict(in_qa='qa' in identity.current.groups,
                    admin='qa-admin' in identity.current.groups,
                    c=c,
                    tests=Test.select(),
                    trees=Tree.select(Tree.q.compose_id == c))

    # JSON RPC calls
    @expose("json")
    def list_tests(self):
        tests = Test.select()
        return dict(tests=list(tests))

    @expose("json")
    def get_trees(self, *args, **kw):
        qlist = []
        for field in 'arch', 'compose_id', 'tree_time', 'repodata_time':
            if field in kw:
                qlist.append('%s == %s' % (field, Tree.sqlrepr(kw[field])))
        tree_results = Tree.select(' AND '.join(qlist))
        return dict(trees=list(tree_results))

    @expose("json")
    @identity.require(identity.in_group("qa-admin"))
    def add_tree(self, arch, compose_id, tree_time=None, repodata_time=None):
        t = Tree(arch=arch,
                 compose_id=compose_id,
                 tree_time=tree_time,
                 repodata_time=repodata_time)
        hub.commit()
        return dict(tree=t)

    @expose("json")
    @identity.require(identity.in_group("qa"))
    def add_result(self, treeid, testid, result):
        tr = TestResult(tree=treeid, test=testid, result=result)
        hub.commit() # XXX redundant?
        return dict(id=tr.id)

    @expose("json")
    @identity.require(identity.in_group("qa-admin"))
    def delete_result(self, id):
        return dict(youwantedtodelete=id, thisisastub=True)

    # Identity stuff (login/logout)
    @expose(template="israwhidebroken.templates.login")
    @expose(allow_json=True)
    def login(self, forward_url=None, *args, **kw):

        if forward_url:
            if isinstance(forward_url, list):
                forward_url = forward_url.pop(0)
            else:
                del request.params['forward_url']

        # If the login was successful...
        if not identity.current.anonymous and identity.was_login_attempted() \
                and not identity.get_identity_errors():
            if 'json' == fedora.tg.util.request_format():
                return dict(user=identity.current.user)
            flash("Welcome, %s" % identity.current.user_name)
            redirect(tg.url(forward_url or '/', kw))

        if identity.was_login_attempted():
            msg = _("The credentials you supplied were not correct or "
                   "did not grant access to this resource.")
        elif identity.get_identity_errors():
            msg = _("You must provide your credentials before accessing "
                   "this resource.")
        else:
            msg = _("Please log in.")
            if not forward_url:
                forward_url = request.headers.get("Referer", "/")

        response.status = 401
        return dict(logging_in=True, message=msg,
            forward_url=forward_url, previous_url=request.path_info,
            original_parameters=request.params)

    @expose(allow_json=True)
    def logout(self):
        identity.current.logout()
        if 'json' in fedora.tg.util.request_format():
            return dict()
        redirect("/")