Skip to content

Instantly share code, notes, and snippets.

@jaymzcd
Created July 13, 2011 11:23
Show Gist options
  • Save jaymzcd/1080127 to your computer and use it in GitHub Desktop.
Save jaymzcd/1080127 to your computer and use it in GitHub Desktop.
Assign slugs and images based on a models fields - commands to use with a django shell
def assign_file_to_model(module, model_class=None, file_field='main_image', source_field='body', skip_existing=True, verbose=False):
""" We tend to have "main image" fields on models that hold a primary image used
across the sites for things like list pages and headers. Often on import of
existing data we have images mangled within copy or lists. """
import urllib2
from posixpath import basename
from BeautifulSoup import BeautifulSoup as Soup
from django.db import IntegrityError
from django.core.files import File
if model_class is None:
model_class = module.capitalize()
mod = __import__(module)
model= getattr(getattr(mod, 'models'), model_class)
items = model.objects.all()
for item in items:
if getattr(item, file_field) and skip_existing:
if verbose:
print "Skipped %s" % item
continue
img_src = Soup(getattr(item, source_field)).find('img')['src']
file_path = '/tmp/%s' % basename(img_src)
with open(file_path, 'w') as fd:
fd.write(urllib2.urlopen(img_src).read())
try:
f = File(open(file_path))
item.main_image = f
item.save()
except IntegrityError:
if verbose:
print "Error settings for %s" % item
else:
pass
if verbose:
print "Downloaded to %s and assigned for %s" % (file_path, item)
if verbose:
print "Completed all %d %s items" % (items.count(), model.__name__)
def slug_model(module, model_class=None, slug_field='slug', source_field='name', skip_existing=True, verbose=False):
""" Loops over a given model assigning a slug field to it """
import sys
from django.template.defaultfilters import slugify
from django.db import IntegrityError
from datetime import datetime
from hashlib import md5
if model_class is None:
model_class = module.capitalize()
mod = __import__(module)
model= getattr(getattr(mod, 'models'), model_class)
items = model.objects.all()
for item in items:
if (getattr(item, slug_field) is not None) and skip_existing:
if verbose:
print "Skipped %s" % getattr(item, source_field)
continue
slugged = slugify(getattr(item, source_field))
try:
setattr(item, slug_field, slugged)
item.save()
except IntegrityError:
hashed = md5('%s%s' % (slugged, datetime.now()))
setattr(item, slug_field, '%s-%s' % (slugged, hashed))
item.save()
if verbose:
print "Set slug for %s" % (getattr(item, source_field))
if verbose:
print "Completed all %d %s items" % (items.count(), model.__name__)
@jaymzcd
Copy link
Author

jaymzcd commented Jul 13, 2011

I use this within iPython when working on importing data to a django table which may not have a slug. In case a generated slug already exists rather we concatenate the md5 hash with a random digit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment