]> git.openstreetmap.org Git - osqa.git/blob - forum/actions/user.py
Merge pull request #42 from aniketschneider/validation-email-template
[osqa.git] / forum / actions / user.py
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
10
11 from django.contrib import messages
12
13 class UserJoinsAction(ActionProxy):
14     verb = _("joined")
15
16     def repute_users(self):
17         self.repute(self.user, int(settings.INITIAL_REP))
18
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})
22
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,
28         }
29
30 class UserLoginAction(ActionProxy):
31     verb = _("logged in")
32
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')),
37         }
38
39 class EmailValidationAction(ActionProxy):
40     verb = _("validated e-mail")
41
42     def repute_users(self):
43         self.repute(self.user, int(settings.REP_GAIN_BY_EMAIL_VALIDATION))
44
45     def process_action(self):
46         self.user.email_isvalid = True
47         self.user.save()
48
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 ""
54         }
55
56 class EditProfileAction(ActionProxy):
57     verb = _("edited profile")
58
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')),
64         }
65
66 class BonusRepAction(ActionProxy):
67     verb = _("gave bonus")
68
69     def process_data(self, value, affected):
70         self._value = value
71         self._affected = affected
72
73
74     def repute_users(self):
75         self.repute(self._affected, self._value)
76
77         if self._value > 0:
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')))
81         else:
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', ''))
84
85     def describe(self, viewer=None):
86         value = self.extra.get('value', _('unknown'))
87         message = self.extra.get('message', '')
88
89         try:
90             if int(value) > 0:
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
94                 }
95             else:
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
99                 }
100         except Exception, e:
101             return ''
102
103 class AwardPointsAction(ActionProxy):
104     verb = _("gave reputation points")
105
106     def process_data(self, value, affected):
107         self._value = value
108         self._affected = affected
109
110
111     def repute_users(self):
112         self.repute(self._affected, self._value)
113         self.repute(self.user, -self._value)
114
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()
120                     })
121
122     def describe(self, viewer=None):
123         value = self.extra.get('value', _('unknown'))
124
125         try:
126             if int(value) > 0:
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),
130                 }
131             else:
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),
135                 }
136         except Exception, e:
137             return ''
138
139 class AwardAction(ActionProxy):
140     verb = _("was awarded")
141
142     def process_data(self, badge, trigger):
143         self.__dict__['_badge'] = badge
144         self.__dict__['_trigger'] = trigger
145
146     def process_action(self):
147         badge = self.__dict__['_badge']
148         trigger = self.__dict__['_trigger']
149
150         award = Award(user=self.user, badge=badge, trigger=trigger, action=self)
151         if self.node:
152             award.node = self.node
153
154         award.save()
155         award.badge.awarded_count = F('awarded_count') + 1
156         award.badge.save()
157
158         if award.badge.type == Badge.GOLD:
159             self.user.gold += 1
160         if award.badge.type == Badge.SILVER:
161             self.user.silver += 1
162         if award.badge.type == Badge.BRONZE:
163             self.user.bronze += 1
164
165         self.user.save()
166
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>."""
169         ) % dict(
170             badge_name=award.badge.name,
171             badge_url=award.badge.get_absolute_url()))
172
173     def cancel_action(self):
174         award = self.award
175         badge = award.badge
176         badge.awarded_count = F('awarded_count') - 1
177         badge.save()
178         award.delete()
179
180     @classmethod
181     def get_for(cls, user, badge, node=False):
182         try:
183             if node is False:
184                 return Award.objects.get(user=user, badge=badge).action
185             else:
186                 return Award.objects.get(user=user, node=node, badge=badge).action
187         except:
188             return None
189
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,
195         }
196
197
198 class ReportAction(ActionProxy):
199     verb = _("suspended")
200
201     def process_data(self, **kwargs):
202         self.extra = kwargs
203         # message here?
204
205
206     def process_action(self):
207
208         all_superusers = User.objects.filter(is_superuser=True)
209
210
211         send_template_email(all_superusers, "notifications/user_reported.html", {
212             'reported': self.extra['reported'],
213             'user':self.user,
214             'message': self.extra['publicmsg']
215             }
216             )
217
218     def describe(self, viewer=None):
219
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'))
224         }
225
226 class SuspendAction(ActionProxy):
227     verb = _("suspended")
228
229     def process_data(self, **kwargs):
230         self._suspended = kwargs.pop('suspended')
231         self.extra = kwargs
232
233     def repute_users(self):
234         self.repute(self._suspended, 0)
235
236     def process_action(self):
237         self._suspended.is_active = False
238         self._suspended.save()
239
240     def cancel_action(self):
241         for u in User.objects.filter(reputes__action=self).distinct():
242             u.is_active = True
243             u._pop_suspension_cache()
244             u.save()
245             u.message_set.create(message=_("Your suspension has been removed."))
246
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']
250         else:
251             suspension = _("indefinetely")
252
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'))
256         }