X-Git-Url: https://git.openstreetmap.org./osqa.git/blobdiff_plain/aedb39383100b0ae2613c2f3beee6d397cd560d4..fe388980b431aef835eaf43f4547e51f763c3f4b:/forum/models/node.py diff --git a/forum/models/node.py b/forum/models/node.py index 6f4e12b..b375ec4 100644 --- a/forum/models/node.py +++ b/forum/models/node.py @@ -1,4 +1,5 @@ from base import * +import re from tag import Tag import markdown @@ -79,13 +80,21 @@ class NodeMetaClass(BaseMetaClass): class NodeQuerySet(CachedQuerySet): + def obj_from_datadict(self, datadict): + cls = NodeMetaClass.types.get(datadict.get("node_type", ""), None) + if cls: + obj = cls() + obj.__dict__.update(datadict) + return obj + else: + return super(NodeQuerySet, self).obj_from_datadict(datadict) + def get(self, *args, **kwargs): - node = super(NodeQuerySet, self).get(*args, **kwargs) - cls = NodeMetaClass.types.get(node.node_type, None) + return super(NodeQuerySet, self).get(*args, **kwargs).leaf - if cls and (node.__class__ is not cls): - return node.leaf - return node + def filter_state(self, **kwargs): + apply_bool = lambda q, b: b and q or ~q + return self.filter(*[apply_bool(models.Q(state_string__contains="(%s)" % s), b) for s, b in kwargs.items()]) class NodeManager(CachedManager): @@ -103,6 +112,60 @@ class NodeManager(CachedManager): kwargs['node_type__in'] = [t.get_type() for t in types] return self.get(*args, **kwargs) + def filter_state(self, **kwargs): + return self.all().filter_state(**kwargs) + + +class NodeStateDict(object): + def __init__(self, node): + self.__dict__['_node'] = node + + def __getattr__(self, name): + if self.__dict__.get(name, None): + return self.__dict__[name] + + try: + node = self.__dict__['_node'] + action = NodeState.objects.get(node=node, state_type=name).action + self.__dict__[name] = action + return action + except: + return None + + def __setattr__(self, name, value): + current = self.__getattr__(name) + + if value: + if current: + current.action = value + current.save() + else: + node = self.__dict__['_node'] + state = NodeState(node=node, action=value, state_type=name) + state.save() + self.__dict__[name] = value + + if not "(%s)" % name in node.state_string: + node.state_string = "%s(%s)" % (node.state_string, name) + node.save() + else: + if current: + node = self.__dict__['_node'] + node.state_string = "".join("(%s)" % s for s in re.findall('\w+', node.state_string) if s != name) + node.save() + current.node_state.delete() + del self.__dict__[name] + + +class NodeStateQuery(object): + def __init__(self, node): + self.__dict__['_node'] = node + + def __getattr__(self, name): + node = self.__dict__['_node'] + return "(%s)" % name in node.state_string + + class Node(BaseModel, NodeContent): __metaclass__ = NodeMetaClass @@ -114,8 +177,7 @@ class Node(BaseModel, NodeContent): added_at = models.DateTimeField(default=datetime.datetime.now) score = models.IntegerField(default=0) - deleted = models.ForeignKey('Action', null=True, unique=True, related_name="deleted_node") - in_moderation = models.ForeignKey('Action', null=True, unique=True, related_name="moderated_node") + state_string = models.TextField(default='') last_edited = models.ForeignKey('Action', null=True, unique=True, related_name="edited_node") last_activity_by = models.ForeignKey(User, null=True) @@ -126,10 +188,8 @@ class Node(BaseModel, NodeContent): extra_ref = models.ForeignKey('Node', null=True) extra_count = models.IntegerField(default=0) - extra_action = models.ForeignKey('Action', null=True, related_name="extra_node") - + marked = models.BooleanField(default=False) - wiki = models.BooleanField(default=False) comment_count = DenormalizedField("children", node_type="comment", canceled=False) flag_count = DenormalizedField("flags") @@ -140,7 +200,7 @@ class Node(BaseModel, NodeContent): @classmethod def cache_key(cls, pk): - return '%s.node:%s' % (settings.APP_URL, pk) + return '%s:node:%s' % (settings.APP_URL, pk) @classmethod def get_type(cls): @@ -157,12 +217,36 @@ class Node(BaseModel, NodeContent): leaf.__dict__ = self.__dict__ return leaf + @property + def nstate(self): + state = self.__dict__.get('_nstate', None) + + if state is None: + state = NodeStateDict(self) + self._nstate = state + + return state + + @property + def nis(self): + nis = self.__dict__.get('_nis', None) + + if nis is None: + nis = NodeStateQuery(self) + self._nis = nis + + return nis + + @property + def deleted(self): + return self.nis.deleted + @property def absolute_parent(self): if not self.abs_parent_id: - return self.leaf + return self - return self.abs_parent.leaf + return self.abs_parent @property def summary(self): @@ -209,8 +293,8 @@ class Node(BaseModel, NodeContent): if not 'tagnames' in dirty: return None else: - if dirty['tagnames']: - old_tags = set(name for name in dirty['tagnames'].split(u' ')) + if self._original_state['tagnames']: + old_tags = set(name for name in self._original_state['tagnames'].split(u' ')) else: old_tags = set() new_tags = set(name for name in self.tagnames.split(u' ') if name) @@ -234,18 +318,16 @@ class Node(BaseModel, NodeContent): except: tag = Tag.objects.create(name=name, created_by=self._last_active_user()) - if not self.deleted: + if not self.nis.deleted: tag.used_count = models.F('used_count') + 1 tag.save() - if not self.deleted: + if not self.nis.deleted: for name in tag_changes['removed']: try: tag = Tag.objects.get(name=name) tag.used_count = models.F('used_count') - 1 tag.save() - if tag.used_count == 0: - tag.mark_deleted(self._last_active_user()) except: pass @@ -254,21 +336,21 @@ class Node(BaseModel, NodeContent): return False def mark_deleted(self, action): - self.deleted = action + self.nstate.deleted = action self.save() if action: for tag in self.tags.all(): tag.used_count = models.F('used_count') - 1 tag.save() - if tag.used_count == 0: - tag.mark_deleted(self._last_active_user()) else: for tag in Tag.objects.filter(name__in=self.tagname_list()): tag.used_count = models.F('used_count') + 1 tag.save() def save(self, *args, **kwargs): + tags_changed = self._process_changes_in_tags() + if not self.id: self.node_type = self.get_type() super(BaseModel, self).save(*args, **kwargs) @@ -278,8 +360,6 @@ class Node(BaseModel, NodeContent): if self.parent_id and not self.abs_parent_id: self.abs_parent = self.parent.absolute_parent - tags_changed = self._process_changes_in_tags() - super(Node, self).save(*args, **kwargs) if tags_changed: self.tags = list(Tag.objects.filter(name__in=self.tagname_list())) @@ -298,3 +378,13 @@ class NodeRevision(BaseModel, NodeContent): app_label = 'forum' +class NodeState(models.Model): + node = models.ForeignKey(Node, related_name='states') + state_type = models.CharField(max_length=16) + action = models.OneToOneField('Action', related_name="node_state") + + class Meta: + unique_together = ('node', 'state_type') + app_label = 'forum' + +