Commit c13358756e50b6291ee7acfa826a590777d163b9
1 parent
95c40aa2
Exists in
master
and in
39 other branches
Refactoring user's counters
Showing
3 changed files
with
52 additions
and
33 deletions
Show diff stats
src/accounts/search_indexes.py
| ... | ... | @@ -4,6 +4,7 @@ from haystack import indexes |
| 4 | 4 | from django.db.models import Count |
| 5 | 5 | |
| 6 | 6 | from proxy.models import Revision, Ticket, Wiki |
| 7 | +from badger.utils import get_users_counters | |
| 7 | 8 | from .models import User |
| 8 | 9 | |
| 9 | 10 | |
| ... | ... | @@ -34,6 +35,12 @@ class UserIndex(indexes.SearchIndex, indexes.Indexable): |
| 34 | 35 | def get_model(self): |
| 35 | 36 | return User |
| 36 | 37 | |
| 38 | + @property | |
| 39 | + def badge_counters(self): | |
| 40 | + if not hasattr(self, '_badge_counters'): | |
| 41 | + self._badge_counters = get_users_counters() | |
| 42 | + return self._badge_counters | |
| 43 | + | |
| 37 | 44 | def get_updated_field(self): |
| 38 | 45 | return 'date_joined' |
| 39 | 46 | |
| ... | ... | @@ -66,16 +73,16 @@ class UserIndex(indexes.SearchIndex, indexes.Indexable): |
| 66 | 73 | return u'user' |
| 67 | 74 | |
| 68 | 75 | def prepare_message_count(self, obj): |
| 69 | - return obj.emails.aggregate(Count('message'))['message__count'] | |
| 76 | + return self.badge_counters[obj.username]['messages'] | |
| 70 | 77 | |
| 71 | 78 | def prepare_changeset_count(self, obj): |
| 72 | - return Revision.objects.filter(author=obj.username).count() | |
| 79 | + return self.badge_counters[obj.username]['revisions'] | |
| 73 | 80 | |
| 74 | 81 | def prepare_ticket_count(self, obj): |
| 75 | - return Ticket.objects.filter(author=obj.username).count() | |
| 82 | + return self.badge_counters[obj.username]['tickets'] | |
| 76 | 83 | |
| 77 | 84 | def prepare_wiki_count(self, obj): |
| 78 | - return Wiki.objects.filter(author=obj.username).count() | |
| 85 | + return self.badge_counters[obj.username]['wikis'] | |
| 79 | 86 | |
| 80 | 87 | def index_queryset(self, using=None): |
| 81 | 88 | return self.get_model().objects.filter(is_active=True) | ... | ... |
src/badger/management/commands/update_badges.py
src/badger/utils.py
| ... | ... | @@ -2,32 +2,45 @@ |
| 2 | 2 | |
| 3 | 3 | from django.db.models import Count |
| 4 | 4 | |
| 5 | -from haystack.query import SearchQuerySet | |
| 6 | - | |
| 7 | 5 | from proxy.models import Revision, Ticket, Wiki |
| 8 | -from super_archives.models import Message | |
| 9 | - | |
| 10 | - | |
| 11 | -def get_counters_to_badge(user): | |
| 12 | - # count_revisions = Revision.objects.filter(author=user.username).count() | |
| 13 | - # count_tickets = Ticket.objects.filter(author=user.username).count() | |
| 14 | - # count_wikis = Wiki.objects.filter(author=user.username).count() | |
| 15 | - count_revisions = SearchQuerySet().filter( | |
| 16 | - type='changeset', | |
| 17 | - author=user.username | |
| 18 | - ).count() | |
| 19 | - count_tickets = SearchQuerySet().filter( | |
| 20 | - type='ticket', | |
| 21 | - author=user.username | |
| 22 | - ).count() | |
| 23 | - count_wikis = SearchQuerySet().filter( | |
| 24 | - type='wiki', | |
| 25 | - author=user.username | |
| 26 | - ).count() | |
| 27 | - return dict( | |
| 28 | - messages=user.emails.aggregate(Count('message'))['message__count'], | |
| 29 | - revisions=count_revisions, | |
| 30 | - tickets=count_tickets, | |
| 31 | - wikis=count_wikis, | |
| 32 | - contributions=count_revisions + count_tickets + count_wikis, | |
| 33 | - ) | |
| 6 | +from accounts.models import User | |
| 7 | + | |
| 8 | + | |
| 9 | +def get_wiki_counters(): | |
| 10 | + return { | |
| 11 | + author: count for author, count in Wiki.objects.values_list( | |
| 12 | + 'author' | |
| 13 | + ).annotate(count=Count('author')) | |
| 14 | + } | |
| 15 | + | |
| 16 | + | |
| 17 | +def get_revision_counters(): | |
| 18 | + return { | |
| 19 | + author: count for author, count in Revision.objects.values_list( | |
| 20 | + 'author' | |
| 21 | + ).annotate(count=Count('author')) | |
| 22 | + } | |
| 23 | + | |
| 24 | + | |
| 25 | +def get_ticket_counters(): | |
| 26 | + return { | |
| 27 | + author: count for author, count in Ticket.objects.values_list( | |
| 28 | + 'author' | |
| 29 | + ).annotate(count=Count('author')) | |
| 30 | + } | |
| 31 | + | |
| 32 | + | |
| 33 | +def get_users_counters(): | |
| 34 | + wiki_counters = get_wiki_counters() | |
| 35 | + revision_counters = get_revision_counters() | |
| 36 | + ticket_counters = get_ticket_counters() | |
| 37 | + | |
| 38 | + users_counters = {} | |
| 39 | + for user in User.objects.annotate(message_count=Count('emails__message')): | |
| 40 | + users_counters[user.username] = { | |
| 41 | + 'messages': user.message_count, | |
| 42 | + 'wikis': wiki_counters.get(user.username, 0), | |
| 43 | + 'revisions': revision_counters.get(user.username, 0), | |
| 44 | + 'tickets': ticket_counters.get(user.username, 0), | |
| 45 | + } | |
| 46 | + return users_counters | ... | ... |