Commit d2f3f322e94656e18a861b99264cb68ff9c6656d
1 parent
6193fead
Exists in
master
and in
39 other branches
Adding command to update the badges
Showing
4 changed files
with
57 additions
and
0 deletions
Show diff stats
| @@ -0,0 +1,30 @@ | @@ -0,0 +1,30 @@ | ||
| 1 | +# -*- coding: utf-8 -*- | ||
| 2 | + | ||
| 3 | +from django.core.management.base import BaseCommand, CommandError | ||
| 4 | + | ||
| 5 | +from accounts.models import User | ||
| 6 | +from badger.utils import get_counters_to_badge | ||
| 7 | +from badger.models import Badge | ||
| 8 | + | ||
| 9 | + | ||
| 10 | +class Command(BaseCommand): | ||
| 11 | + help = "Updates the user's badges" | ||
| 12 | + | ||
| 13 | + def handle(self, *args, **kwargs): | ||
| 14 | + for badge in Badge.objects.filter(type='auto'): | ||
| 15 | + if not badge.comparison: | ||
| 16 | + continue | ||
| 17 | + for user in User.objects.all(): | ||
| 18 | + user_counters = get_counters_to_badge(user) | ||
| 19 | + | ||
| 20 | + # TODO remove user if it doesn't sastify the conditions of the | ||
| 21 | + # badge anymore | ||
| 22 | + if badge.comparison == 'gte': | ||
| 23 | + if user_counters[badge.user_attr] >= badge.value: | ||
| 24 | + badge.awardees.add(user) | ||
| 25 | + elif badge.comparison == 'lte': | ||
| 26 | + if user_counters[badge.user_attr] <= badge.value: | ||
| 27 | + badge.awardees.add(user) | ||
| 28 | + elif badge.comparison == 'equal': | ||
| 29 | + if user_counters[badge.user_attr] == badge.value: | ||
| 30 | + badge.awardees.add(user) |
| @@ -0,0 +1,27 @@ | @@ -0,0 +1,27 @@ | ||
| 1 | +# -*- coding: utf-8 -*- | ||
| 2 | + | ||
| 3 | +from django.db.models import Count | ||
| 4 | + | ||
| 5 | +from proxy.models import Revision, Ticket, Wiki | ||
| 6 | +from super_archives.models import Message | ||
| 7 | + | ||
| 8 | + | ||
| 9 | +def get_counters_to_badge(user): | ||
| 10 | + count_revisions = Revision.objects.filter(author=user.username).count() | ||
| 11 | + count_tickets = Ticket.objects.filter(author=user.username).count() | ||
| 12 | + count_wikis = Wiki.objects.filter(author=user.username).count() | ||
| 13 | + | ||
| 14 | + return dict( | ||
| 15 | + messages=user.emails.aggregate(Count('message'))['message__count'], | ||
| 16 | + revisions=count_revisions, | ||
| 17 | + tickets=count_tickets, | ||
| 18 | + wikis=count_wikis, | ||
| 19 | + contributions=count_revisions + count_tickets + count_wikis, | ||
| 20 | + ) | ||
| 21 | + | ||
| 22 | +# using haystack | ||
| 23 | +# sqs = SearchQuerySet() | ||
| 24 | +# sqs.filter(type='changeset', author=user.get_full_name()).count() | ||
| 25 | +# sqs.filter(type='wiki', author=user.get_full_name()).count() | ||
| 26 | +# sqs.filter(type='ticket', author=user.get_full_name()).count() | ||
| 27 | +# Should it use the author_and_username attr too? |