from django.contrib.auth import get_user_model
from django.core.management.base import BaseCommand
from rest_framework.authtoken.models import Token


UserModel = get_user_model()


class Command(BaseCommand):
    help = 'Create DRF Token for a given user'

    def create_user_token(self, username):
        user = UserModel._default_manager.get_by_natural_key(username)
        token = Token.objects.get_or_create(user=user)
        return token[0]

    def add_arguments(self, parser):
        parser.add_argument('username', type=str, nargs='+')

    def handle(self, *args, **options):
        username = options['username']

        try:
            token = self.create_user_token(username)
        except UserModel.DoesNotExist:
            print('Cannot create the Token: user {0} does not exist'.format(
                username
            ))
        print('Generated token {0} for user {1}'.format(token.key, username))

from django.contrib.auth import get_user_model
from django.core.management.base import BaseCommand, CommandError
from rest_framework.authtoken.models import Token


UserModel = get_user_model()


class Command(BaseCommand):
    help = 'Create DRF Token for a given user'

    def create_user_token(self, username):
        user = UserModel._default_manager.get_by_natural_key(username)
        token = Token.objects.get_or_create(user=user)
        return token[0]

    def add_arguments(self, parser):
        parser.add_argument('username', type=str, nargs='+')

    def handle(self, *args, **options):
        username = options['username']

        try:
            token = self.create_user_token(username)
        except UserModel.DoesNotExist:
            raise CommandError(
                'Cannot create the Token: user {0} does not exist'.format(
                    username)
            )
        self.stdout.write(
            'Generated token {0} for user {1}'.format(token.key, username))

Use self.sdtout and CommandError to print output
Use self.sdtout and CommandError to print output

  from django.contrib.auth import get_user_model
- from django.core.management.base import BaseCommand
+ from django.core.management.base import BaseCommand, CommandError
  from rest_framework.authtoken.models import Token
  
  
  UserModel = get_user_model()
  
  
  class Command(BaseCommand):
      help = 'Create DRF Token for a given user'
  
      def create_user_token(self, username):
          user = UserModel._default_manager.get_by_natural_key(username)
          token = Token.objects.get_or_create(user=user)
          return token[0]
  
      def add_arguments(self, parser):
          parser.add_argument('username', type=str, nargs='+')
  
      def handle(self, *args, **options):
          username = options['username']
  
          try:
              token = self.create_user_token(username)
          except UserModel.DoesNotExist:
+             raise CommandError(
-             print('Cannot create the Token: user {0} does not exist'.format(
+                 'Cannot create the Token: user {0} does not exist'.format(
-                 username
+                     username)
-             ))
+             )
+         self.stdout.write(
-         print('Generated token {0} for user {1}'.format(token.key, username))
+             'Generated token {0} for user {1}'.format(token.key, username))
  
Use self.sdtout and CommandError to print output
## Code Before:
from django.contrib.auth import get_user_model
from django.core.management.base import BaseCommand
from rest_framework.authtoken.models import Token


UserModel = get_user_model()


class Command(BaseCommand):
    help = 'Create DRF Token for a given user'

    def create_user_token(self, username):
        user = UserModel._default_manager.get_by_natural_key(username)
        token = Token.objects.get_or_create(user=user)
        return token[0]

    def add_arguments(self, parser):
        parser.add_argument('username', type=str, nargs='+')

    def handle(self, *args, **options):
        username = options['username']

        try:
            token = self.create_user_token(username)
        except UserModel.DoesNotExist:
            print('Cannot create the Token: user {0} does not exist'.format(
                username
            ))
        print('Generated token {0} for user {1}'.format(token.key, username))

## Instruction:
Use self.sdtout and CommandError to print output
## Code After:
from django.contrib.auth import get_user_model
from django.core.management.base import BaseCommand, CommandError
from rest_framework.authtoken.models import Token


UserModel = get_user_model()


class Command(BaseCommand):
    help = 'Create DRF Token for a given user'

    def create_user_token(self, username):
        user = UserModel._default_manager.get_by_natural_key(username)
        token = Token.objects.get_or_create(user=user)
        return token[0]

    def add_arguments(self, parser):
        parser.add_argument('username', type=str, nargs='+')

    def handle(self, *args, **options):
        username = options['username']

        try:
            token = self.create_user_token(username)
        except UserModel.DoesNotExist:
            raise CommandError(
                'Cannot create the Token: user {0} does not exist'.format(
                    username)
            )
        self.stdout.write(
            'Generated token {0} for user {1}'.format(token.key, username))
