Statistics
| Branch: | Tag: | Revision:

root / helpdesk / helpdesk.py @ 50a48b39

History | View | Annotate | Download (3.7 kB)

1 28f97815 Georgios Gousios
# vim: set fileencoding=utf-8 :
2 05310288 Georgios Gousios
# Copyright 2011 GRNET S.A. All rights reserved.
3 05310288 Georgios Gousios
#
4 05310288 Georgios Gousios
# Redistribution and use in source and binary forms, with or without
5 05310288 Georgios Gousios
# modification, are permitted provided that the following conditions
6 05310288 Georgios Gousios
# are met:
7 05310288 Georgios Gousios
#
8 05310288 Georgios Gousios
#   1. Redistributions of source code must retain the above copyright
9 05310288 Georgios Gousios
#      notice, this list of conditions and the following disclaimer.
10 05310288 Georgios Gousios
#
11 05310288 Georgios Gousios
#  2. Redistributions in binary form must reproduce the above copyright
12 05310288 Georgios Gousios
#     notice, this list of conditions and the following disclaimer in the
13 05310288 Georgios Gousios
#     documentation and/or other materials provided with the distribution.
14 05310288 Georgios Gousios
#
15 05310288 Georgios Gousios
# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
16 05310288 Georgios Gousios
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 05310288 Georgios Gousios
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 05310288 Georgios Gousios
# ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
19 05310288 Georgios Gousios
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 05310288 Georgios Gousios
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 05310288 Georgios Gousios
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 05310288 Georgios Gousios
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 05310288 Georgios Gousios
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 05310288 Georgios Gousios
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 05310288 Georgios Gousios
# SUCH DAMAGE.
26 05310288 Georgios Gousios
#
27 05310288 Georgios Gousios
# The views and conclusions contained in the software and documentation are
28 05310288 Georgios Gousios
# those of the authors and should not be interpreted as representing official
29 05310288 Georgios Gousios
# policies, either expressed or implied, of GRNET S.A.
30 68688470 Georgios Gousios
import json
31 15e9cf1a Georgios Gousios
import time 
32 05310288 Georgios Gousios
33 05310288 Georgios Gousios
from django.views.decorators.csrf import csrf_protect
34 05310288 Georgios Gousios
from django.template.loader import render_to_string
35 05310288 Georgios Gousios
from django.template.context import RequestContext
36 dd0e2b2d Georgios Gousios
from django.http import HttpResponse, HttpResponseBadRequest
37 05310288 Georgios Gousios
from synnefo.db.models import SynnefoUser, Invitations
38 05310288 Georgios Gousios
from synnefo.api.common import method_not_allowed
39 dd0e2b2d Georgios Gousios
from synnefo.logic import users
40 05310288 Georgios Gousios
41 05310288 Georgios Gousios
@csrf_protect
42 05310288 Georgios Gousios
def index(request):
43 05310288 Georgios Gousios
44 05310288 Georgios Gousios
    if request.method == 'GET':
45 05310288 Georgios Gousios
        data = render_to_string('helpdesk.html',
46 05310288 Georgios Gousios
                                {'users': get_users(request)},
47 05310288 Georgios Gousios
                                context_instance=RequestContext(request))
48 68688470 Georgios Gousios
        return HttpResponse(data)
49 05310288 Georgios Gousios
    else:
50 05310288 Georgios Gousios
        method_not_allowed(request)
51 05310288 Georgios Gousios
52 05310288 Georgios Gousios
def get_users(request):
53 05310288 Georgios Gousios
    #XXX: The following filter should change when the invitations app is removed
54 05310288 Georgios Gousios
    invitations = Invitations.objects.filter(accepted = False)
55 05310288 Georgios Gousios
    ids = map(lambda x: x.target.id, invitations)
56 e55b6c56 Georgios Gousios
    users = SynnefoUser.objects.exclude(id__in = ids)\
57 e55b6c56 Georgios Gousios
                               .exclude(type__exact = "HELPDESK")\
58 e55b6c56 Georgios Gousios
                               .order_by('realname')
59 05310288 Georgios Gousios
    result = []
60 05310288 Georgios Gousios
61 05310288 Georgios Gousios
    for user in users:
62 05310288 Georgios Gousios
        resultentry = {}
63 05310288 Georgios Gousios
64 05310288 Georgios Gousios
        resultentry['id'] = user.id
65 05310288 Georgios Gousios
        resultentry['name'] = user.realname
66 05310288 Georgios Gousios
67 05310288 Georgios Gousios
        result.append(resultentry)
68 05310288 Georgios Gousios
69 05310288 Georgios Gousios
    return result
70 05310288 Georgios Gousios
71 05310288 Georgios Gousios
def get_tmp_token(request):
72 dd0e2b2d Georgios Gousios
73 dd0e2b2d Georgios Gousios
    try:
74 dd0e2b2d Georgios Gousios
        user_id = request.GET['user_id']
75 dd0e2b2d Georgios Gousios
    except KeyError:
76 dd0e2b2d Georgios Gousios
        return HttpResponseBadRequest()
77 dd0e2b2d Georgios Gousios
78 dd0e2b2d Georgios Gousios
    user = SynnefoUser.objects.get(id = user_id)
79 dd0e2b2d Georgios Gousios
80 dd0e2b2d Georgios Gousios
    if user is None:
81 dd0e2b2d Georgios Gousios
        return HttpResponseBadRequest()
82 dd0e2b2d Georgios Gousios
83 15e9cf1a Georgios Gousios
    if  user.tmp_auth_token_expires is None or \
84 15e9cf1a Georgios Gousios
        time.time() - time.mktime(user.tmp_auth_token_expires.timetuple()) > 0:
85 dd0e2b2d Georgios Gousios
        users.create_tmp_token(user)
86 dd0e2b2d Georgios Gousios
87 68688470 Georgios Gousios
    token = dict()
88 dd0e2b2d Georgios Gousios
    token['token'] = user.tmp_auth_token
89 15e9cf1a Georgios Gousios
    token['expires'] = int(time.mktime(user.tmp_auth_token_expires.timetuple()))
90 68688470 Georgios Gousios
91 5ac53b64 Georgios Gousios
    response = HttpResponse(json.dumps(token))
92 5ac53b64 Georgios Gousios
93 5ac53b64 Georgios Gousios
    expire_fmt = user.tmp_auth_token_expires.strftime('%a, %d-%b-%Y %H:%M:%S %Z')
94 5ac53b64 Georgios Gousios
    response.set_cookie('X-Auth-Tmp-Token', value=user.tmp_auth_token,
95 5ac53b64 Georgios Gousios
                            expires = expire_fmt,
96 5ac53b64 Georgios Gousios
                            path='/')
97 5ac53b64 Georgios Gousios
    return response