PowerFeed.phase to slug (#3569)

This commit is contained in:
Jeremy Stretch 2019-11-25 21:14:04 -05:00
parent bb8b012397
commit 2dc07ca30d
6 changed files with 50 additions and 18 deletions

View File

@ -703,8 +703,8 @@ class PowerFeedSerializer(TaggitSerializer, CustomFieldModelSerializer):
default=PowerFeedSupplyChoices.SUPPLY_AC default=PowerFeedSupplyChoices.SUPPLY_AC
) )
phase = ChoiceField( phase = ChoiceField(
choices=POWERFEED_PHASE_CHOICES, choices=PowerFeedPhaseChoices,
default=POWERFEED_PHASE_SINGLE default=PowerFeedPhaseChoices.PHASE_SINGLE
) )
tags = TagListSerializerField( tags = TagListSerializerField(
required=False required=False

View File

@ -900,11 +900,27 @@ class PowerFeedSupplyChoices(ChoiceSet):
SUPPLY_DC = 'dc' SUPPLY_DC = 'dc'
CHOICES = ( CHOICES = (
(SUPPLY_AC, 'Primary'), (SUPPLY_AC, 'AC'),
(SUPPLY_DC, 'Redundant'), (SUPPLY_DC, 'DC'),
) )
LEGACY_MAP = { LEGACY_MAP = {
SUPPLY_AC: 1, SUPPLY_AC: 1,
SUPPLY_DC: 2, SUPPLY_DC: 2,
} }
class PowerFeedPhaseChoices(ChoiceSet):
PHASE_SINGLE = 'single-phase'
PHASE_3PHASE = 'three-phase'
CHOICES = (
(PHASE_SINGLE, 'Single phase'),
(PHASE_3PHASE, 'Three-phase'),
)
LEGACY_MAP = {
PHASE_SINGLE: 1,
PHASE_3PHASE: 3,
}

View File

@ -68,12 +68,6 @@ COMPATIBLE_TERMINATION_TYPES = {
} }
# Power feeds # Power feeds
POWERFEED_PHASE_SINGLE = 1
POWERFEED_PHASE_3PHASE = 3
POWERFEED_PHASE_CHOICES = (
(POWERFEED_PHASE_SINGLE, 'Single phase'),
(POWERFEED_PHASE_3PHASE, 'Three-phase'),
)
POWERFEED_STATUS_OFFLINE = 0 POWERFEED_STATUS_OFFLINE = 0
POWERFEED_STATUS_ACTIVE = 1 POWERFEED_STATUS_ACTIVE = 1
POWERFEED_STATUS_PLANNED = 2 POWERFEED_STATUS_PLANNED = 2

View File

@ -3870,7 +3870,7 @@ class PowerFeedCSVForm(forms.ModelForm):
help_text='AC/DC' help_text='AC/DC'
) )
phase = CSVChoiceField( phase = CSVChoiceField(
choices=POWERFEED_PHASE_CHOICES, choices=PowerFeedPhaseChoices,
required=False, required=False,
help_text='Single or three-phase' help_text='Single or three-phase'
) )
@ -3948,7 +3948,7 @@ class PowerFeedBulkEditForm(BootstrapMixin, AddRemoveTagsForm, CustomFieldBulkEd
widget=StaticSelect2() widget=StaticSelect2()
) )
phase = forms.ChoiceField( phase = forms.ChoiceField(
choices=add_blank_choice(POWERFEED_PHASE_CHOICES), choices=add_blank_choice(PowerFeedPhaseChoices),
required=False, required=False,
initial='', initial='',
widget=StaticSelect2() widget=StaticSelect2()
@ -4024,7 +4024,7 @@ class PowerFeedFilterForm(BootstrapMixin, CustomFieldFilterForm):
widget=StaticSelect2() widget=StaticSelect2()
) )
phase = forms.ChoiceField( phase = forms.ChoiceField(
choices=add_blank_choice(POWERFEED_PHASE_CHOICES), choices=add_blank_choice(PowerFeedPhaseChoices),
required=False, required=False,
widget=StaticSelect2() widget=StaticSelect2()
) )

