# Buggy code
def handle(self, *args, **options):
    record = options["record"]
    force = options["force"]
    version = options["version"]

    if options.get("slugs", []):
        for slug in options["slugs"]:
            if version and version != "all":
                log.info("Updating version %s for %s", version, slug)
                for version in Version.objects.filter(
                    project__slug=slug,
                    slug=version,
                ):
                    trigger_build(project=version.project, version=version)
            elif version == "all":
                log.info("Updating all versions for %s", slug)
                for version in Version.objects.filter(
                    project__slug=slug,
                    active=True,
                    uploaded=False,
                ):
                    build_pk = None
                    if record:
                        build = Build.objects.create(
                            project=version.project,
                            version=version,
                            type="html",
                            state="triggered",
                        )
                        build_pk = build.pk

                    tasks.UpdateDocsTask().run(
                        pk=version.project_id,
                        build_pk=build_pk,
                        record=record,
                        version_pk=version.pk,
                    )
            else:
                p = Project.all_objects.get(slug=slug)
                log.info("Building %s", p)
                trigger_build(project=p, force=force, record=record)
    else:
        if version == "all":
            log.info("Updating all versions")
            for version in Version.objects.filter(
                active=True,
                uploaded=False,
            ):
                tasks.UpdateDocsTask().run(
                    pk=version.project_id,
                    record=record,
                    force=force,
                    version_pk=version.pk,
                )
        else:
            log.info("Updating all docs")
            for project in Project.objects.all():
                tasks.UpdateDocsTask().run(
                    pk=project.pk,
                    record=record,
                    force=force,
                )


# Traceback (HttpClientError)
$ python manage.py update_repos pip
[28/Feb/2018 04:43:25] root:124[23847]: INFO Generating grammar tables from /usr/lib/python2.7/lib2to3/Grammar.txt
[28/Feb/2018 04:43:25] root:124[23847]: INFO Generating grammar tables from /usr/lib/python2.7/lib2to3/PatternGrammar.txt
System check identified some issues:

