# Path: oscar_mws/feeds/gateway.py
# def switch_product_fulfillment(marketplace, products, dry_run=False):
#     """
#     Switches the list of *products* sold on the given :class:`AmazonMarketplace
#     <oscar_mws.models.AmazonMarketplace` *marketplace* to
#     the fulfillment set on the :class:`AmazonProfile
#     <oscar_mws.models.AmazonProfile>`'s ``fulfillment_by`` attribute. If the
#     fulfillment is changed to "Fulfillment By Amazon (FBA)" all manually set
#     stock information is discarded and handled by Amazon automatically based on
#     inbound shipments (refer to the Amazon MWS documentation for more details).
# 
#     :param AmazonMarketplace marketplace: an Amazon marketplace instance
#     :param list products: a list of Oscar products
#     :param boolean dry_run: flag to enable dry run. If set to ``True`` the
#         generated XML is printed to stdout rather than submitted to MWS.
#         Default: ``False``
# 
#     :rtype FeedSubmission: a submission object representing the succesfully
#         submitted XML feed. Used to track the processing status of the feed.
# 
#     :raises MWSError: if an error occurs while communicating with MWS.
#     """
#     seller_id = marketplace.merchant.seller_id
#     writer = writers.InventoryFeedWriter(seller_id)
#     for product in products:
#         writer.add_product(
#             product,
#             fulfillment_by=product.amazon_profile.fulfillment_by,
#             fulfillment_center_id=marketplace.fulfillment_center_id,
#         )
# 
#     xml_data = writer.as_string(pretty_print=dry_run)
#     logger.debug(
#         "Submitting inventory feed with XML:\n{0}".format(xml_data)
#     )
#     if dry_run:
#         print xml_data
#         return
# 
#     feeds_api = get_merchant_connection(
#         marketplace.merchant.seller_id, 'feeds')
#     try:
#         response = feeds_api.submit_feed(
#             feed=xml_data, feed_type=am.TYPE_POST_INVENTORY_AVAILABILITY_DATA,
#             marketplaceids=[marketplace.marketplace_id])
#     except MWSError:
#         logger.error(
#             "failed submitting feed to switch fulfillment", exc_info=1,
#             extra={'seller_id': seller_id, 'feed_xml': xml_data,
#                    'marketplace_id': marketplace.marketplace_id,
#                    'product_ids': [p.id for p in products]})
#         raise
#     return handle_feed_submission_response(
#         marketplace.merchant, response.parsed, feed_xml=xml_data)


from optparse import make_option
from django.conf import settings
from django.db.models import get_model
from django.core.management.base import NoArgsCommand, CommandError
from oscar_mws.feeds.gateway import switch_product_fulfillment


class Command(NoArgsCommand):
    option_list = NoArgsCommand.option_list + (
        make_option(
            '--dry-run',
            action='store_true',
            dest='dry_run',
            default=False,
            help=('Do not submit the inventory feed put print the generated '
                  'XML to stdout.')
        ),
        make_option(
            '--fulfilled-by',
            dest='fulfilled_by',
            help=('Switch fulfillment for all products to FULFILLED_BY')
        ),
    )

    def handle_noargs(self, **options):
        fulfilled_by = options.get('fulfilled_by')
        if not fulfilled_by:
            raise CommandError(
                "Fulfillment type is required but was not specified, please "
                "specify a fulfillment type (MFN or AFN)"
            )

        if fulfilled_by != AmazonProfile.FULFILLMENT_BY_AMAZON \
           and fulfilled_by != AmazonProfile.FULFILLMENT_BY_MERCHANT:
            raise CommandError(

# Path: oscar_mws/feeds/gateway.py
# def switch_product_fulfillment(marketplace, products, dry_run=False):
#     """
#     Switches the list of *products* sold on the given :class:`AmazonMarketplace
#     <oscar_mws.models.AmazonMarketplace` *marketplace* to
#     the fulfillment set on the :class:`AmazonProfile
#     <oscar_mws.models.AmazonProfile>`'s ``fulfillment_by`` attribute. If the
#     fulfillment is changed to "Fulfillment By Amazon (FBA)" all manually set
#     stock information is discarded and handled by Amazon automatically based on
#     inbound shipments (refer to the Amazon MWS documentation for more details).
# 
#     :param AmazonMarketplace marketplace: an Amazon marketplace instance
#     :param list products: a list of Oscar products
#     :param boolean dry_run: flag to enable dry run. If set to ``True`` the
#         generated XML is printed to stdout rather than submitted to MWS.
#         Default: ``False``
# 
#     :rtype FeedSubmission: a submission object representing the succesfully
#         submitted XML feed. Used to track the processing status of the feed.
# 
#     :raises MWSError: if an error occurs while communicating with MWS.
#     """
#     seller_id = marketplace.merchant.seller_id
#     writer = writers.InventoryFeedWriter(seller_id)
#     for product in products:
#         writer.add_product(
#             product,
#             fulfillment_by=product.amazon_profile.fulfillment_by,
#             fulfillment_center_id=marketplace.fulfillment_center_id,
#         )
# 
#     xml_data = writer.as_string(pretty_print=dry_run)
#     logger.debug(
#         "Submitting inventory feed with XML:\n{0}".format(xml_data)
#     )
#     if dry_run:
#         print xml_data
#         return
# 
#     feeds_api = get_merchant_connection(
#         marketplace.merchant.seller_id, 'feeds')
#     try:
#         response = feeds_api.submit_feed(
#             feed=xml_data, feed_type=am.TYPE_POST_INVENTORY_AVAILABILITY_DATA,
#             marketplaceids=[marketplace.marketplace_id])
#     except MWSError:
#         logger.error(
#             "failed submitting feed to switch fulfillment", exc_info=1,
#             extra={'seller_id': seller_id, 'feed_xml': xml_data,
#                    'marketplace_id': marketplace.marketplace_id,
#                    'product_ids': [p.id for p in products]})
#         raise
#     return handle_feed_submission_response(
#         marketplace.merchant, response.parsed, feed_xml=xml_data)


# Path: oscar_mws/management/commands/mws_switch_product_fulfillment.py
from optparse import make_option
from django.conf import settings
from django.db.models import get_model
from django.core.management.base import NoArgsCommand, CommandError
from oscar_mws.feeds.gateway import switch_product_fulfillment



class Command(NoArgsCommand):
    option_list = NoArgsCommand.option_list + (
        make_option(
            '--dry-run',
            action='store_true',
            dest='dry_run',
            default=False,
            help=('Do not submit the inventory feed put print the generated '
                  'XML to stdout.')
        ),
        make_option(
            '--fulfilled-by',
            dest='fulfilled_by',
            help=('Switch fulfillment for all products to FULFILLED_BY')
        ),
    )

    def handle_noargs(self, **options):
        fulfilled_by = options.get('fulfilled_by')
        if not fulfilled_by:
            raise CommandError(
                "Fulfillment type is required but was not specified, please "
                "specify a fulfillment type (MFN or AFN)"
            )

        if fulfilled_by != AmazonProfile.FULFILLMENT_BY_AMAZON \
           and fulfilled_by != AmazonProfile.FULFILLMENT_BY_MERCHANT:
            raise CommandError(

                ("Invalid fulfillment type specified. Valid values are {0} "