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, User
5 from forum import settings
6 from forum.settings import APP_SHORT_NAME
7 from forum.utils.mail import send_template_email
9 class UserJoinsAction(ActionProxy):
12 def repute_users(self):
13 self.repute(self.user, int(settings.INITIAL_REP))
15 def process_action(self):
16 hash = ValidationHash.objects.create_new(self.user, 'email', [self.user.email])
17 send_template_email([self.user], "auth/welcome_email.html", {'validation_code': hash})
19 def describe(self, viewer=None):
20 return _("%(user)s %(have_has)s joined the %(app_name)s Q&A community") % {
21 'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
22 'have_has': self.viewer_or_user_verb(viewer, self.user, _('have'), _('has')),
23 'app_name': APP_SHORT_NAME,
26 class EmailValidationAction(ActionProxy):
27 verb = _("validated e-mail")
29 def repute_users(self):
30 self.repute(self.user, int(settings.REP_GAIN_BY_EMAIL_VALIDATION))
32 def process_action(self):
33 self.user.email_isvalid = True
36 def describe(self, viewer=None):
37 return _("%(user)s %(have_has)s validated the e-mail %(email)s") % {
38 'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
39 'have_has': self.viewer_or_user_verb(viewer, self.user, _('have'), _('has')),
40 'email' : self.user.email
43 class EditProfileAction(ActionProxy):
44 verb = _("edited profile")
46 def describe(self, viewer=None):
47 return _("%(user)s edited %(hes_or_your)s %(profile_link)s") % {
48 'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
49 'hes_or_your': self.viewer_or_user_verb(viewer, self.user, _('your'), _('his')),
50 'profile_link': self.hyperlink(self.user.get_profile_url(), _('profile')),
53 class BonusRepAction(ActionProxy):
54 verb = _("gave bonus")
56 def process_data(self, value, affected):
58 self._affected = affected
61 def repute_users(self):
62 self.repute(self._affected, self._value)
65 self._affected.message_set.create(
66 message=_("Congratulations, you have been awarded an extra %s reputation points.") % self._value +
67 '<br />%s' % self.extra.get('message', _('Thank you')))
69 self._affected.message_set.create(
70 message=_("You have been penalized in %s reputation points.") % self._value +
71 '<br />%s' % self.extra.get('message', ''))
73 def describe(self, viewer=None):
74 value = self.extra.get('value', _('unknown'))
75 message = self.extra.get('message', '')
79 return _("%(user)s awarded an extra %(value)s reputation points to %(users)s: %(message)s") % {
80 'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
81 'value': value, 'users':self.affected_links(viewer), 'message': message
84 return _("%(user)s penalised %(users)s in %(value)s reputation points: %(message)s") % {
85 'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
86 'value': value, 'users':self.affected_links(viewer), 'message': message
91 class AwardAction(ActionProxy):
92 verb = _("was awarded")
94 def process_data(self, badge, trigger):
95 self.__dict__['_badge'] = badge
96 self.__dict__['_trigger'] = trigger
98 def process_action(self):
99 badge = self.__dict__['_badge']
100 trigger = self.__dict__['_trigger']
102 award = Award(user=self.user, badge=badge, trigger=trigger, action=self)
104 award.node = self.node
107 award.badge.awarded_count = F('awarded_count') + 1
110 if award.badge.type == Badge.GOLD:
112 if award.badge.type == Badge.SILVER:
113 self.user.silver += 1
114 if award.badge.type == Badge.BRONZE:
115 self.user.bronze += 1
119 self.user.message_set.create(message=_(
120 """Congratulations, you have received a badge '%(badge_name)s'. Check out <a href=\"%(profile_url)s\">your profile</a>."""
122 dict(badge_name=award.badge.name, profile_url=self.user.get_profile_url()))
124 def cancel_action(self):
127 badge.awarded_count = F('awarded_count') - 1
132 def get_for(cls, user, badge, node=False):
135 return Award.objects.get(user=user, badge=badge).action
137 return Award.objects.get(user=user, node=node, badge=badge).action
141 def describe(self, viewer=None):
142 return _("%(user)s %(were_was)s awarded the %(badge_name)s badge") % {
143 'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
144 'were_was': self.viewer_or_user_verb(viewer, self.user, _('were'), _('was')),
145 'badge_name': self.award.badge.name,
148 class SuspendAction(ActionProxy):
149 verb = _("suspended")
151 def process_data(self, **kwargs):
152 self._suspended = kwargs.pop('suspended')
155 def repute_users(self):
156 self.repute(self._suspended, 0)
158 def process_action(self):
159 self._suspended.is_active = False
160 self._suspended.save()
162 def cancel_action(self):
163 for u in User.objects.filter(reputes__action=self).distinct():
165 u._pop_suspension_cache()
167 u.message_set.create(message=_("Your suspension has been removed."))
169 def describe(self, viewer=None):
170 if self.extra.get('bantype', 'indefinitely') == 'forxdays' and self.extra.get('forxdays', None):
171 suspension = _("for %s days") % self.extra['forxdays']
173 suspension = _("indefinetely")
175 return _("%(user)s suspended %(users)s %(suspension)s: %(msg)s") % {
176 'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
177 'users': self.affected_links(viewer), 'suspension': suspension, 'msg': self.extra.get('publicmsg', _('Bad behaviour'))