1 from django.utils.translation import ugettext as _
2 from django.utils.encoding import smart_unicode
4 from utils import PickledObjectField
5 from threading import Thread
6 from forum.utils import html
10 class ActionQuerySet(CachedQuerySet):
11 def obj_from_datadict(self, datadict):
12 cls = ActionProxyMetaClass.types.get(datadict['action_type'], None)
15 obj.__dict__.update(datadict)
18 return super(ActionQuerySet, self).obj_from_datadict(datadict)
20 def get(self, *args, **kwargs):
21 action = super(ActionQuerySet, self).get(*args, **kwargs).leaf
23 if not isinstance(action, self.model):
24 raise self.model.DoesNotExist()
28 class ActionManager(CachedManager):
29 use_for_related_fields = True
31 def get_queryset(self):
32 qs = ActionQuerySet(self.model)
34 if self.model is not Action:
35 return qs.filter(action_type=self.model.get_type())
39 def get_for_types(self, types, *args, **kwargs):
40 kwargs['action_type__in'] = [t.get_type() for t in types]
41 return self.get(*args, **kwargs)
44 class Action(BaseModel):
45 user = models.ForeignKey('User', related_name="actions")
46 real_user = models.ForeignKey('User', related_name="proxied_actions", null=True)
47 ip = models.CharField(max_length=39)
48 node = models.ForeignKey('Node', null=True, related_name="actions")
49 action_type = models.CharField(max_length=32)
50 action_date = models.DateTimeField(default=datetime.datetime.now)
52 extra = PickledObjectField()
54 canceled = models.BooleanField(default=False)
55 canceled_by = models.ForeignKey('User', null=True, related_name="canceled_actions")
56 canceled_at = models.DateTimeField(null=True)
57 canceled_ip = models.CharField(max_length=39)
61 objects = ActionManager()
65 return self.action_date
71 def repute_users(self):
74 def process_data(self, **data):
77 def process_action(self):
80 def cancel_action(self):
87 def describe(self, viewer=None):
88 return self.__class__.__name__
90 def get_absolute_url(self):
92 return self.node.get_absolute_url()
94 return self.user.get_profile_url()
96 def repute(self, user, value):
97 repute = ActionRepute(action=self, user=user, value=value)
101 def cancel_reputes(self):
102 for repute in self.reputes.all():
103 cancel = ActionRepute(action=self, user=repute.user, value=(-repute.value), by_canceled=True)
108 leaf_cls = ActionProxyMetaClass.types.get(self.action_type, None)
115 leaf.__dict__.update(self._as_dict())
121 return re.sub(r'action$', '', cls.__name__.lower())
123 def save(self, data=None, threaded=True, *args, **kwargs):
127 self.action_type = self.__class__.get_type()
131 self.process_data(**data)
133 super(Action, self).save(*args, **kwargs)
136 if (self.node is None) or (not self.node.nis.wiki):
138 self.process_action()
139 self.trigger_hooks(threaded, True)
143 def delete(self, *args, **kwargs):
145 super(Action, self).delete(*args, **kwargs)
147 def cancel(self, user=None, ip=None):
148 if not self.canceled:
150 self.canceled_at = datetime.datetime.now()
151 self.canceled_by = (user is None) and self.user or user
153 self.canceled_ip = ip
155 self.cancel_reputes()
157 #self.trigger_hooks(False)
160 def get_current(cls, **kwargs):
161 kwargs['canceled'] = False
164 return cls.objects.get(**kwargs)
165 except cls.MultipleObjectsReturned:
166 logging.error("Got multiple values for action %s with args %s", cls.__name__,
167 ", ".join(["%s='%s'" % i for i in kwargs.items()]))
169 except cls.DoesNotExist:
174 if not Action.hooks.get(cls, None):
175 Action.hooks[cls] = []
177 Action.hooks[cls].append(fn)
179 def trigger_hooks(self, threaded, new=True):
181 thread = Thread(target=trigger_hooks, args=[self, Action.hooks, new])
182 thread.setDaemon(True)
185 trigger_hooks(self, Action.hooks, new)
190 def trigger_hooks(action, hooks, new):
191 for cls, hooklist in hooks.items():
192 if isinstance(action, cls):
193 for hook in hooklist:
195 hook(action=action, new=new)
198 logging.error("Error in %s hook: %s" % (cls.__name__, str(e)))
199 logging.error(traceback.format_exc())
201 class ActionProxyMetaClass(BaseMetaClass):
204 def __new__(cls, *args, **kwargs):
205 new_cls = super(ActionProxyMetaClass, cls).__new__(cls, *args, **kwargs)
206 cls.types[new_cls.get_type()] = new_cls
214 class ActionProxy(Action):
215 __metaclass__ = ActionProxyMetaClass
217 def friendly_username(self, viewer, user):
218 return (viewer == user) and _('You') or smart_unicode(user.username)
220 def friendly_ownername(self, owner, user):
221 return (owner == user) and _('your') or smart_unicode(user.username)
223 def viewer_or_user_verb(self, viewer, user, viewer_verb, user_verb):
224 return (viewer == user) and viewer_verb or user_verb
226 def hyperlink(self, url, title, **attrs):
227 return html.hyperlink(url, title, **attrs)
229 def describe_node(self, viewer, node):
230 node_link = self.hyperlink(node.get_absolute_url(), node.headline)
233 node_desc = _("on %(link)s") % {'link': node_link}
235 node_desc = node_link
237 return _("%(user)s %(node_name)s %(node_desc)s") % {
238 'user': self.hyperlink(node.author.get_profile_url(), self.friendly_ownername(viewer, node.author)),
239 'node_name': node.friendly_name,
240 'node_desc': node_desc,
243 def affected_links(self, viewer):
244 return ", ".join([self.hyperlink(u.get_profile_url(), self.friendly_username(viewer, u)) for u in set([r.user for r in self.reputes.all()])])
249 class DummyActionProxyMetaClass(type):
250 def __new__(cls, *args, **kwargs):
251 new_cls = super(DummyActionProxyMetaClass, cls).__new__(cls, *args, **kwargs)
252 ActionProxyMetaClass.types[new_cls.get_type()] = new_cls
255 class DummyActionProxy(object):
256 __metaclass__ = DummyActionProxyMetaClass
260 def __init__(self, ip=None):
263 def process_data(self, **data):
266 def process_action(self):
269 def save(self, data=None):
270 self.process_action()
273 self.process_data(**data)
275 for hook in self.__class__.hooks:
280 return re.sub(r'action$', '', cls.__name__.lower())
287 class ActionRepute(models.Model):
288 action = models.ForeignKey(Action, related_name='reputes')
289 date = models.DateTimeField(default=datetime.datetime.now)
290 user = models.ForeignKey('User', related_name='reputes')
291 value = models.IntegerField(default=0)
292 by_canceled = models.BooleanField(default=False)
296 if self.value > 0: return self.value
301 if self.value < 0: return self.value
304 def _add_to_rep(self, value):
305 if int(self.user.reputation + value) < 1 and not settings.ALLOW_NEGATIVE_REPUTATION:
308 return models.F('reputation') + value
310 def save(self, *args, **kwargs):
311 super(ActionRepute, self).save(*args, **kwargs)
312 self.user.reputation = self._add_to_rep(self.value)
316 self.user.reputation = self._add_to_rep(-self.value)
318 super(ActionRepute, self).delete()