Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
RECIRCLE
/
portal
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
11
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
83f92285
authored
Jul 18, 2019
by
Chif Gergő
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactor views to viewsets
parent
4f79bde7
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
41 additions
and
32 deletions
+41
-32
recircle/instance/urls.py
+6
-9
recircle/instance/views.py
+34
-22
recircle/interface_openstack
+1
-1
No files found.
recircle/instance/urls.py
View file @
83f92285
from
django.urls
import
path
from
rest_framework.urlpatterns
import
format_suffix_patterns
from
instance
import
views
from
rest_framework
import
routers
urlpatterns
=
[
path
(
"instances/"
,
views
.
InstanceList
.
as_view
()),
path
(
"instances/<int:pk>/"
,
views
.
InstanceDetail
.
as_view
()),
# path('instances/<int:pk>/action/', views.InstanceAction.as_view())
path
(
"flavors/"
,
views
.
FlavorListView
.
as_view
()),
]
router
=
routers
.
SimpleRouter
()
router
.
register
(
r'instances'
,
views
.
InstanceViewSet
,
basename
=
'instance'
)
router
.
register
(
r'flavors'
,
views
.
FlavorViewSet
,
basename
=
'flavor'
)
urlpatterns
=
format_suffix_patterns
(
urlpatterns
)
urlpatterns
=
router
.
urls
recircle/instance/views.py
View file @
83f92285
from
instance.serializers
import
InstanceSerializer
,
FlavorSerializer
from
django.http
import
Http404
from
django.conf
import
settings
from
rest_framework.views
import
APIView
from
rest_framework.views
ets
import
ViewSet
from
rest_framework.response
import
Response
from
rest_framework
import
status
from
interface_openstack.implementation.vm.instance
import
(
...
...
@@ -11,12 +11,19 @@ from template.models import InstanceTemplate
from
instance.models
import
Instance
,
Flavor
,
Lease
class
InstanceList
(
APIView
):
def
get
(
self
,
request
,
format
=
None
):
class
InstanceViewSet
(
ViewSet
):
def
get_object
(
self
,
pk
):
try
:
return
Instance
.
objects
.
get
(
pk
=
pk
)
except
Instance
.
DoesNotExist
:
raise
Http404
def
list
(
self
,
request
):
instances
=
Instance
.
objects
.
all
()
return
Response
(
InstanceSerializer
(
instances
,
many
=
True
)
.
data
)
def
post
(
self
,
request
,
format
=
None
):
def
create
(
self
,
request
):
data
=
request
.
data
template
=
InstanceTemplate
.
objects
.
get
(
pk
=
data
[
"template"
])
flavor
=
Flavor
.
objects
.
get
(
pk
=
data
[
"flavor"
])
...
...
@@ -37,19 +44,7 @@ class InstanceList(APIView):
)
return
Response
(
newInstance
.
pk
)
class
InstanceDetail
(
APIView
):
"""
Retrieve, update or delete a snippet instance.
"""
def
get_object
(
self
,
pk
):
try
:
return
Instance
.
objects
.
get
(
pk
=
pk
)
except
Instance
.
DoesNotExist
:
raise
Http404
def
get
(
self
,
request
,
pk
,
format
=
None
):
def
retrieve
(
self
,
request
):
instance
=
self
.
get_object
(
pk
)
instanceDict
=
InstanceSerializer
(
instance
)
.
data
...
...
@@ -61,7 +56,7 @@ class InstanceDetail(APIView):
return
Response
(
merged_dict
)
def
put
(
self
,
request
,
pk
,
format
=
None
):
def
update
(
self
,
request
,
pk
,
format
=
None
):
instance
=
self
.
get_object
(
pk
)
serializer
=
InstanceSerializer
(
instance
,
data
=
request
.
data
)
if
serializer
.
is_valid
():
...
...
@@ -69,22 +64,28 @@ class InstanceDetail(APIView):
return
Response
(
serializer
.
data
)
return
Response
(
serializer
.
errors
,
status
=
status
.
HTTP_400_BAD_REQUEST
)
def
de
lete
(
self
,
request
,
pk
,
format
=
None
):
def
de
stroy
(
self
,
request
,
pk
,
format
=
None
):
instance
=
self
.
get_object
(
pk
)
instance
.
delete
()
return
Response
(
status
=
status
.
HTTP_204_NO_CONTENT
)
class
Flavor
ListView
(
APIView
):
class
Flavor
ViewSet
(
ViewSet
):
"""
Create, update or delete a flavor.
"""
def
get
(
self
,
request
,
format
=
None
):
def
get_object
(
self
,
pk
):
try
:
return
Flavor
.
objects
.
get
(
pk
=
pk
)
except
Flavor
.
DoesNotExist
:
raise
Http404
def
list
(
self
,
request
,
format
=
None
):
flavors
=
Flavor
.
objects
.
all
()
return
Response
(
FlavorSerializer
(
flavors
,
many
=
True
)
.
data
)
def
post
(
self
,
request
,
format
=
None
):
def
create
(
self
,
request
,
format
=
None
):
data
=
request
.
data
new_flavor
=
Flavor
.
create
(
name
=
data
[
"name"
],
description
=
data
[
"description"
],
...
...
@@ -94,3 +95,14 @@ class FlavorListView(APIView):
priority
=
data
[
"priority"
])
return
Response
(
new_flavor
.
pk
)
def
update
(
self
,
request
,
pk
):
return
Response
(
status
=
status
.
HTTP_400_BAD_REQUEST
)
def
partial_update
(
self
,
request
,
pk
):
return
Response
(
status
=
status
.
HTTP_400_BAD_REQUEST
)
def
destroy
(
self
,
request
,
pk
):
flavor
=
self
.
get_object
(
pk
)
flavor
.
delete
()
return
Response
(
status
=
status
.
HTTP_204_NO_CONTENT
)
interface_openstack
@
e01d873c
Subproject commit
7b2647531e54da62fcf1db4c61a5e0530e977da4
Subproject commit
e01d873c78ac17fed0438936f979de3cbaca6a5e
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