import tempfile
import sys
from optparse import make_option

from django.core.management.base import BaseCommand

from ...lockfile import FileLock, FileLocked
from ...mail import send_queued
from ...logutils import setup_loghandlers


logger = setup_loghandlers()
default_lockfile = tempfile.gettempdir() + "/post_office"


class Command(BaseCommand):

    option_list = BaseCommand.option_list + (
        make_option('-p', '--processes', type='int',
                    help='Number of processes used to send emails', default=1),
        make_option('-L', '--lockfile', type='string', default=default_lockfile,
                    help='Absolute path of lockfile to acquire'),
        make_option('-l', '--log-level', type='int',
                    help='"0" to log nothing, "1" to only log errors'),
    )

    def handle(self, *args, **options):
        logger.info('Acquiring lock for sending queued emails at %s.lock' %
                    options['lockfile'])
        try:
            with FileLock(options['lockfile']):
                try:
                    send_queued(options['processes'], options.get('log_level'))
                except Exception as e:
                    logger.error(e, exc_info=sys.exc_info(), extra={'status_code': 500})
                    raise
        except FileLocked:
            logger.info('Failed to acquire lock, terminating now.')

import tempfile
import sys

from django.core.management.base import BaseCommand

from ...lockfile import FileLock, FileLocked
from ...mail import send_queued
from ...logutils import setup_loghandlers


logger = setup_loghandlers()
default_lockfile = tempfile.gettempdir() + "/post_office"


class Command(BaseCommand):
    def add_arguments(self, parser):
        parser.add_argument('-p', '--processes',
            type='int',
            help='Number of processes used to send emails',
            default=1
            )
        parser.add_argument('-L', '--lockfile',
            type='string',
            default=default_lockfile,
            help='Absolute path of lockfile to acquire'
            )
        parser.add_argument('-l', '--log-level',
            type='int',
            help='"0" to log nothing, "1" to only log errors'
            )

    def handle(self, *args, **options):
        logger.info('Acquiring lock for sending queued emails at %s.lock' %
                    options['lockfile'])
        try:
            with FileLock(options['lockfile']):
                try:
                    send_queued(options['processes'], options.get('log_level'))
                except Exception as e:
                    logger.error(e, exc_info=sys.exc_info(), extra={'status_code': 500})
                    raise
        except FileLocked:
            logger.info('Failed to acquire lock, terminating now.')

Switch to using the `add_arguments` method.
Switch to using the `add_arguments` method.

This is an alternative to using the `option_list` and
`optparse.make_option`. Django deprecated the use of `optparse` in
management commands in Django 1.8 and removed it in Django 1.10.

  import tempfile
  import sys
- from optparse import make_option
  
  from django.core.management.base import BaseCommand
  
  from ...lockfile import FileLock, FileLocked
  from ...mail import send_queued
  from ...logutils import setup_loghandlers
  
  
  logger = setup_loghandlers()
  default_lockfile = tempfile.gettempdir() + "/post_office"
  
  
  class Command(BaseCommand):
- 
-     option_list = BaseCommand.option_list + (
-         make_option('-p', '--processes', type='int',
+     def add_arguments(self, parser):
+         parser.add_argument('-p', '--processes',
+             type='int',
-                     help='Number of processes used to send emails', default=1),
+             help='Number of processes used to send emails',
-         make_option('-L', '--lockfile', type='string', default=default_lockfile,
+             default=1
+             )
+         parser.add_argument('-L', '--lockfile',
+             type='string',
+             default=default_lockfile,
-                     help='Absolute path of lockfile to acquire'),
+             help='Absolute path of lockfile to acquire'
-         make_option('-l', '--log-level', type='int',
+             )
+         parser.add_argument('-l', '--log-level',
+             type='int',
-                     help='"0" to log nothing, "1" to only log errors'),
+             help='"0" to log nothing, "1" to only log errors'
-     )
+             )
  
      def handle(self, *args, **options):
          logger.info('Acquiring lock for sending queued emails at %s.lock' %
                      options['lockfile'])
          try:
              with FileLock(options['lockfile']):
                  try:
                      send_queued(options['processes'], options.get('log_level'))
                  except Exception as e:
                      logger.error(e, exc_info=sys.exc_info(), extra={'status_code': 500})
                      raise
          except FileLocked:
              logger.info('Failed to acquire lock, terminating now.')
  
Switch to using the `add_arguments` method.
## Code Before:
import tempfile
import sys
from optparse import make_option

from django.core.management.base import BaseCommand

from ...lockfile import FileLock, FileLocked
from ...mail import send_queued
from ...logutils import setup_loghandlers


logger = setup_loghandlers()
default_lockfile = tempfile.gettempdir() + "/post_office"


class Command(BaseCommand):

    option_list = BaseCommand.option_list + (
        make_option('-p', '--processes', type='int',
                    help='Number of processes used to send emails', default=1),
        make_option('-L', '--lockfile', type='string', default=default_lockfile,
                    help='Absolute path of lockfile to acquire'),
        make_option('-l', '--log-level', type='int',
                    help='"0" to log nothing, "1" to only log errors'),
    )

    def handle(self, *args, **options):
        logger.info('Acquiring lock for sending queued emails at %s.lock' %
                    options['lockfile'])
        try:
            with FileLock(options['lockfile']):
                try:
                    send_queued(options['processes'], options.get('log_level'))
                except Exception as e:
                    logger.error(e, exc_info=sys.exc_info(), extra={'status_code': 500})
                    raise
        except FileLocked:
            logger.info('Failed to acquire lock, terminating now.')

## Instruction:
Switch to using the `add_arguments` method.
## Code After:
import tempfile
import sys

from django.core.management.base import BaseCommand

from ...lockfile import FileLock, FileLocked
from ...mail import send_queued
from ...logutils import setup_loghandlers


logger = setup_loghandlers()
default_lockfile = tempfile.gettempdir() + "/post_office"


class Command(BaseCommand):
    def add_arguments(self, parser):
        parser.add_argument('-p', '--processes',
            type='int',
            help='Number of processes used to send emails',
            default=1
            )
        parser.add_argument('-L', '--lockfile',
            type='string',
            default=default_lockfile,
            help='Absolute path of lockfile to acquire'
            )
        parser.add_argument('-l', '--log-level',
            type='int',
            help='"0" to log nothing, "1" to only log errors'
            )

    def handle(self, *args, **options):
        logger.info('Acquiring lock for sending queued emails at %s.lock' %
                    options['lockfile'])
        try:
            with FileLock(options['lockfile']):
                try:
                    send_queued(options['processes'], options.get('log_level'))
                except Exception as e:
                    logger.error(e, exc_info=sys.exc_info(), extra={'status_code': 500})
                    raise
        except FileLocked:
            logger.info('Failed to acquire lock, terminating now.')