WARNINGS:
?: (1_8.W001) The standalone TEMPLATE_* settings were deprecated in Django 1.8 and the TEMPLATES dictionary takes precedence. You must put the values of the following settings into your default TEMPLATES dict: TEMPLATE_DEBUG.
?: (guardian.W001) Guardian authentication backend is not hooked. You can add this in settings as eg: `AUTHENTICATION_BACKENDS = ('django.contrib.auth.backends.ModelBackend', 'guardian.backends.ObjectPermissionBackend')`.
[28/Feb/2018 04:43:27] readthedocs.core.management.commands.update_repos:80[23847]: INFO Building Pip
[28/Feb/2018 04:43:27] requests.packages.urllib3.connectionpool:207[23847]: INFO Starting new HTTP connection (1): localhost
[28/Feb/2018 04:43:27] requests.packages.urllib3.connectionpool:242[23847]: INFO Resetting dropped connection: localhost
[28/Feb/2018 04:43:27] readthedocs.projects.tasks:233[23847]: INFO (Build) [pip:latest] Updating docs from VCS
[28/Feb/2018 04:43:27] readthedocs.projects.tasks:233[23847]: INFO (Build) [pip:latest] Checking out version latest: master
[28/Feb/2018 04:43:27] readthedocs.doc_builder.environments:117[23847]: INFO Running: 'git status' [/home/rm/src/readthedocs/rtd/checkouts/readthedocs.org/user_builds/pip/checkouts/latest]
[28/Feb/2018 04:43:27] readthedocs.doc_builder.environments:117[23847]: INFO Running: 'git remote set-url origin https://github.com/pypa/pip' [/home/rm/src/readthedocs/rtd/checkouts/readthedocs.org/user_builds/pip/checkouts/latest]
[28/Feb/2018 04:43:27] readthedocs.doc_builder.environments:117[23847]: INFO Running: 'git fetch --tags --prune' [/home/rm/src/readthedocs/rtd/checkouts/readthedocs.org/user_builds/pip/checkouts/latest]
[28/Feb/2018 04:43:29] readthedocs.doc_builder.environments:117[23847]: INFO Running: 'git show-ref remotes/origin/master' [/home/rm/src/readthedocs/rtd/checkouts/readthedocs.org/user_builds/pip/checkouts/latest]
[28/Feb/2018 04:43:29] readthedocs.doc_builder.environments:193[23847]: WARNING Recording command exit_code as success
[28/Feb/2018 04:43:29] requests.packages.urllib3.connectionpool:242[23847]: INFO Resetting dropped connection: localhost
[28/Feb/2018 04:43:29] readthedocs.vcs_support.utils:101[23847]: INFO Lock (pip): Releasing
[28/Feb/2018 04:43:29] readthedocs.doc_builder.environments:450[23847]: ERROR (Build) [pip:latest] Client Error 400: http://localhost:8000/api/v2/command/
Traceback (most recent call last):
File "/home/rm/src/readthedocs/rtd/checkouts/readthedocs.org/readthedocs/projects/tasks.py", line 333, in run_setup
self.setup_vcs()
File "/home/rm/src/readthedocs/rtd/checkouts/readthedocs.org/readthedocs/projects/tasks.py", line 452, in setup_vcs
self.sync_repo()
File "/home/rm/src/readthedocs/rtd/checkouts/readthedocs.org/readthedocs/projects/tasks.py", line 124, in sync_repo
version_repo.checkout(self.version.identifier)
File "/home/rm/src/readthedocs/rtd/checkouts/readthedocs.org/readthedocs/vcs_support/backends/git.py", line 183, in checkout
identifier = self.find_ref(identifier)
File "/home/rm/src/readthedocs/rtd/checkouts/readthedocs.org/readthedocs/vcs_support/backends/git.py", line 207, in find_ref
if self.ref_exists('remotes/origin/' + ref):
File "/home/rm/src/readthedocs/rtd/checkouts/readthedocs.org/readthedocs/vcs_support/backends/git.py", line 213, in ref_exists
code, _, _ = self.run('git', 'show-ref', ref, record_as_success=True)
File "/home/rm/src/readthedocs/rtd/checkouts/readthedocs.org/readthedocs/vcs_support/base.py", line 97, in run
build_cmd = self.environment.run(*cmd, **kwargs)
File "/home/rm/src/readthedocs/rtd/checkouts/readthedocs.org/readthedocs/doc_builder/environments.py", line 470, in run
return super(BuildEnvironment, self).run(*cmd, **kwargs)
File "/home/rm/src/readthedocs/rtd/checkouts/readthedocs.org/readthedocs/doc_builder/environments.py", line 310, in run
return self.run_command_class(cls=self.command_class, cmd=cmd, **kwargs)
File "/home/rm/src/readthedocs/rtd/checkouts/readthedocs.org/readthedocs/doc_builder/environments.py", line 476, in run_command_class
return super(BuildEnvironment, self).run_command_class(*cmd, **kwargs)
File "/home/rm/src/readthedocs/rtd/checkouts/readthedocs.org/readthedocs/doc_builder/environments.py", line 356, in run_command_class
self.record_command(build_cmd)
File "/home/rm/src/readthedocs/rtd/checkouts/readthedocs.org/readthedocs/doc_builder/environments.py", line 456, in record_command
command.save()
File "/home/rm/src/readthedocs/rtd/checkouts/readthedocs.org/readthedocs/doc_builder/environments.py", line 205, in save
api_v2.command.post(data)
File "/home/rm/src/readthedocs/rtd/local/lib/python2.7/site-packages/slumber/__init__.py", line 167, in post
resp = self._request("POST", data=data, files=files, params=kwargs)
File "/home/rm/src/readthedocs/rtd/local/lib/python2.7/site-packages/slumber/__init__.py", line 101, in _request
raise exception_class("Client Error %s: %s" % (resp.status_code, url), response=resp, content=resp.content)
HttpClientError: Client Error 400: http://localhost:8000/api/v2/command/
[28/Feb/2018 04:43:29] readthedocs.doc_builder.environments:431[23847]: INFO (Build) [pip:latest] Build finished
[28/Feb/2018 04:43:29] readthedocs.projects.tasks:233[23847]: INFO (Build) [pip:latest] Failing build because of setup failure: Client Error 400: http://localhost:8000/api/v2/command/
[28/Feb/2018 04:43:29] readthedocs.projects.tasks:284[23847]: ERROR An unhandled exception was raised during build setup
Traceback (most recent call last):
File "/home/rm/src/readthedocs/rtd/checkouts/readthedocs.org/readthedocs/projects/tasks.py", line 276, in run
setup_successful = self.run_setup(record=record)
File "/home/rm/src/readthedocs/rtd/checkouts/readthedocs.org/readthedocs/projects/tasks.py", line 355, in run_setup
self.send_notifications()
File "/home/rm/src/readthedocs/rtd/checkouts/readthedocs.org/readthedocs/projects/tasks.py", line 663, in send_notifications
send_notifications.delay(self.version.pk, build_pk=self.build['id'])
KeyError: 'id'
[28/Feb/2018 04:43:29] celery.app.trace:123[23847]: INFO Task readthedocs.projects.tasks.update_docs[87cb2b57-91fb-48b7-b97b-ec22c31099f1] succeeded in 1.796398073s: False

# Fixed code
def handle(self, *args, **options):
    force = options["force"]
    version = options["version"]

    if options.get("slugs", []):
        for slug in options["slugs"]:
            if version and version != "all":
                log.info("Updating version %s for %s", version, slug)
                for version in Version.objects.filter(
                    project__slug=slug,
                    slug=version,
                ):
                    trigger_build(project=version.project, version=version)
            elif version == "all":
                log.info("Updating all versions for %s", slug)
                for version in Version.objects.filter(
                    project__slug=slug,
                    active=True,
                    uploaded=False,
                ):
                    build = Build.objects.create(
                        project=version.project,
                        version=version,
                        type="html",
                        state="triggered",
                    )

                    tasks.UpdateDocsTask().run(
                        pk=version.project_id,
                        build_pk=build.pk,
                        version_pk=version.pk,
                    )
            else:
                p = Project.all_objects.get(slug=slug)
                log.info("Building %s", p)
                trigger_build(project=p, force=force)
    else:
        if version == "all":
            log.info("Updating all versions")
            for version in Version.objects.filter(
                active=True,
                uploaded=False,
            ):
                tasks.UpdateDocsTask().run(
                    pk=version.project_id,
                    force=force,
                    version_pk=version.pk,
                )
        else:
            log.info("Updating all docs")
            for project in Project.objects.all():
                tasks.UpdateDocsTask().run(
                    pk=project.pk,
                    force=force,
                )