Created
November 27, 2011 14:26
-
-
Save rmoch/1397622 to your computer and use it in GitHub Desktop.
Create embeded CMS Picture plugins from images hotlinked in TextPlugins.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import hashlib | |
import os | |
import urllib2 | |
from optparse import make_option | |
from BeautifulSoup import BeautifulSoup as bf | |
from django.conf import settings | |
from django.core.management import base | |
from cms import models | |
from custom_cms.cms_api import add_plugin | |
class Command(base.BaseCommand): | |
def get_image(self, img, page): | |
self.stdout.write(u'Found %s in page #%d\n' % (img['src'], page.id)) | |
image = urllib2.urlopen(img['src']) | |
basename = os.path.basename(img['src']) | |
if basename > 50: # shorten long filenames | |
filename, ext = os.path.splitext(basename) | |
basename = hashlib.sha1(basename).hexdigest()[:16] + ext | |
image_name = '%s/%d/%s' % ('cms_page_media', page.id, basename) | |
image_src = '/media/' + image_name | |
path = settings.PROJECT_PATH + image_src | |
if not os.path.exists(os.path.split(path)[0]): # create folder | |
print 'created folder ' + os.path.split(path)[0] | |
os.makedirs(os.path.split(path)[0]) | |
output = open(path, 'wb') | |
output.write(image.read()) | |
output.close() | |
return image_name, image_src, path | |
help = 'Create CMS image plugins from images hotlinked in TextPlugins.' | |
option_list = base.NoArgsCommand.option_list + ( | |
make_option('-g', '--go', action='store_true', dest='go', | |
default=False, help='Actually create plugins'), | |
make_option('-p', '--page', action='store', dest='page', | |
default=False, help='Work on a specific page'), | |
) | |
def handle(self, *args, **options): | |
if options['page']: | |
pages = [models.Page.objects.get(id=options['page'])] | |
else: | |
pages = models.Page.objects.all() | |
for page in pages: | |
for placeholder in page.placeholders.all(): | |
for plugin in placeholder.get_plugins(): | |
if plugin.plugin_type == 'TextPlugin': | |
soup = bf(plugin.text.body) | |
for img in soup.findAll('img'): | |
# if img does not have id attribute, hasattr() says yes, but acces raise KeyError | |
image_src = None | |
try: | |
if not img['id'].startswith('plugin_obj_'): | |
image_name, image_src, path = self.get_image(img, page) | |
except KeyError: | |
image_name, image_src, path = self.get_image(img, page) | |
if image_src and options['go']: | |
picture = add_plugin(placeholder, | |
'PicturePlugin', | |
'fr', | |
image=image_name, | |
longdesc='', | |
) | |
picture.parent=plugin # parent have to be saved after... | |
picture.save() | |
self.stdout.write(u'Created plugin #%d, parent #%d for page #%d\n' % (picture.id, plugin.id, page.id)) | |
title = 'Image - %s' % os.path.split(path)[1] | |
new_img = '<img id="plugin_obj_%d" title="%s" src="/media/cms/images/plugins/image.png" alt="%s" />' % ( | |
picture.id, | |
title, title | |
) | |
plugin.text.body = plugin.text.body.replace(str(img), new_img) | |
plugin.text.save() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment