]> git.openstreetmap.org Git - osqa.git/blob - forum/actions/user.py
Adds the option to exclude users with unvalidate emails from notifications.
[osqa.git] / forum / actions / user.py
1 from django.utils.translation import 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 EditProfileAction(ActionProxy):
27     verb = _("edited profile")
28
29     def describe(self, viewer=None):
30         return _("%(user)s edited %(hes_or_your)s %(profile_link)s") % {
31         'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
32         'hes_or_your': self.viewer_or_user_verb(viewer, self.user, _('your'), _('his')),
33         'profile_link': self.hyperlink(self.user.get_profile_url(), _('profile')),
34         }
35
36 class BonusRepAction(ActionProxy):
37     verb = _("gave bonus")
38
39     def process_data(self, value, affected):
40         self._value = value
41         self._affected = affected
42
43
44     def repute_users(self):
45         self.repute(self._affected, self._value)
46
47         if self._value > 0:
48             self._affected.message_set.create(
49                     message=_("Congratulations, you have been awarded an extra %s reputation points.") % self._value +
50                     '<br />%s' % self.extra.get('message', _('Thank you')))
51         else:
52             self._affected.message_set.create(
53                     message=_("You have been penalized in %s reputation points.") % self._value +
54                     '<br />%s' % self.extra.get('message', ''))
55
56     def describe(self, viewer=None):
57         value = self.extra.get('value', _('unknown'))
58         message = self.extra.get('message', '')
59
60         try:
61             if int(value) > 0:
62                 return _("%(user)s awarded an extra %(value)s reputation points to %(users)s: %(message)s") % {
63                 'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
64                 'value': value, 'users':self.affected_links(viewer), 'message': message
65                 }
66             else:
67                 return _("%(user)s penalised %(users)s in %(value)s reputation points: %(message)s") % {
68                 'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
69                 'value': value, 'users':self.affected_links(viewer), 'message': message
70                 }
71         except Exception, e:
72             return ''
73
74 class AwardAction(ActionProxy):
75     verb = _("was awarded")
76
77     def process_data(self, badge, trigger):
78         self.__dict__['_badge'] = badge
79         self.__dict__['_trigger'] = trigger
80
81     def process_action(self):
82         badge = self.__dict__['_badge']
83         trigger = self.__dict__['_trigger']
84
85         award = Award(user=self.user, badge=badge, trigger=trigger, action=self)
86         if self.node:
87             award.node = self.node
88
89         award.save()
90         award.badge.awarded_count = F('awarded_count') + 1
91         award.badge.save()
92
93         if award.badge.type == Badge.GOLD:
94             self.user.gold += 1
95         if award.badge.type == Badge.SILVER:
96             self.user.silver += 1
97         if award.badge.type == Badge.BRONZE:
98             self.user.bronze += 1
99
100         self.user.save()
101
102         self.user.message_set.create(message=_(
103                 """Congratulations, you have received a badge '%(badge_name)s'. Check out <a href=\"%(profile_url)s\">your profile</a>."""
104                 ) %
105         dict(badge_name=award.badge.name, profile_url=self.user.get_profile_url()))
106
107     def cancel_action(self):
108         award = self.award
109         badge = award.badge
110         badge.awarded_count = F('awarded_count') - 1
111         badge.save()
112         award.delete()
113
114     @classmethod
115     def get_for(cls, user, badge, node=False):
116         try:
117             if node is False:
118                 return Award.objects.get(user=user, badge=badge).action
119             else:
120                 return Award.objects.get(user=user, node=node, badge=badge).action
121         except:
122             return None
123
124     def describe(self, viewer=None):
125         return _("%(user)s %(were_was)s awarded the %(badge_name)s badge") % {
126         'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
127         'were_was': self.viewer_or_user_verb(viewer, self.user, _('were'), _('was')),
128         'badge_name': self.award.badge.name,
129         }
130
131 class SuspendAction(ActionProxy):
132     verb = _("suspended")
133
134     def process_data(self, **kwargs):
135         self._suspended = kwargs.pop('suspended')
136         self.extra = kwargs
137
138     def repute_users(self):
139         self.repute(self._suspended, 0)
140
141     def process_action(self):
142         self._suspended.is_active = False
143         self._suspended.save()
144
145     def cancel_action(self):
146         for u in User.objects.filter(reputes__action=self).distinct():
147             u.is_active = True
148             u._pop_suspension_cache()
149             u.save()
150             u.message_set.create(message=_("Your suspension has been removed."))
151
152     def describe(self, viewer=None):
153         if self.extra.get('bantype', 'indefinitely') == 'forxdays' and self.extra.get('forxdays', None):
154             suspension = _("for %s days") % self.extra['forxdays']
155         else:
156             suspension = _("indefinetely")
157
158         return _("%(user)s suspended %(users)s %(suspension)s: %(msg)s") % {
159         'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
160         'users': self.affected_links(viewer), 'suspension': suspension, 'msg': self.extra.get('publicmsg', _('Bad behaviour'))
161         }