def build_text(node, depth=2): depth = min(depth, 6) text = "=" * depth + node.title + "=" * depth + "\n" + node.text.text for slot in node.children.prefetch_related('children').all(): favorite = backend.get_favorite_if_slot(slot) text += "\n\n" + build_text(favorite, depth + 1) return text
def load_text(request, path): prefix, path_type = parse_suffix(path) try: node = backend.get_node_for_path(prefix) except backend.IllegalPath: return json_error_response("IllegalPath", "Illegal Path: " + path) paragraphs = [ { "wikiText": "=" + node.title + "=\n" + node.text.text, "path": path, "isFollowing": node.votes.filter(user=request.user.id).count() > 0, "authorGroup": [create_user_info(a) for a in node.text.authors.all()], } ] for slot in backend.get_ordered_children_for(node): favorite = backend.get_favorite_if_slot(slot) paragraphs.append( { "wikiText": build_text(favorite, depth=2), "path": path + "/" + slot.title + "." + str(favorite.get_index(slot)), "isFollowing": favorite.votes.filter(user=request.user.id).count() > 0, "authorGroup": [create_user_info(a) for a in favorite.text.authors.all()], } ) return json_response( { "loadTextResponse": { "paragraphs": paragraphs, "isFollowing": node.votes.filter(user=request.user.id).count() > 0, } } )
def create_index_node_for_slot(slot): favorit = backend.get_favorite_if_slot(slot) ##optimization: switch to get_favorit_for_slot that also returns the index index_node = dict( shortTitle = slot.title, fullTitle = favorit.title, index = favorit.get_index(slot), authorGroup = [create_user_info(a) for a in favorit.text.authors.all()] ) return index_node
def create_post(text, author, path=None, do_escape=True): if do_escape: text = escape(text) split_text = user_ref_pattern.split(text) mentions = [] for i in range(1, len(split_text), 2): username = split_text[i] try: u = User.objects.get(username__iexact=username) split_text[i] = '<a href="/#/user/{0}">@{0}</a>'.format(u.username) mentions.append(u) except User.DoesNotExist: split_text[i] = '@' + username text = "".join(split_text) split_text = tag_pattern.split(text) for i in range(1, len(split_text), 2): tagname = split_text[i] split_text[i] = '<a href="/#/search/{0}">#{0}</a>'.format(tagname) text = "".join(split_text) split_text = internal_link_pattern.split(text) nodes = [] if path is not None: nodes.append(backend.get_node_for_path(path)) for i in range(1, len(split_text), 2): path = split_text[i] try: n = backend.get_node_for_path(path) if n.node_type == backend.Node.SLOT: slot = n n = backend.get_favorite_if_slot(n) position = backend.NodeOrder.objects.filter(child=n).filter( parent=slot).all()[0].position path += "." + str(position) split_text[i] = '<a href="{0}">{1}</a>'.format( '/#' + path, path.rsplit('/', 1)[1]) nodes.append(n) except backend.IllegalPath: pass text = "".join(split_text) split_text = url_pattern.split(text) for i in range(1, len(split_text), 2): link = split_text[i] split_text[i] = '<a href="{0}">{0}</a>'.format(link) text = "".join(split_text) post = Post() post.text = text post.author = author post.save() post.mentions.add(*mentions) post.node_references.add(*nodes) post.save() return post