]> git.openstreetmap.org Git - osqa.git/commitdiff
Adds pagination in user stats (questions and answers) and a feed icon in the user...
authorhernani <hernani@0cfe37f9-358a-4d5e-be75-b63607b5c754>
Thu, 8 Jul 2010 13:48:32 +0000 (13:48 +0000)
committerhernani <hernani@0cfe37f9-358a-4d5e-be75-b63607b5c754>
Thu, 8 Jul 2010 13:48:32 +0000 (13:48 +0000)
git-svn-id: http://svn.osqa.net/svnroot/osqa/trunk@501 0cfe37f9-358a-4d5e-be75-b63607b5c754

forum/skins/default/media/style/style.css
forum/skins/default/templates/users/stats.html
forum/utils/pagination.py
forum/views/readers.py
forum/views/users.py

index e74bba65ad4d322ec900207b5fe46040d63ab2eb..77ef96d44feb2e338da5f40ddf60ebc52d930ee7 100644 (file)
@@ -325,6 +325,10 @@ blockquote {
     width: 740px;
 }
 
+#listA .short-summary:first-of-type {
+    border-top: 0;
+}
+
 .short-summary h2 a {
     color: #2A5594;
     font-family: "Trebuchet MS", "segoe ui", arial, sans-serif;
@@ -1735,5 +1739,5 @@ div.dialog.prompt .dialog-content select, div.dialog.prompt .dialog-content text
 .feed-icon {
     width: 14px;
     height: 14px;
-    float: left;
+    display: inline-block;
 }
\ No newline at end of file
index d3806d91f8b46ab1566b994a4416a42a21c51ac5..547f01eee51cde637e370c75d9801aff22de53b1 100644 (file)
@@ -7,49 +7,41 @@
 {% load question_list_tags %}
                {% block usercontent %}
 
-               {% declare %}
-                   question_count = questions.count()
-                   show_more_questions_link = question_count > 15
-                   questions = questions[:15]
-
-                   answer_count = answers.count()
-                   show_more_answers_link = answer_count > 30
-                   answers = answers[:30]
-               {% enddeclare %}
-       
         <a name="questions"></a>
         {% spaceless %}
             <h2>
-                {% blocktrans count question_count as counter %}
+                {% blocktrans count questions.paginator.count as counter %}
                 <span class="count">1</span> Question
                 {% plural %}
                 <span class="count">{{counter}}</span> Questions
                 {% endblocktrans %}
+                <a class="feed-icon" style="background-image:url('{% media "media/images/feed-icon-small.png" %}');"
+                    href="{{ view_user.get_asked_url }}?type=rss" title="{% trans "subscribe to question RSS feed" %}"></a>
             </h2>
         {% endspaceless %}
-               <div class="user-stats-table">
-                   {% for question in questions %}
+        <div>{{ questions.paginator.sort_tabs }}</div>
+               <div id="listA">
+                   {% for question in questions.paginator.page %}
                        {% question_list_item question favorite_count=yes signature_type=badges %}
                    {% endfor %}
-            {% if show_more_questions_link %}
-                <div class="short-summary"  style="text-align: right">
-                    <a href="{{ view_user.get_asked_url }}">{% blocktrans with view_user.username as username %}View all questions by {{ username }}{% endblocktrans %}</a>
-                </div>
-            {% endif %}
                </div>
                <br clear="all"/>
+               <div class="pager">{{ questions.paginator.page_numbers }}</div>
+               <br clear="all"/>
+
         <a name="answers"></a>
         {% spaceless %}
             <h2>
-                {% blocktrans count answer_count as counter %}
+                {% blocktrans count answers.paginator.count as counter %}
                 <span class="count">1</span> Answer
                 {% plural %}
                 <span class="count">{{counter}}</span> Answers
                 {% endblocktrans %}
             </h2>
         {% endspaceless %}
+        <div>{{ answers.paginator.sort_tabs }}</div>
         <div class="user-stats-table">
-            {% for answer in answers %}
+            {% for answer in answers.paginator.page %}
             <div class="answer-summary">
                 <a title="{{answer.question.title}}"
                     href="{{ answer.get_absolute_url }}">
                 </div>
             </div>
             {% endfor %}
-            {% if show_more_answers_link %}
-                <div class="short-summary"  style="text-align: right">
-                    <a href="{{ view_user.get_answered_url }}">{% blocktrans with view_user.username as username %}View all questions answered by {{ username }}{% endblocktrans %}</a>
-                </div>
-            {% endif %}
         </div>
+        <br clear="all"/>
+               <div class="pager">{{ answers.paginator.page_numbers }}</div>
         <br clear="all"/>
         <a name="votes"></a>
         {% spaceless %}
index 3f873ddcafe46d445ad91cd2bb405a8e099e6338..e3cfc291e2c40dc949746f747b5172e2ddef3c64 100644 (file)
@@ -33,7 +33,7 @@ class PaginatorContext(object):
     base_path = None
 
     def __init__(self, id, sort_methods=None, default_sort=None, force_sort = None, sticky_sort=False,
-                 pagesizes=None, default_pagesize=None):
+                 pagesizes=None, default_pagesize=None, prefix=''):
         self.id = id
         if sort_methods:
             self.has_sort = True
