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