Piki is a minimal wiki
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

migrate_to_db.py 3.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. from django.conf import settings
  2. from django.contrib.auth.models import User
  3. from django.core.management.base import BaseCommand
  4. from pages.page import full_path_all_pages, page_wrapped
  5. from pages.models import PikiPage
  6. from datetime import datetime
  7. import fstools
  8. import os
  9. import shutil
  10. from zoneinfo import ZoneInfo
  11. def add_page_data(rel_path, tags, page_txt, creation_time, creation_user, modified_time, modified_user):
  12. try:
  13. page = PikiPage.objects.get(rel_path=rel_path)
  14. except PikiPage.DoesNotExist:
  15. page = PikiPage(rel_path=rel_path)
  16. #
  17. page.tags = tags
  18. page.page_txt = page_txt
  19. #
  20. page.creation_time = datetime.fromtimestamp(creation_time, ZoneInfo("UTC"))
  21. creation_user = creation_user or "dirk"
  22. page.creation_user = User.objects.get(username=creation_user)
  23. modified_user = modified_user or "dirk"
  24. page.modified_time = datetime.fromtimestamp(modified_time, ZoneInfo("UTC"))
  25. page.modified_user = User.objects.get(username=modified_user)
  26. page.owner = page.owner or page.creation_user
  27. #
  28. page.save()
  29. class Command(BaseCommand):
  30. def handle(self, *args, **options):
  31. for path in full_path_all_pages():
  32. fs_page = page_wrapped(None, path)
  33. if fs_page._page.is_available():
  34. self.stdout.write(self.style.MIGRATE_HEADING("Migration of page '%s'" % fs_page.rel_path))
  35. for history_number in fs_page._page.history_numbers_list():
  36. self.stdout.write(self.style.MIGRATE_HEADING(" * Adding history version %d" % history_number))
  37. h_page = page_wrapped(None, path, history_version=history_number)
  38. add_page_data(
  39. rel_path=h_page.rel_path,
  40. tags=h_page.tags,
  41. page_txt=h_page._page.raw_page_src,
  42. #
  43. creation_time=h_page.creation_time,
  44. creation_user=h_page.creation_user,
  45. modified_time=h_page.modified_time,
  46. modified_user=h_page.modified_user
  47. )
  48. #
  49. self.stdout.write(self.style.MIGRATE_HEADING(" * Adding current version"))
  50. add_page_data(
  51. rel_path=fs_page.rel_path,
  52. tags=fs_page.tags,
  53. page_txt=fs_page._page.raw_page_src,
  54. #
  55. creation_time=fs_page.creation_time,
  56. creation_user=fs_page.creation_user,
  57. modified_time=fs_page.modified_time,
  58. modified_user=fs_page.modified_user
  59. )
  60. #
  61. src = os.path.join(path, "attachments")
  62. if os.path.isdir(src):
  63. dst = os.path.join(settings.MYCREOLE_ROOT, fs_page.rel_path)
  64. for attachment in fstools.filelist(src):
  65. self.stdout.write(self.style.MIGRATE_HEADING(" * Copy attachment ''%s to new location" % os.path.basename(attachment)))
  66. fstools.mkdir(dst)
  67. shutil.copy(attachment, dst)