]> git.openstreetmap.org Git - osqa.git/blob - forum/actions/user.py
OSQA-349
[osqa.git] / forum / actions / user.py
1 from django.utils.translation import ugettext as _
2 from django.db.models import F
3 from django.core.exceptions import ObjectDoesNotExist, MultipleObjectsReturned
4 from forum.models.action import ActionProxy
5 from forum.models import Award, Badge, ValidationHash
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     def repute_users(self):
12         self.repute(self.user, int(settings.INITIAL_REP))
13
14     def process_action(self):
15         hash = ValidationHash.objects.create_new(self.user, 'email', [self.user.email])
16         send_template_email([self.user], "auth/welcome_email.html", {'validation_code': hash})
17
18     def describe(self, viewer=None):
19         return _("%(user)s %(have_has)s joined the %(app_name)s Q&A community") % {
20         'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
21         'have_has': self.viewer_or_user_verb(viewer, self.user, _('have'), _('has')),
22         'app_name': APP_SHORT_NAME,
23         }
24
25 class EditProfileAction(ActionProxy):
26     def describe(self, viewer=None):
27         return _("%(user)s edited %(hes_or_your)s %(profile_link)s") % {
28         'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
29         'hes_or_your': self.viewer_or_user_verb(viewer, self.user, _('your'), _('his')),
30         'profile_link': self.hyperlink(self.user.get_profile_url(), _('profile')),
31         }
32
33 class BonusRepAction(ActionProxy):
34     def process_data(self, value):
35         self._value = value
36
37     def repute_users(self):
38         self.repute(self.user, self._value)
39         self.user.message_set.create(
40                 message=_("Congratulations, you have been awarded an extra %s reputation points.") % self._value +
41                 '<br />%s' % self.extra.get('message', _('Thank you')))
42
43     def describe(self, viewer=None):
44         value = self.extra.get('value', _('unknown'))
45         message = self.extra.get('message', '')
46
47         return _("%(user)s %(was_were)s awarded %(value)s reputation points: %(message)s") % {
48         'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
49         'was_were': self.viewer_or_user_verb(viewer, self.user, _('were'), _('was')),
50         'value': value, 'message': message
51         }
52
53 class AwardAction(ActionProxy):
54     def process_data(self, badge, trigger):
55         self.__dict__['_badge'] = badge
56         self.__dict__['_trigger'] = trigger
57
58     def process_action(self):
59         badge = self.__dict__['_badge']
60         trigger = self.__dict__['_trigger']
61
62         award = Award(user=self.user, badge=badge, trigger=trigger, action=self)
63         if self.node:
64             award.node = self.node
65
66         award.save()
67         award.badge.awarded_count = F('awarded_count') + 1
68         award.badge.save()
69
70         if award.badge.type == Badge.GOLD:
71             self.user.gold += 1
72         if award.badge.type == Badge.SILVER:
73             self.user.silver += 1
74         if award.badge.type == Badge.BRONZE:
75             self.user.bronze += 1
76
77         self.user.save()
78
79         self.user.message_set.create(message=_(
80                 """Congratulations, you have received a badge '%(badge_name)s'. Check out <a href=\"%(profile_url)s\">your profile</a>."""
81                 ) %
82         dict(badge_name=award.badge.name, profile_url=self.user.get_profile_url()))
83
84     def cancel_action(self):
85         award = self.award
86         badge = award.badge
87         badge.awarded_count = F('awarded_count') - 1
88         badge.save()
89         award.delete()
90
91     @classmethod
92     def get_for(cls, user, badge, node=False):
93         try:
94             if node is False:
95                 return Award.objects.get(user=user, badge=badge).action
96             else:
97                 return Award.objects.get(user=user, node=node, badge=badge).action
98         except ObjectDoesNotExist:
99             return None
100
101     def describe(self, viewer=None):
102         return _("%(user)s %(were_was)s awarded the %(badge_name)s badge") % {
103         'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
104         'were_was': self.viewer_or_user_verb(viewer, self.user, _('were'), _('was')),
105         'badge_name': self.award.badge.name,
106         }
107
108 class SuspendAction(ActionProxy):
109     def process_data(self, **kwargs):
110         self.extra = kwargs
111
112     def process_action(self):
113         self.user.is_active = False
114         self.user.save()
115
116     def cancel_action(self):
117         self.user.is_active = True
118         self.user._pop_suspension_cache()
119         self.user.save()
120         self.user.message_set.create(message=_("Your suspension has been removed."))
121
122     def describe(self, viewer=None):
123         if self.extra.get('bantype', 'indefinitely') == 'forxdays' and self.extra.get('forxdays', None):
124             suspension = _("for %s days") % self.extra['forxdays']
125         else:
126             suspension = _("indefinetely")
127
128         return _("%(user)s %(were_was)s suspended %(suspension)s: %(msg)s") % {
129         'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
130         'were_was': self.viewer_or_user_verb(viewer, self.user, _('were'), _('was')),
131         'suspension': suspension, 'msg': self.extra.get('publicmsg', _('Bad behaviour'))
132         }