summaryrefslogtreecommitdiff
path: root/app/posts/importer.py
blob: 7ed4782a8fe94d7bc33473787727a8d2d851b932 (plain)
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
for e in essaysold:
    if e.featured_image:
        feat = e.featured_image
    else:
        feat = None
    if e.meta_description:
        meta = e.meta_description
    else:
        meta = "need meta"
    new, created = Post.objects.get_or_create(
        old_id=e.pk,
        post_type=2,
        title=e.title,
        subtitle=e.sub_title,
        dek=e.dek,
        slug=e.slug,
        prologue_markdown=e.preamble,
        body_markdown=e.body_markdown,
        pub_date=e.pub_date,
        enable_comments=e.enable_comments,
        status=e.status,
        meta_description=meta,
        originally_published_by=e.originally_published_by,
        originally_published_by_url=e.originally_published_by_url,
        featured_image=feat,
        has_video=e.has_video,
        epilogue_markdown=e.afterword,
    )
    print(created)



# migrate jrnl to posts
for e in Entry.objects.all():
    if e.meta_description:
        meta_description = e.meta_description
    else:
        meta_description = "needs"
    if e.image:
        old_image = e.image
    else:
        old_image = None
    p, created = Post.objects.get_or_create(
        old_id=e.pk,
        title = e.title,
        short_title = '',
        subtitle = e.subtitle,
        slug = e.slug,
        body_markdown = e.body_markdown,
        body_html = e.body_html,
        dek = e.dek,
        meta_description = meta_description,
        pub_date = e.pub_date,
        enable_comments = e.enable_comments,
        status = e.status,
        featured_image = e.featured_image,
        post_type = PostType.JRNL,
        template_name = e.template_name,
        has_video = e.has_video,
        point = e.point,
        location = e.location,
        old_image=old_image
    )
    for b in e.books.all(): 
        c = Book.objects.get(
             slug=b.slug,
             title=b.title,
        ) 
        p.books.add(c)
    for f in e.field_notes.all(): 
        c = Post.objects.get(
             slug=f.slug,
             title=f.title,
        ) 
        p.field_notes.add(c)
    p.save()
    
#Then after they're all in there:
ctype = ContentType.objects.get(app_label='posts',model='post')
oldctype = ContentType.objects.get(app_label='jrnl',model='entry')
for e in Entry.objects.all():
    p = Post.objects.get(title=e.title,old_id=e.id)
    if e.related:
        for t in e.related.all(): 
            if t.model_name == oldctype:
                tp = ctype
            else:
                tp = t.model_name
            c = RelatedPost.objects.get(
                model_name=tp,
                title=t.title,
                slug=t.slug,
                pub_date=t.pub_date
            ) 
            p.related.add(c)
    p.save()

# Then to port comments:
ctype = ContentType.objects.get(app_label='posts',model='post')
oldctype = ContentType.objects.get(app_label='jrnl',model='entry')
for c in Comment.objects.filter(content_type=oldctype):
    e = Entry.objects.get(pk=c.object_pk)
    p = Post.objects.get(title=e.title,old_id=e.id)
    c.object_pk = p.pk
    c.content_type = ctype
    print("%s --> %s" %(c.content_object,p))
    c.save()