db.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. """
  2. Database utility functions for ArchiveBox.
  3. """
  4. __package__ = 'archivebox.misc'
  5. from io import StringIO
  6. from pathlib import Path
  7. from typing import List, Tuple
  8. from archivebox.config import DATA_DIR
  9. from archivebox.misc.util import enforce_types
  10. @enforce_types
  11. def list_migrations(out_dir: Path = DATA_DIR) -> List[Tuple[bool, str]]:
  12. """List all Django migrations and their status"""
  13. from django.core.management import call_command
  14. out = StringIO()
  15. call_command("showmigrations", list=True, stdout=out)
  16. out.seek(0)
  17. migrations = []
  18. for line in out.readlines():
  19. if line.strip() and ']' in line:
  20. status_str, name_str = line.strip().split(']', 1)
  21. is_applied = 'X' in status_str
  22. migration_name = name_str.strip()
  23. migrations.append((is_applied, migration_name))
  24. return migrations
  25. @enforce_types
  26. def apply_migrations(out_dir: Path = DATA_DIR) -> List[str]:
  27. """Apply pending Django migrations"""
  28. from django.core.management import call_command
  29. out1 = StringIO()
  30. call_command("migrate", interactive=False, database='default', stdout=out1)
  31. out1.seek(0)
  32. return [
  33. line.strip() for line in out1.readlines() if line.strip()
  34. ]
  35. @enforce_types
  36. def get_admins(out_dir: Path = DATA_DIR) -> List:
  37. """Get list of superuser accounts"""
  38. from django.contrib.auth.models import User
  39. return User.objects.filter(is_superuser=True).exclude(username='system')