prompt: You will be provided with a partial code base and an issue statement explaining a problem to resolve.
  <issue>
  [BUG] Password reset after docker container restarted
  *Copy from old repository*: https://github.com/jonaswinkler/paperless-ng/issues/1511
  
  **Describe the bug**
  I deployed Paperless-NG in TrueNAS via the TrueCharts integration. TrueCharts uses the official docker container and passes environment variables to configure the superuser.
  
  I changed the admin password in the Django admin interface. However, after redeploying the application (for example due to an update) the password gets overridden by the initial password passed via environment variable.
  
  **To Reproduce**
  Steps to reproduce the behavior:
  1. Deploy Paperless with credentials admin//secret
  2. Open Paperless
  3. Navigate to admin interface
  4. Change password to "mysupersecretpassword"
  5. Restart/update the docker container
  6. Navigate to Paperless and try to login with admin/mysupersecretpassword
  7. You can't login.
  
  **Expected behavior**
  The admin password should not be overridden by the initial password.
  
  **Relevant information**
   - Version 
   - Installation method: **docker**
   - Any configuration changes you made in `docker-compose.yml`, `docker-compose.env` or `paperless.conf`. -
  
  I think this is related to the admin user password reset when the docker container is started:
  docker-entrypoint.sh calls docker-prepare.sh calls the manage_superuser mgmt command and there the password is updated:
  https://github.com/jonaswinkler/paperless-ng/blob/master/src/documents/management/commands/manage_superuser.py#L29
  
  Am I missing something?
  
  </issue>
  <code>
  [start of src/documents/management/commands/manage_superuser.py]
  1 import logging
  2 import os
  3 
  4 from django.contrib.auth.models import User
  5 from django.core.management.base import BaseCommand
  6 
  7 
  8 logger = logging.getLogger("paperless.management.superuser")
  9 
  10 
  11 class Command(BaseCommand):
  12 
  13     help = """
  14         Creates a Django superuser based on env variables.
  15     """.replace(
  16         "    ",
  17         "",
  18     )
  19 
  20     def handle(self, *args, **options):
  21 
  22         username = os.getenv("PAPERLESS_ADMIN_USER")
  23         if not username:
  24             return
  25 
  26         mail = os.getenv("PAPERLESS_ADMIN_MAIL", "root@localhost")
  27         password = os.getenv("PAPERLESS_ADMIN_PASSWORD")
  28 
  29         # Check if user exists already, leave as is if it does
  30         if User.objects.filter(username=username).exists():
  31             user: User = User.objects.get_by_natural_key(username)
  32             user.set_password(password)
  33             user.save()
  34             self.stdout.write(f"Changed password of user {username}.")
  35         elif password:
  36             # Create superuser based on env variables
  37             User.objects.create_superuser(username, mail, password)
  38             self.stdout.write(f'Created superuser "{username}" with provided password.')
  39         else:
  40             self.stdout.write(f'Did not create superuser "{username}".')
  41             self.stdout.write(
  42                 'Make sure you specified "PAPERLESS_ADMIN_PASSWORD" in your '
  43                 '"docker-compose.env" file.',
  44             )
  45
  [end of src/documents/management/commands/manage_superuser.py]
  </code>
  
  I need you to solve this issue by generating a single patch file that I can apply directly to this repository using git apply. Please respond with a single patch file in the following format.
  <patch>
  diff --git a/file.py b/file.py
  --- a/file.py
  +++ b/file.py
  @@ -1,27 +1,35 @@
   def euclidean(a, b):
  -    while b:
  -        a, b = b, a % b
  -    return a
  +    if b == 0:
  +        return a
  +    return euclidean(b, a % b)
   
   
   def bresenham(x0, y0, x1, y1):
       points = []
       dx = abs(x1 - x0)
       dy = abs(y1 - y0)
  -    sx = 1 if x0 < x1 else -1
  -    sy = 1 if y0 < y1 else -1
  -    err = dx - dy
  +    x, y = x0, y0
  +    sx = -1 if x0 > x1 else 1
  +    sy = -1 if y0 > y1 else 1
   
  -    while True:
  -        points.append((x0, y0))
  -        if x0 == x1 and y0 == y1:
  -            break
  -        e2 = 2 * err
  -        if e2 > -dy:
  -            err -= dy
  -            x0 += sx
  -        if e2 < dx:
  -            err += dx
  -            y0 += sy
  +    if dx > dy:
  +        err = dx / 2.0
  +        while x != x1:
  +            points.append((x, y))
  +            err -= dy
  +            if err < 0:
  +                y += sy
  +                err += dx
  +            x += sx
  +    else:
  +        err = dy / 2.0
  +        while y != y1:
  +            points.append((x, y))
  +            err -= dx
  +            if err < 0:
  +                x += sx
  +                err += dy
  +            y += sy
  +
  +    points.append((x, y))
       return points
  
  </patch>
