]> git.openstreetmap.org Git - osqa.git/blobdiff - forum/views/users.py
several fixes in users profile
[osqa.git] / forum / views / users.py
index 162ba2d214f8c484e3f5faca54de980dc7812e81..e3dcc08f2e6ca0110d0e01d58a1d00ec1d1d8faa 100644 (file)
@@ -1,5 +1,6 @@
 from django.contrib.auth.decorators import login_required\r
 from forum.models import User\r
+from django.db.models import Q, Count\r
 from django.core.paginator import Paginator, EmptyPage, InvalidPage\r
 from django.template.defaultfilters import slugify\r
 from django.contrib.contenttypes.models import ContentType\r
@@ -146,80 +147,18 @@ def edit_user(request, id):
 def user_stats(request, user_id, user_view):\r
     user = get_object_or_404(User, id=user_id)\r
 \r
-    questions = Question.objects.filter(deleted=False, author=user).order_by('-added_at')\r
-\r
-    answered_questions = Question.objects.filter(deleted=False, answers__author=user).distinct()\r
-\r
-    """answered_questions = Question.objects.extra(\r
-        select={\r
-            'vote_up_count' : 'answer.vote_up_count',\r
-            'vote_down_count' : 'answer.vote_down_count',\r
-            'answer_id' : 'answer.id',\r
-            'accepted' : 'answer.accepted',\r
-            'vote_count' : 'answer.score',\r
-            'comment_count' : 'answer.comment_count'\r
-            },\r
-        tables=['question', 'answer'],\r
-        where=['NOT answer.deleted AND NOT question.deleted AND answer.author_id=%s AND answer.question_id=question.id'],\r
-        params=[user_id],\r
-        order_by=['-vote_count', '-answer_id'],\r
-        select_params=[user_id]\r
-    ).distinct().values('comment_count',\r
-                        'id',\r
-                        'answer_id',\r
-                        'title',\r
-                        'author_id',\r
-                        'accepted',\r
-                        'vote_count',\r
-                        'answer_count',\r
-                        'vote_up_count',\r
-                        'vote_down_count')[:100]"""\r
+    questions = user.questions.filter(deleted=False).order_by('-added_at')\r
+    answers = user.answers.filter(deleted=False).order_by('-added_at')\r
 \r
     up_votes = user.get_up_vote_count()\r
     down_votes = user.get_down_vote_count()\r
     votes_today = user.get_vote_count_today()\r
     votes_total = int(settings.MAX_VOTES_PER_DAY)\r
 \r
-    #question_id_set = set(map(lambda v: v['id'], list(questions))) \\r
-    #                    | set(map(lambda v: v['id'], list(answered_questions)))\r
+    user_tags = Tag.objects.filter(Q(questions__author=user) | Q(questions__answers__author=user)) \\r
+        .annotate(user_tag_usage_count=Count('name')).order_by('-user_tag_usage_count')\r
 \r
-    user_tags = Tag.objects.filter(questions__author=user)\r
-    try:\r
-        from django.db.models import Count\r
-        awards = Award.objects.extra(\r
-                                        select={'id': 'badge.id', \r
-                                                'name':'badge.name', \r
-                                                'description': 'badge.description', \r
-                                                'type': 'badge.type'},\r
-                                        tables=['award', 'badge'],\r
-                                        order_by=['-awarded_at'],\r
-                                        where=['user_id=%s AND badge_id=badge.id'],\r
-                                        params=[user.id]\r
-                                    ).values('id', 'name', 'description', 'type')\r
-        total_awards = awards.count()\r
-        awards = awards.annotate(count = Count('badge__id'))\r
-        user_tags = user_tags.annotate(user_tag_usage_count=Count('name'))\r
-\r
-    except ImportError:\r
-        awards = Award.objects.extra(\r
-                                        select={'id': 'badge.id', \r
-                                                'count': 'count(badge_id)', \r
-                                                'name':'badge.name', \r
-                                                'description': 'badge.description', \r
-                                                'type': 'badge.type'},\r
-                                        tables=['award', 'badge'],\r
-                                        order_by=['-awarded_at'],\r
-                                        where=['user_id=%s AND badge_id=badge.id'],\r
-                                        params=[user.id]\r
-                                    ).values('id', 'count', 'name', 'description', 'type')\r
-        total_awards = awards.count()\r
-        awards.query.group_by = ['badge_id']\r
-\r
-        user_tags = user_tags.extra(\r
-            select={'user_tag_usage_count': 'COUNT(1)',},\r
-            order_by=['-user_tag_usage_count'],\r
-        )\r
-        user_tags.query.group_by = ['name']\r
+    awards = Badge.objects.filter(award_badge__user=user).annotate(count=Count('name')).order_by('-count')\r
 \r
     if request.user.is_superuser:\r
         moderate_user_form = ModerateUserForm(instance=user)\r
@@ -233,7 +172,7 @@ def user_stats(request, user_id, user_view):
                                 "page_title" : user_view.page_title,\r
                                 "view_user" : user,\r
                                 "questions" : questions,\r
-                                "answered_questions" : answered_questions,\r
+                                "answers" : answers,\r
                                 "up_votes" : up_votes,\r
                                 "down_votes" : down_votes,\r
                                 "total_votes": up_votes + down_votes,\r
@@ -241,7 +180,7 @@ def user_stats(request, user_id, user_view):
                                 "votes_total_per_day": votes_total,\r
                                 "user_tags" : user_tags[:50],\r
                                 "awards": awards,\r
-                                "total_awards" : total_awards,\r
+                                "total_awards" : awards.count(),\r
                             }, context_instance=RequestContext(request))\r
 \r
 def user_recent(request, user_id, user_view):\r
@@ -507,7 +446,7 @@ def user_responses(request, user_id, user_view):
         def __init__(self, type, title, question_id, answer_id, time, username, user_id, content):\r
             self.type = type\r
             self.title = title\r
-            self.titlelink = reverse('question', args=[question_id]) + u'%s#%s' % (slugify(title), answer_id)\r
+            self.titlelink = reverse('question', kwargs={'id': question_id, 'slug': slugify(title)}) +u'#%s' % answer_id\r
             self.time = time\r
             self.userlink = reverse('users') + u'%s/%s/' % (user_id, username)\r
             self.username = username\r