def handle(self, *args, **options):
        print 'Import started...'
        if not 'path' in options:
            return
        path = options['path']
        archived_path = os.path.join(path, 'archive')
        if not os.path.exists(archived_path):
            os.mkdir(archived_path)

        if 'type' in options and options['type'] == 'ftp' and ftpclient:
            print 'Using remote options..'
            if 'ip' in options:
                host = options['ip']
            if 'user' in options:
                user = options['user']
            if 'pwd' in options:
                pwd = options['pwd']
            try:
                transport = ftpclient.Transport(host)
                transport.connect(username=user, password=pwd)
                ftp = ftpclient.SFTPClient.from_transport(transport)

                images = filter(is_image, ftp.listdir(path))
                local = appsettings.IMPORT_LOCAL_TMP_DIR + '%s'
                remote = path + '%s'
                for fi in images:
                    print 'Getting image: %s' % fi
                    try:
                        ftp.get(remote % fi, local % fi)
                    except Exception, e:
                        print 'Get Image Failed: %s' % e
                        continue

                local_images = [
                    os.path.join(appsettings.IMPORT_LOCAL_TMP_DIR, x)
                    for x in os.listdir(appsettings.IMPORT_LOCAL_TMP_DIR)
                    if is_image(x)
                ]
                for image in local_images:
                    print 'Procssing image: %s' % image
                    try:
                        img = Image.objects.create(
                            title=os.path.basename(image),
                            slug=sha_constructor(
                                image +
                                str(os.stat(image).st_size)).hexdigest())
                        img.file.save(os.path.basename(image),
                                      ContentFile(open(image, 'rb').read()))
                        transaction.commit()
                    except Exception, e:
                        transaction.rollback()
                        print 'Caught exception: %s' % e
                        continue

                ftp.close()
                transport.close()
 def handle(self, *args, **options):
     print 'Import started...'
     if not 'path' in options:
         return
     path = options['path']
     archived_path = os.path.join(path, 'archive')
     if not os.path.exists(archived_path):
         os.mkdir(archived_path)
     
     if 'type' in options and options['type'] == 'ftp' and ftpclient:
         print 'Using remote options..'
         if 'ip' in options:
             host = options['ip']
         if 'user' in options:
             user = options['user']
         if 'pwd' in options:
             pwd = options['pwd']
         try:
             transport = ftpclient.Transport(host)
             transport.connect(username=user, password=pwd)
             ftp = ftpclient.SFTPClient.from_transport(transport)
             
             images = filter(is_image, ftp.listdir(path))
             local = appsettings.IMPORT_LOCAL_TMP_DIR + '%s'
             remote = path + '%s'
             for fi in images:
                 print 'Getting image: %s' % fi
                 try:
                     ftp.get(remote % fi, local % fi)
                 except Exception, e:
                     print 'Get Image Failed: %s' % e
                     continue
                     
             local_images = [os.path.join(appsettings.IMPORT_LOCAL_TMP_DIR, x) for x in os.listdir(appsettings.IMPORT_LOCAL_TMP_DIR) if is_image(x)]
             for image in local_images:
                 print 'Procssing image: %s' % image
                 try:
                     img = Image.objects.create(title=os.path.basename(image),slug=sha_constructor(image+str(os.stat(image).st_size)).hexdigest())
                     img.file.save(os.path.basename(image),
                         ContentFile(open(image,'rb').read()))
                     transaction.commit()
                 except Exception, e:
                     transaction.rollback()
                     print 'Caught exception: %s' % e
                     continue
                               
             ftp.close()
             transport.close()
                transport.close()
            except Exception, e:
                transaction.rollback()
                print 'Caught exception: %s' % e
                try:
                    ftp.close()
                except:
                    pass
                try:
                    transport.close()
                except:
                    pass
        else:
            print 'Using local options...'
            images = [
                os.path.join(path, x) for x in os.listdir(path) if is_image(x)
            ]

            for image in images:
                print 'Processing image: %s' % image
                try:
                    try:
                        im = PilImage.open(open(image, 'rb'))
                        im.verify()
                    except Exception, e:
                        print 'Image open exception: %s' % e
                        try:
                            print 'Tring to Move image to archive...'
                            shutil.move(
                                image,
                                os.path.join(archived_path,
         ftp.close()
         transport.close()
     except Exception, e:
         transaction.rollback()
         print 'Caught exception: %s' % e
         try:
             ftp.close()
         except:
             pass
         try:
             transport.close()
         except:
             pass
 else:
     print 'Using local options...'
     images = [os.path.join(path, x) for x in os.listdir(path) if is_image(x)]
 
     for image in images:
         print 'Processing image: %s' % image
         try:
             try:
                 im = PilImage.open(open(image,'rb'))
                 im.verify()
             except Exception, e:
                 print 'Image open exception: %s' % e
                 try:
                     print 'Tring to Move image to archive...'
                     shutil.move(image, os.path.join(archived_path, os.path.basename(image)))
                     print 'Move Complete'
                 except Exception, e:
                     print 'Move exception: %s' % e