-
Notifications
You must be signed in to change notification settings - Fork 4
/
structure.py
39 lines (35 loc) · 1.1 KB
/
structure.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import re
import json
from django.template.defaultfilters import slugify
from feincms.module.page.models import Page
def import_structure(f, root_id=None):
"""docstring for import_structure"""
parents = {}
if root_id:
parents[0] = Page.objects.get(id=root_id)
else:
parents[0] = None
kwargs = {
'active': True,
'in_navigation': True,
}
for line in f:
match = re.match(r"^(#+) (\{[^)]*} )?(.*)$", unicode(line))
if match:
level = len(match.group(1))
opts = match.group(2)
if opts:
js = json.loads(opts)
kwargs.update(**dict([(str(k), v) for k, v in js.items()]))
title = match.group(3)
parent = parents[level-1]
parent_page = Page.objects.get(pk=parent.pk) if parent else None
p = Page(
title=title,
slug=slugify(title),
parent=parent_page,
**kwargs
)
p.save()
parents[level] = p
print('adding FeinCMS page: %s' % title)