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)