]> git.openstreetmap.org Git - osqa.git/blob - forum/actions/user.py
d9c6be920e9411fc592666dd08e25db1ff1dbe38
[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
7 from forum.settings import APP_SHORT_NAME
8 from forum.utils.mail import send_template_email
9
10 class UserJoinsAction(ActionProxy):
11     verb = _("joined")
12
13     def repute_users(self):
14         self.repute(self.user, int(settings.INITIAL_REP))
15
16     def process_action(self):
17         hash = ValidationHash.objects.create_new(self.user, 'email', [self.user.email])
18         send_template_email([self.user], "auth/welcome_email.html", {'validation_code': hash})
19
20     def describe(self, viewer=None):
21         return _("%(user)s %(have_has)s joined the %(app_name)s Q&A community") % {
22         'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
23         'have_has': self.viewer_or_user_verb(viewer, self.user, _('have'), _('has')),
24         'app_name': APP_SHORT_NAME,
25         }
26
27 class UserLoginAction(ActionProxy):
28     verb = _("logged in")
29
30     def describe(self, viewer=None):
31         return _("%(user)s %(have_has)s logged in") % {
32             'user' : self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
33             'have_has': self.viewer_or_user_verb(viewer, self.user, _('have'), _('has')),
34         }
35
36 class EmailValidationAction(ActionProxy):
37     verb = _("validated e-mail")
38
39     def repute_users(self):
40         self.repute(self.user, int(settings.REP_GAIN_BY_EMAIL_VALIDATION))
41
42     def process_action(self):
43         self.user.email_isvalid = True
44         self.user.save()
45
46     def describe(self, viewer=None):
47         return _("%(user)s %(have_has)s validated the e-mail %(email)s") % {
48         'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
49         'have_has': self.viewer_or_user_verb(viewer, self.user, _('have'), _('has')),
50         'email' : self.user.email if viewer.is_superuser or viewer.is_staff or viewer == self.user else ""
51         }
52
53 class EditProfileAction(ActionProxy):
54     verb = _("edited profile")
55
56     def describe(self, viewer=None):
57         return _("%(user)s edited %(hes_or_your)s %(profile_link)s") % {
58         'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
59         'hes_or_your': self.viewer_or_user_verb(viewer, self.user, _('your'), _('his')),
60         'profile_link': self.hyperlink(self.user.get_profile_url(), _('profile')),
61         }
62
63 class BonusRepAction(ActionProxy):
64     verb = _("gave bonus")
65
66     def process_data(self, value, affected):
67         self._value = value
68         self._affected = affected
69
70
71     def repute_users(self):
72         self.repute(self._affected, self._value)
73
74         if self._value > 0:
75             self._affected.message_set.create(
76                     message=_("Congratulations, you have been awarded an extra %s reputation points.") % self._value +
77                     '<br />%s' % self.extra.get('message', _('Thank you')))
78         else:
79             self._affected.message_set.create(
80                     message=_("You have been penalized in %s reputation points.") % self._value +
81                     '<br />%s' % self.extra.get('message', ''))
82
83     def describe(self, viewer=None):
84         value = self.extra.get('value', _('unknown'))
85         message = self.extra.get('message', '')
86
87         try:
88             if int(value) > 0:
89                 return _("%(user)s awarded an extra %(value)s reputation points to %(users)s: %(message)s") % {
90                 'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
91                 'value': value, 'users':self.affected_links(viewer), 'message': message
92                 }
93             else:
94                 return _("%(user)s penalised %(users)s in %(value)s reputation points: %(message)s") % {
95                 'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
96                 'value': value, 'users':self.affected_links(viewer), 'message': message
97                 }
98         except Exception, e:
99             return ''
100
101 class AwardPointsAction(ActionProxy):
102     verb = _("gave reputation points")
103
104     def process_data(self, value, affected):
105         self._value = value
106         self._affected = affected
107
108
109     def repute_users(self):
110         self.repute(self._affected, self._value)
111         self.repute(self.user, -self._value)
112
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 class SuspendAction(ActionProxy):
197     verb = _("suspended")
198
199     def process_data(self, **kwargs):
200         self._suspended = kwargs.pop('suspended')
201         self.extra = kwargs
202
203     def repute_users(self):
204         self.repute(self._suspended, 0)
205
206     def process_action(self):
207         self._suspended.is_active = False
208         self._suspended.save()
209
210     def cancel_action(self):
211         for u in User.objects.filter(reputes__action=self).distinct():
212             u.is_active = True
213             u._pop_suspension_cache()
214             u.save()
215             u.message_set.create(message=_("Your suspension has been removed."))
216
217     def describe(self, viewer=None):
218         if self.extra.get('bantype', 'indefinitely') == 'forxdays' and self.extra.get('forxdays', None):
219             suspension = _("for %s days") % self.extra['forxdays']
220         else:
221             suspension = _("indefinetely")
222
223         return _("%(user)s suspended %(users)s %(suspension)s: %(msg)s") % {
224         'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
225         'users': self.affected_links(viewer), 'suspension': suspension, 'msg': self.extra.get('publicmsg', _('Bad behaviour'))
226         }