from models import *
from django.core.files import File
from glob import glob
import re

def importFile(filePath, project):
    f = File(open(filePath))
    pi = ProjectImage()
    pi.project = project
    pi.image = f
    pi.caption = ''
    pi.save()
    print "%s saved" % filePath    
    return


def doDir(dirpath, project, ext='JPG'):
    fileList = glob("%s/*.%s" % (dirpath, ext,))
    sortedList = sorted(fileList, key=lambda f: int(re.findall(r'\d+', f)[0])) #if there's numbers in the filename, use them as indexes to order by.
    print sortedList
    for f in sortedList:
        importFile(f, project)
    print "all done"
    return
            

