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()),