]> de.git.xonotic.org Git - xonotic/xonstat.git/blob - xonstat/views/admin.py
Remove more refs to sqlahelper.
[xonotic/xonstat.git] / xonstat / views / admin.py
1 from pyramid.httpexceptions import HTTPFound
2 from pyramid.security import remember
3 from pyramid.session import check_csrf_token
4 from xonstat.models import DBSession, Player
5
6
7 def forbidden(request):
8     '''A simple forbidden view. Does nothing more than set the status and then
9     gets the heck out of dodge. The forbidden.mako template does the work.'''
10     request.response.status = 403
11     return {}
12
13 def login(request):
14     # Verify the assertion and get the email of the user
15     # Short-circuit this to prevent anyone from logging in right now.
16     persona_email = None
17
18     # Check that the email exists in the players table
19     player_email = DBSession.query(Player).\
20             filter(Player.email_addr == persona_email).one()
21
22     #log.debug("Verified email address: %s" % persona_email)
23     #log.debug("Corresponding player is %s" % player_email)
24
25     if player_email is not None:
26         # Add the headers required to remember the user to the response
27         request.response.headers.extend(remember(request, persona_email))
28     else:
29         url = request.route_url("forbidden")
30         return HTTPFound(location=url)
31
32     # Return a json message containing the address or path to redirect to.
33     return {'redirect': request.POST['came_from'], 'success': True}
34
35
36 def merge(request):
37     '''A simple merge view. The merge.mako template does the work.'''
38     s = DBSession()
39
40     # only do a merge if we have all of the required data
41     if request.params.has_key("csrf_token"):
42         # check the token to prevent request forgery
43         st = request.session.get_csrf_token()
44         check_csrf_token(request)
45
46         if request.params.has_key("w_pid") and request.params.has_key("l_pid"):
47             w_pid = request.params.get("w_pid")
48             l_pid = request.params.get("l_pid")
49
50             # do the merge, hope for the best!
51             try:
52                 s.execute("select merge_players(:w_pid, :l_pid)",
53                     {"w_pid": w_pid, "l_pid": l_pid})
54
55                 s.commit()
56
57                 request.session.flash(
58                     "Successfully merged player %s into %s!" % (l_pid, w_pid),
59                     "success")
60
61             except:
62                 s.rollback()
63
64                 request.session.flash(
65                     "Could not merge player %s into %s." % (l_pid, w_pid),
66                     "failure")
67
68     return {}