from django.core.management.base import BaseCommand, CommandError
from procat2.models import Catalog


class Command(BaseCommand):
    help = 'Return material numbers for a catalog'

    def add_arguments(self, parser):
        parser.add_argument('cat_ids', nargs='+', type=int)

    def handle(self, *args, **options):
        for cat_id in options['cat_ids']:
            try:
                cat = Catalog.objects.get(pk=cat_id)
            except Catalog.DoesNotExist:
                raise CommandError('Catalog "%s" does not exist' % cat_id)

            for id in cat.product_ids():
                self.stdout.write(id)
