1 from django.utils.translation import ungettext, ugettext as _
2 from django.core.urlresolvers import reverse
3 from django.db.models import F
4 from forum.models.action import ActionProxy
5 from forum.models import Award, Badge, ValidationHash, User
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):
13 def repute_users(self):
14 self.repute(self.user, int(settings.INITIAL_REP))
16 def process_action(self):
17 hash = ValidationHash.objects.create_new(self.user, 'email', [self.user.email])
18 send_template_email([self.user], "auth/welcome_email.html", {'validation_code': hash})
20 def describe(self, viewer=None):
21 return _("%(user)s %(have_has)s joined the %(app_name)s Q&A community") % {
22 'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
23 'have_has': self.viewer_or_user_verb(viewer, self.user, _('have'), _('has')),
24 'app_name': APP_SHORT_NAME,
27 class UserLoginAction(ActionProxy):
30 def describe(self, viewer=None):
31 return _("%(user)s %(have_has)s logged in") % {
32 'user' : self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
33 'have_has': self.viewer_or_user_verb(viewer, self.user, _('have'), _('has')),
36 class EmailValidationAction(ActionProxy):
37 verb = _("validated e-mail")
39 def repute_users(self):
40 self.repute(self.user, int(settings.REP_GAIN_BY_EMAIL_VALIDATION))
42 def process_action(self):
43 self.user.email_isvalid = True
46 def describe(self, viewer=None):
47 return _("%(user)s %(have_has)s validated the e-mail %(email)s") % {
48 'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
49 'have_has': self.viewer_or_user_verb(viewer, self.user, _('have'), _('has')),
50 'email' : self.user.email if viewer.is_superuser or viewer.is_staff or viewer == self.user else ""
53 class EditProfileAction(ActionProxy):
54 verb = _("edited profile")
56 def describe(self, viewer=None):
57 return _("%(user)s edited %(hes_or_your)s %(profile_link)s") % {
58 'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
59 'hes_or_your': self.viewer_or_user_verb(viewer, self.user, _('your'), _('his')),
60 'profile_link': self.hyperlink(self.user.get_profile_url(), _('profile')),
63 class BonusRepAction(ActionProxy):
64 verb = _("gave bonus")
66 def process_data(self, value, affected):
68 self._affected = affected
71 def repute_users(self):
72 self.repute(self._affected, self._value)
75 self._affected.message_set.create(
76 message=_("Congratulations, you have been awarded an extra %s reputation points.") % self._value +
77 '<br />%s' % self.extra.get('message', _('Thank you')))
79 self._affected.message_set.create(
80 message=_("You have been penalized in %s reputation points.") % self._value +
81 '<br />%s' % self.extra.get('message', ''))
83 def describe(self, viewer=None):
84 value = self.extra.get('value', _('unknown'))
85 message = self.extra.get('message', '')
89 return _("%(user)s awarded an extra %(value)s reputation points to %(users)s: %(message)s") % {
90 'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
91 'value': value, 'users':self.affected_links(viewer), 'message': message
94 return _("%(user)s penalised %(users)s in %(value)s reputation points: %(message)s") % {
95 'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
96 'value': value, 'users':self.affected_links(viewer), 'message': message
101 class AwardPointsAction(ActionProxy):
102 verb = _("gave reputation points")
104 def process_data(self, value, affected):
106 self._affected = affected
109 def repute_users(self):
110 self.repute(self._affected, self._value)
111 self.repute(self.user, -self._value)
114 self._affected.message_set.create(
115 message=_("Congratulations, you have been awarded an extra %(points)s reputation %(points_label)s on <a href=\"%(answer_url)s\">this</a> answer.") % {
116 'points': self._value,
117 'points_label': ungettext('point', 'points', self._value),
118 'answer_url': self.node.get_absolute_url()
121 def describe(self, viewer=None):
122 value = self.extra.get('value', _('unknown'))
126 return _("%(user)s awarded an extra %(value)s reputation points to %(users)s") % {
127 'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
128 'value': value, 'users':self.affected_links(viewer),
131 return _("%(user)s penalised %(users)s in %(value)s reputation points") % {
132 'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
133 'value': value, 'users':self.affected_links(viewer),
138 class AwardAction(ActionProxy):
139 verb = _("was awarded")
141 def process_data(self, badge, trigger):
142 self.__dict__['_badge'] = badge
143 self.__dict__['_trigger'] = trigger
145 def process_action(self):
146 badge = self.__dict__['_badge']
147 trigger = self.__dict__['_trigger']
149 award = Award(user=self.user, badge=badge, trigger=trigger, action=self)
151 award.node = self.node
154 award.badge.awarded_count = F('awarded_count') + 1
157 if award.badge.type == Badge.GOLD:
159 if award.badge.type == Badge.SILVER:
160 self.user.silver += 1
161 if award.badge.type == Badge.BRONZE:
162 self.user.bronze += 1
166 self.user.message_set.create(message=_(
167 """Congratulations, you have received a badge '%(badge_name)s'. <a href="%(badge_url)s">Find out who has it, too</a>."""
169 badge_name=award.badge.name,
170 badge_url=award.badge.get_absolute_url()))
172 def cancel_action(self):
175 badge.awarded_count = F('awarded_count') - 1
180 def get_for(cls, user, badge, node=False):
183 return Award.objects.get(user=user, badge=badge).action
185 return Award.objects.get(user=user, node=node, badge=badge).action
189 def describe(self, viewer=None):
190 return _("%(user)s %(were_was)s awarded the %(badge_name)s badge") % {
191 'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
192 'were_was': self.viewer_or_user_verb(viewer, self.user, _('were'), _('was')),
193 'badge_name': self.award.badge.name,
196 class SuspendAction(ActionProxy):
197 verb = _("suspended")
199 def process_data(self, **kwargs):
200 self._suspended = kwargs.pop('suspended')
203 def repute_users(self):
204 self.repute(self._suspended, 0)
206 def process_action(self):
207 self._suspended.is_active = False
208 self._suspended.save()
210 def cancel_action(self):
211 for u in User.objects.filter(reputes__action=self).distinct():
213 u._pop_suspension_cache()
215 u.message_set.create(message=_("Your suspension has been removed."))
217 def describe(self, viewer=None):
218 if self.extra.get('bantype', 'indefinitely') == 'forxdays' and self.extra.get('forxdays', None):
219 suspension = _("for %s days") % self.extra['forxdays']
221 suspension = _("indefinetely")
223 return _("%(user)s suspended %(users)s %(suspension)s: %(msg)s") % {
224 'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
225 'users': self.affected_links(viewer), 'suspension': suspension, 'msg': self.extra.get('publicmsg', _('Bad behaviour'))