- 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