@@ -60,9 +60,10 @@ class PaginatorContext(object):
 
         self.force_sort = force_sort
         self.sticky_sort = sticky_sort
+        self.prefix = prefix
 
     def session_preferences(self, request):
-        return request.session.get('paginator_%s' % self.id, {})
+        return request.session.get('paginator_%s%s' % (self.prefix, self.id), {})
 
     def pagesize(self, request, session_prefs=None):
         if not session_prefs:
@@ -70,18 +71,18 @@ class PaginatorContext(object):
 
 
         if self.has_pagesize:
-            if request.GET.get(labels.PAGESIZE, None):
+            if request.GET.get(self.PAGESIZE, None):
                 try:
-                    pagesize = int(request.GET[labels.PAGESIZE])
+                    pagesize = int(request.GET[self.PAGESIZE])
                 except ValueError:
                     logging.error('Found invalid page size "%s", loading %s, refered by %s' % (
-                        request.GET.get(labels.PAGESIZE, ''), request.path, request.META.get('HTTP_REFERER', 'UNKNOWN')
+                        request.GET.get(self.PAGESIZE, ''), request.path, request.META.get('HTTP_REFERER', 'UNKNOWN')
                     ))
                     raise Http404()
 
-                session_prefs[labels.PAGESIZE] = pagesize
+                session_prefs[self.PAGESIZE] = pagesize
             else:
-                pagesize = session_prefs.get(labels.PAGESIZE, self.default_pagesize)
+                pagesize = session_prefs.get(self.PAGESIZE, self.default_pagesize)
 
             if not pagesize in self.pagesizes:
                 pagesize = self.default_pagesize
@@ -92,10 +93,10 @@ class PaginatorContext(object):
 
     def page(self, request):
         try:
-            return int(request.GET.get(labels.PAGE, 1))
+            return int(request.GET.get(self.PAGE, 1))
         except ValueError:
             logging.error('Found invalid page number "%s", loading %s, refered by %s' % (
-                request.GET.get(labels.PAGE, ''), request.path, request.META.get('HTTP_REFERER', 'UNKNOWN')
+                request.GET.get(self.PAGE, ''), request.path, request.META.get('HTTP_REFERER', 'UNKNOWN')
             ))
             raise Http404()
 
@@ -105,12 +106,12 @@ class PaginatorContext(object):
 
         sort = None
         if self.has_sort:
-            if request.GET.get(labels.SORT, None):
-                sort = request.GET[labels.SORT]
+            if request.GET.get(self.SORT, None):
+                sort = request.GET[self.SORT]
                 if self.sticky_sort or session_prefs.get('sticky_sort', False):