View File

@ -11,6 +11,11 @@ POWERFEED_SUPPLY_CHOICES = (
(2, 'dc'), (2, 'dc'),
) )
POWERFEED_PHASE_CHOICES = (
(1, 'single-phase'),
(3, 'three-phase'),
)
def powerfeed_type_to_slug(apps, schema_editor): def powerfeed_type_to_slug(apps, schema_editor):
PowerFeed = apps.get_model('dcim', 'PowerFeed') PowerFeed = apps.get_model('dcim', 'PowerFeed')
@ -24,6 +29,12 @@ def powerfeed_supply_to_slug(apps, schema_editor):
PowerFeed.objects.filter(supply=id).update(supply=slug) PowerFeed.objects.filter(supply=id).update(supply=slug)
def powerfeed_phase_to_slug(apps, schema_editor):
PowerFeed = apps.get_model('dcim', 'PowerFeed')
for id, slug in POWERFEED_PHASE_CHOICES:
PowerFeed.objects.filter(phase=id).update(phase=slug)
class Migration(migrations.Migration): class Migration(migrations.Migration):
atomic = False atomic = False
@ -53,4 +64,14 @@ class Migration(migrations.Migration):
code=powerfeed_supply_to_slug code=powerfeed_supply_to_slug
), ),
# PowerFeed.phase
migrations.AlterField(
model_name='powerfeed',
name='phase',
field=models.CharField(blank=True, max_length=50),
),
migrations.RunPython(
code=powerfeed_phase_to_slug
),
] ]

View File

@ -2108,7 +2108,7 @@ class PowerPort(CableTermination, ComponentModel):
} }
# Calculate per-leg aggregates for three-phase feeds # Calculate per-leg aggregates for three-phase feeds
if self._connected_powerfeed and self._connected_powerfeed.phase == POWERFEED_PHASE_3PHASE: if self._connected_powerfeed and self._connected_powerfeed.phase == PowerFeedPhaseChoices.PHASE_3PHASE:
for leg, leg_name in POWERFEED_LEG_CHOICES: for leg, leg_name in POWERFEED_LEG_CHOICES:
outlet_ids = PowerOutlet.objects.filter(power_port=self, feed_leg=leg).values_list('pk', flat=True) outlet_ids = PowerOutlet.objects.filter(power_port=self, feed_leg=leg).values_list('pk', flat=True)
utilization = PowerPort.objects.filter(_connected_poweroutlet_id__in=outlet_ids).aggregate( utilization = PowerPort.objects.filter(_connected_poweroutlet_id__in=outlet_ids).aggregate(
@ -3121,9 +3121,10 @@ class PowerFeed(ChangeLoggedModel, CableTermination, CustomFieldModel):
choices=PowerFeedSupplyChoices, choices=PowerFeedSupplyChoices,
default=PowerFeedSupplyChoices.SUPPLY_AC default=PowerFeedSupplyChoices.SUPPLY_AC
) )
phase = models.PositiveSmallIntegerField( phase = models.CharField(
choices=POWERFEED_PHASE_CHOICES, max_length=50,
default=POWERFEED_PHASE_SINGLE choices=PowerFeedPhaseChoices,
default=PowerFeedPhaseChoices.PHASE_SINGLE
) )
voltage = models.PositiveSmallIntegerField( voltage = models.PositiveSmallIntegerField(
validators=[MinValueValidator(1)], validators=[MinValueValidator(1)],
@ -3197,7 +3198,7 @@ class PowerFeed(ChangeLoggedModel, CableTermination, CustomFieldModel):
# Cache the available_power property on the instance # Cache the available_power property on the instance
kva = self.voltage * self.amperage * (self.max_utilization / 100) kva = self.voltage * self.amperage * (self.max_utilization / 100)
if self.phase == POWERFEED_PHASE_3PHASE: if self.phase == PowerFeedPhaseChoices.PHASE_3PHASE:
self.available_power = round(kva * 1.732) self.available_power = round(kva * 1.732)
else: else:
self.available_power = round(kva) self.available_power = round(kva)