Browse Source

Add check for existing user, change varable names

Ben Muthalaly 2 years ago
parent
commit
521ea70e0c
2 changed files with 11 additions and 12 deletions
  1. 2 2
      archivebox/config.py
  2. 9 10
      archivebox/main.py

+ 2 - 2
archivebox/config.py

@@ -91,8 +91,8 @@ CONFIG_SCHEMA: Dict[str, ConfigDefaultDict] = {
         'OUTPUT_PERMISSIONS':       {'type': str,   'default': '644'},
         'OUTPUT_PERMISSIONS':       {'type': str,   'default': '644'},
         'RESTRICT_FILE_NAMES':      {'type': str,   'default': 'windows'},
         'RESTRICT_FILE_NAMES':      {'type': str,   'default': 'windows'},
         'URL_BLACKLIST':            {'type': str,   'default': r'\.(css|js|otf|ttf|woff|woff2|gstatic\.com|googleapis\.com/css)(\?.*)?$'},  # to avoid downloading code assets as their own pages
         'URL_BLACKLIST':            {'type': str,   'default': r'\.(css|js|otf|ttf|woff|woff2|gstatic\.com|googleapis\.com/css)(\?.*)?$'},  # to avoid downloading code assets as their own pages
-        'ARCHIVEBOX_USERNAME':      {'type': str,   'default': None},
-        'ARCHIVEBOX_PASSWORD':      {'type': str,   'default': None},
+        'ADMIN_USERNAME':      {'type': str,   'default': None},
+        'ADMIN_PASSWORD':      {'type': str,   'default': None},
         'URL_WHITELIST':            {'type': str,   'default': None},
         'URL_WHITELIST':            {'type': str,   'default': None},
         'ENFORCE_ATOMIC_WRITES':    {'type': bool,  'default': True},
         'ENFORCE_ATOMIC_WRITES':    {'type': bool,  'default': True},
         'TAG_SEPARATOR_PATTERN':    {'type': str,   'default': r'[,]'},
         'TAG_SEPARATOR_PATTERN':    {'type': str,   'default': r'[,]'},

+ 9 - 10
archivebox/main.py

@@ -112,8 +112,8 @@ from .config import (
     load_all_config,
     load_all_config,
     CONFIG,
     CONFIG,
     USER_CONFIG,
     USER_CONFIG,
-    ARCHIVEBOX_USERNAME,
-    ARCHIVEBOX_PASSWORD,
+    ADMIN_USERNAME,
+    ADMIN_PASSWORD,
     get_real_name,
     get_real_name,
     setup_django,
     setup_django,
 )
 )
@@ -421,17 +421,16 @@ def init(force: bool=False, quick: bool=False, setup: bool=False, out_dir: Path=
         write_main_index(list(pending_links.values()), out_dir=out_dir)
         write_main_index(list(pending_links.values()), out_dir=out_dir)
 
 
     print('\n{green}----------------------------------------------------------------------{reset}'.format(**ANSI))
     print('\n{green}----------------------------------------------------------------------{reset}'.format(**ANSI))
+
+    from django.contrib.auth.models import User
+
+    if (ADMIN_USERNAME and ADMIN_PASSWORD) and not User.objects.filter(username=ADMIN_USERNAME, is_superuser=True).exists():
+        User.objects.create_superuser(username=ADMIN_USERNAME, password=ADMIN_PASSWORD)
+        print('{green}[+] New ADMIN_USERNAME and  ADMIN_PASSWORD configuration options found. Creating new admin user.{reset}'.format(ADMIN_USERNAME, ADMIN_PASSWORD, **ANSI))
+
     if existing_index:
     if existing_index:
         print('{green}[√] Done. Verified and updated the existing ArchiveBox collection.{reset}'.format(**ANSI))
         print('{green}[√] Done. Verified and updated the existing ArchiveBox collection.{reset}'.format(**ANSI))
     else:
     else:
-        if ARCHIVEBOX_USERNAME and ARCHIVEBOX_PASSWORD:
-            print('{green}[+] ARCHIVEBOX_USERNAME and  ARCHIVEBOX_PASSWORD configuration options found. Creating new admin user.{reset}'.format(**ANSI))
-            from django.contrib.auth.models import User
-            User.objects.create_superuser(username=ARCHIVEBOX_USERNAME, password=ARCHIVEBOX_PASSWORD)
-        # if config.HTTP_USER and config.HTTP_PASS:
-        #     from django.contrib.auth.models import User
-        #     User.objects.create_superuser(HTTP_USER, '', HTTP_PASS)
-
         print('{green}[√] Done. A new ArchiveBox collection was initialized ({} links).{reset}'.format(len(all_links) + len(pending_links), **ANSI))
         print('{green}[√] Done. A new ArchiveBox collection was initialized ({} links).{reset}'.format(len(all_links) + len(pending_links), **ANSI))
 
 
     json_index = out_dir / JSON_INDEX_FILENAME
     json_index = out_dir / JSON_INDEX_FILENAME