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 class UserJoinsAction(ActionProxy):
14 def repute_users(self):
15 self.repute(self.user, int(settings.INITIAL_REP))
17 def process_action(self):
18 hash = ValidationHash.objects.create_new(self.user, 'email', [self.user.email])
19 send_template_email([self.user], "auth/welcome_email.html", {'validation_code': hash})
21 def describe(self, viewer=None):
22 return _("%(user)s %(have_has)s joined the %(app_name)s Q&A community") % {
23 'user': self.hyperlink(self.user.get_absolute_url(), self.friendly_username(viewer, self.user)),
24 'have_has': self.viewer_or_user_verb(viewer, self.user, _('have'), _('has')),
25 'app_name': APP_SHORT_NAME,
28 class UserLoginAction(ActionProxy):
31 def describe(self, viewer=None):
32 return _("%(user)s %(have_has)s logged in") % {
33 'user' : self.hyperlink(self.user.get_absolute_url(), self.friendly_username(viewer, self.user)),
34 'have_has': self.viewer_or_user_verb(viewer, self.user, _('have'), _('has')),
37 class EmailValidationAction(ActionProxy):
38 verb = _("validated e-mail")
40 def repute_users(self):
41 self.repute(self.user, int(settings.REP_GAIN_BY_EMAIL_VALIDATION))
43 def process_action(self):
44 self.user.email_isvalid = True
47 def describe(self, viewer=None):
48 return _("%(user)s %(have_has)s validated the e-mail %(email)s") % {
49 'user': self.hyperlink(self.user.get_absolute_url(), self.friendly_username(viewer, self.user)),
50 'have_has': self.viewer_or_user_verb(viewer, self.user, _('have'), _('has')),
51 'email' : self.user.email if viewer.is_superuser or viewer.is_staff or viewer == self.user else ""
54 class EditProfileAction(ActionProxy):
55 verb = _("edited profile")
57 def describe(self, viewer=None):
58 return _("%(user)s edited %(hes_or_your)s %(profile_link)s") % {
59 'user': self.hyperlink(self.user.get_absolute_url(), self.friendly_username(viewer, self.user)),
60 'hes_or_your': self.viewer_or_user_verb(viewer, self.user, _('your'), _('his')),
61 'profile_link': self.hyperlink(self.user.get_absolute_url(), _('profile')),
64 class BonusRepAction(ActionProxy):
65 verb = _("gave bonus")
67 def process_data(self, value, affected):
69 self._affected = affected
72 def repute_users(self):
73 self.repute(self._affected, self._value)
76 messages.info(REQUEST_HOLDER.request, _("You have penalized %s in %s reputation points.") % (self._affected, self._value) +
77 '<br />%s' % self.extra.get('message', ''))
79 def describe(self, viewer=None):
80 value = self.extra.get('value', _('unknown'))
81 message = self.extra.get('message', '')
85 return _("%(user)s awarded an extra %(value)s reputation points to %(users)s: %(message)s") % {
86 'user': self.hyperlink(self.user.get_absolute_url(), self.friendly_username(viewer, self.user)),
87 'value': value, 'users':self.affected_links(viewer), 'message': message
90 return _("%(user)s penalised %(users)s in %(value)s reputation points: %(message)s") % {
91 'user': self.hyperlink(self.user.get_absolute_url(), self.friendly_username(viewer, self.user)),
92 'value': value, 'users':self.affected_links(viewer), 'message': message
97 class AwardPointsAction(ActionProxy):
98 verb = _("gave reputation points")
100 def process_data(self, value, affected):
102 self._affected = affected
105 def repute_users(self):
106 self.repute(self._affected, self._value)
107 self.repute(self.user, -self._value)
109 def describe(self, viewer=None):
110 value = self.extra.get('value', _('unknown'))
114 return _("%(user)s awarded an extra %(value)s reputation points to %(users)s") % {
115 'user': self.hyperlink(self.user.get_absolute_url(), self.friendly_username(viewer, self.user)),
116 'value': value, 'users':self.affected_links(viewer),
119 return _("%(user)s penalised %(users)s in %(value)s reputation points") % {
120 'user': self.hyperlink(self.user.get_absolute_url(), self.friendly_username(viewer, self.user)),
121 'value': value, 'users':self.affected_links(viewer),
126 class AwardAction(ActionProxy):
127 verb = _("was awarded")
129 def process_data(self, badge, trigger):
130 self.__dict__['_badge'] = badge
131 self.__dict__['_trigger'] = trigger
133 def process_action(self):
134 badge = self.__dict__['_badge']
135 trigger = self.__dict__['_trigger']
137 award = Award(user=self.user, badge=badge, trigger=trigger, action=self)
139 award.node = self.node
142 award.badge.awarded_count = F('awarded_count') + 1
145 if award.badge.type == Badge.GOLD:
147 if award.badge.type == Badge.SILVER:
148 self.user.silver += 1
149 if award.badge.type == Badge.BRONZE:
150 self.user.bronze += 1
154 def cancel_action(self):
157 badge.awarded_count = F('awarded_count') - 1
162 def get_for(cls, user, badge, node=False):
165 return Award.objects.get(user=user, badge=badge).action
167 return Award.objects.get(user=user, node=node, badge=badge).action
171 def describe(self, viewer=None):
172 return _("%(user)s %(were_was)s awarded the %(badge_name)s badge") % {
173 'user': self.hyperlink(self.user.get_absolute_url(), self.friendly_username(viewer, self.user)),
174 'were_was': self.viewer_or_user_verb(viewer, self.user, _('were'), _('was')),
175 'badge_name': self.award.badge.name,
179 class ReportAction(ActionProxy):
180 verb = _("suspended")
182 def process_data(self, **kwargs):
187 def process_action(self):
189 all_superusers = User.objects.filter(is_superuser=True)
192 send_template_email(all_superusers, "notifications/user_reported.html", {
193 'reported': self.extra['reported'],
195 'message': self.extra['publicmsg']
199 def describe(self, viewer=None):
201 return _("%(user)s reported %(reported) : %(msg)s") % {
202 'user': self.hyperlink(self.user.get_absolute_url(), self.friendly_username(viewer, self.user)),
203 'reporter': self.extra.get('reported').username,
204 'msg': self.extra.get('publicmsg', _('N/A'))
207 class SuspendAction(ActionProxy):
208 verb = _("suspended")
210 def process_data(self, **kwargs):
211 self._suspended = kwargs.pop('suspended')
214 def repute_users(self):
215 self.repute(self._suspended, 0)
217 def process_action(self):
218 self._suspended.is_active = False
219 self._suspended.save()
221 def cancel_action(self):
222 for u in User.objects.filter(reputes__action=self).distinct():
224 u._pop_suspension_cache()
227 def describe(self, viewer=None):
228 if self.extra.get('bantype', 'indefinitely') == 'forxdays' and self.extra.get('forxdays', None):
229 suspension = _("for %s days") % self.extra['forxdays']
231 suspension = _("indefinetely")
233 return _("%(user)s suspended %(users)s %(suspension)s: %(msg)s") % {
234 'user': self.hyperlink(self.user.get_absolute_url(), self.friendly_username(viewer, self.user)),
235 'users': self.affected_links(viewer), 'suspension': suspension, 'msg': self.extra.get('publicmsg', _('Bad behaviour'))