def update_es_for_promoted(sender, instance, **kw): from olympia.addons.models import update_search_index from olympia.amo.tasks import trigger_sync_objects_to_basket # Update ES because Addon.promoted depends on it. update_search_index(sender=sender, instance=instance.addon, **kw) # Sync the related add-on to basket when promoted groups is changed trigger_sync_objects_to_basket('addon', [instance.addon.pk], 'promoted change')
def watch_new_unlisted_version(sender=None, instance=None, **kwargs): # Sync the related add-on to basket when an unlisted version is uploaded. # Unlisted version deletion is handled by watch_changes() above, and new # version approval changes are handled by watch_changes() # in olympia.addon.models (since _current_version will change). # What's left here is unlisted version upload. if instance and instance.channel == amo.RELEASE_CHANNEL_UNLISTED: from olympia.amo.tasks import trigger_sync_objects_to_basket trigger_sync_objects_to_basket('addon', [instance.addon.pk], 'new unlisted version')
def watch_changes(old_attr=None, new_attr=None, instance=None, sender=None, **kw): if old_attr is None: old_attr = {} if new_attr is None: new_attr = {} changes = { x for x in new_attr if not x.startswith('_') and new_attr[x] != old_attr.get(x) } # Log email changes. if ('email' in changes and new_attr['email'] is not None and old_attr.get('email') is not None): log.info('Creating user history for user: %s', instance.pk) UserHistory.objects.create(email=old_attr.get('email'), user_id=instance.pk) # If username or display_name changes, reindex the user add-ons, if there # are any. if 'username' in changes or 'display_name' in changes: from olympia.addons.tasks import index_addons ids = [addon.pk for addon in instance.get_addons_listed()] if ids: index_addons.delay(ids) basket_relevant_changes = ( 'deleted', 'display_name', 'fxa_id', 'homepage', 'last_login', 'location', ) if any(field in changes for field in basket_relevant_changes): from olympia.amo.tasks import trigger_sync_objects_to_basket trigger_sync_objects_to_basket('userprofile', [instance.pk], 'attribute change')
def watch_changes(old_attr=None, new_attr=None, instance=None, sender=None, **kwargs): if old_attr is None: old_attr = {} if new_attr is None: new_attr = {} changes = { x for x in new_attr if not x.startswith('_') and new_attr[x] != old_attr.get(x) } if instance.channel == amo.RELEASE_CHANNEL_UNLISTED and 'deleted' in changes: # Sync the related add-on to basket when an unlisted version is # deleted. (When a listed version is deleted, watch_changes() in # olympia.addon.models should take care of it (since _current_version # will change). from olympia.amo.tasks import trigger_sync_objects_to_basket trigger_sync_objects_to_basket( 'addon', [instance.addon.pk], 'unlisted version deleted' )
def ban_and_disable_related_content_bulk(cls, users, move_files=False): """Admin method to ban users and disable the content they produced. Similar to deletion, except that the content produced by the user is forcibly disabled instead of being deleted where possible, and the user is not fully anonymized: we keep their fxa_id and email so that they are never able to log back in. """ from olympia.addons.models import Addon, AddonUser from olympia.addons.tasks import index_addons from olympia.bandwagon.models import Collection from olympia.files.models import File from olympia.ratings.models import Rating # collect affected addons addon_ids = set( Addon.unfiltered.exclude(status=amo.STATUS_DELETED).filter( addonuser__user__in=users).values_list('id', flat=True)) # First addons who have other authors we aren't banning addon_joint_ids = set( AddonUser.objects.filter(addon_id__in=addon_ids).exclude( user__in=users).values_list('addon_id', flat=True)) AddonUser.objects.filter(user__in=users, addon_id__in=addon_joint_ids).delete() # Then deal with users who are the sole author addons_sole = Addon.unfiltered.filter(id__in=addon_ids - addon_joint_ids) # set the status to disabled - using the manager update() method addons_sole.update(status=amo.STATUS_DISABLED) # collect Files that need to be disabled now the addons are disabled files_to_disable = File.objects.filter(version__addon__in=addons_sole) files_to_disable.update(status=amo.STATUS_DISABLED) if move_files: # if necessary move the files out of the CDN (expensive operation) for file_ in files_to_disable: file_.hide_disabled_file() # Finally run Addon.force_disable to add the logging; update versions # Status was already DISABLED so shouldn't fire watch_disabled again. addons_sole_ids = [] for addon in addons_sole: addons_sole_ids.append(addon.pk) addon.force_disable() index_addons.delay(addons_sole_ids) # delete the other content associated with the user Collection.objects.filter(author__in=users).delete() Rating.objects.filter(user__in=users).delete( user_responsible=core.get_user()) # And then delete the users. ids = [] for user in users: log.info( f'User ({user}: <{user.email}>) is being anonymized and banned.' ) user.banned = user.modified = datetime.now() user.deleted = True ids.append(user.pk) cls.anonymize_users(users) cls.objects.bulk_update(users, fields=('banned', 'deleted', 'modified') + cls.ANONYMIZED_FIELDS) from olympia.amo.tasks import trigger_sync_objects_to_basket trigger_sync_objects_to_basket('userprofile', ids, 'user ban') trigger_sync_objects_to_basket('addon', addons_sole_ids, 'user ban content')
def test_trigger_sync_to_basket_wrapper_switch_is_not_active( self, sync_objects_to_basket_mock ): trigger_sync_objects_to_basket('blah', [4815162342], 'reason') assert sync_objects_to_basket_mock.delay.call_count == 0
def test_trigger_sync_to_basket_wrapper(self, sync_objects_to_basket_mock): trigger_sync_objects_to_basket('blah', [4815162342], 'reason') assert sync_objects_to_basket_mock.delay.call_count == 1 sync_objects_to_basket_mock.delay.assert_called_with('blah', [4815162342])