-                    session_prefs[labels.SORT] = sort
+                    session_prefs[self.SORT] = sort
             else:
-                sort = self.force_sort or session_prefs.get(labels.SORT, self.default_sort)
+                sort = self.force_sort or session_prefs.get(self.SORT, self.default_sort)
 
             if not sort in self.sort_methods:
                 sort = self.default_sort
@@ -125,20 +126,33 @@ class PaginatorContext(object):
 
         return sort, objects
 
+    @property
+    def PAGESIZE(self):
+        return self.prefix and "%s_%s" % (self.prefix, _('pagesize')) or _('pagesize')
 
-class labels(object):
-    PAGESIZE = _('pagesize')
-    PAGE = _('page')
-    SORT = _('sort')
+    @property
+    def PAGE(self):
+        return self.prefix and "%s_%s" % (self.prefix, _('page')) or _('page')
+
+    @property
+    def SORT(self):
+        return self.prefix and "%s_%s" % (self.prefix, _('sort')) or _('sort')
 
 page_numbers_template = template.loader.get_template('paginator/page_numbers.html')
 page_sizes_template = template.loader.get_template('paginator/page_sizes.html')
 sort_tabs_template = template.loader.get_template('paginator/sort_tabs.html')
 
-def paginated(request, list_name, context, tpl_context):
-    session_prefs = context.session_preferences(request)
+def paginated(request, paginators, tpl_context):
+    if len(paginators) == 2 and isinstance(paginators[0], basestring):
+        paginators = (paginators,)
+
+    for list_name, context in paginators:
+        tpl_context[list_name] = _paginated(request, tpl_context[list_name], context)
 
-    objects = tpl_context[list_name]
+    return tpl_context
+
+def _paginated(request, objects, context):
+    session_prefs = context.session_preferences(request)
 
     pagesize = context.pagesize(request, session_prefs)
     page = context.page(request)
@@ -150,7 +164,7 @@ def paginated(request, list_name, context, tpl_context):
         page_obj = paginator.page(page)
     except EmptyPage:
         logging.error('Found invalid page number "%s", loading %s, refered by %s' % (
-            request.GET.get(labels.PAGE, ''), request.path, request.META.get('HTTP_REFERER', 'UNKNOWN')
+            request.GET.get(context.PAGE, ''), request.path, request.META.get('HTTP_REFERER', 'UNKNOWN')
         ))
         raise Http404()
 
@@ -158,7 +172,7 @@ def paginated(request, list_name, context, tpl_context):
         base_path = context.base_path
     else:
         base_path = request.path
-        get_params = ["%s=%s" % (k, v) for k, v in request.GET.items() if not k in (labels.PAGE, labels.PAGESIZE, labels.SORT)]
+        get_params = ["%s=%s" % (k, v) for k, v in request.GET.items() if not k in (context.PAGE, context.PAGESIZE, context.SORT)]
 
         if get_params:
             base_path += "?" + "&".join(get_params)
@@ -199,9 +213,9 @@ def paginated(request, list_name, context, tpl_context):
             page_numbers = []
 
             if sort:
-                url_builder = lambda n: mark_safe("%s%s%s=%s&%s=%s" % (base_path, url_joiner, labels.SORT, sort, labels.PAGE, n))
+                url_builder = lambda n: mark_safe("%s%s%s=%s&%s=%s" % (base_path, url_joiner, context.SORT, sort, context.PAGE, n))
             else:
-                url_builder = lambda n: mark_safe("%s%s%s=%s" % (base_path, url_joiner, labels.PAGE, n))
+                url_builder = lambda n: mark_safe("%s%s%s=%s" % (base_path, url_joiner, context.PAGE, n))
 
             if range_start > (context.outside_page_range + 1):
                 page_numbers.append([(n, url_builder(n)) for n in range(1, context.outside_page_range + 1)])
@@ -232,9 +246,9 @@ def paginated(request, list_name, context, tpl_context):
     if pagesize:
         def page_sizes():
             if sort:
