prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  importchannel command is broken
  ## Summary
  
  * `importchannel` command is broken
  
  ## System information
  
   - Version: 0.6
   - Operating system: Linux
  
  ```
  kolibri manage importchannel -- network bcd99d8aeef04ce6b9e25a88d87eedb7
  INFO     Downloading data for channel id bcd99d8aeef04ce6b9e25a88d87eedb7
    0%|                                             | 0/239616 [00:00<?, ?it/s]
  Traceback (most recent call last):
    File "/home/christian/.virtualenvs/kolibri/bin/kolibri", line 9, in <module>
      load_entry_point('kolibri', 'console_scripts', 'kolibri')()
    File "/home/christian/repos/kolibri/kolibri/utils/cli.py", line 580, in main
      manage(command, args=django_args)
    File "/home/christian/repos/kolibri/kolibri/utils/cli.py", line 411, in manage
      execute_from_command_line(argv=argv)
    File "/home/christian/.virtualenvs/kolibri/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 353, in execute_from_command_line
      utility.execute()
    File "/home/christian/.virtualenvs/kolibri/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 345, in execute
      self.fetch_command(subcommand).run_from_argv(self.argv)
    File "/home/christian/.virtualenvs/kolibri/local/lib/python2.7/site-packages/django/core/management/base.py", line 348, in run_from_argv
      self.execute(*args, **cmd_options)
    File "/home/christian/.virtualenvs/kolibri/local/lib/python2.7/site-packages/django/core/management/base.py", line 399, in execute
      output = self.handle(*args, **options)
    File "/home/christian/repos/kolibri/kolibri/tasks/management/commands/base.py", line 98, in handle
      return self.handle_async(*args, **options)
    File "/home/christian/repos/kolibri/kolibri/content/management/commands/importchannel.py", line 89, in handle_async
      self.download_channel(options["channel_id"])
    File "/home/christian/repos/kolibri/kolibri/content/management/commands/importchannel.py", line 42, in download_channel
      self._transfer(DOWNLOAD_METHOD, channel_id)
    File "/home/christian/repos/kolibri/kolibri/content/management/commands/importchannel.py", line 73, in _transfer
      if self.is_cancelled():
    File "/home/christian/repos/kolibri/kolibri/tasks/management/commands/base.py", line 108, in is_cancelled
      self.check_for_cancel(last_stage)
  TypeError: 'NoneType' object is not callable
  
  ```
  
  ## How to reproduce
  
  1. Use the `importchannel` command
  
  ## Real-life consequences
  
  * Breaks my workflow.
  
  --- END ISSUE ---
  
  Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
  --- BEGIN FILES ---
  Path: `kolibri/tasks/management/commands/base.py`
  Content:
  ```
  1 import abc
  2 from collections import namedtuple
  3 
  4 from barbequeue.exceptions import UserCancelledError
  5 from django.core.management.base import BaseCommand
  6 from tqdm import tqdm
  7 
  8 Progress = namedtuple(
  9     'Progress',
  10     [
  11         'progress_fraction',
  12         'message',
  13         'extra_data',
  14         'level',
  15     ]
  16 )
  17 
  18 
  19 class ProgressTracker():
  20 
  21     def __init__(self, total=100, level=0, update_callback=None):
  22 
  23         # set default values
  24         self.progress = 0
  25         self.message = ""
  26         self.extra_data = None
  27 
  28         # store provided arguments
  29         self.total = total
  30         self.level = level
  31         self.update_callback = update_callback
  32 
  33         # initialize the tqdm progress bar
  34         self.progressbar = tqdm(total=total)
  35 
  36     def update_progress(self, increment=1, message="", extra_data=None):
  37 
  38         self.progressbar.update(increment)
  39 
  40         self.progress += increment
  41 
  42         self.message = message
  43 
  44         self.extra_data = extra_data
  45 
  46         if callable(self.update_callback):
  47             p = self.get_progress()
  48             self.update_callback(p.progress_fraction, p)
  49 
  50     def get_progress(self):
  51 
  52         return Progress(
  53             progress_fraction=0 if self.total == 0 else self.progress / float(self.total),
  54             message=self.message,
  55             extra_data=self.extra_data,
  56             level=self.level,
  57         )
  58 
  59     def __enter__(self):
  60         return self.update_progress
  61 
  62     def __exit__(self, *exc_details):
  63         if self.progressbar is not None:
  64             self.progressbar.close()
  65 
  66 
  67 class AsyncCommand(BaseCommand):
  68     """A management command with added convenience functions for displaying
  69     progress to the user.
  70 
  71     Rather than implementing handle() (as is for BaseCommand), subclasses, must
  72     implement handle_async(), which accepts the same arguments as handle().
  73 
  74     If ran from the command line, AsynCommand displays a progress bar to the
  75     user. If ran asynchronously through kolibri.tasks.schedule_command(),
  76     AsyncCommand sends results through the Progress class to the main Django
  77     process. Anyone who knows the task id for the command instance can check
  78     the intermediate progress by looking at the task's AsyncResult.result
  79     variable.
  80 
  81     """
  82 
  83     def __init__(self, *args, **kwargs):
  84         self.progresstrackers = []
  85 
  86     def _update_all_progress(self, progress_fraction, progress):
  87         if callable(self.update_progress):
  88             progress_list = [p.get_progress() for p in self.progresstrackers]
  89             # HACK (aron): self.update_progress' signature has changed between django_q
  90             # and iceqube/bbq. It now expects the current progress,
  91             # the total progress, and then derives the
  92             # percentage progress manually.
  93             self.update_progress(progress_list[0].progress_fraction, 1.)
  94 
  95     def handle(self, *args, **options):
  96         self.update_progress = options.pop("update_progress", None)
  97         self.check_for_cancel = options.pop("check_for_cancel", None)
  98         return self.handle_async(*args, **options)
  99 
  100     def start_progress(self, total=100):
  101         level = len(self.progresstrackers)
  102         tracker = ProgressTracker(total=total, level=level, update_callback=self._update_all_progress)
  103         self.progresstrackers.append(tracker)
  104         return tracker
  105 
  106     def is_cancelled(self, last_stage="CANCELLING"):
  107         try:
  108             self.check_for_cancel(last_stage)
  109             return False
  110         except UserCancelledError:
  111             return True
  112 
  113     def cancel(self, last_stage="CANCELLED"):
  114         self.check_for_cancel(last_stage)
  115 
  116     @abc.abstractmethod
  117     def handle_async(self, *args, **options):
  118         """
  119         handle_async should be reimplemented by any Subclass of AsyncCommand.
  120         """
  121         pass
  122
  ```
  --- END FILES ---
  
  Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.
  
  Here is an example:
  
  ```diff
  diff --git a/examples/server_async.py b/examples/server_async.py
  --- a/examples/server_async.py
  +++ b/examples/server_async.py
  @@ -313,4 +313,4 @@
   
   
   if __name__ == "__main__":
  -    asyncio.run(run_async_server("."), debug=True)
  +    asyncio.run(run_async_server(), debug=True)
  diff --git a/examples/server_sync.py b/examples/server_sync.py
  --- a/examples/server_sync.py
  +++ b/examples/server_sync.py
  @@ -313,5 +313,5 @@
   
   
   if __name__ == "__main__":
  -    server = run_sync_server(".")
  +    server = run_sync_server()
       server.shutdown()
  
  ```
  
