forked from ungleich-public/dynamicweb
fix virtual machine serializer e.t.c
This commit is contained in:
parent
afb096b7a3
commit
6b21b86ffe
5 changed files with 62 additions and 23 deletions
|
|
@ -68,7 +68,7 @@ from cms.utils.i18n import get_language_list
|
|||
from cms.utils.page import get_available_slug, get_clean_username
|
||||
from cms.utils.permissions import get_current_user, reset_current_user, set_current_user
|
||||
from cms.utils.plugins import copy_plugins_to_placeholder, downcast_plugins
|
||||
from .menus.menu_pool import menu_pool
|
||||
from menus.menu_pool import menu_pool
|
||||
|
||||
# ===============================================================================
|
||||
# Helpers/Internals
|
||||
|
|
|
|||
|
|
@ -307,30 +307,35 @@ class DCLSectionPromoPluginModel(CMSPlugin):
|
|||
class MultipleChoiceArrayField(ArrayField):
|
||||
"""
|
||||
A field that allows us to store an array of choices.
|
||||
|
||||
Uses Django's Postgres ArrayField
|
||||
and a MultipleChoiceField for its formfield.
|
||||
"""
|
||||
VMTemplateChoices = []
|
||||
if settings.OPENNEBULA_DOMAIN != 'test_domain':
|
||||
VMTemplateChoices = list(
|
||||
(
|
||||
str(obj.opennebula_vm_template_id),
|
||||
(obj.name + ' - ' + VMTemplate.IPV6.title()
|
||||
if obj.vm_type == VMTemplate.IPV6 else obj.name
|
||||
)
|
||||
)
|
||||
for obj in VMTemplate.objects.all()
|
||||
)
|
||||
|
||||
def formfield(self, **kwargs):
|
||||
vm_template_choices = []
|
||||
|
||||
if settings.OPENNEBULA_DOMAIN != 'test_domain':
|
||||
vm_template_choices = [
|
||||
(
|
||||
str(obj.opennebula_vm_template_id),
|
||||
(
|
||||
obj.name + ' - ' + VMTemplate.IPV6.title()
|
||||
if obj.vm_type == VMTemplate.IPV6
|
||||
else obj.name
|
||||
),
|
||||
)
|
||||
for obj in VMTemplate.objects.all()
|
||||
]
|
||||
|
||||
defaults = {
|
||||
'form_class': forms.MultipleChoiceField,
|
||||
'choices': self.VMTemplateChoices,
|
||||
'choices': vm_template_choices,
|
||||
}
|
||||
|
||||
defaults.update(kwargs)
|
||||
# Skip our parent's formfield implementation completely as we don't
|
||||
# care for it.
|
||||
# pylint:disable=bad-super-call
|
||||
|
||||
# Skip ArrayField.formfield() because we don't need its implementation.
|
||||
return super(ArrayField, self).formfield(**defaults)
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -120,7 +120,7 @@ INSTALLED_APPS = (
|
|||
#TODO'aldryn_translation_tools',
|
||||
'treebeard', # utilities for implementing a tree
|
||||
'sekizai', # for javascript and css management
|
||||
#TODO'menus', # helper for model independent hierarchical website navigation
|
||||
'menus', # helper for model independent hierarchical website navigation
|
||||
#TODO'cmsplugin_filer_image',
|
||||
|
||||
# 2nd migrate
|
||||
|
|
|
|||
|
|
@ -492,4 +492,32 @@ def legacy_charfield_init(self, *args, **kwargs):
|
|||
_original_charfield_init(self, **kwargs)
|
||||
|
||||
|
||||
forms.CharField.__init__ = legacy_charfield_init
|
||||
forms.CharField.__init__ = legacy_charfield_init
|
||||
|
||||
|
||||
|
||||
import sys
|
||||
import types
|
||||
from collections.abc import (
|
||||
MutableSequence,
|
||||
MutableMapping,
|
||||
MutableSet,
|
||||
Sequence,
|
||||
Mapping,
|
||||
Set,
|
||||
)
|
||||
|
||||
|
||||
def install_backport_collections_shim():
|
||||
module = types.ModuleType("backport_collections")
|
||||
|
||||
module.MutableSequence = MutableSequence
|
||||
module.MutableMapping = MutableMapping
|
||||
module.MutableSet = MutableSet
|
||||
module.Sequence = Sequence
|
||||
module.Mapping = Mapping
|
||||
module.Set = Set
|
||||
|
||||
sys.modules["backport_collections"] = module
|
||||
|
||||
install_backport_collections_shim()
|
||||
|
|
@ -43,6 +43,7 @@ class VirtualMachineTemplateSerializer(serializers.Serializer):
|
|||
|
||||
|
||||
class VirtualMachineSerializer(serializers.Serializer):
|
||||
|
||||
"""Serializer to map the virtual machine instance into JSON format."""
|
||||
|
||||
name = serializers.SerializerMethodField()
|
||||
|
|
@ -50,7 +51,6 @@ class VirtualMachineSerializer(serializers.Serializer):
|
|||
disk = serializers.IntegerField(write_only=True)
|
||||
set_memory = serializers.IntegerField(write_only=True, label='Memory')
|
||||
memory = serializers.SerializerMethodField()
|
||||
|
||||
disk_size = serializers.SerializerMethodField()
|
||||
hdd_size = serializers.SerializerMethodField()
|
||||
ssd_size = serializers.SerializerMethodField()
|
||||
|
|
@ -63,14 +63,20 @@ class VirtualMachineSerializer(serializers.Serializer):
|
|||
configuration = serializers.SerializerMethodField()
|
||||
|
||||
template_id = serializers.ChoiceField(
|
||||
choices=[(key.id, key.name) for key in
|
||||
OpenNebulaManager().try_get_templates()
|
||||
],
|
||||
choices=[],
|
||||
source='template.template_id',
|
||||
write_only=True,
|
||||
default=[]
|
||||
default=[],
|
||||
)
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
|
||||
self.fields['template_id'].choices = [
|
||||
(key.id, key.name)
|
||||
for key in OpenNebulaManager().try_get_templates()
|
||||
]
|
||||
|
||||
def create(self, validated_data):
|
||||
owner = validated_data['owner']
|
||||
ssh_key = validated_data['ssh_key']
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue