Example #1
0
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 'recommendation_approved' in changes:
        from olympia.addons.models import update_search_index
        # Update ES because Addon.is_recommended depends on it.
        update_search_index(
            sender=sender, instance=instance.addon, **kwargs)

    if (instance.channel == amo.RELEASE_CHANNEL_UNLISTED and
            'deleted' in changes) or 'recommendation_approved' in changes:
        # Sync the related add-on to basket when recommendation_approved is
        # changed or 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 sync_object_to_basket
        sync_object_to_basket.delay('addon', instance.addon.pk)
Example #2
0
def watch_recommendable_changes(old_attr=None, new_attr=None, instance=None,
                                sender=None, **kwargs):
    if 'recommendable' in old_attr or 'recommendable' in new_attr:
        old_value = old_attr.get('recommendable')
        new_value = new_attr.get('recommendable')
        if old_value != new_value:
            # Update ES because is_recommended depends on it.
            update_search_index(
                sender=sender, instance=instance.addon, **kwargs)
Example #3
0
def update_es_for_promoted(sender, instance, **kw):
    from olympia.addons.models import update_search_index
    from olympia.amo.tasks import sync_object_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
    sync_object_to_basket.delay('addon', instance.addon.pk)
Example #4
0
    def refresh(self, update_denorm=False):
        from olympia.addons.models import update_search_index
        from . import tasks

        if update_denorm:
            # Do this immediately so is_latest is correct.
            self.update_denormalized_fields()

        # Rating counts have changed, so run the task and trigger a reindex.
        tasks.addon_rating_aggregates.delay(self.addon_id)
        update_search_index(self.addon.__class__, self.addon)
Example #5
0
    def refresh(self, update_denorm=False):
        from olympia.addons.models import update_search_index
        from . import tasks

        if update_denorm:
            # Do this immediately so is_latest is correct.
            self.update_denormalized_fields()

        # Rating counts have changed, so run the task and trigger a reindex.
        tasks.addon_rating_aggregates.delay(self.addon_id)
        update_search_index(self.addon.__class__, self.addon)
Example #6
0
def watch_recommendation_changes(old_attr=None, new_attr=None, instance=None,
                                 sender=None, **kwargs):
    from olympia.addons.models import update_search_index

    if ('recommendation_approved' in old_attr or
            'recommendation_approved' in new_attr):
        old_value = old_attr.get('recommendation_approved')
        new_value = new_attr.get('recommendation_approved')
        if old_value != new_value:
            # Update ES because Addon.is_recommended depends on it.
            update_search_index(
                sender=sender, instance=instance.addon, **kwargs)
Example #7
0
    def refresh(self, update_denorm=False):
        from olympia.addons.models import update_search_index
        from . import tasks

        if update_denorm:
            pair = self.addon_id, self.user_id
            # Do this immediately so is_latest is correct. Use default
            # to avoid slave lag.
            tasks.update_denorm(pair, using='default')

        # Review counts have changed, so run the task and trigger a reindex.
        tasks.addon_review_aggregates.delay(self.addon_id, using='default')
        update_search_index(self.addon.__class__, self.addon)
Example #8
0
    def post_save(sender, instance, created, **kwargs):
        from olympia.addons.models import update_search_index
        from . import tasks

        if kwargs.get('raw'):
            return

        if getattr(instance, 'user_responsible', None):
            # user_responsible is not a field on the model, so it's not
            # persistent: it's just something the views will set temporarily
            # when manipulating a Rating that indicates a real user made that
            # change.
            action = 'New' if created else 'Edited'
            if instance.reply_to:
                log.info('%s reply to %s: %s' %
                         (action, instance.reply_to_id, instance.pk))
            else:
                log.info('%s rating: %s' % (action, instance.pk))

            # For new ratings - not replies - and all edits (including replies
            # this time) by users we want to insert a new ActivityLog.
            new_rating_or_edit = not instance.reply_to or not created
            if new_rating_or_edit:
                action = amo.LOG.ADD_RATING if created else amo.LOG.EDIT_RATING
                activity.log_create(action,
                                    instance.addon,
                                    instance,
                                    user=instance.user_responsible)

            # For new ratings and new replies we want to send an email.
            if created:
                instance.send_notification_email()

        if created:
            # Do this immediately synchronously so is_latest is correct before
            # we fire the aggregates task.
            instance.update_denormalized_fields()

        # Rating counts have changed, so run the task and trigger a reindex.
        tasks.addon_rating_aggregates.delay(instance.addon_id)
        update_search_index(instance.addon.__class__, instance.addon)