class Meta:
model = SubscriptionSettings
+class UserPreferencesForm(forms.Form):
+ sticky_sorts = forms.BooleanField(required=False, initial=False)
+
(f.name, getattr(self, f.name)) for f in self._meta.fields if self._original_state[f.attname] != self.__dict__[f.attname]
])
- def save(self, *args, **kwargs):
+ def save(self, full_save=False, *args, **kwargs):
put_back = [k for k, v in self.__dict__.items() if isinstance(v, models.expressions.ExpressionNode)]
- if self.id:
+ if self.id and not full_save:
self.__class__.objects.filter(id=self.id).update(**self._get_update_kwargs())
else:
super(BaseModel, self).save()
class Meta:
app_label = 'forum'
-class UserProperty(models.Model):
+class UserProperty(BaseModel):
user = models.ForeignKey(User, related_name='properties')
key = models.CharField(max_length=16)
value = PickledObjectField()
app_label = 'forum'
unique_together = ('user', 'key')
+ def cache_key(self):
+ return self._generate_cache_key("%s:%s" % (self.user.id, self.key))
+
+ @classmethod
+ def infer_cache_key(cls, querydict):
+ if 'user' in querydict and 'key' in querydict:
+ return cls._generate_cache_key("%s:%s" % (querydict['user'].id, querydict['key']))
+
+ return None
+
class UserPropertyDict(object):
def __init__(self, user):
self.__dict__['_user'] = user
if current:
current.value = value
self.__dict__[name] = value
- current.save()
+ current.save(full_save=True)
else:
user = self.__dict__['_user']
prop = UserProperty(user=user, value=value, key=name)
span_attrs={'class': 'user-subscriptions'},
weight=200
),
+ ui.UserMenuItem(
+ label=_("other preferences"),
+ url=lambda u, c: reverse('user_preferences', kwargs={'id': c['user'].id, 'slug': slugify(c['user'].username)}),
+ weight=200
+ ),
ModerationMenuGroup(_("Moderation tools"), items=(
ui.UserMenuItem(
label=lambda u, c: c['user'].is_suspended() and _("withdraw suspension") or _("suspend this user"),
--- /dev/null
+{% extends "user.html" %}
+{% load i18n %}
+{% load extra_tags %}
+
+{% block usercontent %}
+ <h2>{% trans "Preferences" %}</h2>
+ <div class='inline-block'>
+ <form method="POST">
+ <p class="message">
+ {% trans "Here you can set some personal preferences." %}
+ </p>
+ {{ form.errors }}
+ <table class="form-as-table">
+ <tr>
+ <td colspan="2">
+ <strong>{% trans "Navigation:" %}</strong>
+ </td>
+ </tr>
+ <tr>
+ <td>{{ form.sticky_sorts }}</td>
+ <td>{% trans "Allways remember the sort order I apply to the lists I view" %}</td>
+ </tr>
+ </table>
+ <div class="submit-row">
+ <input type="submit" class="submit" name="save" value="{% trans "Save" %}"/>
+ </div>
+ </form>
+ </div>
+{% endblock %}
\ No newline at end of file
app.users.user_powers, name='user_powers'),
url(r'^%s(?P<id>\d+)/(?P<slug>.+)/%s$' % (_('users/'), _('subscriptions/')), app.users.user_subscriptions,
name='user_subscriptions'),
+ url(r'^%s(?P<id>\d+)/(?P<slug>.+)/%s$' % (_('users/'), _('preferences/')), app.users.user_preferences,
+ name='user_preferences'),
url(r'^%s(?P<id>\d+)/(?P<slug>.+)/%s$' % (_('users/'), _('favorites/')),
app.users.user_favorites, name='user_favorites'),
url(r'^%s(?P<id>\d+)/(?P<slug>.+)/%s$' % (_('users/'), _('reputation/')),
base_path = None
- def __init__(self, id, sort_methods=None, default_sort=None, force_sort = None, sticky_sort=False,
+ def __init__(self, id, sort_methods=None, default_sort=None, force_sort = None,
pagesizes=None, default_pagesize=None, prefix=''):
self.id = id
if sort_methods:
self.has_pagesize = False
self.force_sort = force_sort
- self.sticky_sort = sticky_sort
self.prefix = prefix
- def session_preferences(self, request):
- return request.session.get('paginator_%s%s' % (self.prefix, self.id), {})
+ def preferences(self, request):
+ if request.user.is_authenticated():
+ if request.user.prop.pagination:
+ preferences = request.user.prop.pagination.get(self.id, {})
+ else:
+ preferences = {}
+ else:
+ preferences = request.session.get('paginator_%s%s' % (self.prefix, self.id), {})
+
+ return preferences
+
+ def set_preferences(self, request, preferences):
+ if request.user.is_authenticated():
+ all_preferences = request.user.prop.pagination or {}
+ all_preferences[self.id] = preferences
+ request.user.prop.pagination = all_preferences
+ else:
+ request.session['paginator_%s%s' % (self.prefix, self.id)] = preferences
def pagesize(self, request, session_prefs=None):
if not session_prefs:
- session_prefs = self.session_preferences(request)
+ session_prefs = self.preferences(request)
if self.has_pagesize:
def sort(self, request, session_prefs=None):
if not session_prefs:
- session_prefs = self.session_preferences(request)
+ session_prefs = self.preferences(request)
sort = None
+ sticky = request.user.is_authenticated() and request.user.prop.preferences and request.user.prop.preferences.get('sticky_sorts', False)
+
if self.has_sort:
if request.GET.get(self.SORT, None):
sort = request.GET[self.SORT]
- if self.sticky_sort or session_prefs.get('sticky_sort', False):
+
+ if sticky:
session_prefs[self.SORT] = sort
else:
- sort = self.force_sort or session_prefs.get(self.SORT, self.default_sort)
+ sort = self.force_sort or (sticky and session_prefs.get(self.SORT, None)) or self.default_sort
if not sort in self.sort_methods:
sort = self.default_sort
return tpl_context
def _paginated(request, objects, context):
- session_prefs = context.session_preferences(request)
+ session_prefs = context.preferences(request)
pagesize = context.pagesize(request, session_prefs)
page = context.page(request)
else:
paginator.sort_tabs = ''
- request.session['paginator_%s' % context.id] = session_prefs
+ context.set_preferences(request, session_prefs)
objects.paginator = paginator
return objects
\ No newline at end of file
(_('oldest'), pagination.SimpleSort(_('oldest answers'), ('-marked', 'added_at'), _("oldest answers will be shown first"))),
(_('newest'), pagination.SimpleSort(_('newest answers'), ('-marked', '-added_at'), _("newest answers will be shown first"))),
(_('votes'), pagination.SimpleSort(_('popular answers'), ('-marked', '-score', 'added_at'), _("most voted answers will be shown first"))),
- ), default_sort=_('votes'), sticky_sort = True, pagesizes=(5, 10, 20), default_pagesize=default_pagesize, prefix=prefix)
+ ), default_sort=_('votes'), pagesizes=(5, 10, 20), default_pagesize=default_pagesize, prefix=prefix)
class TagPaginatorContext(pagination.PaginatorContext):
def __init__(self):
@decorators.render('index.html')
def index(request):
+ paginator_context = QuestionListPaginatorContext()
+ paginator_context.base_path = reverse('questions')
return question_list(request,
Question.objects.all(),
sort=request.utils.set_sort_method('active'),
base_path=reverse('questions'),
- feed_url=reverse('latest_questions_feed'))
+ feed_url=reverse('latest_questions_feed'),
+ paginator_context=paginator_context)
@decorators.render('questions.html', 'unanswered', _('unanswered'), weight=400)
def unanswered(request):
(_('oldest'), pagination.SimpleSort(_('oldest answers'), 'added_at', _("oldest answers will be shown first"))),\r
(_('newest'), pagination.SimpleSort(_('newest answers'), '-added_at', _("newest answers will be shown first"))),\r
(_('votes'), pagination.SimpleSort(_('popular answers'), '-score', _("most voted answers will be shown first"))),\r
- ), default_sort=_('votes'), sticky_sort = True, pagesizes=(5, 10, 20), default_pagesize=20, prefix=_('answers'))\r
+ ), default_sort=_('votes'), pagesizes=(5, 10, 20), default_pagesize=20, prefix=_('answers'))\r
\r
USERS_PAGE_SIZE = 35# refactor - move to some constants file\r
\r
suser = request.REQUEST.get('q', "")\r
users = User.objects.all()\r
\r
- if suser == "":\r
+ if suser != "":\r
users = users.filter(username__icontains=suser)\r
\r
return pagination.paginated(request, ('users', UserListPaginatorContext()), {\r
\r
return {'view_user':user, 'notificatons_on': enabled, 'form':form}\r
\r
+@user_view('users/preferences.html', 'preferences', _('preferences'), _('preferences'), True, tabbed=False)\r
+def user_preferences(request, user):\r
+ if request.POST:\r
+ form = UserPreferencesForm(request.POST)\r
+\r
+ if form.is_valid():\r
+ user.prop.preferences = form.cleaned_data\r
+ request.user.message_set.create(message=_('New preferences saved'))\r
+\r
+ else:\r
+ preferences = user.prop.preferences\r
+\r
+ if preferences:\r
+ form = UserPreferencesForm(initial=preferences)\r
+ else:\r
+ form = UserPreferencesForm()\r
+ \r
+ return {'view_user': user, 'form': form}\r
+\r
@login_required\r
def account_settings(request):\r
logging.debug('')\r