]> de.git.xonotic.org Git - xonotic/xonstat.git/blob - xonstat/views/admin.py
8d5c20c885cf66a08c922a1406f279ffc5f732fc
[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         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 {}