]> git.openstreetmap.org Git - osqa.git/blob - forum/actions/user.py
Merging the experimental branch back to trunk.
[osqa.git] / forum / actions / user.py
1 from django.utils.translation import ugettext as _\r
2 from django.db.models import F\r
3 from forum.models.action import ActionProxy\r
4 from forum.models import Award\r
5 from forum import settings\r
6 \r
7 class UserJoinsAction(ActionProxy):\r
8     def repute_users(self):\r
9         self.repute(self.user, int(settings.INITIAL_REP))\r
10 \r
11 class EditProfileAction(ActionProxy):\r
12     pass\r
13 \r
14 class AwardAction(ActionProxy):\r
15     def process_data(self, badge, trigger):\r
16         self.__dict__['_badge'] = badge\r
17         self.__dict__['_trigger'] = trigger\r
18 \r
19     def process_action(self):\r
20         badge = self.__dict__['_badge']\r
21         trigger = self.__dict__['_trigger']\r
22 \r
23         award = Award(user=self.user, badge=badge, trigger=trigger, action=self)\r
24         if self.node:\r
25             award.node = self.node\r
26 \r
27         award.save()\r
28         award.badge.awarded_count = F('awarded_count') + 1\r
29         award.badge.save()\r
30         self.user.message_set.create(message=_("Congratulations, you have received a badge '%(badge_name)s'. " \\r
31                 + u"Check out <a href=\"%(profile_url)s\">your profile</a>.") % dict(badge_name=award.badge.name,\r
32                                                                                     profile_url=self.user.get_profile_url()))\r
33 \r
34     def cancel_action(self):\r
35         award = self.award\r
36         badge = award.badge\r
37         badge.awarded_count = F('awarded_count') - 1\r
38         badge.save()\r
39         award.delete()\r
40 \r
41     @classmethod\r
42     def get_for(cls, user, node, badge):\r
43         try:\r
44             award = Award.objects.get(user=user, node=node, badge=badge)\r
45             return award.action\r
46         except:\r
47             return None\r
48 \r
49     def describe(self, viewer=None):\r
50         return _("%(user)s was awarded the %(badge_name)s badge") % {\r
51             'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),\r
52             'badge_name': self.award.all()[0].badge.name,\r
53         }