from base import *\r
import re\r
\r
-class ActionManager(models.Manager):\r
+class ActionQuerySet(CachedQuerySet):\r
+ def obj_from_datadict(self, datadict):\r
+ cls = ActionProxyMetaClass.types.get(datadict['action_type'], None)\r
+ if cls:\r
+ obj = cls()\r
+ obj.__dict__.update(datadict)\r
+ return obj\r
+ else:\r
+ return super(ActionQuerySet, self).obj_from_datadict(datadict)\r
+\r
+ def get(self, *args, **kwargs):\r
+ return super(ActionQuerySet, self).get(*args, **kwargs).leaf()\r
+\r
+class ActionManager(CachedManager):\r
use_for_related_fields = True\r
\r
def get_query_set(self):\r
- qs = super(ActionManager, self).get_query_set().filter(canceled=False)\r
+ qs = ActionQuerySet(self.model)\r
\r
if self.model is not Action:\r
return qs.filter(action_type=self.model.get_type())\r
else:\r
return qs\r
\r
- def get(self, *args, **kwargs):\r
- action = super(ActionManager, self).get(*args, **kwargs)\r
- if self.model == Action:\r
- return action.leaf()\r
- return action\r
-\r
def get_for_types(self, types, *args, **kwargs):\r
kwargs['action_type__in'] = [t.get_type() for t in types]\r
return self.get(*args, **kwargs)\r
\r
- \r
\r
-class Action(models.Model):\r
+class Action(BaseModel):\r
user = models.ForeignKey('User', related_name="actions")\r
ip = models.CharField(max_length=16)\r
node = models.ForeignKey('Node', null=True, related_name="actions")\r
return ""\r
\r
def describe(self, viewer=None):\r
- return ""\r
+ return self.__class__.__name__\r
\r
def get_absolute_url(self):\r
if self.node:\r
return self\r
\r
leaf = leaf_cls()\r
- leaf.__dict__ = self.__dict__\r
+ d = self._as_dict()\r
+ leaf.__dict__.update(self._as_dict())\r
+ l = leaf._as_dict()\r
return leaf\r
\r
@classmethod\r
super(Action, self).save(*args, **kwargs)\r
\r
if isnew:\r
- if (self.node is None) or (not self.node.wiki):\r
+ if (self.node is None) or (not self.node.nis.wiki):\r
self.repute_users()\r
self.process_action()\r
self.trigger_hooks(True)\r
\r
return self\r
\r
- def delete(self):\r
+ def delete(self, *args, **kwargs):\r
self.cancel_action()\r
- super(Action, self).delete()\r
+ super(Action, self).delete(*args, **kwargs)\r
\r
def cancel(self, user=None, ip=None):\r
if not self.canceled:\r
except Exception, e:\r
logging.error("Error in %s hook: %s" % (cls.__name__, str(e)))\r
\r
-class ActionProxyMetaClass(models.Model.__metaclass__):\r
+class ActionProxyMetaClass(BaseMetaClass):\r
types = {}\r
\r
def __new__(cls, *args, **kwargs):\r
class Meta:\r
proxy = True\r
\r
-class DummyActionProxy(Action):\r
- __metaclass__ = ActionProxyMetaClass\r
+class DummyActionProxyMetaClass(type):\r
+ def __new__(cls, *args, **kwargs):\r
+ new_cls = super(DummyActionProxyMetaClass, cls).__new__(cls, *args, **kwargs)\r
+ ActionProxyMetaClass.types[new_cls.get_type()] = new_cls\r
+ return new_cls\r
+\r
+class DummyActionProxy(object):\r
+ __metaclass__ = DummyActionProxyMetaClass\r
\r
hooks = []\r
\r
+ def __init__(self, ip=None):\r
+ self.ip = ip\r
+\r
def process_data(self, **data):\r
pass\r
\r
\r
def save(self, *args, **kwargs):\r
super(ActionRepute, self).save(*args, **kwargs)\r
- self.user.reputation = models.F('reputation') + self.value\r
+ self.user.reputation += self.value\r
self.user.save()\r
\r
def delete(self):\r
- self.user.reputation = models.F('reputation') - self.value\r
+ self.user.reputation -= self.value\r
self.user.save()\r
super(ActionRepute, self).delete()\r
\r