Login

Simple Plone Migration

Author:
msm-art
Posted:
January 18, 2008
Language:
Python
Version:
.96
Tags:
migration import plone
Score:
2 (after 2 ratings)

This is a very simple script to do a simple migration from plone content to django.

ATNewsItems and PloneArticles are imported into the django model Article (with Foreignkey to the django models File and Image). ATDocuments are imported into the django model Page.

Usage

  1. Make sure that the Python version running Zope can import django
  2. The django database should be writeable from within the Zope environment
  3. Set the shell variable DJANGO_MODULE_SETTING to the settings file of the django project you want to import your Plone content to
  4. Start the Zope server in the debug modus:

    $ bin/zopectl debug

Then import the python module above, and pass the site you want to migrate to the start function:

import simple_migration
mysite = app.mysite
simple_migration.start(mysite)
Found 1253 objects
Saved Test Document Type: ATNewsItem
...

Hope it helps someone.

Used for the migration of Plone-2.5.3 content in a Python-2.4.4 environment.

  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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
from os import makedirs
from django.conf import settings
from django.contrib.auth.models import User
from django.template import defaultfilters

# The following models should be located in models.py of your django app
# In place of the code below import the models e.g. 
# from mydjangosite.app.models import Article, Page, File, Image, Tag

# Start

class Article(models.Model):
    objects = GroupManager()    
    title = models.CharField(max_length=150)
    slug = models.SlugField(max_length=150, prepopulate_from=('title',))
    STATE_CHOICES = (('D','Draft'),('P','Published'),('S','Private'))
    TYPE_CHOICES = (('N','News'),('C','CD feature'),('A','article'),('I','interviews'),('S','series'), ('B','blog'))
    state = models.CharField(max_length=1, choices=STATE_CHOICES, default='D')
    type = models.CharField(max_length=1, choices=TYPE_CHOICES, default='N')
    description = models.TextField(blank=True, null=True) 
    content = models.TextField()
    author = models.ForeignKey(User, default=2)
    creation_date = models.DateTimeField(blank=True)
    publication_date = models.DateTimeField(blank=True, null=True)
    modification_date = models.DateTimeField(blank=True)
    tags = models.ManyToManyField(Tag , filter_interface=models.HORIZONTAL, blank=True, null=True) 
    links = models.ManyToManyField('self', filter_interface=models.HORIZONTAL, blank=True, null=True) 

class File(models.Model):
    order = models.IntegerField(default=1)
    file = models.FileField(upload_to='%Y/%m/', blank=True, null=True)
    title = models.CharField(max_length=150, null=True, core=True)
    description = models.CharField(max_length=150, null=True, blank=True)
    FORMAT = (('audio/mpeg','audio/mpeg'),)
    format = models.CharField(max_length=150, choices=FORMAT, null=True, blank=True)
    article = models.ForeignKey('Article', edit_inline=True, num_in_admin=1)

class Image(models.Model):
    order = models.IntegerField(default=1)
    image = models.ImageField(upload_to='%Y/%m/',blank=True, null=True)
    title = models.CharField(max_length=150, null=True,core=True)
    description = models.CharField(max_length=150, blank=True, null=True)
    article = models.ForeignKey('Article', edit_inline=True, num_in_admin=1)

class Page(models.Model):
    url = models.CharField(_('URL'), max_length=150, validator_list=[validators.isAlphaNumericURL], help_text=_("Example: '/about/contact/'. Make sure to have leading and trailing slashes."))
    title = models.CharField(max_length=150)
    content = models.TextField()

