Making labels additive and not adding a many item to config if already in parent

This commit is contained in:
John Westcott IV 2022-09-09 12:11:14 -04:00 committed by Alan Rominger
parent 68e11d2b81
commit e076f1ee2a
No known key found for this signature in database
GPG Key ID: C2D7EAAA12B63559
3 changed files with 13 additions and 7 deletions

View File

@ -976,11 +976,11 @@ class LaunchTimeConfigBase(BaseModel):
if isinstance(field, models.ManyToManyField):
if not self.pk:
continue # unsaved object can't have related many-to-many
prompt_val = set(getattr(self, prompt_name).all())
if len(prompt_val) > 0:
# We used to return a set but that will cause issues with order for ordered fields (like instance_groups)
# So instead we will return an array of items
data[prompt_name] = [item for item in getattr(self, prompt_name).all()]
prompt_values = list(getattr(self, prompt_name).all())
# Many to manys can't distinguish between None and []
# Because of this, from a config perspective, we assume [] is none and we don't save [] into the config
if len(prompt_values) > 0:
data[prompt_name] = prompt_values
elif prompt_name == 'extra_vars':
if self.extra_vars:
if display:

View File

@ -1007,8 +1007,11 @@ class UnifiedJob(
# Here we are doing a loop to make sure we preserve order in case this is a Ordered field
job_item = kwargs.get(field_name, [])
if job_item:
parent_items = getattr(parent, field_name, []).all()
for item in job_item:
getattr(config, field_name).add(item)
# Do not include this item in the config if its in the parent
if item not in parent_items:
getattr(config, field_name).add(item)
return config

View File

@ -531,13 +531,16 @@ def copy_m2m_relationships(obj1, obj2, fields, kwargs=None):
src_field_value = getattr(obj1, field_name)
if kwargs and field_name in kwargs:
override_field_val = kwargs[field_name]
# TODO: Should we spike this our or just put the for loop inside the next if and make everything respect order?
if field_name == 'instance_groups':
# instance_groups are a list but we need to preserve the order
for ig_id in override_field_val:
getattr(obj2, field_name).add(ig_id)
continue
if isinstance(override_field_val, (set, list, QuerySet)):
# Labels are additive so we are going to add any src labels in addition to the override labels
if field_name == 'labels':
for jt_label in src_field_value.all():
getattr(obj2, field_name).add(jt_label.id)
getattr(obj2, field_name).add(*override_field_val)
continue
if override_field_val.__class__.__name__ == 'ManyRelatedManager':