Commit d091507d by Bodor Máté

Fix flake8 errors

parent a017ac6d
Pipeline #788 failed with stages
in 2 minutes 0 seconds
......@@ -69,7 +69,6 @@ class Image(models.Model):
return inst
@classmethod
def create_from_instance(cls, user, instance, description):
interface = OSVirtualMachineManager(settings.CONNECTION)
......@@ -84,11 +83,12 @@ 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)
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,
......
# from django.shortcuts import render
from rest_framework.viewsets import ModelViewSet
from rest_framework.response import Response
from django.http import HttpResponse
from image.models import Image
from image.serializers import ImageSerializer
class ImageViewSet(ModelViewSet):
serializer_class=ImageSerializer
queryset=Image.objects.all()
serializer_class = ImageSerializer
queryset = Image.objects.all()
# def list(self, request):
# return HttpResponse("list")
......@@ -17,7 +16,7 @@ class ImageViewSet(ModelViewSet):
serializer = ImageSerializer(data=request.data)
serializer.is_valid(raise_exception=True)
data = serializer.validated_data
new_image =Image.create_from_user(
new_image = Image.create_from_user(
description=data['description'],
file_format=data['file_format'],
image_file=data['image_file'],
......@@ -26,7 +25,7 @@ class ImageViewSet(ModelViewSet):
)
serializer = ImageSerializer(instance=new_image)
return Response(serializer.data)
# def retrieve(self, request, pk=None):
# return HttpResponse("retrive")
......
......@@ -9,6 +9,7 @@ from interface_openstack.implementation.vm.instance import (
)
from instance.models import Instance, Flavor, Lease
from instance.serializers import InstanceSerializer, FlavorSerializer
from template.models import ImageTemplate
class InstanceList(APIView):
......
......@@ -29,4 +29,3 @@ urlpatterns = [
re_path(r"^auth/", include("djoser.urls.authtoken")),
path(r"swagger", schema_view),
]
\ No newline at end of file
......@@ -6,8 +6,7 @@ from image.models import Disk
from image.models import Image
from instance.models import Lease
from instance.models import Flavor
from interface_openstack.implementation.storage.openstack_snapshot_manager import SnapshotManager
from interface_openstack.implementation.vm.instance import OSVirtualMachineManager
from interface_openstack.implementation.storage.openstack_snapshot_manager import SnapshotManager
class BaseTemplate(models.Model):
......@@ -47,7 +46,7 @@ class BaseTemplate(models.Model):
related_name='templates')
lease = models.ForeignKey(Lease, on_delete="CASCADE",
related_name='templates')
class DiskTemplate(BaseTemplate):
disk = models.ForeignKey(
......@@ -71,13 +70,12 @@ class DiskTemplate(BaseTemplate):
)
return new_template
class ImageTemplate(BaseTemplate):
TYPES = (
('U', 'User create the template from image'),
('I', 'Template created from instance'),
('P', '"Pure" template'),
)
image = models.ForeignKey(
......@@ -90,13 +88,12 @@ class ImageTemplate(BaseTemplate):
@classmethod
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 = cls(name=name, description=description, image=image, lease=lease,
flavor=flavor, created_by=created_by, type=type)
inst.full_clean()
inst.save()
return inst
@classmethod
def create_from_instance(cls, name, description, instance, user):
image = Image.create_from_instance(user, instance, description)
......@@ -104,7 +101,7 @@ class ImageTemplate(BaseTemplate):
name=name,
description=description,
created_by=user,
image = image,
image=image,
lease=instance.lease,
flavor=instance.flavor,
type="I"
......
......@@ -9,8 +9,8 @@ from template.models import ImageTemplate
class ImageTemplateViewSet(ModelViewSet):
serializer_class=ImageTemplateModelSerializer
queryset=ImageTemplate.objects.all()
serializer_class = ImageTemplateModelSerializer
queryset = ImageTemplate.objects.all()
def create(self, request):
serializer = ImageTemplateModelSerializer(data=request.data)
......@@ -20,24 +20,24 @@ class ImageTemplateViewSet(ModelViewSet):
name=data["name"],
description=data["description"],
created_by=request.user,
image = data["image"],
image=data["image"],
lease=data["lease"],
flavor=data["flavor"],
type="U"
)
serializer = ImageTemplateModelSerializer(instance=new_template)
return Response(serializer.data)
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():
# delete not allowed key
for key in request.data.keys():
if key not in allowed_keys:
request.data.pop(key, None)
request.data.pop(key, None)
return super(QuestionViewSet, self).update(request, partial=True)
return super(ImageTemplateViewSet, self).update(request, partial=True)
@action(detail=True, methods=["post"])
def template(self, request, pk):
......@@ -45,6 +45,7 @@ class ImageTemplateViewSet(ModelViewSet):
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"], instance, request.user)
new_template = ImageTemplate.create_from_instance(data["name"], data["description"],
instance, request.user)
serializer = ImageTemplateModelSerializer(instance=new_template)
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