diff options
author | Adolfo Fitoria <adolfo.fitoria@gmail.com> | 2011-09-01 10:09:16 -0300 |
---|---|---|
committer | Adolfo Fitoria <adolfo.fitoria@gmail.com> | 2011-09-01 10:09:16 -0300 |
commit | 5b66d571ca690033948fc226ac706a170d038d0f (patch) | |
tree | 35a9e481d641d3d7daf38c5fb24e64d5218a0a48 | |
parent | 8877c0fc9cfbb285bae5bccbd6e8ac0b43610fea (diff) | |
parent | d7e5cf7d7d522bc92b6d80487b2cb46e2624e4a3 (diff) | |
download | askbot-5b66d571ca690033948fc226ac706a170d038d0f.tar.gz askbot-5b66d571ca690033948fc226ac706a170d038d0f.tar.bz2 askbot-5b66d571ca690033948fc226ac706a170d038d0f.zip |
Merge branch 'master' of github.com:ASKBOT/askbot-devel
-rw-r--r-- | askbot/conf/skin_general_settings.py | 42 | ||||
-rw-r--r-- | askbot/doc/source/changelog.rst | 2 | ||||
-rw-r--r-- | askbot/doc/source/customizing-skin-in-askbot.rst | 2 | ||||
-rw-r--r-- | askbot/skins/default/templates/macros.html | 2 | ||||
-rw-r--r-- | askbot/skins/utils.py | 33 | ||||
-rw-r--r-- | askbot/startup_procedures.py | 25 | ||||
-rw-r--r-- | askbot/urls.py | 3 | ||||
-rw-r--r-- | askbot/utils/hasher.py | 71 |
8 files changed, 122 insertions, 58 deletions
diff --git a/askbot/conf/skin_general_settings.py b/askbot/conf/skin_general_settings.py index b90d3de5..8521bf87 100644 --- a/askbot/conf/skin_general_settings.py +++ b/askbot/conf/skin_general_settings.py @@ -106,20 +106,7 @@ settings.register( ) ) -settings.register( - values.IntegerValue( - GENERAL_SKIN_SETTINGS, - 'MEDIA_RESOURCE_REVISION', - default = 1, - description = _('Skin media revision number'), - help_text = _( - 'Increment this number when you change ' - 'image in skin media or stylesheet. ' - 'This helps avoid showing your users ' - 'outdated images from their browser cache.' - ) - ) -) + settings.register( values.BooleanValue( @@ -271,3 +258,30 @@ settings.register( ) ) ) + +settings.register( + values.IntegerValue( + GENERAL_SKIN_SETTINGS, + 'MEDIA_RESOURCE_REVISION', + default = 1, + description = _('Skin media revision number'), + help_text = _( + 'Will be set automatically ' + 'but you can modify it if necessary.' + ) + ) +) + +settings.register( + values.StringValue( + GENERAL_SKIN_SETTINGS, + 'MEDIA_RESOURCE_REVISION_HASH', + description = _( + 'Hash to update the media revision number automatically.' + ), + default='', + help_text = _( + 'Will be set automatically, it is not necesary to modify manually.' + ) + ) +) diff --git a/askbot/doc/source/changelog.rst b/askbot/doc/source/changelog.rst index b1c4c1f8..8ca1614a 100644 --- a/askbot/doc/source/changelog.rst +++ b/askbot/doc/source/changelog.rst @@ -3,6 +3,8 @@ Changes in Askbot Development version (not yet on pypi) ------------------------------------- +* Media resource revision is now incremented + automatically any time when media is updated (Adolfo Fitoria, Evgeny Fadeev) * First user automatically becomes site administrator (Adolfo Fitoria) * Avatar displayed on the sidebar can be controlled with livesettings.(Adolfo Fitoria, Evgeny Fadeev) * Avatar box in the sidebar is ordered with priority for real faces.(Adolfo Fitoria) diff --git a/askbot/doc/source/customizing-skin-in-askbot.rst b/askbot/doc/source/customizing-skin-in-askbot.rst index 96c2ec9c..8be21ebd 100644 --- a/askbot/doc/source/customizing-skin-in-askbot.rst +++ b/askbot/doc/source/customizing-skin-in-askbot.rst @@ -56,7 +56,7 @@ Possible approaches to customize skins There are several methods at your disposal, would you like to customize askbot's appearance. -.. note:: +.. deprecated:: 0.7.21 Whenever you change any media files on disk, it will be necessary to increment "skin media revision number" in the skin settings and restart the app, diff --git a/askbot/skins/default/templates/macros.html b/askbot/skins/default/templates/macros.html index 50e768ce..36ab9692 100644 --- a/askbot/skins/default/templates/macros.html +++ b/askbot/skins/default/templates/macros.html @@ -22,7 +22,7 @@ {%- macro share(site = None, site_label = None, icon = False) -%} <a class="{{ site }}-share{% if icon == True %} icon{% endif %}" - alt="{% trans %}Share this question on {{site}}{% endtrans %}" + title="{% trans %}Share this question on {{site}}{% endtrans %}" >{% if icon == False %}{% if site_label %}{{ site_label }}{% else %}{{ site }}{% endif %}{% endif %}</a> {%- endmacro -%} diff --git a/askbot/skins/utils.py b/askbot/skins/utils.py index 7eaeb304..57acd8ae 100644 --- a/askbot/skins/utils.py +++ b/askbot/skins/utils.py @@ -10,6 +10,7 @@ import logging import urllib from django.conf import settings as django_settings from django.utils.datastructures import SortedDict +from askbot.utils import hasher class MediaNotFound(Exception): """raised when media file is not found""" @@ -140,7 +141,7 @@ def get_media_url(url, ignore_missing = False): #determine from which skin take the media file try: use_skin = resolve_skin_for_media(media=url, preferred_skin = use_skin) - except MediaNotFound, e: + except MediaNotFound: log_message = 'missing media resource %s in skin %s' \ % (url, use_skin) logging.critical(log_message) @@ -160,3 +161,33 @@ def get_media_url(url, ignore_missing = False): #after = datetime.datetime.now() #print after - before return url + +def update_media_revision(skin = None): + """update skin media revision number based on the contents + of the skin media directory""" + from askbot.conf import settings as askbot_settings + resource_revision = askbot_settings.MEDIA_RESOURCE_REVISION + + if skin: + if skin in get_skin_choices(): + skin_path = get_path_to_skin(skin) + else: + raise MediaNotFound('Skin %s not found' % skin) + else: + skin = 'default' + skin_path = get_path_to_skin(askbot_settings.ASKBOT_DEFAULT_SKIN) + + media_dirs = [os.path.join(skin_path, 'media'),] + + if skin != 'default': + #we have default skin as parent of the custom skin + default_skin_path = get_path_to_skin('default') + media_dirs.append(os.path.join(default_skin_path, 'media')) + + current_hash = hasher.get_hash_of_dirs(media_dirs) + + if current_hash != askbot_settings.MEDIA_RESOURCE_REVISION_HASH: + askbot_settings.update('MEDIA_RESOURCE_REVISION', resource_revision + 1) + askbot_settings.update('MEDIA_RESOURCE_REVISION_HASH', current_hash) + logging.debug('MEDIA_RESOURCE_REVISION changed') + askbot_settings.MEDIA_RESOURCE_REVISION diff --git a/askbot/startup_procedures.py b/askbot/startup_procedures.py index bb269600..4fb41eb6 100644 --- a/askbot/startup_procedures.py +++ b/askbot/startup_procedures.py @@ -22,6 +22,7 @@ PREAMBLE = """\n ************************""" def askbot_warning(line): + """prints a warning with the nice header, but does not quit""" print >> sys.stderr, PREAMBLE + '\n' + line def format_as_text_tuple_entries(items): @@ -81,7 +82,9 @@ def test_middleware(): 'askbot.middleware.view_log.ViewLogMiddleware', ) if 'debug_toolbar' in django_settings.INSTALLED_APPS: - required_middleware += ('debug_toolbar.middleware.DebugToolbarMiddleware',) + required_middleware += ( + 'debug_toolbar.middleware.DebugToolbarMiddleware', + ) installed_middleware_set = set(django_settings.MIDDLEWARE_CLASSES) missing_middleware_set = set(required_middleware) - installed_middleware_set @@ -101,7 +104,8 @@ https://github.com/ASKBOT/askbot-devel/blob/master/askbot/setup_templates/settin ) #'debug_toolbar.middleware.DebugToolbarMiddleware', - remove_middleware_set = set(canceled_middleware) & installed_middleware_set + remove_middleware_set = set(canceled_middleware) \ + & installed_middleware_set if remove_middleware_set: error_message = """\n\nPlease remove the following middleware entries from the list of MIDDLEWARE_CLASSES in your settings.py - these are not used any more:\n\n""" @@ -111,6 +115,7 @@ the list of MIDDLEWARE_CLASSES in your settings.py - these are not used any more def test_i18n(): + """askbot requires use of USE_I18N setting""" if getattr(django_settings, 'USE_I18N', False) == False: raise ImproperlyConfigured( 'Please set USE_I18N = True in settings.py and ' @@ -119,26 +124,31 @@ def test_i18n(): ) def try_import(module_name, pypi_package_name): + """tries importing a module and advises to install + A corresponding Python package in the case import fails""" try: load_module(module_name) - except ImportError, e: - message = unicode(e) + ' run\npip install %s' % pypi_package_name + except ImportError, error: + message = unicode(error) + ' run\npip install %s' % pypi_package_name message += '\nTo install all the dependencies at once, type:' message += '\npip install -r askbot_requirements.txt\n' raise ImproperlyConfigured(message) def test_modules(): + """tests presence of required modules""" try_import('recaptcha_works', 'django-recaptcha-works') def test_postgres(): - '''Validates postgres buggy driver 2.4.2''' + """Checks for the postgres buggy driver, version 2.4.2""" if hasattr(django_settings, 'DATABASE_ENGINE'): if django_settings.DATABASE_ENGINE in ('postgresql_psycopg2',): try: import psycopg2 version = psycopg2.__version__.split(' ')[0].split('.') if version == ['2', '4', '2']: - raise ImproperlyConfigured('Please install psycopg2 version 2.4.1,\n version 2.4.2 has a bug') + raise ImproperlyConfigured( + 'Please install psycopg2 version 2.4.1,\n version 2.4.2 has a bug' + ) elif version > ['2', '4', '2']: pass #don't know what to do else: @@ -181,5 +191,6 @@ def run(): try: badges.init_badges() transaction.commit() - except: + except Exception, error: + print error transaction.rollback() diff --git a/askbot/urls.py b/askbot/urls.py index 6910cb17..54c50d96 100644 --- a/askbot/urls.py +++ b/askbot/urls.py @@ -10,8 +10,11 @@ from django.utils.translation import ugettext as _ from askbot import views from askbot.feed import RssLastestQuestionsFeed from askbot.sitemap import QuestionsSitemap +from askbot.skins.utils import update_media_revision admin.autodiscover() +update_media_revision()#needs to be run once, so put it here + feeds = { 'rss': RssLastestQuestionsFeed } diff --git a/askbot/utils/hasher.py b/askbot/utils/hasher.py index 4c68ed79..5a73213d 100644 --- a/askbot/utils/hasher.py +++ b/askbot/utils/hasher.py @@ -1,40 +1,43 @@ +"""hasher function that will calculate sha1 hash +directory contents +""" import hashlib, os -from askbot.conf import settings as askbot_settings -use_skin = askbot_settings.ASKBOT_DEFAULT_SKIN -resource_revision = askbot_settings.MEDIA_RESOURCE_REVISION +import logging -def GetHashofDirs(directory, verbose=0): - SHAhash = hashlib.sha1() - if not os.path.exists (directory): - return -1 - - try: - for root, dirs, files in os.walk(directory): - for names in files: - filepath = os.path.join(root,names) +def get_hash_of_dirs(dirs): + """Hasher function for a directory and its files""" + sha_hash = hashlib.sha1() + for directory in dirs: + if not os.path.exists (directory): + return -1 + try: - f1 = open(filepath, 'rb') - except: - # You can't open the file for some reason - f1.close() - continue + for root, dirs, files in os.walk(directory): + for names in files: + filepath = os.path.join(root, names) + try: + file_obj = open(filepath, 'rb') + except Exception, error: + # You can't open the file for some reason + logging.critical( + 'cannot open file %s: %s', + filepath, + error + ) + file_obj.close() + continue - while 1: - # Read file in as little chunks - buf = f1.read(4096) - if not buf : break - SHAhash.update(hashlib.sha1(buf).hexdigest()) - f1.close() + while 1: + # Read file in as little chunks + buf = file_obj.read(4096) + if not buf : break + sha_hash.update(hashlib.sha1(buf).hexdigest()) + file_obj.close() - except: - import traceback - # Print the stack traceback - traceback.print_exc() - return -2 + except Exception: + import traceback + # Print the stack traceback + traceback.print_exc() + return -2 - return SHAhash.hexdigest() - -if __name__ == '__main__': - #directory = raw_input('directory:') - #print GetHashofDirs(directory, 0) - print GetHashofDirs('skins/default/media', 0) + return sha_hash.hexdigest() |