mirror of
https://git.cmoser.eu/tinytools/django-tinywiki.git
synced 2026-02-04 06:06:33 +01:00
added twexport command
This commit is contained in:
Binary file not shown.
|
After Width: | Height: | Size: 5.1 MiB |
Binary file not shown.
|
After Width: | Height: | Size: 5.1 MiB |
0
tinywiki/management/__init__.py
Normal file
0
tinywiki/management/__init__.py
Normal file
0
tinywiki/management/commands/__init__.py
Normal file
0
tinywiki/management/commands/__init__.py
Normal file
28
tinywiki/management/commands/twexport.py
Normal file
28
tinywiki/management/commands/twexport.py
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
from django.core.management.base import BaseCommand, CommandError # noqa
|
||||||
|
from ...utils import export_wiki_content
|
||||||
|
|
||||||
|
|
||||||
|
class Command(BaseCommand):
|
||||||
|
help = "Export wiki content for an app"
|
||||||
|
|
||||||
|
def add_arguments(self, parser):
|
||||||
|
parser.add_argument('app', nargs=1, type=str)
|
||||||
|
parser.add_argument('--file', type=str)
|
||||||
|
parser.add_argument('--prefix', type=str)
|
||||||
|
parser.add_argument('--page-version', type=int)
|
||||||
|
parser.add_argument('--image-version', type=int)
|
||||||
|
|
||||||
|
def handle(self, *args, **options):
|
||||||
|
app = options['app'][0]
|
||||||
|
page_version = options['page_version']
|
||||||
|
if page_version is None:
|
||||||
|
page_version = 0
|
||||||
|
image_version = options['image_version']
|
||||||
|
if image_version is None:
|
||||||
|
image_version = 0
|
||||||
|
prefix = options['prefix']
|
||||||
|
filename = options['file']
|
||||||
|
if filename is None:
|
||||||
|
filename = f"{app}.tinyiki.zip"
|
||||||
|
|
||||||
|
export_wiki_content(app, filename, prefix, page_version, image_version)
|
||||||
@@ -76,7 +76,7 @@ class Migration(migrations.Migration):
|
|||||||
data = json.loads(ifile.read())
|
data = json.loads(ifile.read())
|
||||||
|
|
||||||
version = data["version"]
|
version = data["version"]
|
||||||
app = data["app"]
|
app = 'tinywiki'
|
||||||
prefix = data['prefix']
|
prefix = data['prefix']
|
||||||
|
|
||||||
for slug, spec in data['pages'].items():
|
for slug, spec in data['pages'].items():
|
||||||
@@ -109,7 +109,7 @@ class Migration(migrations.Migration):
|
|||||||
images_data = json.loads(ifile.read())
|
images_data = json.loads(ifile.read())
|
||||||
|
|
||||||
version = images_data['version']
|
version = images_data['version']
|
||||||
app = images_data['app']
|
app = 'tinywiki'
|
||||||
prefix = images_data['prefix']
|
prefix = images_data['prefix']
|
||||||
|
|
||||||
for slug, _spec in images_data['images'].items():
|
for slug, _spec in images_data['images'].items():
|
||||||
|
|||||||
@@ -262,7 +262,7 @@ def import_builtin_images_from_zip(zip: str | Path, user=None):
|
|||||||
spec['slug'] = slug
|
spec['slug'] = slug
|
||||||
spec['user'] = user if user else get_tinywiki_default_user()
|
spec['user'] = user if user else get_tinywiki_default_user()
|
||||||
|
|
||||||
img = Imagwith zie(**spec)
|
img = Image(**spec)
|
||||||
with zf.open(image_filename) as ifile:
|
with zf.open(image_filename) as ifile:
|
||||||
img_file = ImageFile(ifile, image_basename)
|
img_file = ImageFile(ifile, image_basename)
|
||||||
img.image.save(image_basename, img_file)
|
img.image.save(image_basename, img_file)
|
||||||
@@ -271,9 +271,10 @@ def import_builtin_images_from_zip(zip: str | Path, user=None):
|
|||||||
BuiltinImages.objects.create(app=app, prefix=prefix, version=version)
|
BuiltinImages.objects.create(app=app, prefix=prefix, version=version)
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
def export_wiki_content(app: str,
|
def export_wiki_content(app: str,
|
||||||
filename,
|
filename,
|
||||||
prefix: str|None = None,
|
prefix: str | None = None,
|
||||||
page_version: int = 0,
|
page_version: int = 0,
|
||||||
image_version: int = 0) -> bool:
|
image_version: int = 0) -> bool:
|
||||||
prefix = prefix
|
prefix = prefix
|
||||||
@@ -281,7 +282,7 @@ def export_wiki_content(app: str,
|
|||||||
bp = BuiltinPages.objects.get(app=app)
|
bp = BuiltinPages.objects.get(app=app)
|
||||||
if page_version < bp.version:
|
if page_version < bp.version:
|
||||||
page_version = bp.version + 1
|
page_version = bp.version + 1
|
||||||
if prefix is None:
|
if not prefix:
|
||||||
prefix = bp.prefix
|
prefix = bp.prefix
|
||||||
except BuiltinPages.DoesNotExist:
|
except BuiltinPages.DoesNotExist:
|
||||||
bp = None
|
bp = None
|
||||||
@@ -290,16 +291,16 @@ def export_wiki_content(app: str,
|
|||||||
bi = BuiltinImages.objects.get(app=app)
|
bi = BuiltinImages.objects.get(app=app)
|
||||||
if image_version < bi.version:
|
if image_version < bi.version:
|
||||||
image_version = bi.version + 1
|
image_version = bi.version + 1
|
||||||
if prefix is None:
|
if not prefix:
|
||||||
prefix = bp.prefix
|
prefix = bp.prefix
|
||||||
except BuiltinImages.DoesNotExist:
|
except BuiltinImages.DoesNotExist:
|
||||||
bi = None
|
bi = None
|
||||||
|
|
||||||
if not prefix:
|
if not prefix:
|
||||||
raise RuntimeError("No slug prefix! Can not export!", file=sys.stderr)
|
raise RuntimeError("No slug prefix! Can not export!")
|
||||||
|
|
||||||
pages = Page.objects.filter(slug__startswith=prefix)
|
pages = Page.objects.filter(slug__startswith=prefix)
|
||||||
images = Image.objects.filter(slug__statrswith=prefix)
|
images = Image.objects.filter(slug__startswith=prefix)
|
||||||
|
|
||||||
if not pages and not images:
|
if not pages and not images:
|
||||||
return False
|
return False
|
||||||
@@ -353,7 +354,7 @@ def export_wiki_content(app: str,
|
|||||||
'description': i.description,
|
'description': i.description,
|
||||||
}
|
}
|
||||||
zf.write(i.image.path, arcname)
|
zf.write(i.image.path, arcname)
|
||||||
zf.writestr(images.json, json.dumps(images_data, ensure_ascii=False, indent=4))
|
zf.writestr('images.json', json.dumps(images_data, ensure_ascii=False, indent=4))
|
||||||
if bi:
|
if bi:
|
||||||
bi.version = image_version
|
bi.version = image_version
|
||||||
bi.save()
|
bi.save()
|
||||||
|
|||||||
Reference in New Issue
Block a user