prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  can no longer sign in using a pre-existing superuser account after upgrade
  It appears that my superuser account is no longer available since upgrading to the latest develop. 
  
  I would have expected it to get migrated to an admin account with superuser flags enabled.
  
  It actually looks like the user might still be there:
  
  ![image](https://user-images.githubusercontent.com/2367265/29852726-81f2b1b4-8cf0-11e7-9b0a-adb9fb0cb264.png)
  
  
  
  --- 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/auth/migrations/0004_auto_20170816_1607.py`
  Content:
  ```
  1 # -*- coding: utf-8 -*-
  2 # Generated by Django 1.9.7 on 2017-08-16 23:07
  3 from __future__ import unicode_literals
  4 
  5 import django.core.validators
  6 from django.db import migrations, models
  7 from kolibri.auth.constants.role_kinds import ADMIN
  8 
  9 
  10 def device_owner_to_super_user(apps, schema_editor):
  11     DeviceOwner = apps.get_model('kolibriauth', 'DeviceOwner')
  12     FacilityUser = apps.get_model('kolibriauth', 'FacilityUser')
  13     Facility = apps.get_model('kolibriauth', 'Facility')
  14     default_facility = Facility.objects.all().first()
  15     DevicePermissions = apps.get_model('device', 'DevicePermissions')
  16     DeviceSettings = apps.get_model('device', 'DeviceSettings')
  17     Role = apps.get_model('kolibriauth', 'Role')
  18     from kolibri.auth.models import FacilityUser as RealFacilityUser, Facility as RealFacility, Role as RealRole
  19     real_default_facility = RealFacility.get_default_facility()
  20     # Can't do much if no facilities exist, as no facility to FK the users onto
  21     if default_facility:
  22         for device_owner in DeviceOwner.objects.all():
  23             dataset_id = real_default_facility.dataset_id
  24             real_superuser = RealFacilityUser(
  25                 username=device_owner.username,
  26                 facility=real_default_facility,
  27                 dataset_id=dataset_id
  28             )
  29             uuid = real_superuser.calculate_uuid()
  30             superuser = FacilityUser.objects.create(
  31                 username=device_owner.username,
  32                 password=device_owner.password,
  33                 facility=default_facility,
  34                 full_name=device_owner.full_name,
  35                 date_joined=device_owner.date_joined,
  36                 id=uuid,
  37                 dataset_id=dataset_id,
  38                 _morango_source_id=real_superuser._morango_source_id,
  39                 _morango_partition=real_superuser._morango_partition,
  40             )
  41             real_role = RealRole(
  42                 user=real_superuser,
  43                 collection=real_default_facility,
  44                 kind=ADMIN,
  45                 dataset_id=dataset_id,
  46             )
  47             role_uuid = real_role.calculate_uuid()
  48             role = Role.objects.create(
  49                 user=superuser,
  50                 collection=default_facility,
  51                 kind=ADMIN,
  52                 id=role_uuid,
  53                 dataset_id=dataset_id,
  54                 _morango_source_id=real_role._morango_source_id,
  55                 _morango_partition=real_role._morango_partition,
  56             )
  57             DevicePermissions.objects.create(user=superuser, is_superuser=True)
  58         # Finally, set the is_provisioned flag
  59         settings, created = DeviceSettings.objects.get_or_create(is_provisioned=True)
  60 
  61 
  62 class Migration(migrations.Migration):
  63 
  64     dependencies = [
  65         ('kolibriauth', '0003_auto_20170621_0958'),
  66         ('device', '0001_initial')
  67     ]
  68 
  69     operations = [
  70         migrations.RunPython(device_owner_to_super_user, migrations.RunPython.noop),
  71         migrations.DeleteModel(
  72             name='DeviceOwner',
  73         ),
  74     ]
  75
  ```
  --- 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/auth/migrations/0004_auto_20170816_1607.py b/kolibri/auth/migrations/0004_auto_20170816_1607.py
  --- a/kolibri/auth/migrations/0004_auto_20170816_1607.py
  +++ b/kolibri/auth/migrations/0004_auto_20170816_1607.py
  @@ -8,17 +8,17 @@
   
   
   def device_owner_to_super_user(apps, schema_editor):
  -    DeviceOwner = apps.get_model('kolibriauth', 'DeviceOwner')
  -    FacilityUser = apps.get_model('kolibriauth', 'FacilityUser')
  -    Facility = apps.get_model('kolibriauth', 'Facility')
  -    default_facility = Facility.objects.all().first()
  -    DevicePermissions = apps.get_model('device', 'DevicePermissions')
  -    DeviceSettings = apps.get_model('device', 'DeviceSettings')
  -    Role = apps.get_model('kolibriauth', 'Role')
       from kolibri.auth.models import FacilityUser as RealFacilityUser, Facility as RealFacility, Role as RealRole
       real_default_facility = RealFacility.get_default_facility()
       # Can't do much if no facilities exist, as no facility to FK the users onto
  -    if default_facility:
  +    if real_default_facility:
  +        DeviceOwner = apps.get_model('kolibriauth', 'DeviceOwner')
  +        FacilityUser = apps.get_model('kolibriauth', 'FacilityUser')
  +        Facility = apps.get_model('kolibriauth', 'Facility')
  +        default_facility = Facility.objects.get(pk=real_default_facility.id)
  +        DevicePermissions = apps.get_model('device', 'DevicePermissions')
  +        DeviceSettings = apps.get_model('device', 'DeviceSettings')
  +        Role = apps.get_model('kolibriauth', 'Role')
           for device_owner in DeviceOwner.objects.all():
               dataset_id = real_default_facility.dataset_id
               real_superuser = RealFacilityUser(
  
verification_info: {"golden_diff": "diff --git a/kolibri/auth/migrations/0004_auto_20170816_1607.py b/kolibri/auth/migrations/0004_auto_20170816_1607.py\n--- a/kolibri/auth/migrations/0004_auto_20170816_1607.py\n+++ b/kolibri/auth/migrations/0004_auto_20170816_1607.py\n@@ -8,17 +8,17 @@\n \n \n def device_owner_to_super_user(apps, schema_editor):\n-    DeviceOwner = apps.get_model('kolibriauth', 'DeviceOwner')\n-    FacilityUser = apps.get_model('kolibriauth', 'FacilityUser')\n-    Facility = apps.get_model('kolibriauth', 'Facility')\n-    default_facility = Facility.objects.all().first()\n-    DevicePermissions = apps.get_model('device', 'DevicePermissions')\n-    DeviceSettings = apps.get_model('device', 'DeviceSettings')\n-    Role = apps.get_model('kolibriauth', 'Role')\n     from kolibri.auth.models import FacilityUser as RealFacilityUser, Facility as RealFacility, Role as RealRole\n     real_default_facility = RealFacility.get_default_facility()\n     # Can't do much if no facilities exist, as no facility to FK the users onto\n-    if default_facility:\n+    if real_default_facility:\n+        DeviceOwner = apps.get_model('kolibriauth', 'DeviceOwner')\n+        FacilityUser = apps.get_model('kolibriauth', 'FacilityUser')\n+        Facility = apps.get_model('kolibriauth', 'Facility')\n+        default_facility = Facility.objects.get(pk=real_default_facility.id)\n+        DevicePermissions = apps.get_model('device', 'DevicePermissions')\n+        DeviceSettings = apps.get_model('device', 'DeviceSettings')\n+        Role = apps.get_model('kolibriauth', 'Role')\n         for device_owner in DeviceOwner.objects.all():\n             dataset_id = real_default_facility.dataset_id\n             real_superuser = RealFacilityUser(\n", "issue": "can no longer sign in using a pre-existing superuser account after upgrade\nIt appears that my superuser account is no longer available since upgrading to the latest develop. \r\n\r\nI would have expected it to get migrated to an admin account with superuser flags enabled.\r\n\r\nIt actually looks like the user might still be there:\r\n\r\n![image](https://user-images.githubusercontent.com/2367265/29852726-81f2b1b4-8cf0-11e7-9b0a-adb9fb0cb264.png)\r\n\r\n\n", "before_files": [{"content": "# -*- coding: utf-8 -*-\n# Generated by Django 1.9.7 on 2017-08-16 23:07\nfrom __future__ import unicode_literals\n\nimport django.core.validators\nfrom django.db import migrations, models\nfrom kolibri.auth.constants.role_kinds import ADMIN\n\n\ndef device_owner_to_super_user(apps, schema_editor):\n    DeviceOwner = apps.get_model('kolibriauth', 'DeviceOwner')\n    FacilityUser = apps.get_model('kolibriauth', 'FacilityUser')\n    Facility = apps.get_model('kolibriauth', 'Facility')\n    default_facility = Facility.objects.all().first()\n    DevicePermissions = apps.get_model('device', 'DevicePermissions')\n    DeviceSettings = apps.get_model('device', 'DeviceSettings')\n    Role = apps.get_model('kolibriauth', 'Role')\n    from kolibri.auth.models import FacilityUser as RealFacilityUser, Facility as RealFacility, Role as RealRole\n    real_default_facility = RealFacility.get_default_facility()\n    # Can't do much if no facilities exist, as no facility to FK the users onto\n    if default_facility:\n        for device_owner in DeviceOwner.objects.all():\n            dataset_id = real_default_facility.dataset_id\n            real_superuser = RealFacilityUser(\n                username=device_owner.username,\n                facility=real_default_facility,\n                dataset_id=dataset_id\n            )\n            uuid = real_superuser.calculate_uuid()\n            superuser = FacilityUser.objects.create(\n                username=device_owner.username,\n                password=device_owner.password,\n                facility=default_facility,\n                full_name=device_owner.full_name,\n                date_joined=device_owner.date_joined,\n                id=uuid,\n                dataset_id=dataset_id,\n                _morango_source_id=real_superuser._morango_source_id,\n                _morango_partition=real_superuser._morango_partition,\n            )\n            real_role = RealRole(\n                user=real_superuser,\n                collection=real_default_facility,\n                kind=ADMIN,\n                dataset_id=dataset_id,\n            )\n            role_uuid = real_role.calculate_uuid()\n            role = Role.objects.create(\n                user=superuser,\n                collection=default_facility,\n                kind=ADMIN,\n                id=role_uuid,\n                dataset_id=dataset_id,\n                _morango_source_id=real_role._morango_source_id,\n                _morango_partition=real_role._morango_partition,\n            )\n            DevicePermissions.objects.create(user=superuser, is_superuser=True)\n        # Finally, set the is_provisioned flag\n        settings, created = DeviceSettings.objects.get_or_create(is_provisioned=True)\n\n\nclass Migration(migrations.Migration):\n\n    dependencies = [\n        ('kolibriauth', '0003_auto_20170621_0958'),\n        ('device', '0001_initial')\n    ]\n\n    operations = [\n        migrations.RunPython(device_owner_to_super_user, migrations.RunPython.noop),\n        migrations.DeleteModel(\n            name='DeviceOwner',\n        ),\n    ]\n", "path": "kolibri/auth/migrations/0004_auto_20170816_1607.py"}], "after_files": [{"content": "# -*- coding: utf-8 -*-\n# Generated by Django 1.9.7 on 2017-08-16 23:07\nfrom __future__ import unicode_literals\n\nimport django.core.validators\nfrom django.db import migrations, models\nfrom kolibri.auth.constants.role_kinds import ADMIN\n\n\ndef device_owner_to_super_user(apps, schema_editor):\n    from kolibri.auth.models import FacilityUser as RealFacilityUser, Facility as RealFacility, Role as RealRole\n    real_default_facility = RealFacility.get_default_facility()\n    # Can't do much if no facilities exist, as no facility to FK the users onto\n    if real_default_facility:\n        DeviceOwner = apps.get_model('kolibriauth', 'DeviceOwner')\n        FacilityUser = apps.get_model('kolibriauth', 'FacilityUser')\n        Facility = apps.get_model('kolibriauth', 'Facility')\n        default_facility = Facility.objects.get(pk=real_default_facility.id)\n        DevicePermissions = apps.get_model('device', 'DevicePermissions')\n        DeviceSettings = apps.get_model('device', 'DeviceSettings')\n        Role = apps.get_model('kolibriauth', 'Role')\n        for device_owner in DeviceOwner.objects.all():\n            dataset_id = real_default_facility.dataset_id\n            real_superuser = RealFacilityUser(\n                username=device_owner.username,\n                facility=real_default_facility,\n                dataset_id=dataset_id\n            )\n            uuid = real_superuser.calculate_uuid()\n            superuser = FacilityUser.objects.create(\n                username=device_owner.username,\n                password=device_owner.password,\n                facility=default_facility,\n                full_name=device_owner.full_name,\n                date_joined=device_owner.date_joined,\n                id=uuid,\n                dataset_id=dataset_id,\n                _morango_source_id=real_superuser._morango_source_id,\n                _morango_partition=real_superuser._morango_partition,\n            )\n            real_role = RealRole(\n                user=real_superuser,\n                collection=real_default_facility,\n                kind=ADMIN,\n                dataset_id=dataset_id,\n            )\n            role_uuid = real_role.calculate_uuid()\n            role = Role.objects.create(\n                user=superuser,\n                collection=default_facility,\n                kind=ADMIN,\n                id=role_uuid,\n                dataset_id=dataset_id,\n                _morango_source_id=real_role._morango_source_id,\n                _morango_partition=real_role._morango_partition,\n            )\n            DevicePermissions.objects.create(user=superuser, is_superuser=True)\n        # Finally, set the is_provisioned flag\n        settings, created = DeviceSettings.objects.get_or_create(is_provisioned=True)\n\n\nclass Migration(migrations.Migration):\n\n    dependencies = [\n        ('kolibriauth', '0003_auto_20170621_0958'),\n        ('device', '0001_initial')\n    ]\n\n    operations = [\n        migrations.RunPython(device_owner_to_super_user, migrations.RunPython.noop),\n        migrations.DeleteModel(\n            name='DeviceOwner',\n        ),\n    ]\n", "path": "kolibri/auth/migrations/0004_auto_20170816_1607.py"}]}