Esempio n. 1
0
    def handle(self, *args, **options):
        partner_code = options['partner']

        catalogs = Catalog.objects.filter(
            partner__short_code__iexact=partner_code)
        ranges = Range.objects.filter(catalog__in=catalogs).distinct()
        benefits = Benefit.objects.filter(range__in=ranges).distinct()
        conditional_ids = ConditionalOffer.objects.filter(
            benefit__in=benefits).distinct().values_list('id', flat=True)

        # distinct query with delete is not working in django32.
        conditional_offers = ConditionalOffer.objects.filter(
            id__in=conditional_ids)

        count = len(conditional_offers)
        if count == 0:
            logger.info('No offer found for partner [%s].', partner_code)
            return

        line_feed = '\n'
        offer_names = 'Conditional offers to be deleted for partner [{partner_code}] {line_feed}'.format(
            partner_code=partner_code, line_feed=line_feed)
        for i in range(count):
            if i == count - 1:
                line_feed = ''
            offer_names = '{names} {index}. {name} {line_feed}'.format(
                names=offer_names,
                index=i + 1,
                name=conditional_offers[i].name,
                line_feed=line_feed)

        # List down all conditional offers to be deleted.
        logger.warning(offer_names)

        pluralized = pluralize(count)
        if query_yes_no(self.CONFIRMATION_PROMPT.format(count=count,
                                                        pluralized=pluralized),
                        default="no"):
            # disconnect post_delete oscar receiver to avoid Condition matching query does not exist.
            signals.post_delete.disconnect(
                receiver=delete_unused_related_conditions_and_benefits,
                sender=ConditionalOffer)

            # delete partner related conditional offers.
            conditional_offers.delete()

            # re-connect post_delete oscar receiver.
            signals.post_delete.connect(
                receiver=delete_unused_related_conditions_and_benefits,
                sender=ConditionalOffer)
            logger.info('%d conditional offer%s removed successfully.', count,
                        pluralized)
        else:
            logger.info('Operation canceled.')
            return
    def handle(self, *args, **options):
        limit = options['limit']
        offset = options['offset']
        priority = options['priority']

        try:
            conditional_offers = ConditionalOffer.objects.filter(
                offer_type=ConditionalOffer.VOUCHER,
                priority=priority)[offset:offset + limit]

            count = len(conditional_offers)
            if count == 0:
                logger.info('No offer found which needs a priority fix')
                return

            line_feed = '\n'
            offer_names = 'Conditional offers to be updated{line_feed}'.format(
                line_feed=line_feed)
            for i in range(count):
                if i == count - 1:
                    line_feed = ''
                offer_names = '{names}{index}. {name}{line_feed}'.format(
                    names=offer_names,
                    index=i + 1,
                    name=conditional_offers[i].name,
                    line_feed=line_feed)

            # List down all conditional which needs to be updated.
            logger.warning(offer_names)

            pluralized = pluralize(count)
            if query_yes_no(self.CONFIRMATION_PROMPT.format(
                    count=count, pluralized=pluralized),
                            default="no"):
                for offer in conditional_offers:
                    offer.priority = OFFER_PRIORITY_VOUCHER
                    offer.save()
                logger.info(
                    'Operation completed. %d conditional offer%s updated successfully.',
                    count, pluralized)
            else:
                logger.info('Operation canceled.')

        except Exception as exc:  # pylint: disable=broad-except
            logger.exception(
                'Command execution failed while executing batch %d,%d\n%s',
                offset, limit, exc)