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 django.contrib import admin
from image.models import Image from image.models import Image, Disk
from image.models import Disk
admin.site.register(Image) admin.site.register(Image)
admin.site.register(Disk) admin.site.register(Disk)
...@@ -7,12 +7,14 @@ from interface_openstack.implementation.image.openstack_image_manager import Ope ...@@ -7,12 +7,14 @@ from interface_openstack.implementation.image.openstack_image_manager import Ope
class Disk(models.Model): class Disk(models.Model):
"""A virtual disk. """A virtual disk.
""" """
name = models.CharField( 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( remote_id = models.CharField(
max_length=40, max_length=40,
...@@ -23,7 +25,6 @@ class Disk(models.Model): ...@@ -23,7 +25,6 @@ class Disk(models.Model):
class Image(models.Model): class Image(models.Model):
"""A virtual image. """A virtual image.
""" """
...@@ -61,48 +62,34 @@ class Image(models.Model): ...@@ -61,48 +62,34 @@ class Image(models.Model):
) )
@classmethod @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): def create_from_instance(cls, user, instance, description):
interface = OSVirtualMachineManager(settings.CONNECTION) 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 remote_id = remote_image.id
name = remote_image.name name = remote_image.name
new_image = cls.create( new_image = Image.objects.create(name=name,
name=name, remote_id=remote_id,
remote_id=remote_id, created_by=user,
created_by=user, uploaded_by_user=False,
uploaded_by_user=False, description=description)
description=description return new_image
)
for attr, value in new_image.__dict__.items():
setattr(remote_image, attr, value)
return remote_image
@classmethod @classmethod
def create_from_user(cls, description, file_format, image_file, name, user): def create_from_user(cls, description, file_format, image_file, name, user):
interface = OpenstackImageManager(settings.CONNECTION) 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) format=file_format)
new_image = cls.create( new_image = Image.objects.create(name=name,
name=name, remote_id=remote_image.id,
remote_id=remote_image.id, created_by=user,
created_by=user, uploaded_by_user=True,
uploaded_by_user=True, description=description)
description=description
)
for attr, value in new_image.__dict__.items(): for attr, value in new_image.__dict__.items():
setattr(remote_image, attr, value) setattr(remote_image, attr, value)
return remote_image return remote_image
def delete(self): def delete(self, **kwargs):
interface = OpenstackImageManager(settings.CONNECTION) interface = OpenstackImageManager(settings.CONNECTION)
if interface.delete(self.remote_id): if interface.delete(self.remote_id):
super().delete() super().delete()
......
...@@ -11,7 +11,7 @@ class ImageUpdateSerializer(serializers.Serializer): ...@@ -11,7 +11,7 @@ class ImageUpdateSerializer(serializers.Serializer):
class ImageSerializer(serializers.ModelSerializer): class ImageSerializer(serializers.ModelSerializer):
image_file = serializers.FileField(write_only=True) image_file = serializers.FileField(write_only=True)
file_format = serializers.CharField(max_length=10) file_format = serializers.CharField(max_length=10)
size = serializers.IntegerField() size = serializers.IntegerField(read_only=True)
class Meta: class Meta:
model = Image model = Image
......
from rest_framework import routers from rest_framework import routers
from image import views from image import views
router = routers.DefaultRouter() router = routers.DefaultRouter()
......
# from django.shortcuts import render
from rest_framework.viewsets import ViewSet from rest_framework.viewsets import ViewSet
from rest_framework.response import Response from rest_framework.response import Response
from django.shortcuts import get_object_or_404 from django.shortcuts import get_object_or_404
...@@ -6,8 +5,7 @@ from rest_framework import status ...@@ -6,8 +5,7 @@ from rest_framework import status
from django.core.exceptions import ObjectDoesNotExist from django.core.exceptions import ObjectDoesNotExist
from image.models import Image from image.models import Image
from image.serializers import ImageSerializer from image.serializers import ImageSerializer, ImageUpdateSerializer
from image.serializers import ImageUpdateSerializer
class ImageViewSet(ViewSet): class ImageViewSet(ViewSet):
...@@ -37,7 +35,6 @@ class ImageViewSet(ViewSet): ...@@ -37,7 +35,6 @@ class ImageViewSet(ViewSet):
def retrieve(self, request, pk=None): def retrieve(self, request, pk=None):
queryset = Image.objects.all() queryset = Image.objects.all()
image = get_object_or_404(queryset, pk=pk) image = get_object_or_404(queryset, pk=pk)
serializer = ImageSerializer(instance=image)
image = image.get() image = image.get()
serializer = ImageSerializer(instance=image) serializer = ImageSerializer(instance=image)
return Response(serializer.data) 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