Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Gelencsér Szabolcs
/
cloud
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
a383b8e4
authored
Dec 09, 2013
by
Őry Máté
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
dashboard: use real Forms for vm-create
parent
ff738cc3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
51 additions
and
61 deletions
+51
-61
circle/dashboard/forms.py
+5
-2
circle/dashboard/static/dashboard/vm-create.js
+12
-2
circle/dashboard/views.py
+34
-57
No files found.
circle/dashboard/forms.py
View file @
a383b8e4
...
@@ -28,10 +28,13 @@ class VmCreateForm(forms.Form):
...
@@ -28,10 +28,13 @@ class VmCreateForm(forms.Form):
disks
=
forms
.
ModelMultipleChoiceField
(
disks
=
forms
.
ModelMultipleChoiceField
(
queryset
=
Disk
.
objects
.
exclude
(
type
=
"qcow2-snap"
),
queryset
=
Disk
.
objects
.
exclude
(
type
=
"qcow2-snap"
),
required
=
False
)
)
managed_networks
=
forms
.
ModelMultipleChoiceField
(
queryset
=
VLANS
)
managed_networks
=
forms
.
ModelMultipleChoiceField
(
unmanaged_networks
=
forms
.
ModelMultipleChoiceField
(
queryset
=
VLANS
)
queryset
=
VLANS
,
required
=
False
)
unmanaged_networks
=
forms
.
ModelMultipleChoiceField
(
queryset
=
VLANS
,
required
=
False
)
def
__init__
(
self
,
*
args
,
**
kwargs
):
def
__init__
(
self
,
*
args
,
**
kwargs
):
super
(
VmCreateForm
,
self
)
.
__init__
(
*
args
,
**
kwargs
)
super
(
VmCreateForm
,
self
)
.
__init__
(
*
args
,
**
kwargs
)
...
...
circle/dashboard/static/dashboard/vm-create.js
View file @
a383b8e4
...
@@ -177,8 +177,18 @@ function vmCreateLoaded() {
...
@@ -177,8 +177,18 @@ function vmCreateLoaded() {
type
:
'POST'
,
type
:
'POST'
,
data
:
$
(
'form'
).
serialize
(),
data
:
$
(
'form'
).
serialize
(),
success
:
function
(
data
,
textStatus
,
xhr
)
{
success
:
function
(
data
,
textStatus
,
xhr
)
{
if
(
data
.
pk
)
{
if
(
data
.
redirect
)
{
window
.
location
.
replace
(
'/dashboard/vm/'
+
data
.
pk
+
'/#activity'
);
window
.
location
.
replace
(
data
.
redirect
+
'#activity'
);
}
else
{
$
(
'#vm-create-modal'
).
remove
();
$
(
'body'
).
append
(
data
);
vmCreateLoaded
();
addSliderMiscs
();
$
(
'#vm-create-modal'
).
modal
(
'show'
);
$
(
'#vm-create-modal'
).
on
(
'hidden.bs.modal'
,
function
()
{
$
(
'#vm-create-modal'
).
remove
();
});
}
}
},
},
error
:
function
(
xhr
,
textStatus
,
error
)
{
error
:
function
(
xhr
,
textStatus
,
error
)
{
...
...
circle/dashboard/views.py
View file @
a383b8e4
...
@@ -6,7 +6,7 @@ import re
...
@@ -6,7 +6,7 @@ import re
from
django.contrib.auth.models
import
User
,
Group
from
django.contrib.auth.models
import
User
,
Group
from
django.contrib.messages
import
warning
from
django.contrib.messages
import
warning
from
django.core.exceptions
import
(
from
django.core.exceptions
import
(
PermissionDenied
,
SuspiciousOperation
,
ObjectDoesNotExist
,
PermissionDenied
,
SuspiciousOperation
,
)
)
from
django.core
import
signing
from
django.core
import
signing
from
django.core.urlresolvers
import
reverse
,
reverse_lazy
from
django.core.urlresolvers
import
reverse
,
reverse_lazy
...
@@ -343,17 +343,26 @@ class NodeList(SingleTableView):
...
@@ -343,17 +343,26 @@ class NodeList(SingleTableView):
class
VmCreate
(
TemplateView
):
class
VmCreate
(
TemplateView
):
form_class
=
VmCreateForm
form
=
None
def
get_template_names
(
self
):
def
get_template_names
(
self
):
if
self
.
request
.
is_ajax
():
if
self
.
request
.
is_ajax
():
return
[
'dashboard/modal-wrapper.html'
]
return
[
'dashboard/modal-wrapper.html'
]
else
:
else
:
return
[
'dashboard/nojs-wrapper.html'
]
return
[
'dashboard/nojs-wrapper.html'
]
def
get
(
self
,
request
,
*
args
,
**
kwargs
):
def
get
(
self
,
request
,
form
=
None
,
*
args
,
**
kwargs
):
if
form
is
None
:
form
=
self
.
form_class
()
context
=
self
.
get_context_data
(
**
kwargs
)
context
=
self
.
get_context_data
(
**
kwargs
)
context
.
update
({
context
.
update
({
'template'
:
'dashboard/vm-create.html'
,
'template'
:
'dashboard/vm-create.html'
,
'box_title'
:
'Create a VM'
'box_title'
:
'Create a VM'
,
'templates'
:
InstanceTemplate
.
objects
.
all
(),
'vlans'
:
Vlan
.
objects
.
all
(),
'disks'
:
Disk
.
objects
.
exclude
(
type
=
"qcow2-snap"
),
'vm_create_form'
:
form
,
})
})
return
self
.
render_to_response
(
context
)
return
self
.
render_to_response
(
context
)
...
@@ -361,81 +370,49 @@ class VmCreate(TemplateView):
...
@@ -361,81 +370,49 @@ class VmCreate(TemplateView):
context
=
super
(
VmCreate
,
self
)
.
get_context_data
(
**
kwargs
)
context
=
super
(
VmCreate
,
self
)
.
get_context_data
(
**
kwargs
)
# TODO acl
# TODO acl
context
.
update
({
context
.
update
({
'templates'
:
InstanceTemplate
.
objects
.
all
(),
'vlans'
:
Vlan
.
objects
.
all
(),
'disks'
:
Disk
.
objects
.
exclude
(
type
=
"qcow2-snap"
),
'vm_create_form'
:
VmCreateForm
,
})
})
return
context
return
context
# TODO handle not ajax posts
# TODO handle not ajax posts
def
post
(
self
,
request
,
*
args
,
**
kwargs
):
def
post
(
self
,
request
,
*
args
,
**
kwargs
):
if
self
.
request
.
user
.
is_authenticated
():
if
not
self
.
request
.
user
.
is_authenticated
():
user
=
self
.
request
.
user
raise
PermissionDenied
()
else
:
user
=
None
resp
=
{}
form
=
self
.
form_class
(
request
.
POST
)
try
:
if
not
form
.
is_valid
():
pk
=
request
.
POST
.
get
(
'template'
)
return
self
.
get
(
request
,
form
,
*
args
,
**
kwargs
)
template
=
InstanceTemplate
.
objects
.
get
(
post
=
form
.
cleaned_data
pk
=
pk
)
user
=
request
.
user
except
ValueError
:
resp
[
'error'
]
=
True
template
=
post
[
'template'
]
resp
[
'message'
]
=
_
(
"Select a VM from the list!"
)
except
InstanceTemplate
.
DoesNotExist
as
e
:
logger
.
warning
(
'VmCreate.post:
%
s (pk=
%
d, user=
%
s)'
,
unicode
(
e
),
pk
,
unicode
(
request
.
user
))
resp
[
'error'
]
=
True
else
:
if
request
.
user
.
has_perm
(
'vm.set_resources'
):
if
request
.
user
.
has_perm
(
'vm.set_resources'
):
ikwargs
=
{
ikwargs
=
{
'num_cores'
:
int
(
request
.
POST
.
get
(
'cpu_count'
))
,
'num_cores'
:
post
[
'cpu_count'
]
,
'ram_size'
:
int
(
request
.
POST
.
get
(
'ram_size'
))
,
'ram_size'
:
post
[
'ram_size'
]
,
'priority'
:
int
(
request
.
POST
.
get
(
'cpu_priority'
))
,
'priority'
:
post
[
'cpu_priority'
]
,
}
}
try
:
networks
=
(
networks
=
[
InterfaceTemplate
(
vlan
=
Vlan
.
objects
.
get
(
pk
=
l
),
[
InterfaceTemplate
(
vlan
=
l
,
managed
=
True
)
managed
=
True
)
for
l
in
post
[
'unmanaged_networks'
]]
+
for
l
in
request
.
POST
.
getlist
(
[
InterfaceTemplate
(
vlan
=
l
,
managed
=
False
)
'managed_networks'
)
for
l
in
post
[
'unmanaged_networks'
]])
]
disks
=
post
[
'disks'
]
unmanaged
=
request
.
POST
.
getlist
(
'unmanaged_networks'
)
networks
.
extend
([
InterfaceTemplate
(
vlan
=
Vlan
.
objects
.
get
(
pk
=
l
),
managed
=
False
)
for
l
in
unmanaged
])
disks
=
Disk
.
objects
.
filter
(
pk__in
=
request
.
POST
.
getlist
(
'disks'
))
inst
=
Instance
.
create_from_template
(
inst
=
Instance
.
create_from_template
(
template
=
template
,
owner
=
user
,
networks
=
networks
,
template
=
template
,
owner
=
user
,
networks
=
networks
,
disks
=
disks
,
**
ikwargs
)
disks
=
disks
,
**
ikwargs
)
except
ObjectDoesNotExist
as
e
:
raise
logger
.
warning
(
'VmCreate.post:
%
s (user=
%
s)'
,
unicode
(
e
),
unicode
(
request
.
user
))
raise
SuspiciousOperation
()
else
:
else
:
inst
=
Instance
.
create_from_template
(
inst
=
Instance
.
create_from_template
(
template
=
template
,
owner
=
user
)
template
=
template
,
owner
=
user
)
inst
.
deploy_async
(
user
=
request
.
user
)
inst
.
deploy_async
(
user
=
request
.
user
)
resp
[
'pk'
]
=
inst
.
pk
messages
.
success
(
request
,
_
(
'VM successfully created!'
))
messages
.
success
(
request
,
_
(
'VM successfully created!'
))
path
=
inst
.
get_absolute_url
()
if
request
.
is_ajax
():
if
request
.
is_ajax
():
return
HttpResponse
(
json
.
dumps
(
resp
),
return
HttpResponse
(
json
.
dumps
({
'redirect'
:
path
}),
content_type
=
"application/json"
,
content_type
=
"application/json"
)
status
=
500
if
resp
.
get
(
'error'
)
else
200
)
else
:
if
'error'
in
resp
:
messages
.
error
(
request
,
_
(
'Failed to create VM.'
))
return
redirect
(
reverse
(
'dashboard.index'
))
else
:
else
:
return
redirect
(
reverse
(
'dashboard.views.detail'
,
return
redirect
(
path
)
args
=
resp
.
values
()))
class
VmDelete
(
DeleteView
):
class
VmDelete
(
DeleteView
):
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment