def validate_microblogging_schema(structure): """ MICROBLOG_POST = { 'author': user_id, 'location': node_id, 'type': one of [userpost, node_created, node_refined, node_spam_marked, node_spam_unmarked, node_followed, node_unfollowed] 'template_text': text, 'mentions': [user_id, None], # have to be sorted and unique 'references': [path, None], # have to be sorted and unique 'answer_to': microblog_id # -1 if not an answer } """ entries = [('author', int), ('location', int), ('type', unicode), ('template_text', unicode), ('mentions', list), ('references', list), ('answer_to', int)] for n, t in entries: assert n in structure, "Required field '%s' is missing." % n e = structure[n] assert isinstance(e, t), \ "Type of field '%s' should be %s but was %s" % (n, t, type(e)) # validate type allowed_types = {"userpost", "node_created", "node_refined", "node_spam_marked", "node_spam_unmarked", "node_followed", "node_unfollowed"} assert structure['type'] in allowed_types, \ "Invalid type '%s'" % structure['type'] # validate mentions mentions = structure['mentions'] assert mentions == sorted(mentions), "mentions must be sorted!" assert len(set(mentions)) == len(mentions), "mentions must be unique!" for m in mentions: assert isinstance(m, int), \ "mentions have to be IDs (int) but was of type %s" % type(m) User.objects.get(id=m) # validate node_references references = structure['references'] assert references == sorted(references), "references must be sorted!" assert len(set(references)) == len(references), "references must be unique!" for r in references: assert isinstance(r, unicode), \ "references have to be paths (unicode) but was %s" % type(r) get_node_for_path(r) # raises Illegal Path if invalid path return True
def validate_microblogging_schema(structure): """ MICROBLOG_POST = { 'author': user_id, 'location': node_id, 'type': one of [userpost, node_created, node_refined, node_spam_marked, node_spam_unmarked, node_followed, node_unfollowed] 'template_text': text, 'mentions': [user_id, None], # have to be sorted and unique 'references': [path, None], # have to be sorted and unique 'answer_to': microblog_id # -1 if not an answer } """ entries = [('author', int), ('location', int), ('type', unicode), ('template_text', unicode), ('mentions', list), ('references', list), ('answer_to', int)] for n, t in entries: assert n in structure, "Required field '%s' is missing." % n e = structure[n] assert isinstance(e, t), \ "Type of field '%s' should be %s but was %s" % (n, t, type(e)) # validate type allowed_types = { "userpost", "node_created", "node_refined", "node_spam_marked", "node_spam_unmarked", "node_followed", "node_unfollowed" } assert structure['type'] in allowed_types, \ "Invalid type '%s'" % structure['type'] # validate mentions mentions = structure['mentions'] assert mentions == sorted(mentions), "mentions must be sorted!" assert len(set(mentions)) == len(mentions), "mentions must be unique!" for m in mentions: assert isinstance(m, int), \ "mentions have to be IDs (int) but was of type %s" % type(m) User.objects.get(id=m) # validate node_references references = structure['references'] assert references == sorted(references), "references must be sorted!" assert len( set(references)) == len(references), "references must be unique!" for r in references: assert isinstance(r, unicode), \ "references have to be paths (unicode) but was %s" % type(r) get_node_for_path(r) # raises Illegal Path if invalid path return True
def test_delete_node_does_not_remove_non_empty_parent_slot(self): self.assertEqual(Node.objects.filter( title='verfassungswiedrig').count(), 1) node = get_node_for_path(self.derivate_path) delete_node(node) self.assertEqual(Node.objects.filter( title='verfassungswiedrig').count(), 1)
def test_delete_node_removes_children(self): self.assertEqual(Node.objects.filter(title='or_to_pee').count(), 1) self.assertEqual(Node.objects.filter(title='or to pee').count(), 1) node = get_node_for_path(self.path) delete_node(node) self.assertEqual(Node.objects.filter(title='or_to_pee').count(), 0) self.assertEqual(Node.objects.filter(title='or to pee').count(), 0)
def parse_microblogging(text, author, location, answer_to=None): """ Parse the text of a Microblog-Post and turn it into a JSON Structure that can then easily be turned into a database entry """ references, template_text = extract_references(text) mentions, template_text = extract_mentions(template_text) return { 'author': author.id, 'location': get_node_for_path(location).id, 'type': "userpost", 'template_text': template_text, 'mentions': mentions, 'references': [get_node_for_path(p) for p in references], 'answer_to': answer_to }
def parse_microblogging(text, author, location, answer_to=None): """ Parse the text of a Microblog-Post and turn it into a JSON Structure that can then easily be turned into a database entry """ references, template_text = extract_references(text) mentions, template_text = extract_mentions(template_text) return { 'author': author.id, 'location': get_node_for_path(location).id, 'type': "userpost", 'template_text': template_text, 'mentions': mentions, 'references': [get_node_for_path(p) for p in references], 'answer_to': answer_to }
def extract_references(text): references = set() for r in set(re.findall(REFERENCE_PATTERN, text)): try: get_node_for_path(r) references.add(r) except IllegalPath: pass references = sorted(references) def reference_sub(m): path = m.group().strip('/') if path in references: return "{n%d}" % references.index(path) else: return '/' + path template_text, _ = re.subn(REFERENCE_PATTERN, reference_sub, text) return references, template_text
def extract_references(text): references = set() for r in set(re.findall(REFERENCE_PATTERN, text)): try: get_node_for_path(r) references.add(r) except IllegalPath: pass references = sorted(references) def reference_sub(m): path = m.group().strip('/') if path in references: return "{n%d}" % references.index(path) else: return '/' + path template_text, _ = re.subn(REFERENCE_PATTERN, reference_sub, text) return references, template_text
def test_add_derivate_preserves_substructure(self): self.assertTrue(self.client.login(username="******", password="******")) text_string = "= Bla =\nBlubb.\n== Level 2 ==\nSome text." response = self.client.post(self.url, dict(wikiTextAlternative=text_string)) self.assertEqual(response.status_code, 200) text_string2 = text_string + "\n== Another Level 2 ==\nSome other text." response = self.client.post( "/.json_storeText/Slot.2", dict(argumentType="con", wikiText="= Argumenttitle =\nThis is better now.", wikiTextAlternative=text_string2)) self.assertEqual(response.status_code, 200) self.assertEqual(json.loads(response.content)['storeTextResponse']['path'], "Slot.3") new_node = get_node_for_path('Slot.3') self.assertEqual(new_node.text.text, "Blubb.") self.assertEqual(new_node.children.all()[0].children.all()[0].text.text, "Some text.") self.assertEqual(new_node.children.all()[1].children.all()[0].text.text, "Some other text.") old_node = get_node_for_path('Slot.2') self.assertEqual(new_node.children.all()[0].children.all()[0], old_node.children.all()[0].children.all()[0])
def handle(self, *args, **options): if len(args) < 1: print("You have to provide a <NodePath>.") if len(args) > 1: print("Please provide exactly one argument!") node_path = args[0] node = get_node_for_path(node_path) assert node, "Node for path '%s' does not exist." % node_path delete_node(node)
def test_delete_node_removes_text(self): self.assertEqual(Text.objects.filter(text='never both').count(), 1) node = get_node_for_path(self.path) delete_node(node) self.assertEqual(Text.objects.filter(text='never both').count(), 0)
def test_delete_node_removes_index_cache_entry(self): self.assertEqual(IndexCache.objects.filter(path=self.path).count(), 1) node = get_node_for_path(self.path) delete_node(node) self.assertEqual(IndexCache.objects.filter(path=self.path).count(), 0)
def test_delete_node_on_derivate_removes_derivation_argument(self): self.assertEqual(Argument.objects.filter(title='zuSchwach').count(), 1) node = get_node_for_path(self.derivate_path) delete_node(node) self.assertEqual(Argument.objects.filter(title='zuSchwach').count(), 0)
def test_delete_node_removes_argument(self): self.assertEqual(Argument.objects.filter(title='no').count(), 1) node = get_node_for_path(self.path) delete_node(node) self.assertEqual(Argument.objects.filter(title='no').count(), 0)
def test_delete_node_removes_derivates(self): self.assertEqual(Node.objects.filter(title='BöserTitel').count(), 2) node = get_node_for_path(self.source_path) delete_node(node) self.assertEqual(Node.objects.filter(title='BöserTitel').count(), 0)
def test_delete_node_removes_node(self): node = get_node_for_path(self.path) delete_node(node) self.assertRaises(IllegalPath, get_node_for_path, self.path)
def test_delete_node_removes_spam_flags(self): self.assertEqual(self.horst.spamflag_set.count(), 1) node = get_node_for_path(self.path) delete_node(node) self.assertEqual(self.horst.spamflag_set.count(), 0)
def test_delete_node_removes_votes(self): self.assertEqual(self.udo.vote_set.count(), 2) node = get_node_for_path(self.path) delete_node(node) self.assertEqual(self.udo.vote_set.count(), 1)
def test_delete_node_removes_derivation(self): self.assertEqual(self.source.derivates.count(), 1) node = get_node_for_path(self.derivate_path) delete_node(node) self.assertEqual(self.source.derivates.count(), 0)
def test_delete_node_does_not_remove_vote_from_source(self): self.assertEqual(self.source.votes.count(), 1) node = get_node_for_path(self.derivate_path) delete_node(node) self.assertEqual(self.source.votes.count(), 1)
def test_delete_node_removes_empty_parent_slot(self): self.assertEqual(Node.objects.filter(title='soon_empty').count(), 1) node = get_node_for_path(self.path) delete_node(node) self.assertEqual(Node.objects.filter(title='soon_empty').count(), 0)