Commits

Artur Barseghyan  committed a86ce57 Draft

adding expiration interval feature; minor version update

  • Participants
  • Parent commits 417b6c9
  • Tags 0.6, dev

Comments (0)

Files changed (13)

File docs/conf.py

 # built documents.
 #
 # The short X.Y version.
-version = '0.5'
+version = '0.6'
 # The full version, including alpha/beta/rc tags.
-release = '0.5'
+release = '0.6'
 
 # The language for content autogenerated by Sphinx. Refer to documentation
 # for a list of supported languages.

File docs/index.rst

 >>> EXIMAGINATION_MEDIA_ROOT = PROJECT_DIR('media/external_images/')
 >>> EXIMAGINATION_MEDIA_URL = '/media/external_images'
 >>> EXIMAGINATION_MEDIA_RELATIVE_ROOT = 'external_images/'
->>> EXIMAGINATION_DEBUG = True
+>>> EXIMAGINATION_EXPIRATION_INTERVAL = 2592000 # After 30 days we re-fetch the file anyway.
 
 Usage example (in a Django template)
 =======================================

File example/example/local_settings.example

 EMAIL_FILE_PATH = PROJECT_DIR('../tmp')
 
 DEFAULT_FROM_EMAIL = '<no-reply@example.com>'
+
+# eximagination conf follows...
+
+# For testing purposes, files exlire after 10 seconds. Set something like 30 days (2592000) in production
+EXIMAGINATION_EXPIRATION_INTERVAL = 10
+
+# Make sure to set this to False in production.
+EXIMAGINATION_DEBUG = True

File example/example/local_settings.py

 EMAIL_FILE_PATH = PROJECT_DIR('../tmp')
 
 DEFAULT_FROM_EMAIL = '<no-reply@example.com>'
+
+# eximagination conf follows...
+
+# For testing purposes, files exlire after 10 seconds. Set something like 30 days (2592000) in production
+EXIMAGINATION_EXPIRATION_INTERVAL = 10
+
+# Make sure to set this to False in production.
+EXIMAGINATION_DEBUG = True

File example/example/settings.py

 EXIMAGINATION_MEDIA_ROOT = PROJECT_DIR('../media/external_images/')
 EXIMAGINATION_MEDIA_URL = '/media/external_images'
 EXIMAGINATION_MEDIA_RELATIVE_ROOT = 'external_images/'
-EXIMAGINATION_DEBUG = True
 
 # A sample logging configuration. The only tangible logging
 # performed by this configuration is to send an email to
 >>> EXIMAGINATION_MEDIA_ROOT = PROJECT_DIR('media/external_images/')
 >>> EXIMAGINATION_MEDIA_URL = '/media/external_images'
 >>> EXIMAGINATION_MEDIA_RELATIVE_ROOT = 'external_images/'
->>> EXIMAGINATION_DEBUG = True
+>>> EXIMAGINATION_EXPIRATION_INTERVAL = 2592000 # After 30 days we re-fetch the file anyway.
 
 Usage example (in a Django template)
 =======================================
 except:
     readme = ''
 
