summaryrefslogtreecommitdiffstats
path: root/askbot/management/commands/askbot_award_badges.py
blob: 265eafcea56ca767cf759e2188a47dc80fcc1414 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
"""WARNING: 
This command is incomplete, current awards only
Civic Duty badge
"""

from askbot.models import badges
from askbot.models import User
from askbot.models import Vote
from askbot.utils.console import ProgressBar
import datetime
from django.core.management.base import NoArgsCommand

class Command(NoArgsCommand):
    def handle_noargs(self, *args, **kwargs):
        now = datetime.datetime.now()
        awarded_count = 0

        users = User.objects.all()
        count = users.count()
        message = 'Awarding badges for each user'
        for user in ProgressBar(users.iterator(), count, message):
            try:
                #get last vote
                vote = Vote.objects.filter(user=user).order_by('-id')[0]
            except IndexError:
                #user did not vote
                continue
            else:
                cd = badges.CivicDuty()
                awarded = cd.consider_award(
                            actor=user,
                            context_object=vote.voted_post,
                            timestamp=now
                        )

        print 'Awarded %d badges' % awarded_count