From 04aadd147b0d494c043caf9702684592652a26ab Mon Sep 17 00:00:00 2001 From: jordan Date: Mon, 18 Jul 2011 21:25:12 +0000 Subject: [PATCH] be able to have user links without ids in the url, wrapped in a setting of the URL settings set, defaults to URLs with IDs included git-svn-id: http://svn.osqa.net/svnroot/osqa/trunk@1124 0cfe37f9-358a-4d5e-be75-b63607b5c754 --- forum/models/user.py | 18 ++++++++++++++++-- forum/settings/urls.py | 5 +++++ .../default/templates/notifications/base.html | 2 +- .../templates/notifications/base_text.html | 2 +- .../default/templates/users/subscriptions.html | 4 ++-- forum/urls.py | 10 ++++++++-- forum/views/users.py | 10 ++++++++-- 7 files changed, 41 insertions(+), 10 deletions(-) diff --git a/forum/models/user.py b/forum/models/user.py index b3822a1..b852813 100644 --- a/forum/models/user.py +++ b/forum/models/user.py @@ -199,7 +199,14 @@ class User(BaseModel, DjangoUser): @models.permalink def get_profile_url(self): - return ('user_profile', (), {'id': self.id, 'slug': slugify(smart_unicode(self.username))}) + keyword_arguments = { + 'slug': slugify(smart_unicode(self.username)) + } + if settings.INCLUDE_ID_IN_USER_URLS: + keyword_arguments.update({ + 'id': self.id, + }) + return ('user_profile', (), keyword_arguments) def get_absolute_url(self): return self.get_profile_url() @@ -210,7 +217,14 @@ class User(BaseModel, DjangoUser): @models.permalink def get_user_subscriptions_url(self): - return ('user_subscriptions', (), { 'id': self.id, 'slug': slugify(smart_unicode(self.username))}) + keyword_arguments = { + 'slug': slugify(smart_unicode(self.username)) + } + if settings.INCLUDE_ID_IN_USER_URLS: + keyword_arguments.update({ + 'id': self.id, + }) + return ('user_subscriptions', (), keyword_arguments) @models.permalink def get_answered_url(self): diff --git a/forum/settings/urls.py b/forum/settings/urls.py index e532c55..e7e6797 100644 --- a/forum/settings/urls.py +++ b/forum/settings/urls.py @@ -3,6 +3,11 @@ from django.utils.translation import ugettext as _ URLS_SET = SettingSet('urls', _('URL settings'), _("Some settings to tweak behaviour of site urls (experimental).")) +INCLUDE_ID_IN_USER_URLS = Setting('INCLUDE_ID_IN_USER_URLS', True, URLS_SET, dict( +label = _("Include IDs in user URLs"), +help_text = _("Choose this if you want to have IDs included in the user-related URLs."), +required=False)) + 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."), diff --git a/forum/skins/default/templates/notifications/base.html b/forum/skins/default/templates/notifications/base.html index 18ac9db..320b164 100644 --- a/forum/skins/default/templates/notifications/base.html +++ b/forum/skins/default/templates/notifications/base.html @@ -24,7 +24,7 @@

{% trans "Thanks" %},
{{settings.APP_SHORT_NAME}}

{% if not exclude_finetune %}

{% trans "P.S. You can always fine-tune which notifications you receive" %} -{% trans "here" %}. +{% trans "here" %}. {% endif %}


diff --git a/forum/skins/default/templates/notifications/base_text.html b/forum/skins/default/templates/notifications/base_text.html index ec8714e..ed2ebdf 100644 --- a/forum/skins/default/templates/notifications/base_text.html +++ b/forum/skins/default/templates/notifications/base_text.html @@ -11,7 +11,7 @@ {% if not exclude_finetune %} {% trans "P.S. You can always fine-tune which notifications you receive here:" %} -{{ settings.APP_URL }}{% url user_subscriptions id=recipient.id,slug=recipient.username|slugify %} +{{ settings.APP_URL }}{{ recipient.get_user_subscriptions_url }} {% endif %} {{ settings.EMAIL_FOOTER_TEXT }} \ No newline at end of file diff --git a/forum/skins/default/templates/users/subscriptions.html b/forum/skins/default/templates/users/subscriptions.html index 573bb0a..5d71919 100644 --- a/forum/skins/default/templates/users/subscriptions.html +++ b/forum/skins/default/templates/users/subscriptions.html @@ -32,8 +32,8 @@

