1
0
Fork 0

feat: Initialize django at the beginning

This commit is contained in:
Cristian 2020-10-26 07:45:21 -05:00
parent 5faadee7d1
commit e1d0b8bce7
5 changed files with 7 additions and 13 deletions

View file

@ -1 +1,5 @@
__package__ = 'archivebox' __package__ = 'archivebox'
from .config import setup_django, OUTPUT_DIR
print(OUTPUT_DIR)
setup_django()

View file

@ -7,14 +7,13 @@ from django.db.models import QuerySet
from .schema import Link from .schema import Link
from ..util import enforce_types from ..util import enforce_types
from ..config import setup_django, OUTPUT_DIR from ..config import OUTPUT_DIR
### Main Links Index ### Main Links Index
@enforce_types @enforce_types
def parse_sql_main_index(out_dir: Path=OUTPUT_DIR) -> Iterator[Link]: def parse_sql_main_index(out_dir: Path=OUTPUT_DIR) -> Iterator[Link]:
setup_django(out_dir, check_db=True)
from core.models import Snapshot from core.models import Snapshot
return ( return (
@ -24,7 +23,6 @@ def parse_sql_main_index(out_dir: Path=OUTPUT_DIR) -> Iterator[Link]:
@enforce_types @enforce_types
def remove_from_sql_main_index(snapshots: QuerySet, out_dir: Path=OUTPUT_DIR) -> None: def remove_from_sql_main_index(snapshots: QuerySet, out_dir: Path=OUTPUT_DIR) -> None:
setup_django(out_dir, check_db=True)
from django.db import transaction from django.db import transaction
with transaction.atomic(): with transaction.atomic():
@ -51,7 +49,6 @@ def write_link_to_sql_index(link: Link):
@enforce_types @enforce_types
def write_sql_main_index(links: List[Link], out_dir: Path=OUTPUT_DIR) -> None: def write_sql_main_index(links: List[Link], out_dir: Path=OUTPUT_DIR) -> None:
setup_django(out_dir, check_db=True)
from django.db import transaction from django.db import transaction
with transaction.atomic(): with transaction.atomic():
@ -61,7 +58,6 @@ def write_sql_main_index(links: List[Link], out_dir: Path=OUTPUT_DIR) -> None:
@enforce_types @enforce_types
def write_sql_link_details(link: Link, out_dir: Path=OUTPUT_DIR) -> None: def write_sql_link_details(link: Link, out_dir: Path=OUTPUT_DIR) -> None:
setup_django(out_dir, check_db=True)
from core.models import Snapshot from core.models import Snapshot
from django.db import transaction from django.db import transaction
@ -84,7 +80,6 @@ def write_sql_link_details(link: Link, out_dir: Path=OUTPUT_DIR) -> None:
@enforce_types @enforce_types
def list_migrations(out_dir: Path=OUTPUT_DIR) -> List[Tuple[bool, str]]: def list_migrations(out_dir: Path=OUTPUT_DIR) -> List[Tuple[bool, str]]:
setup_django(out_dir, check_db=False)
from django.core.management import call_command from django.core.management import call_command
out = StringIO() out = StringIO()
call_command("showmigrations", list=True, stdout=out) call_command("showmigrations", list=True, stdout=out)
@ -101,7 +96,6 @@ def list_migrations(out_dir: Path=OUTPUT_DIR) -> List[Tuple[bool, str]]:
@enforce_types @enforce_types
def apply_migrations(out_dir: Path=OUTPUT_DIR) -> List[str]: def apply_migrations(out_dir: Path=OUTPUT_DIR) -> List[str]:
setup_django(out_dir, check_db=False)
from django.core.management import call_command from django.core.management import call_command
null, out = StringIO(), StringIO() null, out = StringIO(), StringIO()
call_command("makemigrations", interactive=False, stdout=null) call_command("makemigrations", interactive=False, stdout=null)
@ -112,6 +106,5 @@ def apply_migrations(out_dir: Path=OUTPUT_DIR) -> List[str]:
@enforce_types @enforce_types
def get_admins(out_dir: Path=OUTPUT_DIR) -> List[str]: def get_admins(out_dir: Path=OUTPUT_DIR) -> List[str]:
setup_django(out_dir, check_db=False)
from django.contrib.auth.models import User from django.contrib.auth.models import User
return User.objects.filter(is_superuser=True) return User.objects.filter(is_superuser=True)

View file

@ -82,7 +82,6 @@ from .config import (
check_dependencies, check_dependencies,
check_data_folder, check_data_folder,
write_config_file, write_config_file,
setup_django,
VERSION, VERSION,
CODE_LOCATIONS, CODE_LOCATIONS,
EXTERNAL_LOCATIONS, EXTERNAL_LOCATIONS,
@ -305,7 +304,6 @@ def init(force: bool=False, out_dir: Path=OUTPUT_DIR) -> None:
else: else:
print('\n{green}[+] Building main SQL index and running migrations...{reset}'.format(**ANSI)) print('\n{green}[+] Building main SQL index and running migrations...{reset}'.format(**ANSI))
setup_django(out_dir, check_db=False)
DATABASE_FILE = Path(out_dir) / SQL_INDEX_FILENAME DATABASE_FILE = Path(out_dir) / SQL_INDEX_FILENAME
print(f'{DATABASE_FILE}') print(f'{DATABASE_FILE}')
print() print()
@ -1033,7 +1031,6 @@ def server(runserver_args: Optional[List[str]]=None,
config.DEBUG = config.DEBUG or debug config.DEBUG = config.DEBUG or debug
check_data_folder(out_dir=out_dir) check_data_folder(out_dir=out_dir)
setup_django(out_dir)
from django.core.management import call_command from django.core.management import call_command
from django.contrib.auth.models import User from django.contrib.auth.models import User
@ -1070,7 +1067,6 @@ def manage(args: Optional[List[str]]=None, out_dir: Path=OUTPUT_DIR) -> None:
"""Run an ArchiveBox Django management command""" """Run an ArchiveBox Django management command"""
check_data_folder(out_dir=out_dir) check_data_folder(out_dir=out_dir)
setup_django(out_dir)
from django.core.management import execute_from_command_line from django.core.management import execute_from_command_line
if (args and "createsuperuser" in args) and (IN_DOCKER and not IS_TTY): if (args and "createsuperuser" in args) and (IN_DOCKER and not IS_TTY):
@ -1087,7 +1083,6 @@ def shell(out_dir: Path=OUTPUT_DIR) -> None:
check_data_folder(out_dir=out_dir) check_data_folder(out_dir=out_dir)
setup_django(OUTPUT_DIR)
from django.core.management import call_command from django.core.management import call_command
call_command("shell_plus") call_command("shell_plus")

View file

@ -6,6 +6,7 @@ import pytest
@pytest.fixture @pytest.fixture
def process(tmp_path): def process(tmp_path):
os.chdir(tmp_path) os.chdir(tmp_path)
print("should be at", tmp_path)
process = subprocess.run(['archivebox', 'init'], capture_output=True) process = subprocess.run(['archivebox', 'init'], capture_output=True)
return process return process

View file

@ -32,6 +32,7 @@ def test_depth_flag_0_crawls_only_the_arg_page(tmp_path, process, disable_extrac
env=disable_extractors_dict, env=disable_extractors_dict,
) )
breakpoint()
archived_item_path = list(tmp_path.glob('archive/**/*'))[0] archived_item_path = list(tmp_path.glob('archive/**/*'))[0]
with open(archived_item_path / "index.json", "r") as f: with open(archived_item_path / "index.json", "r") as f:
output_json = json.load(f) output_json = json.load(f)