mirror of
https://github.com/retspen/webvirtcloud
synced 2024-12-24 15:15:22 +00:00
Add new forms and templates
This commit is contained in:
parent
00b4d751ad
commit
ed2b28c25e
12 changed files with 263 additions and 25 deletions
|
@ -3,6 +3,7 @@ from django.core.urlresolvers import reverse
|
|||
from django.shortcuts import render
|
||||
from computes.models import Compute
|
||||
from instances.models import Instance
|
||||
from users.models import UserInstance
|
||||
from computes.forms import ComputeAddTcpForm, ComputeAddSshForm, ComputeEditHostForm, ComputeAddTlsForm, ComputeAddSocketForm
|
||||
from vrtManager.hostdetails import wvmHostDetails
|
||||
from vrtManager.connection import CONN_SSH, CONN_TCP, CONN_TLS, CONN_SOCKET, connection_manager
|
||||
|
@ -37,6 +38,7 @@ def computes(request):
|
|||
})
|
||||
return compute_data
|
||||
|
||||
error_messages = []
|
||||
computes = Compute.objects.filter()
|
||||
computes_info = get_hosts_status(computes)
|
||||
|
||||
|
@ -44,11 +46,15 @@ def computes(request):
|
|||
if 'host_del' in request.POST:
|
||||
compute_id = request.POST.get('host_id', '')
|
||||
try:
|
||||
del_inst_on_host = Instance.objects.filter(compute_id=compute_id)
|
||||
del_inst_on_host.delete()
|
||||
del_user_inst_on_host = UserInstance.objects.filter(compute_id=compute_id)
|
||||
del_user_inst_on_host.save()
|
||||
finally:
|
||||
del_host = Compute.objects.get(id=compute_id)
|
||||
del_host.delete()
|
||||
try:
|
||||
del_inst_on_host = Instance.objects.filter(compute_id=compute_id)
|
||||
del_inst_on_host.delete()
|
||||
finally:
|
||||
del_host = Compute.objects.get(id=compute_id)
|
||||
del_host.delete()
|
||||
return HttpResponseRedirect(request.get_full_path())
|
||||
if 'host_tcp_add' in request.POST:
|
||||
form = ComputeAddTcpForm(request.POST)
|
||||
|
@ -61,6 +67,9 @@ def computes(request):
|
|||
password=data['password'])
|
||||
new_tcp_host.save()
|
||||
return HttpResponseRedirect(request.get_full_path())
|
||||
else:
|
||||
for msg_err in form.errors.values():
|
||||
error_messages.append(msg_err.as_text())
|
||||
if 'host_ssh_add' in request.POST:
|
||||
form = ComputeAddSshForm(request.POST)
|
||||
if form.is_valid():
|
||||
|
@ -71,6 +80,9 @@ def computes(request):
|
|||
login=data['login'])
|
||||
new_ssh_host.save()
|
||||
return HttpResponseRedirect(request.get_full_path())
|
||||
else:
|
||||
for msg_err in form.errors.values():
|
||||
error_messages.append(msg_err.as_text())
|
||||
if 'host_tls_add' in request.POST:
|
||||
form = ComputeAddTlsForm(request.POST)
|
||||
if form.is_valid():
|
||||
|
@ -82,6 +94,9 @@ def computes(request):
|
|||
password=data['password'])
|
||||
new_tls_host.save()
|
||||
return HttpResponseRedirect(request.get_full_path())
|
||||
else:
|
||||
for msg_err in form.errors.values():
|
||||
error_messages.append(msg_err.as_text())
|
||||
if 'host_socket_add' in request.POST:
|
||||
form = ComputeAddSocketForm(request.POST)
|
||||
if form.is_valid():
|
||||
|
@ -93,6 +108,9 @@ def computes(request):
|
|||
password='')
|
||||
new_socket_host.save()
|
||||
return HttpResponseRedirect(request.get_full_path())
|
||||
else:
|
||||
for msg_err in form.errors.values():
|
||||
error_messages.append(msg_err.as_text())
|
||||
if 'host_edit' in request.POST:
|
||||
form = ComputeEditHostForm(request.POST)
|
||||
if form.is_valid():
|
||||
|
@ -104,7 +122,9 @@ def computes(request):
|
|||
compute_edit.password = data['password']
|
||||
compute_edit.save()
|
||||
return HttpResponseRedirect(request.get_full_path())
|
||||
|
||||
else:
|
||||
for msg_err in form.errors.values():
|
||||
error_messages.append(msg_err.as_text())
|
||||
return render(request, 'computes.html', locals())
|
||||
|
||||
def overview(request, compute_id):
|
||||
|
|
|
@ -62,7 +62,6 @@ def instances(request):
|
|||
check_uuid.save()
|
||||
conn.close()
|
||||
except libvirtError as lib_err:
|
||||
print 'Error'
|
||||
error_messages.append(lib_err)
|
||||
|
||||
return render(request, 'instances.html', locals())
|
||||
|
|
|
@ -47,6 +47,9 @@ def interfaces(request, compute_id):
|
|||
data['ipv6_type'], data['ipv6_addr'], data['ipv6_gw'],
|
||||
data['stp'], data['delay'])
|
||||
return HttpResponseRedirect(request.get_full_path())
|
||||
else:
|
||||
for msg_err in form.errors.values():
|
||||
error_messages.append(msg_err.as_text())
|
||||
conn.close()
|
||||
except libvirtError as lib_err:
|
||||
error_messages.append(lib_err)
|
||||
|
|
|
@ -50,6 +50,9 @@ def networks(request, compute_id):
|
|||
conn.create_network(data['name'], data['forward'], gateway, netmask,
|
||||
dhcp, data['bridge_name'], data['openvswitch'], data['fixed'])
|
||||
return HttpResponseRedirect(reverse('network', args=[compute_id, data['name']]))
|
||||
else:
|
||||
for msg_err in form.errors.values():
|
||||
error_messages.append(msg_err.as_text())
|
||||
conn.close()
|
||||
except libvirtError as lib_err:
|
||||
error_messages.append(lib_err)
|
||||
|
|
|
@ -47,6 +47,9 @@ def secrets(request, compute_id):
|
|||
data = form.cleaned_data
|
||||
conn.create_secret(data['ephemeral'], data['private'], data['usage_type'], data['data'])
|
||||
return HttpResponseRedirect(request.get_full_path())
|
||||
else:
|
||||
for msg_err in form.errors.values():
|
||||
error_messages.append(msg_err.as_text())
|
||||
if 'delete' in request.POST:
|
||||
uuid = request.POST.get('uuid', '')
|
||||
conn.delete_secret(uuid)
|
||||
|
|
|
@ -58,6 +58,9 @@ def storages(request, compute_id):
|
|||
else:
|
||||
conn.create_storage(data['stg_type'], data['name'], data['source'], data['target'])
|
||||
return HttpResponseRedirect(reverse('storage', args=[compute_id, data['name']]))
|
||||
else:
|
||||
for msg_err in form.errors.values():
|
||||
error_messages.append(msg_err.as_text())
|
||||
conn.close()
|
||||
except libvirtError as lib_err:
|
||||
error_messages.append(lib_err)
|
||||
|
@ -155,6 +158,9 @@ def storage(request, compute_id, pool):
|
|||
return HttpResponseRedirect(request.get_full_path())
|
||||
except libvirtError as lib_err:
|
||||
error_messages.append(lib_err)
|
||||
else:
|
||||
for msg_err in form.errors.values():
|
||||
error_messages.append(msg_err.as_text())
|
||||
if 'del_volume' in request.POST:
|
||||
volname = request.POST.get('volname', '')
|
||||
try:
|
||||
|
@ -191,6 +197,9 @@ def storage(request, compute_id, pool):
|
|||
return HttpResponseRedirect(request.get_full_path())
|
||||
except libvirtError as lib_err:
|
||||
error_messages.append(lib_err)
|
||||
else:
|
||||
for msg_err in form.errors.values():
|
||||
error_messages.append(msg_err.as_text())
|
||||
conn.close()
|
||||
|
||||
return render(request, 'storage.html', locals())
|
||||
|
|
|
@ -1,5 +1,38 @@
|
|||
{% load i18n %}
|
||||
{% if request.user.is_superuser %}
|
||||
<button type="button" class="btn btn-success pull-right">
|
||||
<a href="#AddUser" type="button" class="btn btn-success pull-right" data-toggle="modal">
|
||||
<span class="glyphicon glyphicon-plus" aria-hidden="true"></span>
|
||||
</button>
|
||||
</a>
|
||||
|
||||
<!-- Modal pool -->
|
||||
<div class="modal fade" id="AddUser" tabindex="-1" role="dialog" aria-labelledby="AddUserLabel" aria-hidden="true">
|
||||
<div class="modal-dialog">
|
||||
<div class="modal-content">
|
||||
<div class="modal-header">
|
||||
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
|
||||
<h4 class="modal-title">{% trans "Add New User" %}</h4>
|
||||
</div>
|
||||
<div class="modal-body">
|
||||
<form class="form-horizontal" method="post" action="" role="form">{% csrf_token %}
|
||||
<div class="form-group">
|
||||
<label class="col-sm-4 control-label">{% trans "Name" %}</label>
|
||||
<div class="col-sm-6">
|
||||
<input type="text" class="form-control" name="name" placeholder="john" required pattern="[a-z0-9]+">
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label class="col-sm-4 control-label">{% trans "Password" %}</label>
|
||||
<div class="col-sm-6">
|
||||
<input type="password" class="form-control" name="password" placeholder="*******" required>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
<button type="button" class="btn btn-default" data-dismiss="modal">{% trans "Close" %}</button>
|
||||
<button type="submit" class="btn btn-primary" name="create">{% trans "Create" %}</button>
|
||||
</div>
|
||||
</form>
|
||||
</div> <!-- /.modal-content -->
|
||||
</div> <!-- /.modal-dialog -->
|
||||
</div> <!-- /.modal -->
|
||||
{% endif %}
|
5
templates/create_user_inst_block.html
Normal file
5
templates/create_user_inst_block.html
Normal file
|
@ -0,0 +1,5 @@
|
|||
{% if request.user.is_superuser %}
|
||||
<button type="button" class="btn btn-success pull-right">
|
||||
<span class="glyphicon glyphicon-plus" aria-hidden="true"></span>
|
||||
</button>
|
||||
{% endif %}
|
|
@ -5,6 +5,7 @@
|
|||
<!-- Page Heading -->
|
||||
<div class="row">
|
||||
<div class="col-lg-12">
|
||||
{% include 'create_user_inst_block.html' %}
|
||||
<h1 class="page-header">{{ user }}</h1>
|
||||
</div>
|
||||
</div>
|
||||
|
@ -13,6 +14,102 @@
|
|||
{% include 'errors_block.html' %}
|
||||
|
||||
<div class="row">
|
||||
{{ user }}
|
||||
<div class="col-lg-12">
|
||||
{% if not user_insts %}
|
||||
<div class="col-lg-12">
|
||||
<div class="alert alert-warning alert-dismissable">
|
||||
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
|
||||
<i class="fa fa-exclamation-triangle"></i> <strong>{% trans "Warning:" %}</strong> {% trans "User doesn't have any Instace" %}
|
||||
</div>
|
||||
</div>
|
||||
{% else %}
|
||||
<div class="table-responsive">
|
||||
<table class="table table-bordered table-hover">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>#</th>
|
||||
<th>{% trans "Name" %}</th>
|
||||
<th>{% trans "Is blocked" %}</th>
|
||||
<th>{% trans "Can change" %}</th>
|
||||
<th>{% trans "Can delete" %}</th>
|
||||
<th colspan="2">{% trans "Action" %}</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{% for inst in user_insts %}
|
||||
<tr>
|
||||
<td>{{ forloop.counter }}</td>
|
||||
<td>{{ inst.instance.name }}</td>
|
||||
<td>{{ inst.is_block }}</td>
|
||||
<td>{{ inst.is_change }}</td>
|
||||
<td>{{ inst.is_delete }}</td>
|
||||
<td style="width:5px;">
|
||||
<a href="#editPriv{{ forloop.counter }}" type="button" class="btn btn-xs btn-default" data-toggle="modal">
|
||||
<span class="glyphicon glyphicon-pencil" aria-hidden="true"></span>
|
||||
</a>
|
||||
|
||||
<!-- Modal pool -->
|
||||
<div class="modal fade" id="editPriv{{ forloop.counter }}" tabindex="-1" role="dialog" aria-labelledby="editPrivLabel" aria-hidden="true">
|
||||
<div class="modal-dialog">
|
||||
<div class="modal-content">
|
||||
<div class="modal-header">
|
||||
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
|
||||
<h4 class="modal-title">{% trans "Edit privilegies for" %} {{ inst.instance.name }}</h4>
|
||||
</div>
|
||||
<div class="modal-body">
|
||||
<form class="form-horizontal" method="post" action="" role="form">{% csrf_token %}
|
||||
<div class="form-group">
|
||||
<label class="col-sm-4 control-label">{% trans "Is blocked" %}</label>
|
||||
<div class="col-sm-6">
|
||||
<input type="hidden" name="user_inst" value="{{ inst.id }}">
|
||||
<select type="text" class="form-control" name="inst_block">
|
||||
<option value="">False</option>
|
||||
<option value="1">True</option>
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label class="col-sm-4 control-label">{% trans "Can change" %}</label>
|
||||
<div class="col-sm-6">
|
||||
<select type="text" class="form-control" name="inst_change">
|
||||
<option value="">False</option>
|
||||
<option value="1">True</option>
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label class="col-sm-4 control-label">{% trans "Can delete" %}</label>
|
||||
<div class="col-sm-6">
|
||||
<select type="text" class="form-control" name="inst_delete">
|
||||
<option value="">False</option>
|
||||
<option value="1">True</option>
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
<button type="button" class="btn btn-default" data-dismiss="modal">{% trans "Close" %}</button>
|
||||
<button type="submit" class="btn btn-primary" name="edit">{% trans "Edit" %}</button>
|
||||
</div>
|
||||
</form>
|
||||
</div> <!-- /.modal-content -->
|
||||
</div> <!-- /.modal-dialog -->
|
||||
</div> <!-- /.modal -->
|
||||
</td>
|
||||
<td style="width:5px;">
|
||||
<form action="" method="post" role="form">{% csrf_token %}
|
||||
<input type="hidden" name="user_inst" value="{{ inst.id }}">
|
||||
<button type="submit" class="btn btn-xs btn-default" name="delete" tittle="{% trans "Delete" %}" onclick="return confirm('{% trans "Are you sure?" %}')">
|
||||
<span class="glyphicon glyphicon-trash" aria-hidden="true"></span>
|
||||
</button>
|
||||
</form>
|
||||
</td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
{% endif %}
|
||||
</div>
|
||||
</div>
|
||||
{% endblock %}
|
|
@ -13,22 +13,29 @@
|
|||
|
||||
{% include 'errors_block.html' %}
|
||||
|
||||
{% for user in users %}
|
||||
<div id="{{ user.username }}" class="col-xs-6 col-sm-4">
|
||||
<div class="panel {% if user.is_active %}panel-success{% else %}panel-danger{% endif %} panel-data">
|
||||
<div class="panel-heading">
|
||||
<h3 class="panel-title"><strong>{{ user.username }}</strong>
|
||||
<a data-toggle="modal" href="{% url 'user' user.id %}" class="pull-right"
|
||||
title="{% trans "Edit" %}">
|
||||
<span class="glyphicon glyphicon-cog"></span>
|
||||
</a>
|
||||
</h3>
|
||||
</div>
|
||||
<div class="panel-body">
|
||||
<p>Status: {% if user.is_active %}Active{% else %}Blocked{% endif %}
|
||||
</p>
|
||||
<div class="row">
|
||||
{% for user in users %}
|
||||
<div id="{{ user.username }}" class="col-xs-12 col-sm-4">
|
||||
<div class="panel {% if user.is_active %}panel-success{% else %}panel-danger{% endif %} panel-data">
|
||||
<div class="panel-heading">
|
||||
<h3 class="panel-title">
|
||||
<a href="{% url 'user' user.id %}"><strong>{{ user.username }}</strong></a>
|
||||
</h3>
|
||||
</div>
|
||||
<div class="panel-body">
|
||||
<div class="col-xs-4 col-sm-4">
|
||||
<p><strong>{% trans "Status:" %}</strong></p>
|
||||
</div>
|
||||
<div class="col-xs-4 col-sm-6">
|
||||
{% if user.is_active %}
|
||||
<p>{% trans "Active" %}</p>
|
||||
{% else %}
|
||||
<p>{% trans "Blocked" %}</p>
|
||||
{% endif %}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{% endfor %}
|
||||
{% endfor %}
|
||||
</div>
|
||||
{% endblock %}
|
24
users/forms.py
Normal file
24
users/forms.py
Normal file
|
@ -0,0 +1,24 @@
|
|||
import re
|
||||
from django import forms
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
from django.contrib.auth.models import User
|
||||
|
||||
|
||||
class UserAddForm(forms.Form):
|
||||
name = forms.CharField(label="Name",
|
||||
error_messages={'required': _('No User name has been entered')},
|
||||
max_length=20)
|
||||
password = forms.CharField(required=True, error_messages={'required': _('No password has been entered')},)
|
||||
|
||||
def clean_name(self):
|
||||
name = self.cleaned_data['name']
|
||||
have_symbol = re.match('^[a-z0-9]+$', name)
|
||||
if not have_symbol:
|
||||
raise forms.ValidationError(_('The flavor name must not contain any special characters'))
|
||||
elif len(name) > 20:
|
||||
raise forms.ValidationError(_('The flavor name must not exceed 20 characters'))
|
||||
try:
|
||||
User.objects.get(username=name)
|
||||
except User.DoesNotExist:
|
||||
return name
|
||||
raise forms.ValidationError(_('Flavor name is already use'))
|
|
@ -2,6 +2,8 @@ from django.shortcuts import render
|
|||
from django.http import HttpResponseRedirect
|
||||
from django.core.urlresolvers import reverse
|
||||
from django.contrib.auth.models import User
|
||||
from users.models import UserInstance
|
||||
from users.forms import UserAddForm
|
||||
|
||||
|
||||
def users(request):
|
||||
|
@ -16,8 +18,22 @@ def users(request):
|
|||
if not request.user.is_superuser:
|
||||
return HttpResponseRedirect(reverse('index'))
|
||||
|
||||
error_messages = []
|
||||
users = User.objects.filter(is_staff=False, is_superuser=False)
|
||||
|
||||
if request.method == 'POST':
|
||||
if 'create' in request.POST:
|
||||
form = UserAddForm(request.POST)
|
||||
if form.is_valid():
|
||||
data = form.cleaned_data
|
||||
else:
|
||||
for msg_err in form.errors.values():
|
||||
error_messages.append(msg_err.as_text())
|
||||
if not error_messages:
|
||||
new_user = User.objects.create_user(data['name'], None, data['password'])
|
||||
new_user.save()
|
||||
return HttpResponseRedirect(request.get_full_path())
|
||||
|
||||
return render(request, 'users.html', locals())
|
||||
|
||||
def user(request, user_id):
|
||||
|
@ -33,5 +49,24 @@ def user(request, user_id):
|
|||
return HttpResponseRedirect(reverse('index'))
|
||||
|
||||
user = User.objects.get(id=user_id)
|
||||
user_insts = UserInstance.objects.filter(user_id=user_id)
|
||||
|
||||
if request.method == 'POST':
|
||||
if 'delete' in request.POST:
|
||||
user_inst = request.POST.get('user_inst', '')
|
||||
del_user_inst = UserInstance.objects.get(id=user_inst)
|
||||
del_user_inst.delete()
|
||||
return HttpResponseRedirect(request.get_full_path())
|
||||
if 'edit' in request.POST:
|
||||
user_inst = request.POST.get('user_inst', '')
|
||||
inst_block = request.POST.get('inst_block', '')
|
||||
inst_change = request.POST.get('inst_change', '')
|
||||
inst_delete = request.POST.get('inst_delete', '')
|
||||
edit_user_inst = UserInstance.objects.get(id=user_inst)
|
||||
edit_user_inst.is_block = bool(inst_block)
|
||||
edit_user_inst.is_change = bool(inst_change)
|
||||
edit_user_inst.is_delete = bool(inst_delete)
|
||||
edit_user_inst.save()
|
||||
return HttpResponseRedirect(request.get_full_path())
|
||||
|
||||
return render(request, 'user.html', locals())
|
Loading…
Reference in a new issue