class TagType(models.Model):
    type = models.CharField(maxlength=5

# End

media_root = settings.MEDIA_ROOT

def write_file(dir_name, file_name, plone_object):
    try:            
        makedirs(media_root + dir_name)
    except:
        # Directory already exists
        pass
    fd = open(media_root + dir_name + file_name, 'w')
    try:
        fd.write(plone_object.data)
    except:
        fd.write(plone_object.data.data)

def get_plonearticle_images(plone_object, django_object):

    p, d = plone_object, django_object

    p.REQUEST = 'FAKE' 

    images = p.getImageBrains()

    if len(images)>0:
        for x in images:
            image = x.getObject()
            i = Image()
            i.title = image.Title()
            i.description = image.Description()
            
            dir_name = p.created().strftime('%Y/%m/')
            file_name = d.slug + '-' + image.getId() + '.jpg'

            i.image = dir_name + file_name
            
            write_file(dir_name, file_name, image)
            i.article_id = d.id
            i.save()


def get_plonearticle_links(plone_object, django_object):

    p, d = plone_object, django_object
    for x in p.getLinks():
                l = Article.objects.filter(slug=x.getId())
                if l:
                    d.links.add(l[0])

def get_at_links(plone_object, django_object):

    p, d = plone_object, django_object
    for x in p.getRelatedItems():
            l = Article.objects.filter(slug=x.getId())
            if l:
                d.links.add(l[0])


def get_plonearticle_attchments(plone_object, django_object):

    p, d = plone_object, django_object
    for x in p.getAttachmentBrains():
        file = x.getObject()
        
        f = File()
        f.title = file.Title()
        f.description = file.Description()

        dir_name = p.created().strftime('%Y/%m/')

        if file.Format() == 'audio/mpeg':
            file_name = d.slug + '-' + file.getId() + '.mp3'
        else:
            file_name = d.slug + '-' + file.getId()

        f.file = dir_name + file_name
        f.format = file.Format()

        write_file(dir_name, file_name, file)
        f.article_id = d.id
        f.save()

def get_atnews_image(plone_object, django_object):

    p, d = plone_object, django_object
    image = p.getImage()
    if image:
        i = Image()
        i.title = image.Title()
        i.description = image.Description()
        
        dir_name = p.created().strftime('%Y/%m/')
        file_name = d.slug + '-' + image.getId() + '.jpg'

        i.image = dir_name + file_name
        
        write_file(dir_name, file_name, image)
        i.article_id = d.id
        i.save()


def get_dc_core(plone_object, django_object):
    """ Gets these DC-fields: Title, id, Description, Subject,
    Creation Date, Modification Date, Effective Date"""

    p, d = plone_object, django_object

    d.title = p.Title()
    d.slug = p.getId()

    d.creation_date = p.creation_date.ISO()
    d.modification_date = p.modification_date.ISO()
    if p.effective_date:
        d.publication_date = p.effective_date.ISO()
    
    t, s = User.objects.get_or_create(username=p.Creator())
    d.author_id = t.id 
    d.description = p.Description()
    d.save()

    for subject in p.Subject():
        slug = defaultfilters.slugify(subject)
        t, s = Tag.objects.get_or_create(name=subject, slug=slug)
        d.tags.add(t)

def get_items(r):
    for x in r:
        p = x.getObject()

        if p.meta_type == 'PloneArticle':
            a = Article()
            a.state = 'P'
            a.content = p.getText()
            get_dc_core(p, a)
            get_plonearticle_images(p, a)
            get_plonearticle_links(p, a)
            get_plonearticle_attchments(p, a)
            a.save() 
            print 'Saved', a.title

        if p.meta_type == 'ATNewsItem':
            a = Article()
            a.state = 'P'
            a.content = p.getText()
            get_dc_core(p, a)
            get_at_links(p, a)
            get_atnews_image(p, a)
            a.save() 
            print 'Saved', a.title, 'Type: ATNewsItem'

        if p.meta_type == 'ATDocument':
            d = Page()
            d.content = p.getText()
            d.title = p.Title()
            d.url = p.getId()
            d.save()
            print 'Saved', d.title, 'Type: ATDocument'

        if p.meta_type == 'Portal File':
            pass

def start(context):
    types = ['ATNewsItem','PloneArticle', 'ATDocument']
    r = context.portal_catalog.searchResults(meta_type={'query':types,'operator':'or'},sort_on='Date')
    print 'Found', len(r), ' objects'
    get_items(r)

More like this

  1. FieldsetForm by Ciantic 8 years, 1 month ago
  2. Effective content caching for mass-load site using redirect feature by nnseva 3 years, 10 months ago
  3. Transparently encrypt ORM fields using OpenSSL (via M2Crypto) by ncoghlan 3 years, 10 months ago
  4. Sorl Thumbnail + Amazon S3 by skoczen 5 years, 11 months ago
  5. RSS feed authentication by rileycrane 5 years, 10 months ago

Comments

Please login first before commenting.