# coding: utf8
from __future__ import unicode_literals
from backoffice.models import *
from fcm_django.models import FCMDevice
from fcm_django.fcm import FCMError
from PIL import Image
import os, sys
import logging
# Get an instance of a logger
logger = logging.getLogger('django')


def run():
    path = settings.MEDIA_ROOT + "media/"
    basewidth = 300
    for root, dirnames, filenames in os.walk(path):
        for filename in filenames:
    #for item in dirs:

            try:
                origine = os.path.join(root,filename)
                #logger.info("On veut ouvrir %s" % origine)
                im = Image.open(origine)
                width, height = im.size
                if width > 300:
                    try:
                        destination = os.path.join(root,filename)
                        logger.info("TO_RESIZE: on a %s et %d %d => %s", origine, width, height, destination)
                        wpercent = (basewidth / float(im.size[0]))
                        hsize = int((float(im.size[1]) * float(wpercent)))
                        im = im.resize((basewidth, hsize), Image.ANTIALIAS)
                        im.save(destination)
                        logger.info("Resize done et sauve %s" % destination)
                        im.close()
                    except Exception as e:
                        pass
            except IOError as e:
                    logger.error("Error resize %s" % e)
                    pass
