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 django.contrib import messages
5 from forum.models.action import ActionProxy
6 from forum.models import Award, Badge, ValidationHash, User
7 from forum import settings, REQUEST_HOLDER
8 from forum.settings import APP_SHORT_NAME
9 from forum.utils.mail import send_template_email
11 from django.contrib import messages
13 class UserJoinsAction(ActionProxy):
16 def repute_users(self):
17 self.repute(self.user, int(settings.INITIAL_REP))
19 def process_action(self):
20 hash = ValidationHash.objects.create_new(self.user, 'email', [self.user.email])
21 send_template_email([self.user], "auth/welcome_email.html", {'validation_code': hash})
23 def describe(self, viewer=None):
24 return _("%(user)s %(have_has)s joined the %(app_name)s Q&A community") % {
25 'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
26 'have_has': self.viewer_or_user_verb(viewer, self.user, _('have'), _('has')),
27 'app_name': APP_SHORT_NAME,
30 class UserLoginAction(ActionProxy):
33 def describe(self, viewer=None):
34 return _("%(user)s %(have_has)s logged in") % {
35 'user' : self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
36 'have_has': self.viewer_or_user_verb(viewer, self.user, _('have'), _('has')),
39 class EmailValidationAction(ActionProxy):
40 verb = _("validated e-mail")
42 def repute_users(self):
43 self.repute(self.user, int(settings.REP_GAIN_BY_EMAIL_VALIDATION))
45 def process_action(self):
46 self.user.email_isvalid = True
49 def describe(self, viewer=None):
50 return _("%(user)s %(have_has)s validated the e-mail %(email)s") % {
51 'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
52 'have_has': self.viewer_or_user_verb(viewer, self.user, _('have'), _('has')),
53 'email' : self.user.email if viewer.is_superuser or viewer.is_staff or viewer == self.user else ""
56 class EditProfileAction(ActionProxy):
57 verb = _("edited profile")
59 def describe(self, viewer=None):
60 return _("%(user)s edited %(hes_or_your)s %(profile_link)s") % {
61 'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
62 'hes_or_your': self.viewer_or_user_verb(viewer, self.user, _('your'), _('his')),
63 'profile_link': self.hyperlink(self.user.get_profile_url(), _('profile')),
66 class BonusRepAction(ActionProxy):
67 verb = _("gave bonus")
69 def process_data(self, value, affected):
71 self._affected = affected
74 def repute_users(self):
75 self.repute(self._affected, self._value)
78 self._affected.message_set.create(
79 message=_("Congratulations, you have been awarded an extra %s reputation points.") % self._value +
80 '<br />%s' % self.extra.get('message', _('Thank you')))
82 messages.info(REQUEST_HOLDER.request, _("You have penalized %s in %s reputation points.") % (self._affected, self._value) +
83 '<br />%s' % self.extra.get('message', ''))
85 def describe(self, viewer=None):
86 value = self.extra.get('value', _('unknown'))
87 message = self.extra.get('message', '')
91 return _("%(user)s awarded an extra %(value)s reputation points to %(users)s: %(message)s") % {
92 'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
93 'value': value, 'users':self.affected_links(viewer), 'message': message
96 return _("%(user)s penalised %(users)s in %(value)s reputation points: %(message)s") % {
97 'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
98 'value': value, 'users':self.affected_links(viewer), 'message': message
103 class AwardPointsAction(ActionProxy):
104 verb = _("gave reputation points")
106 def process_data(self, value, affected):
108 self._affected = affected
111 def repute_users(self):
112 self.repute(self._affected, self._value)
113 self.repute(self.user, -self._value)
115 self._affected.message_set.create(
116 message=_("Congratulations, you have been awarded an extra %(points)s reputation %(points_label)s on <a href=\"%(answer_url)s\">this</a> answer.") % {
117 'points': self._value,
118 'points_label': ungettext('point', 'points', self._value),
119 'answer_url': self.node.get_absolute_url()
122 def describe(self, viewer=None):
123 value = self.extra.get('value', _('unknown'))
127 return _("%(user)s awarded an extra %(value)s reputation points to %(users)s") % {
128 'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
129 'value': value, 'users':self.affected_links(viewer),
132 return _("%(user)s penalised %(users)s in %(value)s reputation points") % {
133 'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
134 'value': value, 'users':self.affected_links(viewer),
139 class AwardAction(ActionProxy):
140 verb = _("was awarded")
142 def process_data(self, badge, trigger):
143 self.__dict__['_badge'] = badge
144 self.__dict__['_trigger'] = trigger
146 def process_action(self):
147 badge = self.__dict__['_badge']
148 trigger = self.__dict__['_trigger']
150 award = Award(user=self.user, badge=badge, trigger=trigger, action=self)
152 award.node = self.node
155 award.badge.awarded_count = F('awarded_count') + 1
158 if award.badge.type == Badge.GOLD:
160 if award.badge.type == Badge.SILVER:
161 self.user.silver += 1
162 if award.badge.type == Badge.BRONZE:
163 self.user.bronze += 1
167 self.user.message_set.create(message=_(
168 """Congratulations, you have received a badge '%(badge_name)s'. <a href="%(badge_url)s">Find out who has it, too</a>."""
170 badge_name=award.badge.name,
171 badge_url=award.badge.get_absolute_url()))
173 def cancel_action(self):
176 badge.awarded_count = F('awarded_count') - 1
181 def get_for(cls, user, badge, node=False):
184 return Award.objects.get(user=user, badge=badge).action
186 return Award.objects.get(user=user, node=node, badge=badge).action
190 def describe(self, viewer=None):
191 return _("%(user)s %(were_was)s awarded the %(badge_name)s badge") % {
192 'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
193 'were_was': self.viewer_or_user_verb(viewer, self.user, _('were'), _('was')),
194 'badge_name': self.award.badge.name,
198 class ReportAction(ActionProxy):
199 verb = _("suspended")
201 def process_data(self, **kwargs):
206 def process_action(self):
208 all_superusers = User.objects.filter(is_superuser=True)
211 send_template_email(all_superusers, "notifications/user_reported.html", {
212 'reported': self.extra['reported'],
214 'message': self.extra['publicmsg']
218 def describe(self, viewer=None):
220 return _("%(user)s reported %(reported) : %(msg)s") % {
221 'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
222 'reporter': self.extra.get('reported').username,
223 'msg': self.extra.get('publicmsg', _('N/A'))
226 class SuspendAction(ActionProxy):
227 verb = _("suspended")
229 def process_data(self, **kwargs):
230 self._suspended = kwargs.pop('suspended')
233 def repute_users(self):
234 self.repute(self._suspended, 0)
236 def process_action(self):
237 self._suspended.is_active = False
238 self._suspended.save()
240 def cancel_action(self):
241 for u in User.objects.filter(reputes__action=self).distinct():
243 u._pop_suspension_cache()
245 u.message_set.create(message=_("Your suspension has been removed."))
247 def describe(self, viewer=None):
248 if self.extra.get('bantype', 'indefinitely') == 'forxdays' and self.extra.get('forxdays', None):
249 suspension = _("for %s days") % self.extra['forxdays']
251 suspension = _("indefinetely")
253 return _("%(user)s suspended %(users)s %(suspension)s: %(msg)s") % {
254 'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
255 'users': self.affected_links(viewer), 'suspension': suspension, 'msg': self.extra.get('publicmsg', _('Bad behaviour'))