golden_diff: diff --git a/kolibri/tasks/management/commands/base.py b/kolibri/tasks/management/commands/base.py
  --- a/kolibri/tasks/management/commands/base.py
  +++ b/kolibri/tasks/management/commands/base.py
  @@ -104,14 +104,17 @@
           return tracker
   
       def is_cancelled(self, last_stage="CANCELLING"):
  -        try:
  -            self.check_for_cancel(last_stage)
  -            return False
  -        except UserCancelledError:
  -            return True
  +        if self.check_for_cancel:
  +            try:
  +                self.check_for_cancel(last_stage)
  +                return False
  +            except UserCancelledError:
  +                return True
  +        return False
   
       def cancel(self, last_stage="CANCELLED"):
  -        self.check_for_cancel(last_stage)
  +        if self.check_for_cancel:
  +            return self.check_for_cancel(last_stage)
   
       @abc.abstractmethod
       def handle_async(self, *args, **options):
  
verification_info: {"golden_diff": "diff --git a/kolibri/tasks/management/commands/base.py b/kolibri/tasks/management/commands/base.py\n--- a/kolibri/tasks/management/commands/base.py\n+++ b/kolibri/tasks/management/commands/base.py\n@@ -104,14 +104,17 @@\n         return tracker\n \n     def is_cancelled(self, last_stage=\"CANCELLING\"):\n-        try:\n-            self.check_for_cancel(last_stage)\n-            return False\n-        except UserCancelledError:\n-            return True\n+        if self.check_for_cancel:\n+            try:\n+                self.check_for_cancel(last_stage)\n+                return False\n+            except UserCancelledError:\n+                return True\n+        return False\n \n     def cancel(self, last_stage=\"CANCELLED\"):\n-        self.check_for_cancel(last_stage)\n+        if self.check_for_cancel:\n+            return self.check_for_cancel(last_stage)\n \n     @abc.abstractmethod\n     def handle_async(self, *args, **options):\n", "issue": "importchannel command is broken\n## Summary\r\n\r\n* `importchannel` command is broken\r\n\r\n## System information\r\n\r\n - Version: 0.6\r\n - Operating system: Linux\r\n\r\n```\r\nkolibri manage importchannel -- network bcd99d8aeef04ce6b9e25a88d87eedb7\r\nINFO     Downloading data for channel id bcd99d8aeef04ce6b9e25a88d87eedb7\r\n  0%|                                             | 0/239616 [00:00<?, ?it/s]\r\nTraceback (most recent call last):\r\n  File \"/home/christian/.virtualenvs/kolibri/bin/kolibri\", line 9, in <module>\r\n    load_entry_point('kolibri', 'console_scripts', 'kolibri')()\r\n  File \"/home/christian/repos/kolibri/kolibri/utils/cli.py\", line 580, in main\r\n    manage(command, args=django_args)\r\n  File \"/home/christian/repos/kolibri/kolibri/utils/cli.py\", line 411, in manage\r\n    execute_from_command_line(argv=argv)\r\n  File \"/home/christian/.virtualenvs/kolibri/local/lib/python2.7/site-packages/django/core/management/__init__.py\", line 353, in execute_from_command_line\r\n    utility.execute()\r\n  File \"/home/christian/.virtualenvs/kolibri/local/lib/python2.7/site-packages/django/core/management/__init__.py\", line 345, in execute\r\n    self.fetch_command(subcommand).run_from_argv(self.argv)\r\n  File \"/home/christian/.virtualenvs/kolibri/local/lib/python2.7/site-packages/django/core/management/base.py\", line 348, in run_from_argv\r\n    self.execute(*args, **cmd_options)\r\n  File \"/home/christian/.virtualenvs/kolibri/local/lib/python2.7/site-packages/django/core/management/base.py\", line 399, in execute\r\n    output = self.handle(*args, **options)\r\n  File \"/home/christian/repos/kolibri/kolibri/tasks/management/commands/base.py\", line 98, in handle\r\n    return self.handle_async(*args, **options)\r\n  File \"/home/christian/repos/kolibri/kolibri/content/management/commands/importchannel.py\", line 89, in handle_async\r\n    self.download_channel(options[\"channel_id\"])\r\n  File \"/home/christian/repos/kolibri/kolibri/content/management/commands/importchannel.py\", line 42, in download_channel\r\n    self._transfer(DOWNLOAD_METHOD, channel_id)\r\n  File \"/home/christian/repos/kolibri/kolibri/content/management/commands/importchannel.py\", line 73, in _transfer\r\n    if self.is_cancelled():\r\n  File \"/home/christian/repos/kolibri/kolibri/tasks/management/commands/base.py\", line 108, in is_cancelled\r\n    self.check_for_cancel(last_stage)\r\nTypeError: 'NoneType' object is not callable\r\n\r\n```\r\n\r\n## How to reproduce\r\n\r\n1. Use the `importchannel` command\r\n\r\n## Real-life consequences\r\n\r\n* Breaks my workflow.\n", "before_files": [{"content": "import abc\nfrom collections import namedtuple\n\nfrom barbequeue.exceptions import UserCancelledError\nfrom django.core.management.base import BaseCommand\nfrom tqdm import tqdm\n\nProgress = namedtuple(\n    'Progress',\n    [\n        'progress_fraction',\n        'message',\n        'extra_data',\n        'level',\n    ]\n)\n\n\nclass ProgressTracker():\n\n    def __init__(self, total=100, level=0, update_callback=None):\n\n        # set default values\n        self.progress = 0\n        self.message = \"\"\n        self.extra_data = None\n\n        # store provided arguments\n        self.total = total\n        self.level = level\n        self.update_callback = update_callback\n\n        # initialize the tqdm progress bar\n        self.progressbar = tqdm(total=total)\n\n    def update_progress(self, increment=1, message=\"\", extra_data=None):\n\n        self.progressbar.update(increment)\n\n        self.progress += increment\n\n        self.message = message\n\n        self.extra_data = extra_data\n\n        if callable(self.update_callback):\n            p = self.get_progress()\n            self.update_callback(p.progress_fraction, p)\n\n    def get_progress(self):\n\n        return Progress(\n            progress_fraction=0 if self.total == 0 else self.progress / float(self.total),\n            message=self.message,\n            extra_data=self.extra_data,\n            level=self.level,\n        )\n\n    def __enter__(self):\n        return self.update_progress\n\n    def __exit__(self, *exc_details):\n        if self.progressbar is not None:\n            self.progressbar.close()\n\n\nclass AsyncCommand(BaseCommand):\n    \"\"\"A management command with added convenience functions for displaying\n    progress to the user.\n\n    Rather than implementing handle() (as is for BaseCommand), subclasses, must\n    implement handle_async(), which accepts the same arguments as handle().\n\n    If ran from the command line, AsynCommand displays a progress bar to the\n    user. If ran asynchronously through kolibri.tasks.schedule_command(),\n    AsyncCommand sends results through the Progress class to the main Django\n    process. Anyone who knows the task id for the command instance can check\n    the intermediate progress by looking at the task's AsyncResult.result\n    variable.\n\n    \"\"\"\n\n    def __init__(self, *args, **kwargs):\n        self.progresstrackers = []\n\n    def _update_all_progress(self, progress_fraction, progress):\n        if callable(self.update_progress):\n            progress_list = [p.get_progress() for p in self.progresstrackers]\n            # HACK (aron): self.update_progress' signature has changed between django_q\n            # and iceqube/bbq. It now expects the current progress,\n            # the total progress, and then derives the\n            # percentage progress manually.\n            self.update_progress(progress_list[0].progress_fraction, 1.)\n\n    def handle(self, *args, **options):\n        self.update_progress = options.pop(\"update_progress\", None)\n        self.check_for_cancel = options.pop(\"check_for_cancel\", None)\n        return self.handle_async(*args, **options)\n\n    def start_progress(self, total=100):\n        level = len(self.progresstrackers)\n        tracker = ProgressTracker(total=total, level=level, update_callback=self._update_all_progress)\n        self.progresstrackers.append(tracker)\n        return tracker\n\n    def is_cancelled(self, last_stage=\"CANCELLING\"):\n        try:\n            self.check_for_cancel(last_stage)\n            return False\n        except UserCancelledError:\n            return True\n\n    def cancel(self, last_stage=\"CANCELLED\"):\n        self.check_for_cancel(last_stage)\n\n    @abc.abstractmethod\n    def handle_async(self, *args, **options):\n        \"\"\"\n        handle_async should be reimplemented by any Subclass of AsyncCommand.\n        \"\"\"\n        pass\n", "path": "kolibri/tasks/management/commands/base.py"}], "after_files": [{"content": "import abc\nfrom collections import namedtuple\n\nfrom barbequeue.exceptions import UserCancelledError\nfrom django.core.management.base import BaseCommand\nfrom tqdm import tqdm\n\nProgress = namedtuple(\n    'Progress',\n    [\n        'progress_fraction',\n        'message',\n        'extra_data',\n        'level',\n    ]\n)\n\n\nclass ProgressTracker():\n\n    def __init__(self, total=100, level=0, update_callback=None):\n\n        # set default values\n        self.progress = 0\n        self.message = \"\"\n        self.extra_data = None\n\n        # store provided arguments\n        self.total = total\n        self.level = level\n        self.update_callback = update_callback\n\n        # initialize the tqdm progress bar\n        self.progressbar = tqdm(total=total)\n\n    def update_progress(self, increment=1, message=\"\", extra_data=None):\n\n        self.progressbar.update(increment)\n\n        self.progress += increment\n\n        self.message = message\n\n        self.extra_data = extra_data\n\n        if callable(self.update_callback):\n            p = self.get_progress()\n            self.update_callback(p.progress_fraction, p)\n\n    def get_progress(self):\n\n        return Progress(\n            progress_fraction=0 if self.total == 0 else self.progress / float(self.total),\n            message=self.message,\n            extra_data=self.extra_data,\n            level=self.level,\n        )\n\n    def __enter__(self):\n        return self.update_progress\n\n    def __exit__(self, *exc_details):\n        if self.progressbar is not None:\n            self.progressbar.close()\n\n\nclass AsyncCommand(BaseCommand):\n    \"\"\"A management command with added convenience functions for displaying\n    progress to the user.\n\n    Rather than implementing handle() (as is for BaseCommand), subclasses, must\n    implement handle_async(), which accepts the same arguments as handle().\n\n    If ran from the command line, AsynCommand displays a progress bar to the\n    user. If ran asynchronously through kolibri.tasks.schedule_command(),\n    AsyncCommand sends results through the Progress class to the main Django\n    process. Anyone who knows the task id for the command instance can check\n    the intermediate progress by looking at the task's AsyncResult.result\n    variable.\n\n    \"\"\"\n\n    def __init__(self, *args, **kwargs):\n        self.progresstrackers = []\n\n    def _update_all_progress(self, progress_fraction, progress):\n        if callable(self.update_progress):\n            progress_list = [p.get_progress() for p in self.progresstrackers]\n            # HACK (aron): self.update_progress' signature has changed between django_q\n            # and iceqube/bbq. It now expects the current progress,\n            # the total progress, and then derives the\n            # percentage progress manually.\n            self.update_progress(progress_list[0].progress_fraction, 1.)\n\n    def handle(self, *args, **options):\n        self.update_progress = options.pop(\"update_progress\", None)\n        self.check_for_cancel = options.pop(\"check_for_cancel\", None)\n        return self.handle_async(*args, **options)\n\n    def start_progress(self, total=100):\n        level = len(self.progresstrackers)\n        tracker = ProgressTracker(total=total, level=level, update_callback=self._update_all_progress)\n        self.progresstrackers.append(tracker)\n        return tracker\n\n    def is_cancelled(self, last_stage=\"CANCELLING\"):\n        if self.check_for_cancel:\n            try:\n                self.check_for_cancel(last_stage)\n                return False\n            except UserCancelledError:\n                return True\n        return False\n\n    def cancel(self, last_stage=\"CANCELLED\"):\n        if self.check_for_cancel:\n            return self.check_for_cancel(last_stage)\n\n    @abc.abstractmethod\n    def handle_async(self, *args, **options):\n        \"\"\"\n        handle_async should be reimplemented by any Subclass of AsyncCommand.\n        \"\"\"\n        pass\n", "path": "kolibri/tasks/management/commands/base.py"}]}