From 5748eecedb665675ae62b4bf5eb33dc55209c52f Mon Sep 17 00:00:00 2001 From: PCoder Date: Wed, 13 Jun 2018 11:16:49 +0200 Subject: [PATCH 1/5] Create field for storing os_templates_to_show --- datacenterlight/models.py | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/datacenterlight/models.py b/datacenterlight/models.py index 729bbdf9..3801465a 100644 --- a/datacenterlight/models.py +++ b/datacenterlight/models.py @@ -1,5 +1,7 @@ import logging +from django import forms +from django.contrib.postgres.fields import ArrayField from django.db import models logger = logging.getLogger(__name__) @@ -32,7 +34,31 @@ class VMTemplate(models.Model): return vm_template +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. + """ + + def formfield(self, **kwargs): + defaults = { + 'form_class': forms.MultipleChoiceField, + 'choices': self.base_field.choices, + 'initial': [c[0] for c in self.base_field.choices], + } + defaults.update(kwargs) + # Skip our parent's formfield implementation completely as we don't + # care for it. + # pylint:disable=bad-super-call + return super(ArrayField, self).formfield(**defaults) + + class VMPricing(models.Model): + VMTemplateChoices = list( + (str(obj.opennebula_vm_template_id), obj.name) + for obj in VMTemplate.objects.all() + ) name = models.CharField(max_length=255, unique=True) vat_inclusive = models.BooleanField(default=True) vat_percentage = models.DecimalField( @@ -55,6 +81,18 @@ class VMPricing(models.Model): max_digits=6, decimal_places=2, default=0 ) + vm_templates_to_show = MultipleChoiceArrayField( + base_field=models.CharField( + blank=True, + max_length=256, + choices=VMTemplateChoices + ), + default=list, + blank=True, + help_text="Not selecting any items above will result in showing all " + "templates" + ) + def __str__(self): display_str = self.name + ' => ' + ' - '.join([ '{}/Core'.format(self.cores_unit_price.normalize()), From 94f520be355754a9164014f2569cf248441f4337 Mon Sep 17 00:00:00 2001 From: PCoder Date: Wed, 13 Jun 2018 11:17:18 +0200 Subject: [PATCH 2/5] Add migration --- .../0024_vmpricing_vm_templates_to_show.py | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 datacenterlight/migrations/0024_vmpricing_vm_templates_to_show.py diff --git a/datacenterlight/migrations/0024_vmpricing_vm_templates_to_show.py b/datacenterlight/migrations/0024_vmpricing_vm_templates_to_show.py new file mode 100644 index 00000000..5705df5b --- /dev/null +++ b/datacenterlight/migrations/0024_vmpricing_vm_templates_to_show.py @@ -0,0 +1,21 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.9.4 on 2018-06-13 09:09 +from __future__ import unicode_literals + +import datacenterlight.models +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('datacenterlight', '0023_auto_20180524_0349'), + ] + + operations = [ + migrations.AddField( + model_name='vmpricing', + name='vm_templates_to_show', + field=datacenterlight.models.MultipleChoiceArrayField(base_field=models.CharField(blank=True, choices=[('4', 'CentOS 7'), ('14', 'Debian 8'), ('25', 'Ubuntu 14.04'), ('26', 'Ubuntu 16.04'), ('36', 'Devuan Jessie'), ('65', 'Devuan Ascii'), ('69', 'FreeBSD 11.1')], max_length=256), blank=True, default=list, help_text='Not selecting any items above will result in showing all templates', size=None), + ), + ] From b872777bda964c36dd65073f03650db25e9a977c Mon Sep 17 00:00:00 2001 From: PCoder Date: Wed, 13 Jun 2018 11:53:43 +0200 Subject: [PATCH 3/5] Filter context templates also by the ids that have been set for the calculator --- datacenterlight/cms_plugins.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/datacenterlight/cms_plugins.py b/datacenterlight/cms_plugins.py index 769824e0..9c6279bc 100644 --- a/datacenterlight/cms_plugins.py +++ b/datacenterlight/cms_plugins.py @@ -88,9 +88,15 @@ class DCLCalculatorPlugin(CMSPluginBase): context = super(DCLCalculatorPlugin, self).render( context, instance, placeholder ) - context['templates'] = VMTemplate.objects.filter( - vm_type=instance.vm_type - ) + ids = instance.pricing.vm_templates_to_show + if ids: + context['templates'] = VMTemplate.objects.filter( + vm_type=instance.vm_type + ).filter(opennebula_vm_template_id__in=ids) + else: + context['templates'] = VMTemplate.objects.filter( + vm_type=instance.vm_type + ) return context From 79e83b4480e731b1ac8730227a2c691f74fe4b27 Mon Sep 17 00:00:00 2001 From: PCoder Date: Sun, 24 Jun 2018 09:08:22 +0200 Subject: [PATCH 4/5] Refactor show_vm_templates to DCLCalculatorPluginModel from VMPricing --- datacenterlight/cms_models.py | 45 +++++++++++++++++++ datacenterlight/cms_plugins.py | 2 +- ...culatorpluginmodel_vm_templates_to_show.py | 21 +++++++++ .../0024_vmpricing_vm_templates_to_show.py | 21 --------- datacenterlight/models.py | 38 ---------------- 5 files changed, 67 insertions(+), 60 deletions(-) create mode 100644 datacenterlight/migrations/0024_dclcalculatorpluginmodel_vm_templates_to_show.py delete mode 100644 datacenterlight/migrations/0024_vmpricing_vm_templates_to_show.py diff --git a/datacenterlight/cms_models.py b/datacenterlight/cms_models.py index e1703aaa..deb84dc7 100644 --- a/datacenterlight/cms_models.py +++ b/datacenterlight/cms_models.py @@ -2,6 +2,8 @@ from cms.extensions import PageExtension from cms.extensions.extension_pool import extension_pool from cms.models.fields import PlaceholderField from cms.models.pluginmodel import CMSPlugin +from django import forms +from django.contrib.postgres.fields import ArrayField from django.contrib.sites.models import Site from django.db import models from django.utils.safestring import mark_safe @@ -292,7 +294,35 @@ class DCLSectionPromoPluginModel(CMSPlugin): return extra_classes +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. + """ + + def formfield(self, **kwargs): + defaults = { + 'form_class': forms.MultipleChoiceField, + 'choices': self.base_field.choices, + } + defaults.update(kwargs) + # Skip our parent's formfield implementation completely as we don't + # care for it. + # pylint:disable=bad-super-call + return super(ArrayField, self).formfield(**defaults) + + class DCLCalculatorPluginModel(CMSPlugin): + 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() + ) pricing = models.ForeignKey( VMPricing, related_name="dcl_custom_pricing_vm_pricing", @@ -303,3 +333,18 @@ class DCLCalculatorPluginModel(CMSPlugin): max_length=50, choices=VMTemplate.VM_TYPE_CHOICES, default=VMTemplate.PUBLIC ) + vm_templates_to_show = MultipleChoiceArrayField( + base_field=models.CharField( + blank=True, + max_length=256, + choices=VMTemplateChoices + ), + default=list, + blank=True, + help_text="Recommended: If you wish to show all templates of the " + "corresponding VM Type (public/ipv6only), please do not " + "select any of the items in the above field. " + "This will allow any new template(s) added " + "in the backend to be automatically listed in this " + "calculator instance." + ) diff --git a/datacenterlight/cms_plugins.py b/datacenterlight/cms_plugins.py index 9c6279bc..95a496d8 100644 --- a/datacenterlight/cms_plugins.py +++ b/datacenterlight/cms_plugins.py @@ -88,7 +88,7 @@ class DCLCalculatorPlugin(CMSPluginBase): context = super(DCLCalculatorPlugin, self).render( context, instance, placeholder ) - ids = instance.pricing.vm_templates_to_show + ids = instance.vm_templates_to_show if ids: context['templates'] = VMTemplate.objects.filter( vm_type=instance.vm_type diff --git a/datacenterlight/migrations/0024_dclcalculatorpluginmodel_vm_templates_to_show.py b/datacenterlight/migrations/0024_dclcalculatorpluginmodel_vm_templates_to_show.py new file mode 100644 index 00000000..179dcff9 --- /dev/null +++ b/datacenterlight/migrations/0024_dclcalculatorpluginmodel_vm_templates_to_show.py @@ -0,0 +1,21 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.9.4 on 2018-06-24 06:54 +from __future__ import unicode_literals + +import datacenterlight.cms_models +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('datacenterlight', '0023_auto_20180524_0349'), + ] + + operations = [ + migrations.AddField( + model_name='dclcalculatorpluginmodel', + name='vm_templates_to_show', + field=datacenterlight.cms_models.MultipleChoiceArrayField(base_field=models.CharField(blank=True, choices=[('4', 'CentOS 7'), ('14', 'Debian 8'), ('25', 'Ubuntu 14.04'), ('26', 'Ubuntu 16.04'), ('36', 'Devuan Jessie'), ('65', 'Devuan Ascii'), ('69', 'FreeBSD 11.1')], max_length=256), blank=True, default=list, help_text='Recommended: If you wish to show all templates of the corresponding VM Type (public/ipv6only), please do not select any of the items in the above field. This will allow any new template(s) added in the backend to be automatically listed in this calculator instance.', size=None), + ), + ] diff --git a/datacenterlight/migrations/0024_vmpricing_vm_templates_to_show.py b/datacenterlight/migrations/0024_vmpricing_vm_templates_to_show.py deleted file mode 100644 index 5705df5b..00000000 --- a/datacenterlight/migrations/0024_vmpricing_vm_templates_to_show.py +++ /dev/null @@ -1,21 +0,0 @@ -# -*- coding: utf-8 -*- -# Generated by Django 1.9.4 on 2018-06-13 09:09 -from __future__ import unicode_literals - -import datacenterlight.models -from django.db import migrations, models - - -class Migration(migrations.Migration): - - dependencies = [ - ('datacenterlight', '0023_auto_20180524_0349'), - ] - - operations = [ - migrations.AddField( - model_name='vmpricing', - name='vm_templates_to_show', - field=datacenterlight.models.MultipleChoiceArrayField(base_field=models.CharField(blank=True, choices=[('4', 'CentOS 7'), ('14', 'Debian 8'), ('25', 'Ubuntu 14.04'), ('26', 'Ubuntu 16.04'), ('36', 'Devuan Jessie'), ('65', 'Devuan Ascii'), ('69', 'FreeBSD 11.1')], max_length=256), blank=True, default=list, help_text='Not selecting any items above will result in showing all templates', size=None), - ), - ] diff --git a/datacenterlight/models.py b/datacenterlight/models.py index 3801465a..729bbdf9 100644 --- a/datacenterlight/models.py +++ b/datacenterlight/models.py @@ -1,7 +1,5 @@ import logging -from django import forms -from django.contrib.postgres.fields import ArrayField from django.db import models logger = logging.getLogger(__name__) @@ -34,31 +32,7 @@ class VMTemplate(models.Model): return vm_template -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. - """ - - def formfield(self, **kwargs): - defaults = { - 'form_class': forms.MultipleChoiceField, - 'choices': self.base_field.choices, - 'initial': [c[0] for c in self.base_field.choices], - } - defaults.update(kwargs) - # Skip our parent's formfield implementation completely as we don't - # care for it. - # pylint:disable=bad-super-call - return super(ArrayField, self).formfield(**defaults) - - class VMPricing(models.Model): - VMTemplateChoices = list( - (str(obj.opennebula_vm_template_id), obj.name) - for obj in VMTemplate.objects.all() - ) name = models.CharField(max_length=255, unique=True) vat_inclusive = models.BooleanField(default=True) vat_percentage = models.DecimalField( @@ -81,18 +55,6 @@ class VMPricing(models.Model): max_digits=6, decimal_places=2, default=0 ) - vm_templates_to_show = MultipleChoiceArrayField( - base_field=models.CharField( - blank=True, - max_length=256, - choices=VMTemplateChoices - ), - default=list, - blank=True, - help_text="Not selecting any items above will result in showing all " - "templates" - ) - def __str__(self): display_str = self.name + ' => ' + ' - '.join([ '{}/Core'.format(self.cores_unit_price.normalize()), From 70cac38f819bb67b50290e91a03afcb5bacdb4b6 Mon Sep 17 00:00:00 2001 From: PCoder Date: Sun, 24 Jun 2018 10:28:17 +0200 Subject: [PATCH 5/5] Move initialization of VMTemplates out of the plugin --- datacenterlight/cms_models.py | 29 +++++++++---------- ...culatorpluginmodel_vm_templates_to_show.py | 4 +-- 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/datacenterlight/cms_models.py b/datacenterlight/cms_models.py index deb84dc7..8c31696f 100644 --- a/datacenterlight/cms_models.py +++ b/datacenterlight/cms_models.py @@ -300,20 +300,6 @@ class MultipleChoiceArrayField(ArrayField): Uses Django's Postgres ArrayField and a MultipleChoiceField for its formfield. """ - - def formfield(self, **kwargs): - defaults = { - 'form_class': forms.MultipleChoiceField, - 'choices': self.base_field.choices, - } - defaults.update(kwargs) - # Skip our parent's formfield implementation completely as we don't - # care for it. - # pylint:disable=bad-super-call - return super(ArrayField, self).formfield(**defaults) - - -class DCLCalculatorPluginModel(CMSPlugin): VMTemplateChoices = list( ( str(obj.opennebula_vm_template_id), @@ -323,6 +309,20 @@ class DCLCalculatorPluginModel(CMSPlugin): ) for obj in VMTemplate.objects.all() ) + + def formfield(self, **kwargs): + defaults = { + 'form_class': forms.MultipleChoiceField, + 'choices': self.VMTemplateChoices, + } + defaults.update(kwargs) + # Skip our parent's formfield implementation completely as we don't + # care for it. + # pylint:disable=bad-super-call + return super(ArrayField, self).formfield(**defaults) + + +class DCLCalculatorPluginModel(CMSPlugin): pricing = models.ForeignKey( VMPricing, related_name="dcl_custom_pricing_vm_pricing", @@ -337,7 +337,6 @@ class DCLCalculatorPluginModel(CMSPlugin): base_field=models.CharField( blank=True, max_length=256, - choices=VMTemplateChoices ), default=list, blank=True, diff --git a/datacenterlight/migrations/0024_dclcalculatorpluginmodel_vm_templates_to_show.py b/datacenterlight/migrations/0024_dclcalculatorpluginmodel_vm_templates_to_show.py index 179dcff9..65bfce21 100644 --- a/datacenterlight/migrations/0024_dclcalculatorpluginmodel_vm_templates_to_show.py +++ b/datacenterlight/migrations/0024_dclcalculatorpluginmodel_vm_templates_to_show.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -# Generated by Django 1.9.4 on 2018-06-24 06:54 +# Generated by Django 1.9.4 on 2018-06-24 08:23 from __future__ import unicode_literals import datacenterlight.cms_models @@ -16,6 +16,6 @@ class Migration(migrations.Migration): migrations.AddField( model_name='dclcalculatorpluginmodel', name='vm_templates_to_show', - field=datacenterlight.cms_models.MultipleChoiceArrayField(base_field=models.CharField(blank=True, choices=[('4', 'CentOS 7'), ('14', 'Debian 8'), ('25', 'Ubuntu 14.04'), ('26', 'Ubuntu 16.04'), ('36', 'Devuan Jessie'), ('65', 'Devuan Ascii'), ('69', 'FreeBSD 11.1')], max_length=256), blank=True, default=list, help_text='Recommended: If you wish to show all templates of the corresponding VM Type (public/ipv6only), please do not select any of the items in the above field. This will allow any new template(s) added in the backend to be automatically listed in this calculator instance.', size=None), + field=datacenterlight.cms_models.MultipleChoiceArrayField(base_field=models.CharField(blank=True, max_length=256), blank=True, default=list, help_text='Recommended: If you wish to show all templates of the corresponding VM Type (public/ipv6only), please do not select any of the items in the above field. This will allow any new template(s) added in the backend to be automatically listed in this calculator instance.', size=None), ), ]