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
9 class UserJoinsAction(ActionProxy):
10 def repute_users(self):
11 self.repute(self.user, int(settings.INITIAL_REP))
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})
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,
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')),
32 class BonusRepAction(ActionProxy):
33 def process_data(self, value):
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')))
42 def describe(self, viewer=None):
43 value = self.extra.get('value', _('unknown'))
44 message = self.extra.get('message', '')
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
52 class AwardAction(ActionProxy):
53 def process_data(self, badge, trigger):
54 self.__dict__['_badge'] = badge
55 self.__dict__['_trigger'] = trigger
57 def process_action(self):
58 badge = self.__dict__['_badge']
59 trigger = self.__dict__['_trigger']
61 award = Award(user=self.user, badge=badge, trigger=trigger, action=self)
63 award.node = self.node
66 award.badge.awarded_count = F('awarded_count') + 1
69 if award.badge.type == Badge.GOLD:
71 if award.badge.type == Badge.SILVER:
73 if award.badge.type == Badge.BRONZE:
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>."""
81 dict(badge_name=award.badge.name, profile_url=self.user.get_profile_url()))
83 def cancel_action(self):
86 badge.awarded_count = F('awarded_count') - 1
91 def get_for(cls, user, badge, node=False):
94 return Award.objects.get(user=user, badge=badge).action
96 return Award.objects.get(user=user, node=node, badge=badge).action
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,