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