Added private_key field to UserHostingkey model, some html updates

This commit is contained in:
Siarhei Puhach 2017-07-06 14:18:22 +03:00
parent 8986aa6550
commit d2d96e9951
5 changed files with 45 additions and 10 deletions

View file

@ -71,7 +71,7 @@ class UserHostingKeyForm(forms.ModelForm):
user = forms.models.ModelChoiceField(queryset=CustomUser.objects.all(), user = forms.models.ModelChoiceField(queryset=CustomUser.objects.all(),
required=False, widget=forms.HiddenInput()) required=False, widget=forms.HiddenInput())
name = forms.CharField(required=False, widget=forms.TextInput( name = forms.CharField(required=False, widget=forms.TextInput(
attrs={'class': 'form_key_name', 'placeholder': 'Give a name to your key',})) attrs={'class': 'form_key_name', 'placeholder': 'Give a name to your key'}))
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
self.request = kwargs.pop("request") self.request = kwargs.pop("request")

View file

@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.9.4 on 2017-07-06 09:06
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('hosting', '0040_hostingplan'),
]
operations = [
migrations.AddField(
model_name='userhostingkey',
name='private_key',
field=models.FileField(blank=True, upload_to='private_keys'),
),
]

View file

@ -101,6 +101,7 @@ class HostingOrder(AssignPermissionsMixin, models.Model):
class UserHostingKey(models.Model): class UserHostingKey(models.Model):
user = models.ForeignKey(CustomUser) user = models.ForeignKey(CustomUser)
public_key = models.TextField() public_key = models.TextField()
private_key = models.FileField(upload_to='private_keys', blank=True)
created_at = models.DateTimeField(auto_now_add=True) created_at = models.DateTimeField(auto_now_add=True)
name = models.CharField(max_length=100) name = models.CharField(max_length=100)

View file

@ -40,9 +40,9 @@
<td> <td>
<button type="button" class="btn btn-default btn-custom-delete" data-toggle="modal" <button type="button" class="btn btn-default btn-custom-delete" data-toggle="modal"
data-target="#Modal{{ user_key.id }}"> data-target="#Modal{{ user_key.id }}" style="color: #717274">
<a href="#">
{% trans "Delete"%}</a> {% trans "Delete"%}
</button> </button>
<div class="modal fade" id="Modal{{user_key.id }}" tabindex="-1" role="dialog"> <div class="modal fade" id="Modal{{user_key.id }}" tabindex="-1" role="dialog">
@ -105,10 +105,15 @@
</td> </td>
</td> </td>
<td> <td>
<button type="button" class="btn btn-default" data-toggle="modal" {% if user_key.private_key %}
><a <form action="{{ user_key.private_key.url }}">
href="#">{% trans "Download"%}</a> <button style="color: #717274" type="submit" class="btn btn-default" data-toggle="modal"
</button> >{% trans "Download"%}
</button>
</form>
{% endif %}
</td> </td>
</tr> </tr>
{% endfor %} {% endfor %}

View file

@ -1,3 +1,6 @@
import uuid
from django.core.files.base import ContentFile
from oca.pool import WrongNameError, WrongIdError from oca.pool import WrongNameError, WrongIdError
from django.shortcuts import render from django.shortcuts import render
from django.http import Http404 from django.http import Http404
@ -377,10 +380,12 @@ class SSHKeyChoiceView(LoginRequiredMixin, View):
return render(request, self.template_name, context) return render(request, self.template_name, context)
def post(self, request, *args, **kwargs): def post(self, request, *args, **kwargs):
print('post method HERE!!!')
name = generate_ssh_key_name() name = generate_ssh_key_name()
private_key, public_key = UserHostingKey.generate_keys() private_key, public_key = UserHostingKey.generate_keys()
UserHostingKey.objects.create(user=request.user, public_key=public_key, name=name) content = ContentFile(private_key)
ssh_key = UserHostingKey.objects.create(user=request.user, public_key=public_key, name=name)
filename = name + '_' + str(uuid.uuid4())[:8] + '_private.pem'
ssh_key.private_key.save(filename, content)
return redirect(reverse_lazy('hosting:ssh_keys'), foo='bar') return redirect(reverse_lazy('hosting:ssh_keys'), foo='bar')
@ -399,6 +404,10 @@ class SSHKeyCreateView(LoginRequiredMixin, FormView):
def form_valid(self, form): def form_valid(self, form):
form.save() form.save()
if 'dcl-generated-key-' in form.instance.name:
content = ContentFile(form.cleaned_data.get('private_key'))
filename = form.cleaned_data.get('name') + '_' + str(uuid.uuid4())[:8] + '_private.pem'
form.instance.private_key.save(filename, content)
context = self.get_context_data() context = self.get_context_data()
next_url = self.request.session.get( next_url = self.request.session.get(