mirror of
https://github.com/ansible/awx.git
synced 2026-06-27 09:28:01 -02:30
job template labels init implementation
This commit is contained in:
@@ -19,6 +19,7 @@ from awx.main.models.ha import * # noqa
|
||||
from awx.main.models.configuration import * # noqa
|
||||
from awx.main.models.notifications import * # noqa
|
||||
from awx.main.models.fact import * # noqa
|
||||
from awx.main.models.label import * # noqa
|
||||
|
||||
# Monkeypatch Django serializer to ignore django-taggit fields (which break
|
||||
# the dumpdata command; see https://github.com/alex/django-taggit/issues/155).
|
||||
@@ -64,3 +65,4 @@ activity_stream_registrar.connect(CustomInventoryScript)
|
||||
activity_stream_registrar.connect(TowerSettings)
|
||||
activity_stream_registrar.connect(Notifier)
|
||||
activity_stream_registrar.connect(Notification)
|
||||
activity_stream_registrar.connect(Label)
|
||||
|
||||
@@ -55,6 +55,7 @@ class ActivityStream(models.Model):
|
||||
custom_inventory_script = models.ManyToManyField("CustomInventoryScript", blank=True)
|
||||
notifier = models.ManyToManyField("Notifier", blank=True)
|
||||
notification = models.ManyToManyField("Notification", blank=True)
|
||||
label = models.ManyToManyField("Label", blank=True)
|
||||
|
||||
def get_absolute_url(self):
|
||||
return reverse('api:activity_stream_detail', args=(self.pk,))
|
||||
|
||||
@@ -122,7 +122,11 @@ class JobOptions(BaseModel):
|
||||
become_enabled = models.BooleanField(
|
||||
default=False,
|
||||
)
|
||||
|
||||
labels = models.ManyToManyField(
|
||||
"Label",
|
||||
blank=True,
|
||||
related_name='%(class)s_labels'
|
||||
)
|
||||
|
||||
extra_vars_dict = VarsDictProperty('extra_vars', True)
|
||||
|
||||
@@ -190,7 +194,8 @@ class JobTemplate(UnifiedJobTemplate, JobOptions):
|
||||
return ['name', 'description', 'job_type', 'inventory', 'project',
|
||||
'playbook', 'credential', 'cloud_credential', 'forks', 'schedule',
|
||||
'limit', 'verbosity', 'job_tags', 'extra_vars', 'launch_type',
|
||||
'force_handlers', 'skip_tags', 'start_at_task', 'become_enabled']
|
||||
'force_handlers', 'skip_tags', 'start_at_task', 'become_enabled',
|
||||
'labels',]
|
||||
|
||||
def create_job(self, **kwargs):
|
||||
'''
|
||||
|
||||
33
awx/main/models/label.py
Normal file
33
awx/main/models/label.py
Normal file
@@ -0,0 +1,33 @@
|
||||
# Copyright (c) 2016 Ansible, Inc.
|
||||
# All Rights Reserved.
|
||||
|
||||
# Django
|
||||
from django.db import models
|
||||
from django.core.urlresolvers import reverse
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
# AWX
|
||||
from awx.main.models.base import CommonModelNameNotUnique
|
||||
|
||||
__all__ = ('Label', )
|
||||
|
||||
class Label(CommonModelNameNotUnique):
|
||||
'''
|
||||
Generic Tag. Designed for tagging Job Templates, but expandable to other models.
|
||||
'''
|
||||
|
||||
class Meta:
|
||||
app_label = 'main'
|
||||
unique_together = (("name", "organization"),)
|
||||
ordering = ('organization', 'name')
|
||||
|
||||
organization = models.ForeignKey(
|
||||
'Organization',
|
||||
related_name='labels',
|
||||
help_text=_('Organization this label belongs to.'),
|
||||
on_delete=models.CASCADE,
|
||||
)
|
||||
|
||||
def get_absolute_url(self):
|
||||
return reverse('api:label_detail', args=(self.pk,))
|
||||
|
||||
@@ -310,11 +310,11 @@ class UnifiedJobTemplate(PolymorphicModel, CommonModelNameNotUnique, Notificatio
|
||||
'''
|
||||
Create a new unified job based on this unified job template.
|
||||
'''
|
||||
save_unified_job = kwargs.pop('save', True)
|
||||
unified_job_class = self._get_unified_job_class()
|
||||
parent_field_name = unified_job_class._get_parent_field_name()
|
||||
kwargs.pop('%s_id' % parent_field_name, None)
|
||||
create_kwargs = {}
|
||||
m2m_fields = {}
|
||||
create_kwargs[parent_field_name] = self
|
||||
for field_name in self._get_unified_job_field_names():
|
||||
# Foreign keys can be specified as field_name or field_name_id.
|
||||
@@ -332,14 +332,25 @@ class UnifiedJobTemplate(PolymorphicModel, CommonModelNameNotUnique, Notificatio
|
||||
elif field_name in kwargs:
|
||||
if field_name == 'extra_vars' and isinstance(kwargs[field_name], dict):
|
||||
create_kwargs[field_name] = json.dumps(kwargs['extra_vars'])
|
||||
# We can't get a hold of django.db.models.fields.related.ManyRelatedManager to compare
|
||||
# so this is the next best thing.
|
||||
elif kwargs[field_name].__class__.__name__ is 'ManyRelatedManager':
|
||||
m2m_fields[field_name] = kwargs[field_name]
|
||||
else:
|
||||
create_kwargs[field_name] = kwargs[field_name]
|
||||
elif hasattr(self, field_name):
|
||||
create_kwargs[field_name] = getattr(self, field_name)
|
||||
field_obj = self._meta.get_field_by_name(field_name)[0]
|
||||
# Many to Many can be specified as field_name
|
||||
if isinstance(field_obj, models.ManyToManyField):
|
||||
m2m_fields[field_name] = getattr(self, field_name)
|
||||
else:
|
||||
create_kwargs[field_name] = getattr(self, field_name)
|
||||
new_kwargs = self._update_unified_job_kwargs(**create_kwargs)
|
||||
unified_job = unified_job_class(**new_kwargs)
|
||||
if save_unified_job:
|
||||
unified_job.save()
|
||||
unified_job.save()
|
||||
for field_name, src_field_value in m2m_fields.iteritems():
|
||||
dest_field = getattr(unified_job, field_name)
|
||||
dest_field.add(*list(src_field_value.all().values_list('id', flat=True)))
|
||||
return unified_job
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user