import sys

from django.core.management.base import BaseCommand
from optparse import make_option


class Command(BaseCommand):
    option_list = BaseCommand.option_list + (
        make_option('--list', action='store_true', dest='show_list', default=False,
                    help='List model items waiting to be published (limited to 100)'),
    )

    args = '[modelname] [pk] [--list]'
    help = 'Publish a specific model or models within app'
    usage_str = 'Usage: ./manage.py publish_model app.models.Blog'

    def error(self, message, code=1):
        """
        Print error and stop command
        """
        print >>sys.stderr, message
        sys.exit(code)

    def handle(self, model_name=None, pk=None, show_list=None, *args, **options):
        if not model_name:
            self.error('You must provide an app to publish.\n' + self.usage_str)

        module = self.get_model(model_name)

        # TODO: Validate model is a publisher instance

        qs = module.objects.filter(publisher_is_draft=True).filter(publisher_linked_id=None)

        if pk:
            qs = qs.filter(pk=pk)

        if qs.count() < 1:
            self.error('No model(s) found to publish')

        for model in qs.all():
            model.publish()
            print 'Successfully published %s' % model

    def get_model(self, model_name):
        """
        TODO: Need to validate model name has 2x '.' chars
        """
        klass = None
        try:
            module_name, class_name = model_name.rsplit('.', 1)
            mod = __import__(module_name, fromlist=[class_name])
            klass = getattr(mod, class_name)
        except ImportError, e:
            self.error('Cannot find app %s %s' % (model_name, e))

        return klass

@@ -626,30 +626,17 @@
 rint
- %3E%3Esys.stderr, 
+(
 message
+)
 %0A   
@@ -1175,16 +1175,164 @@
 lish')%0A%0A
+        if show_list:%0A            for model in qs.all():%0A                print(%22%25s doesn't have published version yet%22 %25 model)%0A            return%0A%0A
         
@@ -1399,17 +1399,17 @@
    print
- 
+(
 'Success
@@ -1435,16 +1435,17 @@
  %25 model
+)
 %0A%0A    de
@@ -1792,17 +1792,19 @@
 ortError
-,
+ as
  e:%0A    

Update publish_model command to work with python 3