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
e638f2fd
authored
Jul 21, 2019
by
Bodor Máté
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Create template views
parent
47e13094
Hide whitespace changes
Inline
Side-by-side
Showing
12 changed files
with
163 additions
and
56 deletions
+163
-56
recircle/image/admin.py
+2
-0
recircle/image/models.py
+16
-2
recircle/image/serializers.py
+17
-4
recircle/image/urls.py
+4
-4
recircle/image/views.py
+33
-15
recircle/instance/views.py
+2
-2
recircle/recircle/urls.py
+1
-1
recircle/template/migrations/0006_auto_20190719_1416.py
+18
-0
recircle/template/models.py
+9
-8
recircle/template/serializers.py
+17
-3
recircle/template/urls.py
+4
-9
recircle/template/views.py
+40
-8
No files found.
recircle/image/admin.py
View file @
e638f2fd
from
django.contrib
import
admin
from
image.models
import
Image
from
image.models
import
Disk
admin
.
site
.
register
(
Image
)
admin
.
site
.
register
(
Disk
)
recircle/image/models.py
View file @
e638f2fd
...
...
@@ -3,6 +3,7 @@ from django.contrib.auth.models import User
from
django.conf
import
settings
from
interface_openstack.implementation.vm.instance
import
OSVirtualMachineManager
from
interface_openstack.implementation.image.openstack_image_manager
import
OpenstackImageManager
class
Disk
(
models
.
Model
):
...
...
@@ -70,9 +71,9 @@ class Image(models.Model):
@classmethod
def
create_from_instance
(
cls
,
user
,
instance
_remote_id
,
description
):
def
create_from_instance
(
cls
,
user
,
instance
,
description
):
interface
=
OSVirtualMachineManager
(
settings
.
CONNECTION
)
remote_image
=
interface
.
create_image
(
instance
_
remote_id
)
remote_image
=
interface
.
create_image
(
instance
.
remote_id
)
remote_id
=
remote_image
.
id
name
=
remote_image
.
name
new_image
=
cls
.
create
(
...
...
@@ -83,3 +84,16 @@ class Image(models.Model):
description
=
description
)
return
new_image
@classmethod
def
create_from_user
(
cls
,
description
,
file_format
,
image_file
,
name
,
user
):
interface
=
OpenstackImageManager
(
settings
.
CONNECTION
)
remote_image
=
interface
.
upload_file
(
name
=
name
,
path
=
image_file
.
temporary_file_path
(),
format
=
file_format
)
new_image
=
cls
.
create
(
name
=
name
,
remote_id
=
remote_image
.
id
,
created_by
=
user
,
uploaded_by_user
=
True
,
description
=
description
)
return
new_image
recircle/image/serializers.py
View file @
e638f2fd
from
rest_framework
import
serializers
from
.models
import
Disk
from
.models
import
Image
class
DiskSerializer
(
serializers
.
ModelSerializer
):
class
ImageSerializer
(
serializers
.
ModelSerializer
):
image_file
=
serializers
.
FileField
(
write_only
=
True
)
file_format
=
serializers
.
CharField
(
max_length
=
10
,
write_only
=
True
)
class
Meta
:
model
=
Disk
fields
=
(
"name"
,
"remote_ID"
)
model
=
Image
fields
=
(
"name"
,
"remote_id"
,
"description"
,
"image_file"
,
"file_format"
,
"created_at"
,
"uploaded_by_user"
,
"created_by"
,
)
read_only_fields
=
(
"created_at"
,
"uploaded_by_user"
,
"created_by"
,
"remote_id"
,
)
recircle/image/urls.py
View file @
e638f2fd
from
django.urls
import
path
from
rest_framework.urlpatterns
import
format_suffix_patterns
from
rest_framework
import
routers
from
image
import
views
urlpatterns
=
[
path
(
""
,
views
.
DiskList
.
as_view
())]
router
=
routers
.
DefaultRouter
()
router
.
register
(
r"images"
,
views
.
ImageViewSet
,
basename
=
"image"
)
urlpatterns
=
format_suffix_patterns
(
urlpatterns
)
urlpatterns
=
router
.
urls
recircle/image/views.py
View file @
e638f2fd
# from django.shortcuts import render
from
rest_framework.views
import
APIView
from
rest_framework.views
ets
import
ModelViewSet
from
rest_framework.response
import
Response
from
django.
conf
import
settings
from
django.
http
import
HttpResponse
from
image.serializers
import
DiskSerializer
from
image.models
import
Disk
from
interface_openstack.implementation.image.openstack_image_manager
import
(
OpenstackImageManager
)
from
image.models
import
Image
from
image.serializers
import
ImageSerializer
class
DiskList
(
APIView
):
def
get
(
self
,
request
,
format
=
None
):
# OpenStack
interface
=
OpenstackImageManager
(
settings
.
CONNECTION
)
return
Response
([
disk
.
__dict__
for
disk
in
interface
.
list
()]
)
class
ImageViewSet
(
ModelViewSet
):
serializer_class
=
ImageSerializer
queryset
=
Image
.
objects
.
all
()
# def list(self, request):
# return HttpResponse("list"
)
# Create response
disks
=
Disk
.
object
.
all
()
serializer
=
DiskSerializer
(
disks
,
many
=
True
)
def
create
(
self
,
request
):
serializer
=
ImageSerializer
(
data
=
request
.
data
)
serializer
.
is_valid
(
raise_exception
=
True
)
data
=
serializer
.
validated_data
new_image
=
Image
.
create_from_user
(
description
=
data
[
'description'
],
file_format
=
data
[
'file_format'
],
image_file
=
data
[
'image_file'
],
name
=
data
[
'name'
],
user
=
request
.
user
)
serializer
=
ImageSerializer
(
instance
=
new_image
)
return
Response
(
serializer
.
data
)
# def retrieve(self, request, pk=None):
# return HttpResponse("retrive")
# def update(self, request, pk=None):
# return HttpResponse("update")
# def partial_update(self, request, pk=None):
# return HttpResponse("patch")
# def destroy(self, request, pk=None):
# return HttpResponse("delete")
recircle/instance/views.py
View file @
e638f2fd
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.response
import
Response
from
rest_framework
import
status
from
interface_openstack.implementation.vm.instance
import
(
OSVirtualMachineManager
)
from
template.models
import
ImageTemplate
from
instance.models
import
Instance
,
Flavor
,
Lease
from
instance.serializers
import
InstanceSerializer
,
FlavorSerializer
class
InstanceList
(
APIView
):
...
...
recircle/recircle/urls.py
View file @
e638f2fd
...
...
@@ -21,7 +21,7 @@ from rest_framework_swagger.views import get_swagger_view
schema_view
=
get_swagger_view
(
title
=
"RECIRCLE API"
)
urlpatterns
=
[
path
(
"
images
/"
,
include
(
"image.urls"
)),
path
(
"
api/v1
/"
,
include
(
"image.urls"
)),
path
(
"api/v1/"
,
include
(
"instance.urls"
)),
path
(
"api/v1/"
,
include
(
"template.urls"
)),
path
(
"admin/"
,
admin
.
site
.
urls
),
...
...
recircle/template/migrations/0006_auto_20190719_1416.py
0 → 100644
View file @
e638f2fd
# Generated by Django 2.2.3 on 2019-07-19 14:16
from
django.db
import
migrations
,
models
class
Migration
(
migrations
.
Migration
):
dependencies
=
[
(
'template'
,
'0005_auto_20190717_0948'
),
]
operations
=
[
migrations
.
AlterField
(
model_name
=
'imagetemplate'
,
name
=
'type'
,
field
=
models
.
CharField
(
choices
=
[(
'U'
,
'User create the template from image'
),
(
'I'
,
'Template created from instance'
),
(
'P'
,
'"Pure" template'
)],
default
=
'U'
,
max_length
=
10
),
),
]
recircle/template/models.py
View file @
e638f2fd
...
...
@@ -76,7 +76,7 @@ class ImageTemplate(BaseTemplate):
TYPES
=
(
(
'U'
,
'User create the template from image'
),
(
'I'
,
'Template created from instance'
),
(
'
D'
,
'Default
"Pure" template'
),
(
'
P'
,
'
"Pure" template'
),
)
...
...
@@ -86,11 +86,11 @@ class ImageTemplate(BaseTemplate):
on_delete
=
models
.
CASCADE
,
help_text
=
""
)
type
=
models
.
CharField
(
max_length
=
10
,
choices
=
TYPES
)
type
=
models
.
CharField
(
max_length
=
10
,
choices
=
TYPES
,
default
=
"U"
)
@classmethod
def
create
(
cls
,
name
,
description
,
image
,
lease
,
flavor
,
created_by
):
inst
=
cls
(
name
=
name
,
description
=
description
,
image
=
image
,
lease
=
lease
,
flavor
=
flavor
,
created_by
=
created_by
)
def
create
(
cls
,
name
,
description
,
image
,
lease
,
flavor
,
created_by
,
type
=
'U'
):
inst
=
cls
(
name
=
name
,
description
=
description
,
image
=
image
,
lease
=
lease
,
flavor
=
flavor
,
created_by
=
created_by
,
type
=
type
)
inst
.
full_clean
()
inst
.
save
()
...
...
@@ -98,14 +98,15 @@ class ImageTemplate(BaseTemplate):
@classmethod
def
create_from_instance
(
cls
,
name
,
description
,
instance
_remote_id
,
lease
,
flavor
,
user
):
image
=
Image
.
create_from_instance
(
user
,
instance
_remote_id
,
description
)
def
create_from_instance
(
cls
,
name
,
description
,
instance
,
user
):
image
=
Image
.
create_from_instance
(
user
,
instance
,
description
)
new_template
=
cls
.
create
(
name
=
name
,
description
=
description
,
created_by
=
user
,
image
=
image
,
lease
=
lease
,
flavor
=
flavor
lease
=
instance
.
lease
,
flavor
=
instance
.
flavor
,
type
=
"I"
)
return
new_template
recircle/template/serializers.py
View file @
e638f2fd
...
...
@@ -3,12 +3,26 @@ from rest_framework import serializers
from
template.models
import
ImageTemplate
class
InstanceTemplateSerializer
(
serializers
.
Serializer
):
class
Instance
From
TemplateSerializer
(
serializers
.
Serializer
):
name
=
serializers
.
CharField
()
description
=
serializers
.
CharField
()
instance_remote_id
=
serializers
.
CharField
()
class
ImageTemplateModelSerializer
(
serializers
.
ModelSerializer
):
class
Meta
:
model
=
ImageTemplate
fields
=
(
"name"
,
"description"
,
"created_at"
,
"created_by"
,
"image"
)
fields
=
(
"name"
,
"description"
,
"created_at"
,
"created_by"
,
"image"
,
"flavor"
,
"lease"
,
"type"
,
)
read_only_fields
=
(
"created_at"
,
"created_by"
,
"type"
,
)
recircle/template/urls.py
View file @
e638f2fd
from
django.urls
import
path
from
rest_framework.urlpatterns
import
format_suffix_patterns
from
rest_framework
import
routers
from
template
import
views
urlpatterns
=
[
path
(
"template/"
,
views
.
TemplateList
.
as_view
()),
# path("template/<int:pk>/", views.TemplateDetail.as_view()),
# path('instances/<int:pk>/action/', views.InstanceAction.as_view())
]
router
=
routers
.
DefaultRouter
()
router
.
register
(
r"templates/image-templates"
,
views
.
ImageTemplateViewSet
,
basename
=
"image-template"
)
urlpatterns
=
format_suffix_patterns
(
urlpatterns
)
\ No newline at end of file
urlpatterns
=
router
.
urls
recircle/template/views.py
View file @
e638f2fd
# from django.shortcuts import render
from
rest_framework.response
import
Response
from
rest_framework.views
import
APIView
from
django.http
import
HttpResponse
from
rest_framework.views
ets
import
ModelViewSet
from
rest_framework.decorators
import
action
from
template.serializers
import
InstanceTemplateSerializer
from
template.serializers
import
Instance
From
TemplateSerializer
from
template.serializers
import
ImageTemplateModelSerializer
from
template.models
import
ImageTemplate
class
TemplateList
(
APIView
):
def
post
(
self
,
request
,
format
=
None
):
serializer
=
InstanceTemplateSerializer
(
data
=
request
.
data
)
serializer
.
is_valid
()
class
ImageTemplateViewSet
(
ModelViewSet
):
serializer_class
=
ImageTemplateModelSerializer
queryset
=
ImageTemplate
.
objects
.
all
()
def
create
(
self
,
request
):
serializer
=
ImageTemplateModelSerializer
(
data
=
request
.
data
)
serializer
.
is_valid
(
raise_exception
=
True
)
data
=
serializer
.
validated_data
new_template
=
ImageTemplate
.
create
(
name
=
data
[
"name"
],
description
=
data
[
"description"
],
created_by
=
request
.
user
,
image
=
data
[
"image"
],
lease
=
data
[
"lease"
],
flavor
=
data
[
"flavor"
],
type
=
"U"
)
serializer
=
ImageTemplateModelSerializer
(
instance
=
new_template
)
return
Response
(
serializer
.
data
)
def
update
(
self
,
request
,
*
args
,
**
kwargs
):
# only the name, description, lease, flavor can be updated
allowed_keys
=
[
"name"
,
"description"
,
"lease"
,
"flavor"
]
#delete not allowed key
for
key
in
request
.
data
.
keys
():
if
key
not
in
allowed_keys
:
request
.
data
.
pop
(
key
,
None
)
return
super
(
QuestionViewSet
,
self
)
.
update
(
request
,
partial
=
True
)
@action
(
detail
=
True
,
methods
=
[
"post"
])
def
template
(
self
,
request
,
pk
):
instance
=
self
.
get_object
(
pk
)
serializer
=
InstanceFromTemplateSerializer
(
data
=
request
.
data
)
serializer
.
is_valid
(
raise_exception
=
True
)
data
=
serializer
.
validated_data
new_template
=
ImageTemplate
.
create_from_instance
(
data
[
"name"
],
data
[
"description"
],
data
[
"instance_remote_id"
]
,
request
.
user
)
new_template
=
ImageTemplate
.
create_from_instance
(
data
[
"name"
],
data
[
"description"
],
instance
,
request
.
user
)
serializer
=
ImageTemplateModelSerializer
(
instance
=
new_template
)
return
Response
(
serializer
.
data
)
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