-                url_builder = lambda s: mark_safe("%s%s%s=%s&%s=%s" % (base_path, url_joiner, labels.SORT, sort, labels.PAGESIZE, s))
+                url_builder = lambda s: mark_safe("%s%s%s=%s&%s=%s" % (base_path, url_joiner, context.SORT, sort, context.PAGESIZE, s))
             else:
-                url_builder = lambda s: mark_safe("%s%s%s=%s" % (base_path, url_joiner, labels.PAGESIZE, s))
+                url_builder = lambda s: mark_safe("%s%s%s=%s" % (base_path, url_joiner, context.PAGESIZE, s))
 
             sizes = [(s, url_builder(s)) for s in context.pagesizes]
 
@@ -249,7 +263,7 @@ def paginated(request, list_name, context, tpl_context):
 
     if sort:
         def sort_tabs():
-            url_builder = lambda s: mark_safe("%s%s%s=%s" % (base_path, url_joiner, labels.SORT, s))
+            url_builder = lambda s: mark_safe("%s%s%s=%s" % (base_path, url_joiner, context.SORT, s))
             sorts = [(n, s.label, url_builder(n), s.description) for n, s in context.sort_methods.items()]
 
             return sort_tabs_template.render(template.Context({
@@ -263,5 +277,4 @@ def paginated(request, list_name, context, tpl_context):
 
     request.session['paginator_%s' % context.id] = session_prefs
     objects.paginator = paginator
-    tpl_context[list_name] = objects
-    return tpl_context
\ No newline at end of file
+    return objects
\ No newline at end of file
index 8ed1b27365bf814fd51bc18b8d48ed31eacea3c6..69ef208a2e9119f703d0ab1d5774fa5277682b10 100644 (file)
@@ -32,13 +32,13 @@ from forum.feed import RssQuestionFeed
 import decorators
 
 class QuestionListPaginatorContext(pagination.PaginatorContext):
-    def __init__(self):
-        super (QuestionListPaginatorContext, self).__init__('QUESTIONS_LIST', sort_methods=(
+    def __init__(self, id='QUESTIONS_LIST', prefix='', default_pagesize=30):
+        super (QuestionListPaginatorContext, self).__init__(id, sort_methods=(
             (_('active'), pagination.SimpleSort(_('active'), '-last_activity_at', _("most recently updated questions"))),
             (_('newest'), pagination.SimpleSort(_('newest'), '-added_at', _("most recently asked questions"))),
             (_('hottest'), pagination.SimpleSort(_('hottest'), '-extra_count', _("hottest questions"))),
             (_('mostvoted'), pagination.SimpleSort(_('most voted'), '-score', _("most voted questions"))),
-        ), pagesizes=(15, 30, 50))
+        ), pagesizes=(15, 30, 50), default_pagesize=default_pagesize, prefix=prefix)
 
 class AnswerSort(pagination.SimpleSort):
     def apply(self, objects):
@@ -48,12 +48,12 @@ class AnswerSort(pagination.SimpleSort):
             return objects.order_by('-marked', self.order_by)
 
 class AnswerPaginatorContext(pagination.PaginatorContext):
-    def __init__(self):
-        super (AnswerPaginatorContext, self).__init__('ANSWER_LIST', sort_methods=(
+    def __init__(self, id='ANSWER_LIST', prefix='', default_pagesize=10):
+        super (AnswerPaginatorContext, self).__init__(id, sort_methods=(
             (_('oldest'), AnswerSort(_('oldest answers'), 'added_at', _("oldest answers will be shown first"))),
             (_('newest'), AnswerSort(_('newest answers'), '-added_at', _("newest answers will be shown first"))),
             (_('votes'), AnswerSort(_('popular answers'), '-score', _("most voted answers will be shown first"))),
-        ), default_sort=_('votes'), sticky_sort = True, pagesizes=(5, 10, 20))
+        ), default_sort=_('votes'), sticky_sort = True, pagesizes=(5, 10, 20), default_pagesize=default_pagesize, prefix=prefix)
 
 class TagPaginatorContext(pagination.PaginatorContext):
     def __init__(self):
@@ -164,7 +164,7 @@ def question_list(request, initial,
 
         feed_url = mark_safe(request.path + "?type=rss" + req_params)
 
-    return pagination.paginated(request, 'questions', paginator_context or QuestionListPaginatorContext(), {
+    return pagination.paginated(request, ('questions', paginator_context or QuestionListPaginatorContext()), {
     "questions" : questions,
     "questions_count" : questions.count(),
     "answer_count" : answer_count,
@@ -223,7 +223,7 @@ def tags(request):
         if stag:
             tags = tags.filter(name__contains=stag)
 
-    return pagination.paginated(request, 'tags', TagPaginatorContext(), {
+    return pagination.paginated(request, ('tags', TagPaginatorContext()), {
         "tags" : tags,
         "stag" : stag,
         "keywords" : stag
@@ -322,7 +322,7 @@ def question(request, id, slug, answer=None):
     else:
         subscription = False
 
-    return pagination.paginated(request, 'answers', AnswerPaginatorContext(), {
+    return pagination.paginated(request, ('answers', AnswerPaginatorContext()), {
     "question" : question,
     "answer" : answer_form,
     "answers" : answers,
index fa575421d71acc9942ff6f751aae2e19c52569a8..8bea106278d27ffd89bf0fcf230f112dd6f4f528 100644 (file)
@@ -22,6 +22,7 @@ import decorators
 from forum.actions import EditProfileAction, FavoriteAction, BonusRepAction, SuspendAction\r
 from forum.modules import ui\r
 from forum.utils import pagination\r
+from forum.views.readers import QuestionListPaginatorContext, AnswerPaginatorContext\r
 \r
 import time\r
 import decorators\r
@@ -39,6 +40,14 @@ class UserListPaginatorContext(pagination.PaginatorContext):
             (_('name'), pagination.SimpleSort(_('by username'), 'username', _("sorted by username"))),\r
         ), pagesizes=(20, 35, 60))\r
 \r
+class UserAnswersPaginatorContext(pagination.PaginatorContext):\r
+    def __init__(self):\r
+        super (UserAnswersPaginatorContext, self).__init__('USER_ANSWER_LIST', sort_methods=(\r
+            (_('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
+\r
 USERS_PAGE_SIZE = 35# refactor - move to some constants file\r
 \r
 @decorators.render('users/users.html', 'users', _('users'), weight=200)\r
@@ -49,7 +58,7 @@ def users(request):
     if suser == "":\r
         users = users.filter(username__icontains=suser)\r
 \r
-    return pagination.paginated(request, 'users', UserListPaginatorContext(), {\r
+    return pagination.paginated(request, ('users', UserListPaginatorContext()), {\r
         "users" : users,\r
         "suser" : suser,\r
     })\r
@@ -231,7 +240,9 @@ def user_profile(request, user):
     awards = [(Badge.objects.get(id=b['id']), b['count']) for b in\r
               Badge.objects.filter(awards__user=user).values('id').annotate(count=Count('cls')).order_by('-count')]\r
 \r
-    return {\r
+    return pagination.paginated(request, (\r
+    ('questions', QuestionListPaginatorContext('USER_QUESTION_LIST', _('questions'), 15)),\r
+    ('answers', UserAnswersPaginatorContext())), {\r
     "view_user" : user,\r
     "questions" : questions,\r
     "answers" : answers,\r
@@ -243,7 +254,7 @@ def user_profile(request, user):
     "user_tags" : user_tags[:50],\r
     "awards": awards,\r
     "total_awards" : len(awards),\r
-    }\r
+    })\r
     \r
 @user_view('users/recent.html', 'recent', _('recent activity'), _('recent user activity'))\r
 def user_recent(request, user):\r