@@ -0,0 +1,3232 @@
+%22%22%22 Loop through images in a directory and attempt to match them to a person.%22%22%22%0A%0Aimport re%0Aimport os%0A%0Afrom django.core.exceptions import ObjectDoesNotExist%0Afrom django.core.exceptions import MultipleObjectsReturned%0Afrom django.core.management.base import LabelCommand%0A%0Afrom django.core.files import File%0A%0Afrom django.utils.text import slugify%0A%0Afrom pombola.core.models import (%0A    Person, ContentType, Image%0A)%0A%0Afrom haystack.query import SearchQuerySet%0A%0A# Pretty colours to make it easier to spot things.%0ABRIGHT = '%5C033%5B95m'%0AENDC = '%5C033%5B0m'%0A%0Adef match_person(name):%0A    %22%22%22 Match up a person by name with their database entry. %22%22%22%0A%0A    slug = slugify(name)%0A%0A    # Try match on the name first%0A    try:%0A        person = Person.objects.get(legal_name__iexact=name)%0A    except ObjectDoesNotExist:%0A        try:%0A            person = Person.objects.get(slug=slug)%0A        except ObjectDoesNotExist:%0A            search = SearchQuerySet().models(Person).filter(text=name)%0A            if len(search) == 1 and search%5B0%5D:%0A                person = search%5B0%5D.object%0A            else:%0A                return None%0A%0A    except MultipleObjectsReturned:%0A        print 'Multiple people returned for ' + name + ' (' + slug + '). Cannot continue.'%0A        exit(1)%0A%0A    return person%0A%0Aclass Command(LabelCommand):%0A%0A    help = 'Imports scraped photos from the ZA parliament site, and matches them to people.'%0A%0A    def __init__(self, *args, **kwargs):%0A        super(Command, self).__init__(*args, **kwargs)%0A%0A        self.content_type_person = ContentType.objects.get_for_model(Person)%0A%0A    def handle_label(self, path, **options):%0A%0A        matched = 0%0A        unmatched = 0%0A%0A        for filename in os.listdir(path):%0A%0A            # Strip out the .jpg%0A            name = re.sub('%5C.jpg', '', filename)%0A%0A            # Strip any non-alpha trailing characters%0A            name = re.sub('%5B%5Ea-zA-Z%5D*$', '', name)%0A%0A            # Strip any more trailing whitespace that may have snuck in%0A            name = name.strip()%0A%0A            # Make the name unicode so we can actually work with it in the DB%0A            name = unicode(name)%0A%0A            # Slice the name into two%0A            name = name.split('_')%0A%0A            if len(name) == 2:%0A%0A                # Match up the person%0A                person = match_person(name%5B1%5D + ' ' + name%5B0%5D)%0A%0A                if person is None:%0A                    print BRIGHT + 'Unable to match %22' + filename + '%22 to a person!'+ ENDC%0A                    unmatched += 1%0A                else:%0A                    print 'Matched ' + person.name.encode('utf-8')%0A%0A                    Image.objects.create(%0A                        object_id=person.id,%0A                        content_type=self.content_type_person,%0A                        is_primary=True,%0A                        source='http://www.parliament.gov.za',%0A                        image=File(open(path + filename, 'r'))%0A                    )%0A%0A                    matched += 1%0A%0A            else:%0A%0A                # This name doesn't have two bits, complain.%0A                print BRIGHT + '%22' + filename + '%22 does not parse to a first and last name.'+ ENDC%0A                unmatched += 1%0A%0A        print 'Done! Matched ' + str(matched) + ', failed to match ' + str(unmatched)%0A

Add script to import a folder of named images and attach to people.