diff options
author | Evgeny Fadeev <evgeny.fadeev@gmail.com> | 2010-08-14 21:47:26 -0400 |
---|---|---|
committer | Evgeny Fadeev <evgeny.fadeev@gmail.com> | 2010-08-14 21:47:26 -0400 |
commit | 2f3f90f091212084cb14f57e5d6cea9db4e0a574 (patch) | |
tree | 8a0390d85719da0539f5af4c9d183ddbd8f08f64 | |
parent | e232a9cfcbdf3178a887aafec9c53f142e361dd6 (diff) | |
download | askbot-2f3f90f091212084cb14f57e5d6cea9db4e0a574.tar.gz askbot-2f3f90f091212084cb14f57e5d6cea9db4e0a574.tar.bz2 askbot-2f3f90f091212084cb14f57e5d6cea9db4e0a574.zip |
made logo uploadable
-rw-r--r-- | askbot/conf/skin_general_settings.py | 32 | ||||
-rw-r--r-- | askbot/deps/livesettings/forms.py | 9 | ||||
-rw-r--r-- | askbot/deps/livesettings/templates/livesettings/group_settings.html | 2 | ||||
-rw-r--r-- | askbot/deps/livesettings/values.py | 61 | ||||
-rw-r--r-- | askbot/deps/livesettings/views.py | 14 | ||||
-rw-r--r-- | askbot/deps/livesettings/widgets.py | 12 | ||||
-rw-r--r-- | askbot/setup_templates/settings.py | 2 | ||||
-rw-r--r-- | askbot/skins/default/templates/header.html | 2 | ||||
-rw-r--r-- | askbot/skins/utils.py | 24 | ||||
-rw-r--r-- | askbot/urls.py | 2 | ||||
-rw-r--r-- | setup.py | 2 |
11 files changed, 124 insertions, 38 deletions
diff --git a/askbot/conf/skin_general_settings.py b/askbot/conf/skin_general_settings.py index d71fe7fc..54775c48 100644 --- a/askbot/conf/skin_general_settings.py +++ b/askbot/conf/skin_general_settings.py @@ -4,8 +4,9 @@ General skin settings from askbot.conf.settings_wrapper import settings from askbot.deps.livesettings import ConfigurationGroup from askbot.deps.livesettings import StringValue, IntegerValue, BooleanValue -#from askbot.deps.livesettings import ImageValue +from askbot.deps.livesettings import ImageValue from django.utils.translation import ugettext as _ +from django.conf import settings as django_settings from askbot.skins import utils as skin_utils GENERAL_SKIN_SETTINGS = ConfigurationGroup( @@ -13,21 +14,20 @@ GENERAL_SKIN_SETTINGS = ConfigurationGroup( _('Skin and User Interface settings'), ) -#settings.register( -# ImageValue( -# GENERAL_SKIN_SETTINGS, -# 'SITE_LOGO', -# description = _('Q&A site logo'), -# help_text = _( -# 'To change logo, click "change button", ' -# 'select new logo file, then submit this ' -# 'whole form.' -# ), -# upload_directory = django_settings.ASKBOT_FILE_UPLOAD_DIR, -# default_image_url = skin_utils.get_media_url('logo.gif') -# ) -#) -print skin_utils.get_media_url('/images/logo.gif') +settings.register( + ImageValue( + GENERAL_SKIN_SETTINGS, + 'SITE_LOGO', + description = _('Q&A site logo'), + help_text = _( + 'To change the logo, select new file, ' + 'then submit this whole form.' + ), + upload_directory = django_settings.ASKBOT_FILE_UPLOAD_DIR, + upload_url = '/' + django_settings.ASKBOT_UPLOADED_FILES_URL, + default = skin_utils.get_media_url('/images/logo.gif') + ) +) settings.register( BooleanValue( diff --git a/askbot/deps/livesettings/forms.py b/askbot/deps/livesettings/forms.py index ed4a7e5a..c749dc0e 100644 --- a/askbot/deps/livesettings/forms.py +++ b/askbot/deps/livesettings/forms.py @@ -1,5 +1,5 @@ from django import forms -from askbot.deps.livesettings import * +from askbot.deps.livesettings import ConfigurationGroup import logging log = logging.getLogger('configuration') @@ -18,7 +18,7 @@ class SettingsEditor(forms.Form): flattened.append(s) else: flattened.append(setting) - + for setting in flattened: # Add the field to the customized field list kw = { @@ -28,11 +28,12 @@ class SettingsEditor(forms.Form): 'initial': setting.editor_value } field = setting.make_field(**kw) - + k = '%s__%s' % (setting.group.key, setting.key) + self.fields[k] = field if not setting.group in groups: groups.append(setting.group) #log.debug("Added field: %s = %s" % (k, str(field))) - self.groups = groups
\ No newline at end of file + self.groups = groups diff --git a/askbot/deps/livesettings/templates/livesettings/group_settings.html b/askbot/deps/livesettings/templates/livesettings/group_settings.html index e56f764f..7e00a600 100644 --- a/askbot/deps/livesettings/templates/livesettings/group_settings.html +++ b/askbot/deps/livesettings/templates/livesettings/group_settings.html @@ -23,7 +23,7 @@ </p> {% endif %} {% if form.fields %} -<form method="post"> +<form method="post" enctype="multipart/form-data"> <div class="module"> <table summary="{% filter capfirst %}{% blocktrans with group.name as name %}Settings included in {{ name }}.{% endblocktrans %}{% endfilter %}" width="100%"> {% for field in form %} diff --git a/askbot/deps/livesettings/values.py b/askbot/deps/livesettings/values.py index 0b6e1d20..1150d652 100644 --- a/askbot/deps/livesettings/values.py +++ b/askbot/deps/livesettings/values.py @@ -9,16 +9,20 @@ from django.utils import simplejson from django.utils.datastructures import SortedDict from django.utils.encoding import smart_str from django.utils.translation import gettext, ugettext_lazy as _ +from django.core.files import storage from askbot.deps.livesettings.models import find_setting, LongSetting, Setting, SettingNotSet from askbot.deps.livesettings.overrides import get_overrides from askbot.deps.livesettings.utils import load_module, is_string_like, is_list_or_tuple +from askbot.deps.livesettings.widgets import ImageInput import datetime import logging import signals +import tempfile +import os __all__ = ['BASE_GROUP', 'ConfigurationGroup', 'Value', 'BooleanValue', 'DecimalValue', 'DurationValue', 'FloatValue', 'IntegerValue', 'ModuleValue', 'PercentValue', 'PositiveIntegerValue', 'SortedDotDict', - 'StringValue', 'LongStringValue', 'MultipleStringValue'] + 'StringValue', 'ImageValue', 'LongStringValue', 'MultipleStringValue'] _WARN = {} @@ -561,13 +565,54 @@ class LongStringValue(Value): to_editor = to_python -#class ImageValue(Value): -# -# class field(dddd.forms.ImageField): -# def __init__(self, *args, **kwargs): -# kwargs['required'] = False -# kwargs['widget'] = ddd.forms.ImageInput() -# ddd.forms.ImageField.__init__(self, *args, **kwargs) +class ImageValue(StringValue): + + def __init__(self, *args, **kwargs): + self.upload_directory = kwargs['upload_directory'] + self.upload_url = kwargs['upload_url'] + del kwargs['upload_directory'] + del kwargs['upload_url'] + super(ImageValue, self).__init__(*args, **kwargs) + + class field(forms.FileField): + def __init__(self, *args, **kwargs): + kwargs['required'] = False + kwargs['widget'] = ImageInput + forms.FileField.__init__(self, *args, **kwargs) + + def clean(self, file_data, file_name): + (base_name, ext) = os.path.splitext(file_name) + image_extensions = ('.jpg', '.gif', '.png') + if ext.lower() not in image_extensions: + error_message = _('Allowed image file types are %(types)s') \ + % {'types': ', '.join(image_extensions)} + raise forms.ValidationError(error_message) + + def update(self, uploaded_file): + """uploaded_file is an instance of + django UploadedFile object + """ + #1) come up with a file name + + file_storage = storage.FileSystemStorage( + location = self.upload_directory, + base_url = self.upload_url + ) + + #todo: need better function here to calc name + file_name = file_storage.get_available_name(uploaded_file.name) + file_storage.save(file_name, uploaded_file) + url = file_storage.url(file_name) + + old_file = self.value + old_file = old_file.replace(self.upload_url, '', 1) + old_file_path = os.path.join(self.upload_directory, old_file) + if os.path.isfile(old_file_path): + os.unlink(old_file_path) + + #saved file path is relative to the upload_directory + #so that things could be easily relocated + super(ImageValue, self).update(url) class MultipleStringValue(Value): diff --git a/askbot/deps/livesettings/views.py b/askbot/deps/livesettings/views.py index bfc59d85..c6964743 100644 --- a/askbot/deps/livesettings/views.py +++ b/askbot/deps/livesettings/views.py @@ -5,6 +5,7 @@ from django.template import RequestContext from django.contrib.admin.views.decorators import staff_member_required from django.views.decorators.cache import never_cache from askbot.deps.livesettings import ConfigurationSettings, forms +from askbot.deps.livesettings import ImageValue from askbot.deps.livesettings.overrides import get_overrides import logging @@ -28,16 +29,23 @@ def group_settings(request, group, template='livesettings/group_settings.html'): if request.method == 'POST': # Populate the form with user-submitted data data = request.POST.copy() - form = forms.SettingsEditor(data, settings=settings) + form = forms.SettingsEditor(data, request.FILES, settings=settings) if form.is_valid(): form.full_clean() for name, value in form.cleaned_data.items(): group, key = name.split('__') cfg = mgr.get_config(group, key) - if cfg.update(value): + if isinstance(cfg, ImageValue): + if request.FILES: + value = request.FILES[name] + else: + continue + + if cfg.update(value): # Give user feedback as to which settings were changed - request.user.message_set.create(message='Updated %s on %s' % (cfg.key, cfg.group.key)) + message='Updated %s on %s' % (cfg.key, cfg.group.key) + request.user.message_set.create(message = message) return HttpResponseRedirect(request.path) else: diff --git a/askbot/deps/livesettings/widgets.py b/askbot/deps/livesettings/widgets.py new file mode 100644 index 00000000..36b78e19 --- /dev/null +++ b/askbot/deps/livesettings/widgets.py @@ -0,0 +1,12 @@ +from django import forms +from django.utils import safestring + +class ImageInput(forms.FileInput): + + def render(self, name, value, attrs = None): + output = '<img ' + if attrs and 'image_class' in attrs: + output += 'class="%s" ' % attrs['image_class'] + output += 'src="%s"/><br/>' % value + output += super(ImageInput, self).render(name, value, attrs) + return safestring.mark_safe(output) diff --git a/askbot/setup_templates/settings.py b/askbot/setup_templates/settings.py index b1dec062..23e110ba 100644 --- a/askbot/setup_templates/settings.py +++ b/askbot/setup_templates/settings.py @@ -196,3 +196,5 @@ logging.basicConfig( ASKBOT_URL = '' #no leading slash, default = '' empty string _ = lambda v:v #fake translation function for the login url LOGIN_URL = '/%s%s%s' % (ASKBOT_URL,_('account/'),_('signin/')) +#note - it is important that upload dir url is NOT translated!!! +ASKBOT_UPLOADED_FILES_URL = '/%s%s' % (ASKBOT_URL, 'upfiles/') diff --git a/askbot/skins/default/templates/header.html b/askbot/skins/default/templates/header.html index 6a8068f2..fc166fd2 100644 --- a/askbot/skins/default/templates/header.html +++ b/askbot/skins/default/templates/header.html @@ -34,7 +34,7 @@ <td id="logoContainer"> <div id="logo"> <a href="{% url questions %}?start_over=true"><img - src="{% media "/images/logo.gif" %}" title="{% trans "back to home page" %}" alt="{{settings.APP_TITLE}} logo"/></a> + src="{% media settings.SITE_LOGO %}" title="{% trans "back to home page" %}" alt="{{settings.APP_TITLE}} logo"/></a> </div> </td> <td id="navTabContainer" valign="bottom" align="left"> diff --git a/askbot/skins/utils.py b/askbot/skins/utils.py index cef22f77..3a03e4e3 100644 --- a/askbot/skins/utils.py +++ b/askbot/skins/utils.py @@ -39,7 +39,24 @@ def get_media_url(url): """ while url[0] == '/': url = url[1:] #todo: handles case of multiple skin directories - skins = get_skin_dirs()[0] + + #if file is in upfiles directory, then give that + url_copy = url + if url_copy.startswith(django_settings.ASKBOT_UPLOADED_FILES_URL): + file_path = url_copy.replace( + django_settings.ASKBOT_UPLOADED_FILES_URL, + '', + 1 + ) + file_path = os.path.join( + django_settings.ASKBOT_FILE_UPLOAD_DIR, + file_path + ) + if os.path.isfile(file_path): + url_copy = os.path.normpath('///' + url_copy) + return url_copy + + #2) if it does not exist - look in skins #purpose of this try statement is to determine #which skin is currently used @@ -52,7 +69,9 @@ def get_media_url(url): resource_revision = askbot_settings.MEDIA_RESOURCE_REVISION except ImportError: use_skin = 'default' - resouce_revision = None + resource_revision = None + + skins = get_skin_dirs()[0] #see if file exists, if not, try skins 'default', then 'common' file_path = os.path.join(skins, use_skin, 'media', url) @@ -77,4 +96,3 @@ def get_media_url(url): url += '?v=%d' % resource_revision return url - diff --git a/askbot/urls.py b/askbot/urls.py index 974cbf26..3448478f 100644 --- a/askbot/urls.py +++ b/askbot/urls.py @@ -35,7 +35,7 @@ urlpatterns = patterns('', name='askbot_media', ), url( - r'^%s(?P<path>.*)$' % _('upfiles/'), + r'^%s(?P<path>.*)$' % settings.ASKBOT_UPLOADED_FILES_URL, 'django.views.static.serve', {'document_root': os.path.join(settings.PROJECT_ROOT, 'askbot', 'upfiles').replace('\\','/')}, name='uploaded_file', @@ -20,7 +20,7 @@ if sys.platform not in WIN_PLATFORMS: setup( name = "askbot", - version = "0.6.6", + version = "0.6.7", description = 'Question and Answer forum, like StackOverflow, written in python and Django', packages = find_packages(), author = 'Evgeny.Fadeev', |