]> de.git.xonotic.org Git - xonotic/xonstat.git/blob - xonstat/views/admin.py
Add actual merge functionality.
[xonotic/xonstat.git] / xonstat / views / admin.py
1 from pyramid.response import Response
2 from pyramid.httpexceptions import HTTPForbidden, HTTPFound
3 from pyramid.security import remember, forget
4 from pyramid.session import check_csrf_token
5 from pyramid_persona.views import verify_login
6 from xonstat.models import *
7
8 def forbidden(request):
9     '''A simple forbidden view. Does nothing more than set the status and then
10     gets the heck out of dodge. The forbidden.mako template does the work.'''
11     request.response.status = 403
12     return {}
13
14 def login(request):
15     # Verify the assertion and get the email of the user
16     persona_email = verify_login(request)
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         log.debug("Session token is %s" % st)
45         log.debug("Request token is %s" % request.params.get('csrf_token'))
46         check_csrf_token(request)
47
48         if request.params.has_key("w_pid") and request.params.has_key("l_pid"):
49             w_pid = request.params.get("w_pid")
50             l_pid = request.params.get("l_pid")
51
52             # do the merge, hope for the best!
53             try:
54                 s.execute("select merge_players(:w_pid, :l_pid)",
55                     {"w_pid": w_pid, "l_pid": l_pid})
56
57                 s.commit()
58
59             except:
60                 s.rollback()
61
62     return {}