-version = '0.5'
+version = '0.6'
 
 setup(
     name = 'eximagination',

File src/eximagination/conf.py

 __title__ = 'eximagination.conf'
-__version__ = '0.5'
-__build__ = 0x000005
+__version__ = '0.6'
+__build__ = 0x000006
 __author__ = 'Artur Barseghyan <artur.barseghyan@gmail.com>'
 __all__ = ('get_setting',)
 

File src/eximagination/defaults.py

 __title__ = 'eximagination.defaults'
-__version__ = '0.5'
-__build__ = 0x000005
+__version__ = '0.6'
+__build__ = 0x000006
 __author__ = 'Artur Barseghyan <artur.barseghyan@gmail.com>'
-__all__ = ('MEDIA_ROOT', 'MEDIA_URL', 'MEDIA_RELATIVE_ROOT', 'DEBUG')
+__all__ = ('MEDIA_ROOT', 'MEDIA_URL', 'MEDIA_RELATIVE_ROOT', 'EXPIRATION_INTERVAL', 'DEBUG')
 
 import os
 PROJECT_DIR = lambda base : os.path.join(os.path.dirname(__file__), base).replace('\\','/')
 MEDIA_ROOT = PROJECT_DIR('media/external_images') # Where eximagination cached images will be stored
 MEDIA_URL = '/media/external_images/' # Media URL for stored images
 MEDIA_RELATIVE_ROOT = 'external_images/' # Relative root for images
+EXPIRATION_INTERVAL = 2592000 # After 30 days we re-fetch the file anyway.
 DEBUG = False

File src/eximagination/settings.py

 __title__ = 'eximagination.settings'
-__version__ = '0.5'
-__build__ = 0x000005
+__version__ = '0.6'
+__build__ = 0x000006
 __author__ = 'Artur Barseghyan <artur.barseghyan@gmail.com>'
-__all__ = ('MEDIA_ROOT', 'MEDIA_URL', 'MEDIA_RELATIVE_ROOT', 'DEBUG')
+__all__ = ('MEDIA_ROOT', 'MEDIA_URL', 'MEDIA_RELATIVE_ROOT', 'EXPIRATION_INTERVAL', 'DEBUG')
 
 from eximagination.conf import get_setting
 
 MEDIA_ROOT = get_setting('MEDIA_ROOT')
 MEDIA_URL = get_setting('MEDIA_URL')
 MEDIA_RELATIVE_ROOT = get_setting('MEDIA_RELATIVE_ROOT')
+EXPIRATION_INTERVAL = get_setting('EXPIRATION_INTERVAL')
 DEBUG = get_setting('DEBUG') # Set back to settings from DEBUG.

File src/eximagination/templatetags/eximaginate.py

 __title__ = 'eximagination.templatetags.eximaginate'
-__version__ = '0.5'
-__build__ = 0x000005
+__version__ = '0.6'
+__build__ = 0x000006
 __author__ = 'Artur Barseghyan <artur.barseghyan@gmail.com>'
 __all__ = ('eximaginate',)
 

File src/eximagination/utils.py

 __title__ = 'eximagination.utils'
-__version__ = '0.5'
-__build__ = 0x000005
+__version__ = '0.6'
+__build__ = 0x000006
 __author__ = 'Artur Barseghyan <artur.barseghyan@gmail.com>'
 __all__ = ('_obtain_image',)
 
 from PIL import Image
 import StringIO
 import glob
+import time
+
+from eximagination.settings import EXPIRATION_INTERVAL, DEBUG
 
 def _obtain_image(image_source='', save_to='', media_url='', force_update=False):
     """
     :return list: (relative_url_of_the_image, original_image_width, original_image_height)
     """
     # First we check if any image with such name (without extention) exists in our local external images directory.
+    expired = False
     try:
         filename = os.path.basename(glob.glob(os.path.join(save_to, hashlib.md5(image_source).hexdigest()) + '.*')[0])
-        im = Image.open(os.path.join(save_to, filename)) # Feed the image to PIL to get width and height
-        return (os.path.join(media_url, filename), im.size[0], im.size[1])
-    except:
+
+        # Expiration interval check.
+        full_path = os.path.join(save_to, filename)
+        last_modified = os.path.getmtime(full_path)
+        if time.time() - last_modified > EXPIRATION_INTERVAL:
+            expired = True
+
+        if not expired:
+            im = Image.open(full_path) # Feed the image to PIL to get width and height
+            return (os.path.join(media_url, filename), im.size[0], im.size[1])
+    except Exception, e:
         pass
 
-    # If nothing was found in local directory - we try to load it and save it.
+    # If nothing was found in local directory (or perhaps file expired) - we try to load it and save it.
     try:
         # Loading the file of unknown type into memory. We don't save the image locally before it's validated.
         opener = urllib2.build_opener()
 
         # If filename already exists - returning the filename
         filename_full_path = os.path.join(save_to, filename)
-        if os.path.exists(filename_full_path) and os.path.isfile(filename_full_path) and not force_update:
+        if os.path.exists(filename_full_path) and os.path.isfile(filename_full_path) and not (force_update or expired):
             return (os.path.join(media_url, filename), im.size[0], im.size[1])
 
         # Open file for binary write and save the image.
 
 Should haves
 --------------------------
-- Add redis for file caching and add a management command to update all existing files.
+- Add redis for caching of file lists (avoid call to file system). It should be possible to switch that feature
+  on and off.
+
+- Add a management command to update all existing files.
+
+- Add an expiration time (after which the file shall be downloaded again).
 
 Could haves
 --------------------------