Commit 08457367 by Bodor Máté

Refactor models, seializer, urls, admin and views

parent 3a808815
Pipeline #831 passed with stage
in 1 minute 21 seconds
from django.contrib import admin
from image.models import Image
from image.models import Disk
from image.models import Image, Disk
admin.site.register(Image)
admin.site.register(Disk)
......@@ -7,12 +7,14 @@ from interface_openstack.implementation.image.openstack_image_manager import Ope
class Disk(models.Model):
"""A virtual disk.
"""
name = models.CharField(
blank=True, max_length=100, verbose_name="name", help_text="Name of the disk"
blank=True,
max_length=100,
verbose_name="name",
help_text="Name of the disk"
)
remote_id = models.CharField(
max_length=40,
......@@ -23,7 +25,6 @@ class Disk(models.Model):
class Image(models.Model):
"""A virtual image.
"""
......@@ -61,48 +62,34 @@ class Image(models.Model):
)
@classmethod
def create(cls, name, description, remote_id, uploaded_by_user, created_by):
inst = cls(name=name, description=description, uploaded_by_user=uploaded_by_user,
remote_id=remote_id, created_by=created_by)
inst.full_clean()
inst.save()
return inst
@classmethod
def create_from_instance(cls, user, instance, description):
interface = OSVirtualMachineManager(settings.CONNECTION)
remote_image = interface.create_image(instance.remote_id)
remote_id = remote_image.id
name = remote_image.name
new_image = cls.create(
name=name,
remote_id=remote_id,
created_by=user,
uploaded_by_user=False,
description=description
)
for attr, value in new_image.__dict__.items():
setattr(remote_image, attr, value)
return remote_image
new_image = Image.objects.create(name=name,
remote_id=remote_id,
created_by=user,
uploaded_by_user=False,
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(),
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
)
new_image = Image.objects.create(name=name,
remote_id=remote_image.id,
created_by=user,
uploaded_by_user=True,
description=description)
for attr, value in new_image.__dict__.items():
setattr(remote_image, attr, value)
return remote_image
def delete(self):
def delete(self, **kwargs):
interface = OpenstackImageManager(settings.CONNECTION)
if interface.delete(self.remote_id):
super().delete()
......
......@@ -11,7 +11,7 @@ class ImageUpdateSerializer(serializers.Serializer):
class ImageSerializer(serializers.ModelSerializer):
image_file = serializers.FileField(write_only=True)
file_format = serializers.CharField(max_length=10)
size = serializers.IntegerField()
size = serializers.IntegerField(read_only=True)
class Meta:
model = Image
......
from rest_framework import routers
from image import views
router = routers.DefaultRouter()
......
# from django.shortcuts import render
from rest_framework.viewsets import ViewSet
from rest_framework.response import Response
from django.shortcuts import get_object_or_404
......@@ -6,8 +5,7 @@ from rest_framework import status
from django.core.exceptions import ObjectDoesNotExist
from image.models import Image
from image.serializers import ImageSerializer
from image.serializers import ImageUpdateSerializer
from image.serializers import ImageSerializer, ImageUpdateSerializer
class ImageViewSet(ViewSet):
......@@ -37,7 +35,6 @@ class ImageViewSet(ViewSet):
def retrieve(self, request, pk=None):
queryset = Image.objects.all()
image = get_object_or_404(queryset, pk=pk)
serializer = ImageSerializer(instance=image)
image = image.get()
serializer = ImageSerializer(instance=image)
return Response(serializer.data)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment