2025.09.17-19:07:57
This commit is contained in:
@@ -9,18 +9,90 @@ class Migration(migrations.Migration):
|
||||
|
||||
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)
|
||||
user = get_user_model().objects.create_user(**settings.TINYWIKI_USER_CONFIG)
|
||||
|
||||
def init_tinywiki_groups_and_permissions(apps,schema_editor):
|
||||
from ..models import Page
|
||||
from django.contrib.auth.models import Group,Permission
|
||||
from django.contrib.contenttypes.models import ContentType
|
||||
|
||||
PERMISSIONS = [
|
||||
'tinywiki-read-all',
|
||||
'tinywiki-delete',
|
||||
'tinywiki-create',
|
||||
'tinywiki-create-system',
|
||||
'tinywiki-edit',
|
||||
'tinywiki-edit-system',
|
||||
'tinywiki-edit-all',
|
||||
'tinywiki-delete-all',
|
||||
'tinywiki-delete-system',
|
||||
]
|
||||
|
||||
GROUPS = [
|
||||
('tinywiki-moderator',('tinywiki-read-all',
|
||||
'tinywiki-delete-all',
|
||||
'tinywiki-edit-all',
|
||||
'tinywiki-create')),
|
||||
('tinywiki-author',('tinywiki-create',
|
||||
'tinywiki-edit',
|
||||
'tinywiki-delete')),
|
||||
('tinywiki-reader',('tinywiki-read-all',)),
|
||||
('tinywiki-admin',('tinywiki-read-all',
|
||||
'tinywiki-create',
|
||||
'tinywiki-create-system',
|
||||
'tinywiki-delete-all',
|
||||
'tinywiki-delete-system',
|
||||
'tinywiki-edit-all',
|
||||
'tinywiki-edit-system'))
|
||||
]
|
||||
|
||||
perm_mapping = {}
|
||||
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])
|
||||
|
||||
|
||||
def init_default_pages(apps,schema_editor)->None:
|
||||
from ..models import Page,Image
|
||||
#TODO
|
||||
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_user_pages(apps,schema_edit)->None:
|
||||
from ..models import Page,Image
|
||||
#TODO
|
||||
|
||||
operations = [
|
||||
migrations.RunPython(init_tinywiki_groups_and_permissions),
|
||||
migrations.RunPython(init_tinywiki_user),
|
||||
migrations.RunPython(init_default_pages)
|
||||
migrations.RunPython(init_default_pages),
|
||||
]
|
||||
|
||||
Reference in New Issue
Block a user