From 01fed0849e47c9ab181963b94b76df1b84a5232b Mon Sep 17 00:00:00 2001 From: hernani Date: Fri, 9 Jul 2010 11:27:33 +0000 Subject: [PATCH] Adds options to control the behaviour of urls. git-svn-id: http://svn.osqa.net/svnroot/osqa/trunk@511 0cfe37f9-358a-4d5e-be75-b63607b5c754 --- forum/models/__init__.py | 1 + forum/settings/__init__.py | 1 + forum/settings/urls.py | 15 +++++++++++++ .../templates/osqaadmin/djstyle_base.html | 1 + forum/urls.py | 4 ++-- forum/utils/djangofix.py | 21 +++++++++++++++++++ forum/views/admin.py | 2 +- forum/views/readers.py | 2 +- 8 files changed, 43 insertions(+), 4 deletions(-) create mode 100644 forum/settings/urls.py create mode 100644 forum/utils/djangofix.py diff --git a/forum/models/__init__.py b/forum/models/__init__.py index 7a15132..2449289 100644 --- a/forum/models/__init__.py +++ b/forum/models/__init__.py @@ -1,3 +1,4 @@ +import forum.utils.djangofix from question import Question ,QuestionRevision, QuestionSubscription from answer import Answer, AnswerRevision from tag import Tag, MarkedTag diff --git a/forum/settings/__init__.py b/forum/settings/__init__.py index b7b4aac..6d5b03f 100644 --- a/forum/settings/__init__.py +++ b/forum/settings/__init__.py @@ -36,6 +36,7 @@ from view import * from moderation import * from users import * from static import * +from urls import * BADGES_SET = SettingSet('badges', _('Badges config'), _("Configure badges on your OSQA site."), 500) diff --git a/forum/settings/urls.py b/forum/settings/urls.py new file mode 100644 index 0000000..e532c55 --- /dev/null +++ b/forum/settings/urls.py @@ -0,0 +1,15 @@ +from base import Setting, SettingSet +from django.utils.translation import ugettext as _ + +URLS_SET = SettingSet('urls', _('URL settings'), _("Some settings to tweak behaviour of site urls (experimental).")) + +ALLOW_UNICODE_IN_SLUGS = Setting('ALLOW_UNICODE_IN_SLUGS', False, URLS_SET, dict( +label = _("Allow unicode in slugs"), +help_text = _("Allow unicode/non-latin characters in urls."), +required=False)) + +FORCE_SINGLE_URL = Setting('FORCE_SINGLE_URL', True, URLS_SET, dict( +label = _("Force single url"), +help_text = _("Redirect the request in case there is a mismatch between the slug in the url and the actual slug"), +required=False)) + diff --git a/forum/skins/default/templates/osqaadmin/djstyle_base.html b/forum/skins/default/templates/osqaadmin/djstyle_base.html index 02443a7..d976268 100644 --- a/forum/skins/default/templates/osqaadmin/djstyle_base.html +++ b/forum/skins/default/templates/osqaadmin/djstyle_base.html @@ -68,6 +68,7 @@
  • {{ allsets.users.title }}
  • {{ allsets.email.title }}
  • {{ allsets.paths.title }}
  • +
  • {{ allsets.urls.title }}
  • {{ allsets.extkeys.title }}
  • diff --git a/forum/urls.py b/forum/urls.py index 4598ddc..faea61a 100644 --- a/forum/urls.py +++ b/forum/urls.py @@ -94,8 +94,8 @@ urlpatterns += patterns('', url(r'^%s(?P\d+)/(?P[\w-]*)$' % _('question/'), 'django.views.generic.simple.redirect_to', {'url': '/questions/%(id)s/%(slug)s'}), url(r'^%s(?P\d+)/?$' % _('questions/'), app.readers.question, name='question'), - url(r'^%s(?P\d+)/(?P[\w-]*)$' % _('questions/'), app.readers.question, name='question'), - url(r'^%s(?P\d+)/(?P[\w-]*)/(?P\d+)$' % _('questions/'), app.readers.question), + url(r'^%s(?P\d+)/(?P.*)$' % _('questions/'), app.readers.question, name='question'), + url(r'^%s(?P\d+)/(?P.*)/(?P\d+)$' % _('questions/'), app.readers.question), url(r'^%s$' % _('tags/'), app.readers.tags, name='tags'), diff --git a/forum/utils/djangofix.py b/forum/utils/djangofix.py new file mode 100644 index 0000000..d74e48d --- /dev/null +++ b/forum/utils/djangofix.py @@ -0,0 +1,21 @@ +import re +import urllib +from forum.modules import decorate + +from django.template.defaultfilters import slugify +from django.utils.safestring import mark_safe +from django.utils.http import urlquote_plus + +slug_re = re.compile(r'\w+', re.UNICODE) + +@decorate(slugify) +def imp_slugify(origin, value): + if settings.ALLOW_UNICODE_IN_SLUGS: + try: + bits = slug_re.findall(value.lower()) + return mark_safe("-".join(bits)) + except: + pass + return origin(value) + +from forum import settings \ No newline at end of file diff --git a/forum/views/admin.py b/forum/views/admin.py index ad4f9b9..29fd454 100644 --- a/forum/views/admin.py +++ b/forum/views/admin.py @@ -37,7 +37,7 @@ def admin_page(fn): context['othersets'] = sorted( [s for s in Setting.sets.values() if not s.name in ('basic', 'users', 'email', 'paths', 'extkeys', 'repgain', 'minrep', 'voting', 'badges', 'about', 'faq', 'sidebar', - 'form', 'moderation', 'css', 'headandfoot', 'head', 'view')] + 'form', 'moderation', 'css', 'headandfoot', 'head', 'view', 'urls')] , lambda s1, s2: s1.weight - s2.weight) unsaved = request.session.get('previewing_settings', {}) diff --git a/forum/views/readers.py b/forum/views/readers.py index 0d282b8..4a839b0 100644 --- a/forum/views/readers.py +++ b/forum/views/readers.py @@ -305,7 +305,7 @@ def question(request, id, slug=None, answer=None): return answer_redirect(request, answer) - if (not slug) or (slug != urlquote(slugify(question.title))): + if settings.FORCE_SINGLE_URL and ((not slug) or (slug != slugify(question.title))): return HttpResponsePermanentRedirect(question.get_absolute_url()) if request.POST: -- 2.39.5