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 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,
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')),
31 class BonusRepAction(ActionProxy):
32 def process_data(self, value):
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')))
40 def describe(self, viewer=None):
41 value = self.extra.get('value', _('unknown'))
42 message = self.extra.get('message', '')
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
50 class AwardAction(ActionProxy):
51 def process_data(self, badge, trigger):
52 self.__dict__['_badge'] = badge
53 self.__dict__['_trigger'] = trigger
55 def process_action(self):
56 badge = self.__dict__['_badge']
57 trigger = self.__dict__['_trigger']
59 award = Award(user=self.user, badge=badge, trigger=trigger, action=self)
61 award.node = self.node
64 award.badge.awarded_count = F('awarded_count') + 1
67 if award.badge.type == Badge.GOLD:
69 if award.badge.type == Badge.SILVER:
71 if award.badge.type == Badge.BRONZE:
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()))
79 def cancel_action(self):
82 badge.awarded_count = F('awarded_count') - 1
87 def get_for(cls, user, badge, node=False):
90 return Award.objects.get(user=user, badge=badge).action
92 return Award.objects.get(user=user, node=node, badge=badge).action
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,