golden_diff: diff --git a/src/documents/management/commands/manage_superuser.py b/src/documents/management/commands/manage_superuser.py
  --- a/src/documents/management/commands/manage_superuser.py
  +++ b/src/documents/management/commands/manage_superuser.py
  @@ -11,7 +11,14 @@
   class Command(BaseCommand):
   
       help = """
  -        Creates a Django superuser based on env variables.
  +        Creates a Django superuser:
  +        User named: admin
  +        Email: root@localhost
  +        with password based on env variable.
  +        No superuser will be created, when:
  +        - The username is taken already exists
  +        - A superuser already exists
  +        - PAPERLESS_ADMIN_PASSWORD is not set
       """.replace(
           "    ",
           "",
  @@ -19,26 +26,41 @@
   
       def handle(self, *args, **options):
   
  -        username = os.getenv("PAPERLESS_ADMIN_USER")
  -        if not username:
  -            return
  -
  +        username = os.getenv("PAPERLESS_ADMIN_USER", "admin")
           mail = os.getenv("PAPERLESS_ADMIN_MAIL", "root@localhost")
           password = os.getenv("PAPERLESS_ADMIN_PASSWORD")
   
  -        # Check if user exists already, leave as is if it does
  +        # Check if there's already a user called admin
           if User.objects.filter(username=username).exists():
  -            user: User = User.objects.get_by_natural_key(username)
  -            user.set_password(password)
  -            user.save()
  -            self.stdout.write(f"Changed password of user {username}.")
  -        elif password:
  -            # Create superuser based on env variables
  -            User.objects.create_superuser(username, mail, password)
  -            self.stdout.write(f'Created superuser "{username}" with provided password.')
  +            self.stdout.write(
  +                self.style.NOTICE(
  +                    f"Did not create superuser, a user {username} already exists",
  +                ),
  +            )
  +            return
  +
  +        # Check if any superuseruser
  +        # exists already, leave as is if it does
  +        if User.objects.filter(is_superuser=True).count() > 0:
  +            self.stdout.write(
  +                self.style.NOTICE(
  +                    "Did not create superuser, the DB already contains superusers",
  +                ),
  +            )
  +            return
  +
  +        if password is None:
  +            self.stdout.write(
  +                self.style.ERROR(
  +                    "Please check if PAPERLESS_ADMIN_PASSWORD has been"
  +                    " set in the environment",
  +                ),
  +            )
           else:
  -            self.stdout.write(f'Did not create superuser "{username}".')
  +            # Create superuser with password based on env variable
  +            User.objects.create_superuser(username, mail, password)
               self.stdout.write(
  -                'Make sure you specified "PAPERLESS_ADMIN_PASSWORD" in your '
  -                '"docker-compose.env" file.',
  +                self.style.SUCCESS(
  +                    f'Created superuser "{username}" with provided password.',
  +                ),
               )
  
verification_info: {"golden_diff": "diff --git a/src/documents/management/commands/manage_superuser.py b/src/documents/management/commands/manage_superuser.py\n--- a/src/documents/management/commands/manage_superuser.py\n+++ b/src/documents/management/commands/manage_superuser.py\n@@ -11,7 +11,14 @@\n class Command(BaseCommand):\n \n     help = \"\"\"\n-        Creates a Django superuser based on env variables.\n+        Creates a Django superuser:\n+        User named: admin\n+        Email: root@localhost\n+        with password based on env variable.\n+        No superuser will be created, when:\n+        - The username is taken already exists\n+        - A superuser already exists\n+        - PAPERLESS_ADMIN_PASSWORD is not set\n     \"\"\".replace(\n         \"    \",\n         \"\",\n@@ -19,26 +26,41 @@\n \n     def handle(self, *args, **options):\n \n-        username = os.getenv(\"PAPERLESS_ADMIN_USER\")\n-        if not username:\n-            return\n-\n+        username = os.getenv(\"PAPERLESS_ADMIN_USER\", \"admin\")\n         mail = os.getenv(\"PAPERLESS_ADMIN_MAIL\", \"root@localhost\")\n         password = os.getenv(\"PAPERLESS_ADMIN_PASSWORD\")\n \n-        # Check if user exists already, leave as is if it does\n+        # Check if there's already a user called admin\n         if User.objects.filter(username=username).exists():\n-            user: User = User.objects.get_by_natural_key(username)\n-            user.set_password(password)\n-            user.save()\n-            self.stdout.write(f\"Changed password of user {username}.\")\n-        elif password:\n-            # Create superuser based on env variables\n-            User.objects.create_superuser(username, mail, password)\n-            self.stdout.write(f'Created superuser \"{username}\" with provided password.')\n+            self.stdout.write(\n+                self.style.NOTICE(\n+                    f\"Did not create superuser, a user {username} already exists\",\n+                ),\n+            )\n+            return\n+\n+        # Check if any superuseruser\n+        # exists already, leave as is if it does\n+        if User.objects.filter(is_superuser=True).count() > 0:\n+            self.stdout.write(\n+                self.style.NOTICE(\n+                    \"Did not create superuser, the DB already contains superusers\",\n+                ),\n+            )\n+            return\n+\n+        if password is None:\n+            self.stdout.write(\n+                self.style.ERROR(\n+                    \"Please check if PAPERLESS_ADMIN_PASSWORD has been\"\n+                    \" set in the environment\",\n+                ),\n+            )\n         else:\n-            self.stdout.write(f'Did not create superuser \"{username}\".')\n+            # Create superuser with password based on env variable\n+            User.objects.create_superuser(username, mail, password)\n             self.stdout.write(\n-                'Make sure you specified \"PAPERLESS_ADMIN_PASSWORD\" in your '\n-                '\"docker-compose.env\" file.',\n+                self.style.SUCCESS(\n+                    f'Created superuser \"{username}\" with provided password.',\n+                ),\n             )\n", "issue": "[BUG] Password reset after docker container restarted\n*Copy from old repository*: https://github.com/jonaswinkler/paperless-ng/issues/1511\r\n\r\n**Describe the bug**\r\nI deployed Paperless-NG in TrueNAS via the TrueCharts integration. TrueCharts uses the official docker container and passes environment variables to configure the superuser.\r\n\r\nI changed the admin password in the Django admin interface. However, after redeploying the application (for example due to an update) the password gets overridden by the initial password passed via environment variable.\r\n\r\n**To Reproduce**\r\nSteps to reproduce the behavior:\r\n1. Deploy Paperless with credentials admin//secret\r\n2. Open Paperless\r\n3. Navigate to admin interface\r\n4. Change password to \"mysupersecretpassword\"\r\n5. Restart/update the docker container\r\n6. Navigate to Paperless and try to login with admin/mysupersecretpassword\r\n7. You can't login.\r\n\r\n**Expected behavior**\r\nThe admin password should not be overridden by the initial password.\r\n\r\n**Relevant information**\r\n - Version \r\n - Installation method: **docker**\r\n - Any configuration changes you made in `docker-compose.yml`, `docker-compose.env` or `paperless.conf`. -\r\n\r\nI think this is related to the admin user password reset when the docker container is started:\r\ndocker-entrypoint.sh calls docker-prepare.sh calls the manage_superuser mgmt command and there the password is updated:\r\nhttps://github.com/jonaswinkler/paperless-ng/blob/master/src/documents/management/commands/manage_superuser.py#L29\r\n\r\nAm I missing something?\n", "before_files": [{"content": "import logging\nimport os\n\nfrom django.contrib.auth.models import User\nfrom django.core.management.base import BaseCommand\n\n\nlogger = logging.getLogger(\"paperless.management.superuser\")\n\n\nclass Command(BaseCommand):\n\n    help = \"\"\"\n        Creates a Django superuser based on env variables.\n    \"\"\".replace(\n        \"    \",\n        \"\",\n    )\n\n    def handle(self, *args, **options):\n\n        username = os.getenv(\"PAPERLESS_ADMIN_USER\")\n        if not username:\n            return\n\n        mail = os.getenv(\"PAPERLESS_ADMIN_MAIL\", \"root@localhost\")\n        password = os.getenv(\"PAPERLESS_ADMIN_PASSWORD\")\n\n        # Check if user exists already, leave as is if it does\n        if User.objects.filter(username=username).exists():\n            user: User = User.objects.get_by_natural_key(username)\n            user.set_password(password)\n            user.save()\n            self.stdout.write(f\"Changed password of user {username}.\")\n        elif password:\n            # Create superuser based on env variables\n            User.objects.create_superuser(username, mail, password)\n            self.stdout.write(f'Created superuser \"{username}\" with provided password.')\n        else:\n            self.stdout.write(f'Did not create superuser \"{username}\".')\n            self.stdout.write(\n                'Make sure you specified \"PAPERLESS_ADMIN_PASSWORD\" in your '\n                '\"docker-compose.env\" file.',\n            )\n", "path": "src/documents/management/commands/manage_superuser.py"}]}