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 django import forms
from membership.models import CustomUser from membership.models import CustomUser
from django.contrib.auth import authenticate from django.contrib.auth import authenticate
@ -7,6 +9,10 @@ from django.utils.translation import ugettext_lazy as _
from .models import UserHostingKey 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): class HostingUserLoginForm(forms.Form):
email = forms.CharField(widget=forms.EmailInput()) email = forms.CharField(widget=forms.EmailInput())
@ -64,7 +70,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=True, 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):
@ -80,7 +86,8 @@ class UserHostingKeyForm(forms.ModelForm):
def clean(self): def clean(self):
cleaned_data = self.cleaned_data 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'): if not cleaned_data.get('public_key'):
private_key, public_key = UserHostingKey.generate_keys() private_key, public_key = UserHostingKey.generate_keys()
cleaned_data.update({ 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 thead tr th,
.ssh-keys-table tbody tr td{ .ssh-keys-table tbody tr td{
color: #717274; color: #717274;
@ -97,29 +100,48 @@
display: flex; display: flex;
flex-direction: row; flex-direction: row;
align-items: center; align-items: center;
justify-content: center; justify-content: space-between;
} }
.choice-container div:first-of-type{
border-right: 1px solid #c9c6c6;
}
.choice-container div{ .choice-container div{
padding-top: 30px; padding-top: 30px;
padding-bottom: 30px; padding-bottom: 30px;
display: flex; display: flex;
align-items: center; align-items: center;
flex-direction: column; flex-direction: column;
width: 50%; width: 49%;
} }
.choice-container div h3{ .choice-container div h3{
margin: auto; margin: auto;
width: 95%; width: 95%;
text-align: center; text-align: center;
font-family: 'Lato-Light', sans-serif;
} }
.choice-container div h3 img:hover{ .choice-container div h3 img:hover{
cursor: pointer; 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; max-width: 930px !important;
} }
.choice-button{ .choice-button{
@ -128,14 +150,19 @@
border-radius: 0; border-radius: 0;
color: white; color: white;
margin-top: 70px; margin-top: 70px;
}
.choice-button a{
color: white;
font-size: 20px; font-size: 20px;
} }
@media screen and (max-width: 768px){
.choice-button{
margin-top: 20px;
}
}
.upload-btn{ .upload-btn{
background-color: #337ab7; background-color: #337ab7;
} }
.form_key_name{
width:60%;
}
.form_public_key, .form_public_key,
.form_key_name{ .form_key_name{
border:none; border:none;
@ -233,3 +260,6 @@
.form-ssh h3{ .form-ssh h3{
margin-bottom: 40px; margin-bottom: 40px;
} }
.custom_form_button{
border-radius: 0;
}

View file

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

View file

@ -22,13 +22,13 @@
{% buttons %} {% buttons %}
<div class="underform-contaner"> <div class="underform-contaner">
<h4> {% trans "Use your created key to access to the machine." %} </h4> <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"%} {% trans "Add SSH key"%}
</button> </button>
</div> </div>
<div class="underform-contaner"> <div class="underform-contaner">
<h4>{% trans "Or you can generate a new key pair."%}</h4> <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> </button>
</div> </div>

View file

@ -24,7 +24,7 @@ from utils.forms import BillingAddressForm, PasswordResetRequestForm, UserBillin
from utils.views import PasswordResetViewMixin, PasswordResetConfirmViewMixin, LoginViewMixin from utils.views import PasswordResetViewMixin, PasswordResetConfirmViewMixin, LoginViewMixin
from utils.mailer import BaseEmail from utils.mailer import BaseEmail
from .models import HostingOrder, HostingBill, HostingPlan, UserHostingKey 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 .mixins import ProcessVMSelectionMixin
from opennebula_api.models import OpenNebulaManager from opennebula_api.models import OpenNebulaManager
@ -376,6 +376,13 @@ class SSHKeyChoiceView(LoginRequiredMixin, View):
context = {} context = {}
return render(request, self.template_name, 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): class SSHKeyCreateView(LoginRequiredMixin, FormView):
form_class = UserHostingKeyForm form_class = UserHostingKeyForm
@ -430,6 +437,9 @@ class SSHKeyCreateView(LoginRequiredMixin, FormView):
def post(self, request, *args, **kwargs): def post(self, request, *args, **kwargs):
print(self.request.POST.dict()) print(self.request.POST.dict())
form = self.get_form() 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(): if form.is_valid():
return self.form_valid(form) return self.form_valid(form)
else: else: