Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-app / synnefo / helpdesk / helpdesk.py @ 9c0ac5af

History | View | Annotate | Download (3.7 kB)

1
# vim: set fileencoding=utf-8 :
2
# Copyright 2011 GRNET S.A. All rights reserved.
3
#
4
# Redistribution and use in source and binary forms, with or without
5
# modification, are permitted provided that the following conditions
6
# are met:
7
#
8
#   1. Redistributions of source code must retain the above copyright
9
#      notice, this list of conditions and the following disclaimer.
10
#
11
#  2. Redistributions in binary form must reproduce the above copyright
12
#     notice, this list of conditions and the following disclaimer in the
13
#     documentation and/or other materials provided with the distribution.
14
#
15
# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
16
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18
# ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
19
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25
# SUCH DAMAGE.
26
#
27
# The views and conclusions contained in the software and documentation are
28
# those of the authors and should not be interpreted as representing official
29
# policies, either expressed or implied, of GRNET S.A.
30
import json
31
import time 
32

    
33
from django.views.decorators.csrf import csrf_protect
34
from django.template.loader import render_to_string
35
from django.template.context import RequestContext
36
from django.http import HttpResponse, HttpResponseBadRequest
37
from synnefo.db.models import SynnefoUser, Invitations
38
from synnefo.api.common import method_not_allowed
39
from synnefo.logic import users
40

    
41
@csrf_protect
42
def index(request):
43

    
44
    if request.method == 'GET':
45
        data = render_to_string('helpdesk.html',
46
                                {'users': get_users(request)},
47
                                context_instance=RequestContext(request))
48
        return HttpResponse(data)
49
    else:
50
        method_not_allowed(request)
51

    
52
def get_users(request):
53
    #XXX: The following filter should change when the invitations app is removed
54
    invitations = Invitations.objects.filter(accepted = False)
55
    ids = map(lambda x: x.target.id, invitations)
56
    users = SynnefoUser.objects.exclude(id__in = ids)\
57
                               .exclude(type__exact = "HELPDESK")\
58
                               .order_by('realname')
59
    result = []
60

    
61
    for user in users:
62
        resultentry = {}
63

    
64
        resultentry['id'] = user.id
65
        resultentry['name'] = user.realname
66

    
67
        result.append(resultentry)
68

    
69
    return result
70

    
71
def get_tmp_token(request):
72

    
73
    try:
74
        user_id = request.GET['user_id']
75
    except KeyError:
76
        return HttpResponseBadRequest()
77

    
78
    user = SynnefoUser.objects.get(id = user_id)
79

    
80
    if user is None:
81
        return HttpResponseBadRequest()
82

    
83
    if  user.tmp_auth_token_expires is None or \
84
        time.time() - time.mktime(user.tmp_auth_token_expires.timetuple()) > 0:
85
        users.create_tmp_token(user)
86

    
87
    token = dict()
88
    token['token'] = user.tmp_auth_token
89
    token['expires'] = int(time.mktime(user.tmp_auth_token_expires.timetuple()))
90

    
91
    response = HttpResponse(json.dumps(token))
92

    
93
    expire_fmt = user.tmp_auth_token_expires.strftime('%a, %d-%b-%Y %H:%M:%S %Z')
94
    response.set_cookie('X-Auth-Tmp-Token', value=user.tmp_auth_token,
95
                            expires = expire_fmt,
96
                            path='/')
97
    return response