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, REQUEST_HOLDER
7 from forum.settings import APP_SHORT_NAME
8 from forum.utils.mail import send_template_email
10 from django.contrib import messages
12 class UserJoinsAction(ActionProxy):
15 def repute_users(self):
16 self.repute(self.user, int(settings.INITIAL_REP))
18 def process_action(self):
19 hash = ValidationHash.objects.create_new(self.user, 'email', [self.user.email])
20 send_template_email([self.user], "auth/welcome_email.html", {'validation_code': hash})
22 def describe(self, viewer=None):
23 return _("%(user)s %(have_has)s joined the %(app_name)s Q&A community") % {
24 'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
25 'have_has': self.viewer_or_user_verb(viewer, self.user, _('have'), _('has')),
26 'app_name': APP_SHORT_NAME,
29 class UserLoginAction(ActionProxy):
32 def describe(self, viewer=None):
33 return _("%(user)s %(have_has)s logged in") % {
34 'user' : self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
35 'have_has': self.viewer_or_user_verb(viewer, self.user, _('have'), _('has')),
38 class EmailValidationAction(ActionProxy):
39 verb = _("validated e-mail")
41 def repute_users(self):
42 self.repute(self.user, int(settings.REP_GAIN_BY_EMAIL_VALIDATION))
44 def process_action(self):
45 self.user.email_isvalid = True
48 def describe(self, viewer=None):
49 return _("%(user)s %(have_has)s validated the e-mail %(email)s") % {
50 'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
51 'have_has': self.viewer_or_user_verb(viewer, self.user, _('have'), _('has')),
52 'email' : self.user.email if viewer.is_superuser or viewer.is_staff or viewer == self.user else ""
55 class EditProfileAction(ActionProxy):
56 verb = _("edited profile")
58 def describe(self, viewer=None):
59 return _("%(user)s edited %(hes_or_your)s %(profile_link)s") % {
60 'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
61 'hes_or_your': self.viewer_or_user_verb(viewer, self.user, _('your'), _('his')),
62 'profile_link': self.hyperlink(self.user.get_profile_url(), _('profile')),
65 class BonusRepAction(ActionProxy):
66 verb = _("gave bonus")
68 def process_data(self, value, affected):
70 self._affected = affected
73 def repute_users(self):
74 self.repute(self._affected, self._value)
77 self._affected.message_set.create(
78 message=_("Congratulations, you have been awarded an extra %s reputation points.") % self._value +
79 '<br />%s' % self.extra.get('message', _('Thank you')))
81 messages.info(REQUEST_HOLDER.request, _("You have penalized %s in %s reputation points.") % (self._affected, self._value) +
82 '<br />%s' % self.extra.get('message', ''))
84 def describe(self, viewer=None):
85 value = self.extra.get('value', _('unknown'))
86 message = self.extra.get('message', '')
90 return _("%(user)s awarded an extra %(value)s reputation points to %(users)s: %(message)s") % {
91 'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
92 'value': value, 'users':self.affected_links(viewer), 'message': message
95 return _("%(user)s penalised %(users)s in %(value)s reputation points: %(message)s") % {
96 'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
97 'value': value, 'users':self.affected_links(viewer), 'message': message
102 class AwardPointsAction(ActionProxy):
103 verb = _("gave reputation points")
105 def process_data(self, value, affected):
107 self._affected = affected
110 def repute_users(self):
111 self.repute(self._affected, self._value)
112 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,
197 class ReportAction(ActionProxy):
198 verb = _("suspended")
200 def process_data(self, **kwargs):
205 def process_action(self):
207 all_superusers = User.objects.filter(is_superuser=True)
210 send_template_email(all_superusers, "notifications/user_reported.html", {
211 'reported': self.extra['reported'],
213 'message': self.extra['publicmsg']
217 def describe(self, viewer=None):
219 return _("%(user)s reported %(reported) : %(msg)s") % {
220 'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
221 'reporter': self.extra.get('reported').username,
222 'msg': self.extra.get('publicmsg', _('N/A'))
225 class SuspendAction(ActionProxy):
226 verb = _("suspended")
228 def process_data(self, **kwargs):
229 self._suspended = kwargs.pop('suspended')
232 def repute_users(self):
233 self.repute(self._suspended, 0)
235 def process_action(self):
236 self._suspended.is_active = False
237 self._suspended.save()
239 def cancel_action(self):
240 for u in User.objects.filter(reputes__action=self).distinct():
242 u._pop_suspension_cache()
244 u.message_set.create(message=_("Your suspension has been removed."))
246 def describe(self, viewer=None):
247 if self.extra.get('bantype', 'indefinitely') == 'forxdays' and self.extra.get('forxdays', None):
248 suspension = _("for %s days") % self.extra['forxdays']
250 suspension = _("indefinetely")
252 return _("%(user)s suspended %(users)s %(suspension)s: %(msg)s") % {
253 'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
254 'users': self.affected_links(viewer), 'suspension': suspension, 'msg': self.extra.get('publicmsg', _('Bad behaviour'))