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