diff --git a/forum/urls.py b/forum/urls.py index a6c496d..3d202c7 100644 --- a/forum/urls.py +++ b/forum/urls.py @@ -23,6 +23,12 @@ try: except AttributeError: admin_url = url(r'^%s(.*)' % _('nimda/'), admin.site.urls) +# Choose the user urls pattern +if bool(settings.INCLUDE_ID_IN_USER_URLS.value): + core_user_urls_prefix = r'^%s(?P\d+)/(?P.*)' +else: + core_user_urls_prefix = r'^%s(?P.*)' + core_urls = ( url(r'^$', app.readers.index, name='index'), admin_url, @@ -98,13 +104,13 @@ core_urls = ( url(r'^%s(?P\d+)/%s$' % (_('users/'), _('award/')), app.users.award_points, name='user_award_points'), url(r'^%s(?P\d+)/%s$' % (_('users/'), _('suspend/')), app.users.suspend, name='user_suspend'), url(r'^%s(?P\d+)/%s(?P[a-z]+)/(?P[a-z]+)/$' % (_('users/'), _('powers/')), app.users.user_powers, name='user_powers'), - url(r'^%s(?P\d+)/(?P.*)/%s$' % (_('users/'), _('subscriptions/')), app.users.user_subscriptions, name='user_subscriptions'), + url((core_user_urls_prefix + '/%s$') % (_('users/'), _('subscriptions/')), app.users.user_subscriptions, name='user_subscriptions'), url(r'^%s(?P\d+)/(?P.*)/%s$' % (_('users/'), _('preferences/')), app.users.user_preferences, name='user_preferences'), url(r'^%s(?P\d+)/(?P.*)/%s$' % (_('users/'), _('favorites/')), app.users.user_favorites, name='user_favorites'), url(r'^%s(?P\d+)/(?P.*)/%s$' % (_('users/'), _('reputation/')), app.users.user_reputation, name='user_reputation'), url(r'^%s(?P\d+)/(?P.*)/%s$' % (_('users/'), _('votes/')), app.users.user_votes, name='user_votes'), url(r'^%s(?P\d+)/(?P.*)/%s$' % (_('users/'), _('recent/')), app.users.user_recent, name='user_recent'), - url(r'^%s(?P\d+)/(?P.*)$' % _('users/'), app.users.user_profile, name='user_profile'), + url(core_user_urls_prefix % _('users/'), app.users.user_profile, name='user_profile'), url(r'^%s$' % _('badges/'), app.meta.badges, name='badges'), url(r'^%s(?P\d+)/(?P[\w-]+)?$' % _('badges/'), app.meta.badge, name='badge'), # (r'^admin/doc/' % _('admin/doc'), include('django.contrib.admindocs.urls')), diff --git a/forum/views/users.py b/forum/views/users.py index 6686a26..6eb9baa 100644 --- a/forum/views/users.py +++ b/forum/views/users.py @@ -255,8 +255,14 @@ def suspend(request, id): def user_view(template, tab_name, tab_title, tab_description, private=False, tabbed=True, render_to=None, weight=500): def decorator(fn): - def params(request, id, slug=None): - user = get_object_or_404(User, id=id) + def params(request, id=None, slug=None): + # Get the user object by id if the id parameter has been passed + if id is not None: + user = get_object_or_404(User, id=id) + # ...or by slug if the slug has been given + elif slug is not None: + user = get_object_or_404(User, username=slug) + if private and not (user == request.user or request.user.is_superuser): raise ReturnImediatelyException(HttpResponseUnauthorized(request)) -- 2.39.5