]> git.openstreetmap.org Git - osqa.git/blob - forum/actions/user.py
Added a direct option to mark a post as community wiki, still needs some sort of...
[osqa.git] / forum / actions / user.py
1 from django.utils.translation import ugettext as _
2 from django.db.models import F
3 from forum.models.action import ActionProxy
4 from forum.models import Award, Badge
5 from forum import settings
6 from forum.settings import APP_SHORT_NAME
7
8 class UserJoinsAction(ActionProxy):
9     def repute_users(self):
10         self.repute(self.user, int(settings.INITIAL_REP))
11
12     def describe(self, viewer=None):
13         return _("%(user)s as joined the %(app_name)s Q&A community") % {
14             'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
15             'app_name': APP_SHORT_NAME,
16         }
17
18 class EditProfileAction(ActionProxy):
19     def describe(self, viewer=None):
20         return _("%(user)s edited %(hes_or_your)s %(profile_link)s") % {
21             'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
22             'hes_or_your': self.viewer_or_user_verb(viewer, self.user, _('your'), _('hes')),
23             'profile_link': self.hyperlink(self.user.get_profile_url(), _('profile')),
24         }
25
26 class BonusRepAction(ActionProxy):
27     def process_data(self, value):
28         self._value = value
29
30     def repute_users(self):
31         self.repute(self.user, self._value)
32         self.user.message_set.create(message=_("Congratulations, you have been awarded an extra %s reputation points.") % self._value +
33                                      '<br />%s' % self.extra.get('message', _('Thank you')))
34         
35     def describe(self, viewer=None):
36         value = self.extra.get('value', _('unknown'))
37         message = self.extra.get('message', '')
38
39         return _("%(user)s %(was_were)s awarded %(value)s reputation points: %(message)s") % {
40             'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
41             'was_were': self.viewer_or_user_verb(viewer, self.user, _('were'), _('was')),
42             'value': value, 'message': message
43         }
44
45 class AwardAction(ActionProxy):
46     def process_data(self, badge, trigger):
47         self.__dict__['_badge'] = badge
48         self.__dict__['_trigger'] = trigger
49
50     def process_action(self):
51         badge = self.__dict__['_badge']
52         trigger = self.__dict__['_trigger']
53
54         award = Award(user=self.user, badge=badge, trigger=trigger, action=self)
55         if self.node:
56             award.node = self.node
57
58         award.save()
59         award.badge.awarded_count = F('awarded_count') + 1
60         award.badge.save()
61
62         if award.badge.type == Badge.GOLD:
63             self.user.gold += 1
64         if award.badge.type == Badge.SILVER:
65             self.user.silver += 1
66         if award.badge.type == Badge.BRONZE:
67             self.user.bronze += 1
68
69         self.user.save()
70
71         self.user.message_set.create(message=_("""Congratulations, you have received a badge '%(badge_name)s'. Check out <a href=\"%(profile_url)s\">your profile</a>.""") %
72                                      dict(badge_name=award.badge.name, profile_url=self.user.get_profile_url()))
73
74     def cancel_action(self):
75         award = self.award
76         badge = award.badge
77         badge.awarded_count = F('awarded_count') - 1
78         badge.save()
79         award.delete()
80
81     @classmethod
82     def get_for(cls, user, badge, node=False):
83         try:
84             if node is False:
85                 return Award.objects.get(user=user, badge=badge).action
86             else:
87                 return Award.objects.get(user=user, node=node, badge=badge).action
88         except:
89             return None
90
91     def describe(self, viewer=None):
92         return _("%(user)s %(were_was)s awarded the %(badge_name)s badge") % {
93             'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
94             'were_was': self.viewer_or_user_verb(viewer, self.user, _('were'), _('was')),
95             'badge_name': self.award.badge.name,
96         }