]> git.openstreetmap.org Git - osqa.git/blob - forum/actions/user.py
Fixes a bug happening when processing an user reputation more than once in the same...
[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 as joined the %(app_name)s Q&A community") % {
19             'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
20             'app_name': APP_SHORT_NAME,
21         }
22
23 class EditProfileAction(ActionProxy):
24     def describe(self, viewer=None):
25         return _("%(user)s edited %(hes_or_your)s %(profile_link)s") % {
26             'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
27             'hes_or_your': self.viewer_or_user_verb(viewer, self.user, _('your'), _('hes')),
28             'profile_link': self.hyperlink(self.user.get_profile_url(), _('profile')),
29         }
30
31 class BonusRepAction(ActionProxy):
32     def process_data(self, value):
33         self._value = value
34
35     def repute_users(self):
36         self.repute(self.user, self._value)
37         self.user.message_set.create(message=_("Congratulations, you have been awarded an extra %s reputation points.") % self._value +
38                                      '<br />%s' % self.extra.get('message', _('Thank you')))
39         
40     def describe(self, viewer=None):
41         value = self.extra.get('value', _('unknown'))
42         message = self.extra.get('message', '')
43
44         return _("%(user)s %(was_were)s awarded %(value)s reputation points: %(message)s") % {
45             'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
46             'was_were': self.viewer_or_user_verb(viewer, self.user, _('were'), _('was')),
47             'value': value, 'message': message
48         }
49
50 class AwardAction(ActionProxy):
51     def process_data(self, badge, trigger):
52         self.__dict__['_badge'] = badge
53         self.__dict__['_trigger'] = trigger
54
55     def process_action(self):
56         badge = self.__dict__['_badge']
57         trigger = self.__dict__['_trigger']
58
59         award = Award(user=self.user, badge=badge, trigger=trigger, action=self)
60         if self.node:
61             award.node = self.node
62
63         award.save()
64         award.badge.awarded_count = F('awarded_count') + 1
65         award.badge.save()
66
67         if award.badge.type == Badge.GOLD:
68             self.user.gold += 1
69         if award.badge.type == Badge.SILVER:
70             self.user.silver += 1
71         if award.badge.type == Badge.BRONZE:
72             self.user.bronze += 1
73
74         self.user.save()
75
76         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>.""") %
77                                      dict(badge_name=award.badge.name, profile_url=self.user.get_profile_url()))
78
79     def cancel_action(self):
80         award = self.award
81         badge = award.badge
82         badge.awarded_count = F('awarded_count') - 1
83         badge.save()
84         award.delete()
85
86     @classmethod
87     def get_for(cls, user, badge, node=False):
88         try:
89             if node is False:
90                 return Award.objects.get(user=user, badge=badge).action
91             else:
92                 return Award.objects.get(user=user, node=node, badge=badge).action
93         except:
94             return None
95
96     def describe(self, viewer=None):
97         return _("%(user)s %(were_was)s awarded the %(badge_name)s badge") % {
98             'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
99             'were_was': self.viewer_or_user_verb(viewer, self.user, _('were'), _('was')),
100             'badge_name': self.award.badge.name,
101         }