summaryrefslogtreecommitdiffstats
path: root/israwhidebroken/controllers.py
blob: 01b04970a1be8a77b3617eb649399f5fa18b7dd4 (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
import turbogears as tg
from turbogears import controllers, expose, flash
# from israwhidebroken import model
from turbogears import identity, redirect
from cherrypy import request, response
# from israwhidebroken import json
# import logging
# log = logging.getLogger("israwhidebroken.controllers")

class Root(controllers.RootController):
    @expose(template="israwhidebroken.templates.index")
    def index(self):
        # TODO: find most recent tree(s)
        return dict(in_qa='qa' in identity.current.groups,
                    admin='qa-admin' in identity.current.groups)

    @expose(template="israwhidebroken.templates.login")
    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():
            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()
    def logout(self):
        identity.current.logout()
        redirect("/")