Added generation button with no field input required (generate ssh key name)

This commit is contained in:
Siarhei Puhach 2017-07-06 11:47:12 +03:00
parent 7cc70a95ac
commit 8986aa6550
5 changed files with 76 additions and 22 deletions

View file

@ -1,3 +1,5 @@
import datetime
from django import forms
from membership.models import CustomUser
from django.contrib.auth import authenticate
@ -7,6 +9,10 @@ from django.utils.translation import ugettext_lazy as _
from .models import UserHostingKey
def generate_ssh_key_name():
return 'dcl-generated-key-' + datetime.datetime.now().strftime('%m%d%y%H%M')
class HostingUserLoginForm(forms.Form):
email = forms.CharField(widget=forms.EmailInput())
@ -64,7 +70,7 @@ class UserHostingKeyForm(forms.ModelForm):
)
user = forms.models.ModelChoiceField(queryset=CustomUser.objects.all(),
required=False, widget=forms.HiddenInput())
name = forms.CharField(required=True, widget=forms.TextInput(
name = forms.CharField(required=False, widget=forms.TextInput(
attrs={'class': 'form_key_name', 'placeholder': 'Give a name to your key',}))
def __init__(self, *args, **kwargs):
@ -80,7 +86,8 @@ class UserHostingKeyForm(forms.ModelForm):
def clean(self):
cleaned_data = self.cleaned_data
if not self.cleaned_data.get('name', ''):
self.cleaned_data['name'] = generate_ssh_key_name()
if not cleaned_data.get('public_key'):
private_key, public_key = UserHostingKey.generate_keys()
cleaned_data.update({

View file

@ -1,3 +1,6 @@
h2,h3,h4,h5{
font-family: 'Lato-Light', sans-serif;
}
.ssh-keys-table thead tr th,
.ssh-keys-table tbody tr td{
color: #717274;
@ -97,29 +100,48 @@
display: flex;
flex-direction: row;
align-items: center;
justify-content: center;
justify-content: space-between;
}
.choice-container div:first-of-type{
border-right: 1px solid #c9c6c6;
}
.choice-container div{
padding-top: 30px;
padding-bottom: 30px;
display: flex;
align-items: center;
flex-direction: column;
width: 50%;
width: 49%;
}
.choice-container div h3{
margin: auto;
width: 95%;
text-align: center;
font-family: 'Lato-Light', sans-serif;
}
.choice-container div h3 img:hover{
cursor: pointer;
}
.dashboard-container {
.choice-container div.choice-container-line{
padding-top: 0;
padding-bottom: 0;
border: 1px solid #c9c6c6;
height: 250px;
width:1px;
}
@media screen and (max-width: 768px){
.choice-container div.choice-container-line{
display: none;
}
.choice-container{
flex-direction: column;
}
.choice-container div{
width: 100%;
}
.choice-container div h3 {
font-size: 16px;
}
}
.dashboard-choice-container {
max-width: 930px !important;
}
.choice-button{
@ -128,14 +150,19 @@
border-radius: 0;
color: white;
margin-top: 70px;
}
.choice-button a{
color: white;
font-size: 20px;
}
@media screen and (max-width: 768px){
.choice-button{
margin-top: 20px;
}
}
.upload-btn{
background-color: #337ab7;
}
.form_key_name{
width:60%;
}
.form_public_key,
.form_key_name{
border:none;
@ -233,3 +260,6 @@
.form-ssh h3{
margin-bottom: 40px;
}
.custom_form_button{
border-radius: 0;
}

View file

@ -2,7 +2,7 @@
{% load staticfiles bootstrap3 i18n %}
{% block content %}
<div>
<div class="container virtual-machine-container dashboard-container ">
<div class="container virtual-machine-container dashboard-container dashboard-choice-container">
<div class="row">
<div class="col-md-12">
<div class="col-sm-12">
@ -20,17 +20,24 @@
<h3>I want to generate a new key pair.
<sup><sup><img src="{% static 'hosting/img/g222.png' %}"/></sup></sup>
</h3>
<button type="button" class="btn btn-success choice-button generate-btn">
<a href="#">{% trans "Generate"%}</a>
</button>
<form action="#" method="post">
{% csrf_token %}
<button type="submit" class="btn btn-success choice-button generate-btn">
{% trans "Generate"%}
</button>
</form>
</div>
<div class="choice-container-line"></div>
<div class="right-choice-container">
<h3>I want to use my existing public key.
<sup><sup><img src="{% static 'hosting/img/g222.png' %}"/></sup></sup>
</h3>
<button type="button" class="btn btn-primary choice-button upload-btn">
<a href="{% url 'hosting:create_ssh_key' %}">{% trans "Upload"%}</a>
</button>
<form action="{% url 'hosting:create_ssh_key' %}">
<button type="submit" class="btn btn-primary choice-button upload-btn">
{% trans "Upload"%}
</button>
</form>
</div>
</div>

View file

@ -22,13 +22,13 @@
{% buttons %}
<div class="underform-contaner">
<h4> {% trans "Use your created key to access to the machine." %} </h4>
<button type="submit" class="btn btn-success">
<button type="submit" name='add_ssh' class="btn btn-success custom_form_button">
{% trans "Add SSH key"%}
</button>
</div>
<div class="underform-contaner">
<h4>{% trans "Or you can generate a new key pair."%}</h4>
<button class="btn btn-success">{% trans "Generate"%}
<button type="submit" name='generate' class="btn btn-success custom_form_button">{% trans "Generate"%}
</button>
</div>

View file

@ -24,7 +24,7 @@ from utils.forms import BillingAddressForm, PasswordResetRequestForm, UserBillin
from utils.views import PasswordResetViewMixin, PasswordResetConfirmViewMixin, LoginViewMixin
from utils.mailer import BaseEmail
from .models import HostingOrder, HostingBill, HostingPlan, UserHostingKey
from .forms import HostingUserSignupForm, HostingUserLoginForm, UserHostingKeyForm
from .forms import HostingUserSignupForm, HostingUserLoginForm, UserHostingKeyForm, generate_ssh_key_name
from .mixins import ProcessVMSelectionMixin
from opennebula_api.models import OpenNebulaManager
@ -376,6 +376,13 @@ class SSHKeyChoiceView(LoginRequiredMixin, View):
context = {}
return render(request, self.template_name, context)
def post(self, request, *args, **kwargs):
print('post method HERE!!!')
name = generate_ssh_key_name()
private_key, public_key = UserHostingKey.generate_keys()
UserHostingKey.objects.create(user=request.user, public_key=public_key, name=name)
return redirect(reverse_lazy('hosting:ssh_keys'), foo='bar')
class SSHKeyCreateView(LoginRequiredMixin, FormView):
form_class = UserHostingKeyForm
@ -430,6 +437,9 @@ class SSHKeyCreateView(LoginRequiredMixin, FormView):
def post(self, request, *args, **kwargs):
print(self.request.POST.dict())
form = self.get_form()
required = 'add_ssh' in self.request.POST
form.fields['name'].required = required
form.fields['public_key'].required = required
if form.is_valid():
return self.form_valid(form)
else: