import os
import sys
from time import sleep
from optparse import make_option

from django.core.management.base import NoArgsCommand
from django.db import transaction

from denorm import denorms

PID_FILE = "/tmp/django-denorm-daemon-pid"

if 'set_autocommit' in dir(transaction):
    def commit_manually(fn):  # replacement of transaction.commit_manually decorator removed in Django 1.6
        def _commit_manually(*args, **kwargs):
            transaction.set_autocommit(False)
            res = fn(*args, **kwargs)
            transaction.commit()
            transaction.set_autocommit(True)
            return res
        return _commit_manually
else:  # Django <= 1.5
    commit_manually = transaction.commit_manually


class Command(NoArgsCommand):
    option_list = NoArgsCommand.option_list + (
        make_option(
            '-n',
            action='store_true',
            dest='foreground',
            default=False,
            help='Run in foreground',
        ),
        make_option(
            '-i',
            action='store',
            type='int',
            dest='interval',
            default=1,
            help='The interval - in seconds - between each update',
        ),
        make_option(
            '-f', '--pidfile',
            action='store',
            type='string',
            dest='pidfile',
            default=PID_FILE,
            help='The pid file to use. Defaults to "%s".' % PID_FILE)
    )
    help = "Runs a daemon that checks for dirty fields and updates them in regular intervals."

    def pid_exists(self, pidfile):
        try:
            pid = int(open(pidfile, 'r').read())
            os.kill(pid, 0)
            self.stderr.write(self.style.ERROR("daemon already running as pid: %s\n" % (pid,)))
            return True
        except OSError as err:
            return err.errno == os.errno.EPERM
        except IOError as err:
            if err.errno == 2:
                return False
            else:
                raise

    @commit_manually
    def handle_noargs(self, **options):
        foreground = options['foreground']
        interval = options['interval']
        pidfile = options['pidfile']

        if self.pid_exists(pidfile):
            return

        if not foreground:
            from denorm import daemon
            daemon.daemonize(noClose=True, pidfile=pidfile)

        while True:
            try:
                denorms.flush()
                sleep(interval)
                transaction.commit()
            except KeyboardInterrupt:
                transaction.commit()
                sys.exit()

@@ -67,24 +67,124 @@
 ake_option%0A%0A
+try:  # Django%3E=1.8%0A    from django.core.management.base import BaseCommand%0Aexcept ImportError:%0A    
 from django.
@@ -224,16 +224,31 @@
 sCommand
+ as BaseCommand
 %0Afrom dj
@@ -848,22 +848,20 @@
 Command(
-NoArgs
+Base
 Command)
@@ -880,22 +880,20 @@
 _list = 
-NoArgs
+Base
 Command.
@@ -2140,15 +2140,8 @@
 ndle
-_noargs
 (sel
@@ -2663,32 +2663,32 @@
 action.commit()%0A
-
                 
@@ -2694,12 +2694,104 @@
  sys.exit()%0A
+%0A    def handle_noargs(self, **options):  # Django%3C=1.8%0A        return self.handle(options)%0A

Fix for Django 1.10: importing NoArgsCommand