]> git.openstreetmap.org Git - osqa.git/blob - forum/actions/user.py
show the recaptcha field to unauthenticated users without a metter what is the min...
[osqa.git] / forum / actions / user.py
1 from django.utils.translation import ungettext, ugettext as _
2 from django.db.models import F
3 from forum.models.action import ActionProxy
4 from forum.models import Award, Badge, ValidationHash, User
5 from forum import settings
6 from forum.settings import APP_SHORT_NAME
7 from forum.utils.mail import send_template_email
8
9 class UserJoinsAction(ActionProxy):
10     verb = _("joined")
11
12     def repute_users(self):
13         self.repute(self.user, int(settings.INITIAL_REP))
14
15     def process_action(self):
16         hash = ValidationHash.objects.create_new(self.user, 'email', [self.user.email])
17         send_template_email([self.user], "auth/welcome_email.html", {'validation_code': hash})
18
19     def describe(self, viewer=None):
20         return _("%(user)s %(have_has)s joined the %(app_name)s Q&A community") % {
21         'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
22         'have_has': self.viewer_or_user_verb(viewer, self.user, _('have'), _('has')),
23         'app_name': APP_SHORT_NAME,
24         }
25
26 class UserLoginAction(ActionProxy):
27     verb = _("logged in")
28
29     def describe(self, viewer=None):
30         return _("%(user)s %(have_has)s logged in") % {
31             'user' : self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
32             'have_has': self.viewer_or_user_verb(viewer, self.user, _('have'), _('has')),
33         }
34
35 class EmailValidationAction(ActionProxy):
36     verb = _("validated e-mail")
37
38     def repute_users(self):
39         self.repute(self.user, int(settings.REP_GAIN_BY_EMAIL_VALIDATION))
40
41     def process_action(self):
42         self.user.email_isvalid = True
43         self.user.save()
44
45     def describe(self, viewer=None):
46         return _("%(user)s %(have_has)s validated the e-mail %(email)s") % {
47         'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
48         'have_has': self.viewer_or_user_verb(viewer, self.user, _('have'), _('has')),
49         'email' : self.user.email if viewer.is_superuser or viewer.is_staff or viewer == self.user else ""
50         }
51
52 class EditProfileAction(ActionProxy):
53     verb = _("edited profile")
54
55     def describe(self, viewer=None):
56         return _("%(user)s edited %(hes_or_your)s %(profile_link)s") % {
57         'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
58         'hes_or_your': self.viewer_or_user_verb(viewer, self.user, _('your'), _('his')),
59         'profile_link': self.hyperlink(self.user.get_profile_url(), _('profile')),
60         }
61
62 class BonusRepAction(ActionProxy):
63     verb = _("gave bonus")
64
65     def process_data(self, value, affected):
66         self._value = value
67         self._affected = affected
68
69
70     def repute_users(self):
71         self.repute(self._affected, self._value)
72
73         if self._value > 0:
74             self._affected.message_set.create(
75                     message=_("Congratulations, you have been awarded an extra %s reputation points.") % self._value +
76                     '<br />%s' % self.extra.get('message', _('Thank you')))
77         else:
78             self._affected.message_set.create(
79                     message=_("You have been penalized in %s reputation points.") % self._value +
80                     '<br />%s' % self.extra.get('message', ''))
81
82     def describe(self, viewer=None):
83         value = self.extra.get('value', _('unknown'))
84         message = self.extra.get('message', '')
85
86         try:
87             if int(value) > 0:
88                 return _("%(user)s awarded an extra %(value)s reputation points to %(users)s: %(message)s") % {
89                 'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
90                 'value': value, 'users':self.affected_links(viewer), 'message': message
91                 }
92             else:
93                 return _("%(user)s penalised %(users)s in %(value)s reputation points: %(message)s") % {
94                 'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
95                 'value': value, 'users':self.affected_links(viewer), 'message': message
96                 }
97         except Exception, e:
98             return ''
99
100 class AwardPointsAction(ActionProxy):
101     verb = _("gave reputation points")
102
103     def process_data(self, value, affected):
104         self._value = value
105         self._affected = affected
106
107
108     def repute_users(self):
109         self.repute(self._affected, self._value)
110         self.repute(self.user, -self._value)
111
112
113         self._affected.message_set.create(
114                 message=_("Congratulations, you have been awarded an extra %(points)s reputation %(points_label)s on <a href=\"%(answer_url)s\">this</a> answer.") % {
115                         'points': self._value,
116                         'points_label': ungettext('point', 'points', self._value),
117                         'answer_url': self.node.get_absolute_url()
118                     })
119
120     def describe(self, viewer=None):
121         value = self.extra.get('value', _('unknown'))
122
123         try:
124             if int(value) > 0:
125                 return _("%(user)s awarded an extra %(value)s reputation points to %(users)s") % {
126                 'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
127                 'value': value, 'users':self.affected_links(viewer),
128                 }
129             else:
130                 return _("%(user)s penalised %(users)s in %(value)s reputation points") % {
131                 'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
132                 'value': value, 'users':self.affected_links(viewer),
133                 }
134         except Exception, e:
135             return ''
136
137 class AwardAction(ActionProxy):
138     verb = _("was awarded")
139
140     def process_data(self, badge, trigger):
141         self.__dict__['_badge'] = badge
142         self.__dict__['_trigger'] = trigger
143
144     def process_action(self):
145         badge = self.__dict__['_badge']
146         trigger = self.__dict__['_trigger']
147
148         award = Award(user=self.user, badge=badge, trigger=trigger, action=self)
149         if self.node:
150             award.node = self.node
151
152         award.save()
153         award.badge.awarded_count = F('awarded_count') + 1
154         award.badge.save()
155
156         if award.badge.type == Badge.GOLD:
157             self.user.gold += 1
158         if award.badge.type == Badge.SILVER:
159             self.user.silver += 1
160         if award.badge.type == Badge.BRONZE:
161             self.user.bronze += 1
162
163         self.user.save()
164
165         self.user.message_set.create(message=_(
166                 """Congratulations, you have received a badge '%(badge_name)s'. Check out <a href=\"%(profile_url)s\">your profile</a>."""
167                 ) %
168         dict(badge_name=award.badge.name, profile_url=self.user.get_profile_url()))
169
170     def cancel_action(self):
171         award = self.award
172         badge = award.badge
173         badge.awarded_count = F('awarded_count') - 1
174         badge.save()
175         award.delete()
176
177     @classmethod
178     def get_for(cls, user, badge, node=False):
179         try:
180             if node is False:
181                 return Award.objects.get(user=user, badge=badge).action
182             else:
183                 return Award.objects.get(user=user, node=node, badge=badge).action
184         except:
185             return None
186
187     def describe(self, viewer=None):
188         return _("%(user)s %(were_was)s awarded the %(badge_name)s badge") % {
189         'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
190         'were_was': self.viewer_or_user_verb(viewer, self.user, _('were'), _('was')),
191         'badge_name': self.award.badge.name,
192         }
193
194 class SuspendAction(ActionProxy):
195     verb = _("suspended")
196
197     def process_data(self, **kwargs):
198         self._suspended = kwargs.pop('suspended')
199         self.extra = kwargs
200
201     def repute_users(self):
202         self.repute(self._suspended, 0)
203
204     def process_action(self):
205         self._suspended.is_active = False
206         self._suspended.save()
207
208     def cancel_action(self):
209         for u in User.objects.filter(reputes__action=self).distinct():
210             u.is_active = True
211             u._pop_suspension_cache()
212             u.save()
213             u.message_set.create(message=_("Your suspension has been removed."))
214
215     def describe(self, viewer=None):
216         if self.extra.get('bantype', 'indefinitely') == 'forxdays' and self.extra.get('forxdays', None):
217             suspension = _("for %s days") % self.extra['forxdays']
218         else:
219             suspension = _("indefinetely")
220
221         return _("%(user)s suspended %(users)s %(suspension)s: %(msg)s") % {
222         'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
223         'users': self.affected_links(viewer), 'suspension': suspension, 'msg': self.extra.get('publicmsg', _('Bad behaviour'))
224         }