1 from django.utils.translation import ugettext as _
2 from django.db.models import F
3 from django.core.exceptions import ObjectDoesNotExist, MultipleObjectsReturned
4 from forum.models.action import ActionProxy
5 from forum.models import Award, Badge, ValidationHash
6 from forum import settings
7 from forum.settings import APP_SHORT_NAME
8 from forum.utils.mail import send_template_email
10 class UserJoinsAction(ActionProxy):
11 def repute_users(self):
12 self.repute(self.user, int(settings.INITIAL_REP))
14 def process_action(self):
15 hash = ValidationHash.objects.create_new(self.user, 'email', [self.user.email])
16 send_template_email([self.user], "auth/welcome_email.html", {'validation_code': hash})
18 def describe(self, viewer=None):
19 return _("%(user)s %(have_has)s joined the %(app_name)s Q&A community") % {
20 'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
21 'have_has': self.viewer_or_user_verb(viewer, self.user, _('have'), _('has')),
22 'app_name': APP_SHORT_NAME,
25 class EditProfileAction(ActionProxy):
26 def describe(self, viewer=None):
27 return _("%(user)s edited %(hes_or_your)s %(profile_link)s") % {
28 'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
29 'hes_or_your': self.viewer_or_user_verb(viewer, self.user, _('your'), _('his')),
30 'profile_link': self.hyperlink(self.user.get_profile_url(), _('profile')),
33 class BonusRepAction(ActionProxy):
34 def process_data(self, value):
37 def repute_users(self):
38 self.repute(self.user, self._value)
39 self.user.message_set.create(
40 message=_("Congratulations, you have been awarded an extra %s reputation points.") % self._value +
41 '<br />%s' % self.extra.get('message', _('Thank you')))
43 def describe(self, viewer=None):
44 value = self.extra.get('value', _('unknown'))
45 message = self.extra.get('message', '')
47 return _("%(user)s %(was_were)s awarded %(value)s reputation points: %(message)s") % {
48 'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
49 'was_were': self.viewer_or_user_verb(viewer, self.user, _('were'), _('was')),
50 'value': value, 'message': message
53 class AwardAction(ActionProxy):
54 def process_data(self, badge, trigger):
55 self.__dict__['_badge'] = badge
56 self.__dict__['_trigger'] = trigger
58 def process_action(self):
59 badge = self.__dict__['_badge']
60 trigger = self.__dict__['_trigger']
62 award = Award(user=self.user, badge=badge, trigger=trigger, action=self)
64 award.node = self.node
67 award.badge.awarded_count = F('awarded_count') + 1
70 if award.badge.type == Badge.GOLD:
72 if award.badge.type == Badge.SILVER:
74 if award.badge.type == Badge.BRONZE:
79 self.user.message_set.create(message=_(
80 """Congratulations, you have received a badge '%(badge_name)s'. Check out <a href=\"%(profile_url)s\">your profile</a>."""
82 dict(badge_name=award.badge.name, profile_url=self.user.get_profile_url()))
84 def cancel_action(self):
87 badge.awarded_count = F('awarded_count') - 1
92 def get_for(cls, user, badge, node=False):
95 return Award.objects.get(user=user, badge=badge).action
97 return Award.objects.get(user=user, node=node, badge=badge).action
98 except ObjectDoesNotExist:
101 def describe(self, viewer=None):
102 return _("%(user)s %(were_was)s awarded the %(badge_name)s badge") % {
103 'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
104 'were_was': self.viewer_or_user_verb(viewer, self.user, _('were'), _('was')),
105 'badge_name': self.award.badge.name,
108 class SuspendAction(ActionProxy):
109 def process_data(self, **kwargs):
112 def process_action(self):
113 self.user.is_active = False
116 def cancel_action(self):
117 self.user.is_active = True
118 self.user._pop_suspension_cache()
120 self.user.message_set.create(message=_("Your suspension has been removed."))
122 def describe(self, viewer=None):
123 if self.extra.get('bantype', 'indefinitely') == 'forxdays' and self.extra.get('forxdays', None):
124 suspension = _("for %s days") % self.extra['forxdays']
126 suspension = _("indefinetely")
128 return _("%(user)s %(were_was)s suspended %(suspension)s: %(msg)s") % {
129 'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
130 'were_was': self.viewer_or_user_verb(viewer, self.user, _('were'), _('was')),
131 'suspension': suspension, 'msg': self.extra.get('publicmsg', _('Bad behaviour'))