Esempio n. 1
0
    def save_product_photo(self, photo, main=False):
        if type(photo) is type(''): 
            return False

        file_content = photo.file.read()
        if len(file_content) == 0:
            return False

        filename, ext = os.path.splitext(photo.filename)
        filename = unicodedata.normalize('NFD', filename).encode('ascii', 'ignore')
        md5_ = md5(str(file_content)).hexdigest()
        
        new_filename = md5_ + '_' + filename.lower() + ext.lower()
        
        if not os.path.exists(Photo.BASE_STORAGE_PATH + new_filename[0]):
            os.makedirs(Photo.BASE_STORAGE_PATH + new_filename[0])
            
        new_filename_with_path = Photo.BASE_STORAGE_PATH + new_filename[0] + '/' + new_filename
        
        Photo.save_original(new_filename_with_path, file_content)
        
        img = Image.open(new_filename_with_path)
        #if img.mode != "RBG":
        #    img = img.convert("RGB")
            
        maps_image = Photo.resize(img, Photo.MAPS_THUMBNAIL, new_filename, 'maps_')
        tiny_image = Photo.resize(img, Photo.TINY_THUMBNAIL, new_filename, 'tiny_')
        small_image = Photo.resize(img, Photo.SMALL_THUMBNAIL, new_filename, 'small_')
        big_image = Photo.resize(img, Photo.BIG_THUMBNAIL, new_filename, 'big_')
        maxi_image = Photo.resize(img, Photo.MAXI_THUMBNAIL, new_filename, 'maxi_')

        photo = self.save_photo(new_filename_with_path, main=main)
        return photo
Esempio n. 2
0
    def save_event_photo(cls, event_id, photo):
        if type(photo) is type(''): 
            return False
            
        file_content = photo.file.read()
        if len(file_content) == 0: 
            return False
            
        filename, ext = os.path.splitext(photo.filename)
        filename = unicodedata.normalize('NFD', filename).encode('ascii', 'ignore')
        md5_ = md5(str(file_content)).hexdigest()
        
        new_filename = md5_ + '_' + filename.lower() + ext.lower()
        
        if not os.path.exists(Photo.BASE_STORAGE_PATH + new_filename[0]):
            os.makedirs(Photo.BASE_STORAGE_PATH + new_filename[0])
            
        new_filename_with_path = Photo.BASE_STORAGE_PATH + new_filename[0] + '/' + new_filename
        
        Photo.save_original(new_filename_with_path, file_content)
        
        img = Image.open(new_filename_with_path)
        if img.mode != "RBG":
            img = img.convert("RGB")
        
        tiny_image = Photo.resize(img, Photo.TINY_THUMBNAIL, new_filename, 'tiny_')
        small_image = Photo.resize(img, Photo.SMALL_THUMBNAIL, new_filename, 'small_')
        big_image = Photo.resize(img, Photo.BIG_THUMBNAIL, new_filename, 'big_')
        maxi_image = Photo.resize(img, Photo.MAXI_THUMBNAIL, new_filename, 'maxi_')

        photo = Photo()
        photo.filepath = new_filename_with_path
        photo.event_id = event_id
        photo.is_main = True
        DBSession.add(photo)
        DBSession.flush()
        return photo