mirror of
https://github.com/ansible/awx.git
synced 2026-07-03 04:18:02 -02:30
move code linting to a stricter pep8-esque auto-formatting tool, black
This commit is contained in:
@@ -30,11 +30,29 @@ from crum.signals import current_user_getter
|
||||
|
||||
# AWX
|
||||
from awx.main.models import (
|
||||
ActivityStream, Group, Host, InstanceGroup, Inventory, InventorySource,
|
||||
Job, JobHostSummary, JobTemplate, OAuth2AccessToken, Organization, Project,
|
||||
Role, SystemJob, SystemJobTemplate, UnifiedJob, UnifiedJobTemplate, User,
|
||||
UserSessionMembership, WorkflowJobTemplateNode, WorkflowApproval,
|
||||
WorkflowApprovalTemplate, ROLE_SINGLETON_SYSTEM_ADMINISTRATOR
|
||||
ActivityStream,
|
||||
Group,
|
||||
Host,
|
||||
InstanceGroup,
|
||||
Inventory,
|
||||
InventorySource,
|
||||
Job,
|
||||
JobHostSummary,
|
||||
JobTemplate,
|
||||
OAuth2AccessToken,
|
||||
Organization,
|
||||
Project,
|
||||
Role,
|
||||
SystemJob,
|
||||
SystemJobTemplate,
|
||||
UnifiedJob,
|
||||
UnifiedJobTemplate,
|
||||
User,
|
||||
UserSessionMembership,
|
||||
WorkflowJobTemplateNode,
|
||||
WorkflowApproval,
|
||||
WorkflowApprovalTemplate,
|
||||
ROLE_SINGLETON_SYSTEM_ADMINISTRATOR,
|
||||
)
|
||||
from awx.main.constants import CENSOR_VALUE
|
||||
from awx.main.utils import model_instance_diff, model_to_dict, camelcase_to_underscore, get_current_apps
|
||||
@@ -76,23 +94,19 @@ def emit_update_inventory_on_created_or_deleted(sender, **kwargs):
|
||||
if getattr(_inventory_updates, 'is_updating', False):
|
||||
return
|
||||
instance = kwargs['instance']
|
||||
if ('created' in kwargs and kwargs['created']) or \
|
||||
kwargs['signal'] == post_delete:
|
||||
if ('created' in kwargs and kwargs['created']) or kwargs['signal'] == post_delete:
|
||||
pass
|
||||
else:
|
||||
return
|
||||
sender_name = str(sender._meta.verbose_name)
|
||||
logger.debug("%s created or deleted, updating inventory computed fields: %r %r",
|
||||
sender_name, sender, kwargs)
|
||||
logger.debug("%s created or deleted, updating inventory computed fields: %r %r", sender_name, sender, kwargs)
|
||||
try:
|
||||
inventory = instance.inventory
|
||||
except Inventory.DoesNotExist:
|
||||
pass
|
||||
else:
|
||||
if inventory is not None:
|
||||
connection.on_commit(
|
||||
lambda: update_inventory_computed_fields.delay(inventory.id)
|
||||
)
|
||||
connection.on_commit(lambda: update_inventory_computed_fields.delay(inventory.id))
|
||||
|
||||
|
||||
def rebuild_role_ancestor_list(reverse, model, instance, pk_set, action, **kwargs):
|
||||
@@ -177,17 +191,16 @@ def cleanup_detached_labels_on_deleted_parent(sender, instance, **kwargs):
|
||||
|
||||
|
||||
def save_related_job_templates(sender, instance, **kwargs):
|
||||
'''save_related_job_templates loops through all of the
|
||||
"""save_related_job_templates loops through all of the
|
||||
job templates that use an Inventory that have had their
|
||||
Organization updated. This triggers the rebuilding of the RBAC hierarchy
|
||||
and ensures the proper access restrictions.
|
||||
'''
|
||||
"""
|
||||
if sender is not Inventory:
|
||||
raise ValueError('This signal callback is only intended for use with Project or Inventory')
|
||||
|
||||
update_fields = kwargs.get('update_fields', None)
|
||||
if ((update_fields and not ('organization' in update_fields or 'organization_id' in update_fields)) or
|
||||
kwargs.get('created', False)):
|
||||
if (update_fields and not ('organization' in update_fields or 'organization_id' in update_fields)) or kwargs.get('created', False):
|
||||
return
|
||||
|
||||
if instance._prior_values_store.get('organization_id') != instance.organization_id:
|
||||
@@ -195,9 +208,11 @@ def save_related_job_templates(sender, instance, **kwargs):
|
||||
for jt in jtq:
|
||||
parents_added, parents_removed = update_role_parentage_for_instance(jt)
|
||||
if parents_added or parents_removed:
|
||||
logger.info('Permissions on JT {} changed due to inventory {} organization change from {} to {}.'.format(
|
||||
jt.pk, instance.pk, instance._prior_values_store.get('organization_id'), instance.organization_id
|
||||
))
|
||||
logger.info(
|
||||
'Permissions on JT {} changed due to inventory {} organization change from {} to {}.'.format(
|
||||
jt.pk, instance.pk, instance._prior_values_store.get('organization_id'), instance.organization_id
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
def connect_computed_field_signals():
|
||||
@@ -244,19 +259,17 @@ def migrate_children_from_deleted_group_to_parent_groups(sender, **kwargs):
|
||||
parents_pks = getattr(instance, '_saved_parents_pks', [])
|
||||
hosts_pks = getattr(instance, '_saved_hosts_pks', [])
|
||||
children_pks = getattr(instance, '_saved_children_pks', [])
|
||||
is_updating = getattr(_inventory_updates, 'is_updating', False)
|
||||
is_updating = getattr(_inventory_updates, 'is_updating', False)
|
||||
|
||||
with ignore_inventory_group_removal():
|
||||
with ignore_inventory_computed_fields():
|
||||
if parents_pks:
|
||||
for parent_group in Group.objects.filter(pk__in=parents_pks):
|
||||
for child_host in Host.objects.filter(pk__in=hosts_pks):
|
||||
logger.debug('adding host %s to parent %s after group deletion',
|
||||
child_host, parent_group)
|
||||
logger.debug('adding host %s to parent %s after group deletion', child_host, parent_group)
|
||||
parent_group.hosts.add(child_host)
|
||||
for child_group in Group.objects.filter(pk__in=children_pks):
|
||||
logger.debug('adding group %s to parent %s after group deletion',
|
||||
child_group, parent_group)
|
||||
logger.debug('adding group %s to parent %s after group deletion', child_group, parent_group)
|
||||
parent_group.children.add(child_group)
|
||||
inventory_pk = getattr(instance, '_saved_inventory_pk', None)
|
||||
if inventory_pk and not is_updating:
|
||||
@@ -296,7 +309,7 @@ def _update_host_last_jhs(host):
|
||||
@receiver(pre_delete, sender=Job)
|
||||
def save_host_pks_before_job_delete(sender, **kwargs):
|
||||
instance = kwargs['instance']
|
||||
hosts_qs = Host.objects.filter( last_job__pk=instance.pk)
|
||||
hosts_qs = Host.objects.filter(last_job__pk=instance.pk)
|
||||
instance._saved_hosts_pks = set(hosts_qs.values_list('pk', flat=True))
|
||||
|
||||
|
||||
@@ -324,9 +337,9 @@ activity_stream_enabled = ActivityStreamEnabled()
|
||||
|
||||
@contextlib.contextmanager
|
||||
def disable_activity_stream():
|
||||
'''
|
||||
"""
|
||||
Context manager to disable capturing activity stream changes.
|
||||
'''
|
||||
"""
|
||||
try:
|
||||
previous_value = activity_stream_enabled.enabled
|
||||
activity_stream_enabled.enabled = False
|
||||
@@ -355,6 +368,7 @@ def model_serializer_mapping():
|
||||
|
||||
from awx.conf.models import Setting
|
||||
from awx.conf.serializers import SettingSerializer
|
||||
|
||||
return {
|
||||
Setting: SettingSerializer,
|
||||
models.User: serializers.UserActivityStreamSerializer,
|
||||
@@ -393,14 +407,23 @@ def emit_activity_stream_change(instance):
|
||||
# could be really noisy
|
||||
return
|
||||
from awx.api.serializers import ActivityStreamSerializer
|
||||
|
||||
actor = None
|
||||
if instance.actor:
|
||||
actor = instance.actor.username
|
||||
summary_fields = ActivityStreamSerializer(instance).get_summary_fields(instance)
|
||||
analytics_logger.info('Activity Stream update entry for %s' % str(instance.object1),
|
||||
extra=dict(changes=instance.changes, relationship=instance.object_relationship_type,
|
||||
actor=actor, operation=instance.operation,
|
||||
object1=instance.object1, object2=instance.object2, summary_fields=summary_fields))
|
||||
analytics_logger.info(
|
||||
'Activity Stream update entry for %s' % str(instance.object1),
|
||||
extra=dict(
|
||||
changes=instance.changes,
|
||||
relationship=instance.object_relationship_type,
|
||||
actor=actor,
|
||||
operation=instance.operation,
|
||||
object1=instance.object1,
|
||||
object2=instance.object2,
|
||||
summary_fields=summary_fields,
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
def activity_stream_create(sender, instance, created, **kwargs):
|
||||
@@ -412,21 +435,14 @@ def activity_stream_create(sender, instance, created, **kwargs):
|
||||
changes = model_to_dict(instance, model_serializer_mapping())
|
||||
# Special case where Job survey password variables need to be hidden
|
||||
if type(instance) == Job:
|
||||
changes['credentials'] = [
|
||||
'{} ({})'.format(c.name, c.id)
|
||||
for c in instance.credentials.iterator()
|
||||
]
|
||||
changes['credentials'] = ['{} ({})'.format(c.name, c.id) for c in instance.credentials.iterator()]
|
||||
changes['labels'] = [label.name for label in instance.labels.iterator()]
|
||||
if 'extra_vars' in changes:
|
||||
changes['extra_vars'] = instance.display_extra_vars()
|
||||
if type(instance) == OAuth2AccessToken:
|
||||
changes['token'] = CENSOR_VALUE
|
||||
activity_entry = get_activity_stream_class()(
|
||||
operation='create',
|
||||
object1=object1,
|
||||
changes=json.dumps(changes),
|
||||
actor=get_current_user_or_none())
|
||||
#TODO: Weird situation where cascade SETNULL doesn't work
|
||||
activity_entry = get_activity_stream_class()(operation='create', object1=object1, changes=json.dumps(changes), actor=get_current_user_or_none())
|
||||
# TODO: Weird situation where cascade SETNULL doesn't work
|
||||
# it might actually be a good idea to remove all of these FK references since
|
||||
# we don't really use them anyway.
|
||||
if instance._meta.model_name != 'setting': # Is not conf.Setting instance
|
||||
@@ -435,9 +451,7 @@ def activity_stream_create(sender, instance, created, **kwargs):
|
||||
else:
|
||||
activity_entry.setting = conf_to_dict(instance)
|
||||
activity_entry.save()
|
||||
connection.on_commit(
|
||||
lambda: emit_activity_stream_change(activity_entry)
|
||||
)
|
||||
connection.on_commit(lambda: emit_activity_stream_change(activity_entry))
|
||||
|
||||
|
||||
def activity_stream_update(sender, instance, **kwargs):
|
||||
@@ -458,20 +472,14 @@ def activity_stream_update(sender, instance, **kwargs):
|
||||
if getattr(_type, '_deferred', False):
|
||||
return
|
||||
object1 = camelcase_to_underscore(instance.__class__.__name__)
|
||||
activity_entry = get_activity_stream_class()(
|
||||
operation='update',
|
||||
object1=object1,
|
||||
changes=json.dumps(changes),
|
||||
actor=get_current_user_or_none())
|
||||
activity_entry = get_activity_stream_class()(operation='update', object1=object1, changes=json.dumps(changes), actor=get_current_user_or_none())
|
||||
if instance._meta.model_name != 'setting': # Is not conf.Setting instance
|
||||
activity_entry.save()
|
||||
getattr(activity_entry, object1).add(instance.pk)
|
||||
else:
|
||||
activity_entry.setting = conf_to_dict(instance)
|
||||
activity_entry.save()
|
||||
connection.on_commit(
|
||||
lambda: emit_activity_stream_change(activity_entry)
|
||||
)
|
||||
connection.on_commit(lambda: emit_activity_stream_change(activity_entry))
|
||||
|
||||
|
||||
def activity_stream_delete(sender, instance, **kwargs):
|
||||
@@ -486,10 +494,7 @@ def activity_stream_delete(sender, instance, **kwargs):
|
||||
if not kwargs.get('inventory_delete_flag', False):
|
||||
return
|
||||
# Add additional data about child hosts / groups that will be deleted
|
||||
changes['coalesced_data'] = {
|
||||
'hosts_deleted': instance.hosts.count(),
|
||||
'groups_deleted': instance.groups.count()
|
||||
}
|
||||
changes['coalesced_data'] = {'hosts_deleted': instance.hosts.count(), 'groups_deleted': instance.groups.count()}
|
||||
elif isinstance(instance, (Host, Group)) and instance.inventory.pending_deletion:
|
||||
return # accounted for by inventory entry, above
|
||||
_type = type(instance)
|
||||
@@ -499,15 +504,9 @@ def activity_stream_delete(sender, instance, **kwargs):
|
||||
object1 = camelcase_to_underscore(instance.__class__.__name__)
|
||||
if type(instance) == OAuth2AccessToken:
|
||||
changes['token'] = CENSOR_VALUE
|
||||
activity_entry = get_activity_stream_class()(
|
||||
operation='delete',
|
||||
changes=json.dumps(changes),
|
||||
object1=object1,
|
||||
actor=get_current_user_or_none())
|
||||
activity_entry = get_activity_stream_class()(operation='delete', changes=json.dumps(changes), object1=object1, actor=get_current_user_or_none())
|
||||
activity_entry.save()
|
||||
connection.on_commit(
|
||||
lambda: emit_activity_stream_change(activity_entry)
|
||||
)
|
||||
connection.on_commit(lambda: emit_activity_stream_change(activity_entry))
|
||||
|
||||
|
||||
def activity_stream_associate(sender, instance, **kwargs):
|
||||
@@ -524,7 +523,7 @@ def activity_stream_associate(sender, instance, **kwargs):
|
||||
_type = type(instance)
|
||||
if getattr(_type, '_deferred', False):
|
||||
return
|
||||
object1=camelcase_to_underscore(obj1.__class__.__name__)
|
||||
object1 = camelcase_to_underscore(obj1.__class__.__name__)
|
||||
obj_rel = sender.__module__ + "." + sender.__name__
|
||||
|
||||
for entity_acted in kwargs['pk_set']:
|
||||
@@ -550,17 +549,13 @@ def activity_stream_associate(sender, instance, **kwargs):
|
||||
if isinstance(obj1, SystemJob) or isinstance(obj2_actual, SystemJob):
|
||||
continue
|
||||
activity_entry = get_activity_stream_class()(
|
||||
changes=json.dumps(dict(object1=object1,
|
||||
object1_pk=obj1.pk,
|
||||
object2=object2,
|
||||
object2_pk=obj2_id,
|
||||
action=action,
|
||||
relationship=obj_rel)),
|
||||
changes=json.dumps(dict(object1=object1, object1_pk=obj1.pk, object2=object2, object2_pk=obj2_id, action=action, relationship=obj_rel)),
|
||||
operation=action,
|
||||
object1=object1,
|
||||
object2=object2,
|
||||
object_relationship_type=obj_rel,
|
||||
actor=get_current_user_or_none())
|
||||
actor=get_current_user_or_none(),
|
||||
)
|
||||
activity_entry.save()
|
||||
getattr(activity_entry, object1).add(obj1.pk)
|
||||
getattr(activity_entry, object2).add(obj2_actual.pk)
|
||||
@@ -569,9 +564,7 @@ def activity_stream_associate(sender, instance, **kwargs):
|
||||
if 'role' in kwargs:
|
||||
role = kwargs['role']
|
||||
if role.content_object is not None:
|
||||
obj_rel = '.'.join([role.content_object.__module__,
|
||||
role.content_object.__class__.__name__,
|
||||
role.role_field])
|
||||
obj_rel = '.'.join([role.content_object.__module__, role.content_object.__class__.__name__, role.role_field])
|
||||
|
||||
# If the m2m is from the User side we need to
|
||||
# set the content_object of the Role for our entry.
|
||||
@@ -581,18 +574,16 @@ def activity_stream_associate(sender, instance, **kwargs):
|
||||
activity_entry.role.add(role)
|
||||
activity_entry.object_relationship_type = obj_rel
|
||||
activity_entry.save()
|
||||
connection.on_commit(
|
||||
lambda: emit_activity_stream_change(activity_entry)
|
||||
)
|
||||
connection.on_commit(lambda: emit_activity_stream_change(activity_entry))
|
||||
|
||||
|
||||
@receiver(current_user_getter)
|
||||
def get_current_user_from_drf_request(sender, **kwargs):
|
||||
'''
|
||||
"""
|
||||
Provider a signal handler to return the current user from the current
|
||||
request when using Django REST Framework. Requires that the APIView set
|
||||
drf_request on the underlying Django Request object.
|
||||
'''
|
||||
"""
|
||||
request = get_current_request()
|
||||
drf_request_user = getattr(request, 'drf_request_user', False)
|
||||
return (drf_request_user, 0)
|
||||
@@ -651,10 +642,7 @@ def save_user_session_membership(sender, **kwargs):
|
||||
Session.objects.filter(session_key__in=[membership.session_id]).delete()
|
||||
membership.delete()
|
||||
if len(expired):
|
||||
consumers.emit_channel_notification(
|
||||
'control-limit_reached_{}'.format(user_id),
|
||||
dict(group_name='control', reason='limit_reached')
|
||||
)
|
||||
consumers.emit_channel_notification('control-limit_reached_{}'.format(user_id), dict(group_name='control', reason='limit_reached'))
|
||||
|
||||
|
||||
@receiver(post_save, sender=OAuth2AccessToken)
|
||||
|
||||
Reference in New Issue
Block a user