cc1  v2.1
CC1 source code docs
 All Classes Namespaces Files Functions Variables Pages
key.py
Go to the documentation of this file.
1 # -*- coding: utf-8 -*-
2 # @COPYRIGHT_begin
3 #
4 # Copyright [2010-2014] Institute of Nuclear Physics PAN, Krakow, Poland
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 #
18 # @COPYRIGHT_end
19 
20 ##
21 # @package src.wi.views.user.key
22 #
23 # @author Krzysztof Danielowski
24 # @author Piotr W√≥jcik
25 #
26 import urllib
27 
28 from django.http import HttpResponse
29 from django.shortcuts import redirect
30 from django.template import RequestContext
31 from django.template.loader import render_to_string
32 from django.utils.translation import ugettext as _
33 from django.views.decorators.csrf import csrf_protect
34 
35 from wi.forms.key import GenerateKeyForm
36 from wi.utils import messages_ajax
37 from wi.utils.decorators import django_view, user_permission
38 from wi.utils.messages_ajax import ajax_request
39 from wi.utils.views import prep_data
40 
41 
42 @django_view
43 @user_permission
44 ##
45 #
46 # View returning a file with the ssh key.
47 #
48 def res_key_file(request, redirect_view='res_keys'):
49  if request.method == "GET":
50  if request.GET.get('name') and request.GET.get('file'):
51  response = HttpResponse(content=urllib.unquote_plus(request.GET.get('file')), content_type='plain/text')
52  response['Content-Disposition'] = 'attachment; filename=' + request.GET.get('name')
53  return response
54  return redirect(redirect_view)
55 
56 
57 @django_view
58 @ajax_request
59 @user_permission
60 @csrf_protect
61 ##
62 #
63 # Ajax view for ssh key generation.
64 #
65 def res_ajax_generate_key(request, template_name='generic/form.html', form_class=GenerateKeyForm):
66  if request.method == 'POST':
67  form = form_class(request.POST)
68  if form.is_valid():
69  rest_data = prep_data({'key': ('user/key/generate/', {'name': form.cleaned_data['name']})}, request.session)
70 
71  return messages_ajax.success_with_key(_("You have successfully generated a key"),
72  rest_data['key'],
73  form.cleaned_data['name'])
74  else:
75  form = form_class()
76  return messages_ajax.success(render_to_string(template_name, {'form': form,
77  'confirmation': _('Generate'),
78  'text': _('2048-bit RSA key<br /><b>Note!</b> We don\'t keep a copy of your private key. We can\'t recreate it if your key is lost.')},
79  context_instance=RequestContext(request)),
80  status=1)
81