2025.12.23 17:30:33 (cachyos.cmoser.eu)
This commit is contained in:
@@ -7,13 +7,15 @@ class Migration(migrations.Migration):
|
||||
('tinywiki', '0001_initial'),
|
||||
]
|
||||
|
||||
def init_tinywiki_user(apps,schema_editor):
|
||||
@staticmethod
|
||||
def init_tinywiki_user(apps, schema_editor):
|
||||
from django.contrib.auth import get_user_model
|
||||
user = get_user_model().objects.create_user(**settings.TINYWIKI_USER_CONFIG)
|
||||
|
||||
def init_tinywiki_groups_and_permissions(apps,schema_editor):
|
||||
get_user_model().objects.create_user(**settings.TINYWIKI_USER_CONFIG)
|
||||
|
||||
@staticmethod
|
||||
def init_tinywiki_groups_and_permissions(apps, schema_editor):
|
||||
from ..models import Page
|
||||
from django.contrib.auth.models import Group,Permission
|
||||
from django.contrib.auth.models import Group, Permission
|
||||
from django.contrib.contenttypes.models import ContentType
|
||||
|
||||
PERMISSIONS = [
|
||||
@@ -27,7 +29,7 @@ class Migration(migrations.Migration):
|
||||
'tinywiki-delete-all',
|
||||
'tinywiki-delete-system',
|
||||
]
|
||||
|
||||
|
||||
GROUPS = [
|
||||
('tinywiki-moderator',('tinywiki-read-all',
|
||||
'tinywiki-delete-all',
|
||||
@@ -45,79 +47,79 @@ class Migration(migrations.Migration):
|
||||
'tinywiki-edit-all',
|
||||
'tinywiki-edit-system'))
|
||||
]
|
||||
|
||||
|
||||
perm_mapping = {}
|
||||
content_type = ContentType.objects.get_for_model(Page)
|
||||
content_type = ContentType.objects.get_for_model(Page)
|
||||
for perm in PERMISSIONS:
|
||||
permission = Permission.objects.create(codename=perm,content_type=content_type)
|
||||
perm_mapping[perm] = permission
|
||||
|
||||
|
||||
|
||||
for grp,perms in GROUPS:
|
||||
group = Group.objects.create(name=grp)
|
||||
for perm in perms:
|
||||
group.permissions.add(perm_mapping[perm])
|
||||
|
||||
|
||||
group.permissions.add(perm_mapping[perm])
|
||||
|
||||
|
||||
def init_builtin_pages(apps,schema_editor)->None:
|
||||
from ..models import Page,Image
|
||||
from ..enums import WikiContentType,WikiPageStatus
|
||||
from pathlib import Path
|
||||
import json
|
||||
|
||||
|
||||
page_path = Path(__file__).resolve().parent / "pages"
|
||||
json_file = page_path / "pages.json"
|
||||
if json_file.is_file():
|
||||
with open(json_file,"rt",encoding="utf-8") as ifile:
|
||||
data=json.loads(ifile.read())
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
for slug,spec in data.items():
|
||||
filename = page_path / spec['file']
|
||||
with open(filename,"rt",encoding="utf-8") as ifile:
|
||||
content = ifile.read()
|
||||
|
||||
|
||||
Page.objects.create(slug=slug,
|
||||
title=spec['title'],
|
||||
status_data=WikiPageStatus.from_string(spec['status']).value,
|
||||
content_type_data=WikiContentType.from_string(spec['content_type']).value,
|
||||
content=content)
|
||||
|
||||
|
||||
|
||||
|
||||
def init_builtin_images(apps,schema_edit):
|
||||
from pathlib import Path
|
||||
from ..models import Image
|
||||
from django.core.files.images import ImageFile
|
||||
import json
|
||||
|
||||
|
||||
image_dir = Path(__file__).resolve().parent / "images"
|
||||
images_json_file = image_dir/ "images.json"
|
||||
|
||||
|
||||
if not images_json_file.is_file():
|
||||
return
|
||||
|
||||
|
||||
with open(images_json_file,"rt",encoding="utf-8") as ifile:
|
||||
images_data = json.loads(ifile.read())
|
||||
|
||||
|
||||
for slug,_spec in images_data.items():
|
||||
spec = dict(_spec)
|
||||
image_basename = spec['image']
|
||||
image_filename = image_dir / spec["image"]
|
||||
spec['slug'] = slug
|
||||
del spec['image']
|
||||
|
||||
|
||||
img = Image(**spec)
|
||||
with open(image_filename,"rb") as ifile:
|
||||
img_file = ImageFile(ifile,image_basename)
|
||||
img.image.save(image_basename,img_file)
|
||||
img.save()
|
||||
|
||||
|
||||
|
||||
|
||||
def init_user_imges(apps,schema_edit)->None:
|
||||
#TODO
|
||||
pass
|
||||
|
||||
|
||||
def init_user_pages(apps,schema_edit)->None:
|
||||
#TODO
|
||||
pass
|
||||
@@ -128,4 +130,3 @@ class Migration(migrations.Migration):
|
||||
migrations.RunPython(init_builtin_images),
|
||||
migrations.RunPython(init_builtin_pages),
|
||||
]
|
||||
|
||||
Reference in New Issue
Block a user