Commit 4e4847fb by Scott Duckworth

create views to manage keys

parent 107a42c8
from django import forms
from sshkey.models import UserKey
class UserKeyForm(forms.ModelForm):
class Meta:
model = UserKey
fields = ['name', 'key']
{% if action == 'add' %}
<h1>Add SSH Key</h1>
{% else %}
<h1>Edit SSH Key</h1>
{% endif %}
{% if error_message %}
<p><strong>{{ error_message }}</strong></p>
{% endif %}
<form action="" method="post">
{% csrf_token %}
<table>
{{ form }}
</table>
<input type="submit" value="Save" />
</form>
<h1>My Keys</h1>
<p><a href="{% url sshkey.views.userkey_add %}">Add Key</a></p>
<table>
<tr>
<th>Key</th>
<th></th>
</tr>
{% for userkey in userkey_list %}
<tr>
<td>{{ userkey.name }}</td>
<td><a href="{% url sshkey.views.userkey_edit userkey.pk %}">Edit</a></td>
<td><a href="{% url sshkey.views.userkey_delete userkey.pk %}">Delete</a></td>
</tr>
{% endfor %}
</table>
......@@ -2,4 +2,8 @@ from django.conf.urls.defaults import patterns, url
urlpatterns = patterns('sshkey.views',
url(r'^lookup$', 'lookup'),
url(r'^$', 'userkey_list'),
url(r'^add$', 'userkey_add'),
url(r'^(?P<pk>\d+)$', 'userkey_edit'),
url(r'^(?P<pk>\d+)/delete$', 'userkey_delete'),
)
from django.http import HttpResponse
from django.http import HttpResponse, HttpResponseRedirect
from django.views.decorators.http import require_http_methods, require_GET
from django.shortcuts import get_object_or_404, render_to_response
from django.template import RequestContext
from django.contrib.auth.decorators import login_required
from django.core.exceptions import PermissionDenied
from django.core.urlresolvers import reverse
from django.conf import settings
from sshkey.models import UserKey
from sshkey.forms import UserKeyForm
@require_GET
def lookup(request):
try:
fingerprint = request.GET['fingerprint']
......@@ -24,3 +32,58 @@ def lookup(request):
options = ''
response += options + key.key + '\n'
return HttpResponse(response, mimetype='text/plain')
@login_required
@require_GET
def userkey_list(request):
userkey_list = UserKey.objects.filter(user=request.user)
return render_to_response(
'sshkey/userkey_list.html',
{ 'userkey_list': userkey_list },
context_instance = RequestContext(request),
)
@login_required
@require_http_methods(['GET', 'POST'])
def userkey_add(request):
if request.method == 'POST':
userkey = UserKey(user=request.user)
form = UserKeyForm(request.POST, instance=userkey)
if form.is_valid():
form.save()
return HttpResponseRedirect(reverse('sshkey.views.userkey_list'))
else:
form = UserKeyForm()
return render_to_response(
'sshkey/userkey_detail.html',
{ 'form': form, 'action': 'add' },
context_instance = RequestContext(request),
)
@login_required
@require_http_methods(['GET', 'POST'])
def userkey_edit(request, pk):
userkey = get_object_or_404(UserKey, pk=pk)
if userkey.user != request.user:
raise PermissionDenied
if request.method == 'POST':
form = UserKeyForm(request.POST, instance=userkey)
if form.is_valid():
form.save()
return HttpResponseRedirect(reverse('sshkey.views.userkey_list'))
else:
form = UserKeyForm(instance=userkey)
return render_to_response(
'sshkey/userkey_detail.html',
{ 'form': form, 'action': 'edit' },
context_instance = RequestContext(request),
)
@login_required
@require_GET
def userkey_delete(request, pk):
userkey = get_object_or_404(UserKey, pk=pk)
if userkey.user != request.user:
raise PermissionDenied
userkey.delete()
return HttpResponseRedirect(reverse('sshkey.views.userkey_list'))
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment