1 from django.utils.translation import ungettext, 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 UserLoginAction(ActionProxy):
29 def describe(self, viewer=None):
30 return _("%(user)s %(have_has)s logged in") % {
31 'user' : self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
32 'have_has': self.viewer_or_user_verb(viewer, self.user, _('have'), _('has')),
35 class EmailValidationAction(ActionProxy):
36 verb = _("validated e-mail")
38 def repute_users(self):
39 self.repute(self.user, int(settings.REP_GAIN_BY_EMAIL_VALIDATION))
41 def process_action(self):
42 self.user.email_isvalid = True
45 def describe(self, viewer=None):
46 return _("%(user)s %(have_has)s validated the e-mail %(email)s") % {
47 'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
48 'have_has': self.viewer_or_user_verb(viewer, self.user, _('have'), _('has')),
49 'email' : self.user.email if viewer.is_superuser or viewer.is_staff or viewer == self.user else ""
52 class EditProfileAction(ActionProxy):
53 verb = _("edited profile")
55 def describe(self, viewer=None):
56 return _("%(user)s edited %(hes_or_your)s %(profile_link)s") % {
57 'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
58 'hes_or_your': self.viewer_or_user_verb(viewer, self.user, _('your'), _('his')),
59 'profile_link': self.hyperlink(self.user.get_profile_url(), _('profile')),
62 class BonusRepAction(ActionProxy):
63 verb = _("gave bonus")
65 def process_data(self, value, affected):
67 self._affected = affected
70 def repute_users(self):
71 self.repute(self._affected, self._value)
74 self._affected.message_set.create(
75 message=_("Congratulations, you have been awarded an extra %s reputation points.") % self._value +
76 '<br />%s' % self.extra.get('message', _('Thank you')))
78 self._affected.message_set.create(
79 message=_("You have been penalized in %s reputation points.") % self._value +
80 '<br />%s' % self.extra.get('message', ''))
82 def describe(self, viewer=None):
83 value = self.extra.get('value', _('unknown'))
84 message = self.extra.get('message', '')
88 return _("%(user)s awarded an extra %(value)s reputation points to %(users)s: %(message)s") % {
89 'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
90 'value': value, 'users':self.affected_links(viewer), 'message': message
93 return _("%(user)s penalised %(users)s in %(value)s reputation points: %(message)s") % {
94 'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
95 'value': value, 'users':self.affected_links(viewer), 'message': message
100 class AwardPointsAction(ActionProxy):
101 verb = _("gave reputation points")
103 def process_data(self, value, affected):
105 self._affected = affected
108 def repute_users(self):
109 self.repute(self._affected, self._value)
110 self.repute(self.user, -self._value)
113 self._affected.message_set.create(
114 message=_("Congratulations, you have been awarded an extra %(points)s reputation %(points_label)s on <a href=\"%(answer_url)s\">this</a> answer.") % {
115 'points': self._value,
116 'points_label': ungettext('point', 'points', self._value),
117 'answer_url': self.node.get_absolute_url()
120 def describe(self, viewer=None):
121 value = self.extra.get('value', _('unknown'))
125 return _("%(user)s awarded an extra %(value)s reputation points to %(users)s") % {
126 'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
127 'value': value, 'users':self.affected_links(viewer),
130 return _("%(user)s penalised %(users)s in %(value)s reputation points") % {
131 'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
132 'value': value, 'users':self.affected_links(viewer),
137 class AwardAction(ActionProxy):
138 verb = _("was awarded")
140 def process_data(self, badge, trigger):
141 self.__dict__['_badge'] = badge
142 self.__dict__['_trigger'] = trigger
144 def process_action(self):
145 badge = self.__dict__['_badge']
146 trigger = self.__dict__['_trigger']
148 award = Award(user=self.user, badge=badge, trigger=trigger, action=self)
150 award.node = self.node
153 award.badge.awarded_count = F('awarded_count') + 1
156 if award.badge.type == Badge.GOLD:
158 if award.badge.type == Badge.SILVER:
159 self.user.silver += 1
160 if award.badge.type == Badge.BRONZE:
161 self.user.bronze += 1
165 self.user.message_set.create(message=_(
166 """Congratulations, you have received a badge '%(badge_name)s'. Check out <a href=\"%(profile_url)s\">your profile</a>."""
168 dict(badge_name=award.badge.name, profile_url=self.user.get_profile_url()))
170 def cancel_action(self):
173 badge.awarded_count = F('awarded_count') - 1
178 def get_for(cls, user, badge, node=False):
181 return Award.objects.get(user=user, badge=badge).action
183 return Award.objects.get(user=user, node=node, badge=badge).action
187 def describe(self, viewer=None):
188 return _("%(user)s %(were_was)s awarded the %(badge_name)s badge") % {
189 'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
190 'were_was': self.viewer_or_user_verb(viewer, self.user, _('were'), _('was')),
191 'badge_name': self.award.badge.name,
194 class SuspendAction(ActionProxy):
195 verb = _("suspended")
197 def process_data(self, **kwargs):
198 self._suspended = kwargs.pop('suspended')
201 def repute_users(self):
202 self.repute(self._suspended, 0)
204 def process_action(self):
205 self._suspended.is_active = False
206 self._suspended.save()
208 def cancel_action(self):
209 for u in User.objects.filter(reputes__action=self).distinct():
211 u._pop_suspension_cache()
213 u.message_set.create(message=_("Your suspension has been removed."))
215 def describe(self, viewer=None):
216 if self.extra.get('bantype', 'indefinitely') == 'forxdays' and self.extra.get('forxdays', None):
217 suspension = _("for %s days") % self.extra['forxdays']
219 suspension = _("indefinetely")
221 return _("%(user)s suspended %(users)s %(suspension)s: %(msg)s") % {
222 'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
223 'users': self.affected_links(viewer), 'suspension': suspension, 'msg': self.extra.get('publicmsg', _('Bad behaviour'))