diff options
author | Evgeny Fadeev <evgeny.fadeev@gmail.com> | 2012-07-02 12:43:09 -0400 |
---|---|---|
committer | Evgeny Fadeev <evgeny.fadeev@gmail.com> | 2012-07-02 12:43:09 -0400 |
commit | 3ed4d6c37552cc97464444a65074f3b8ddf7d9f5 (patch) | |
tree | 5d8be200da5fcd3bc1d77f81f17e3b527622e960 /askbot/utils | |
parent | 17ee9fe0f5de3489d35577a783748eb84602fa86 (diff) | |
parent | 33fb5deb70373f251788b257e9fa0cbe16849b95 (diff) | |
download | askbot-3ed4d6c37552cc97464444a65074f3b8ddf7d9f5.tar.gz askbot-3ed4d6c37552cc97464444a65074f3b8ddf7d9f5.tar.bz2 askbot-3ed4d6c37552cc97464444a65074f3b8ddf7d9f5.zip |
merged with the master and tag-editor branches
Diffstat (limited to 'askbot/utils')
-rw-r--r-- | askbot/utils/category_tree.py | 149 | ||||
-rw-r--r-- | askbot/utils/html.py | 9 |
2 files changed, 156 insertions, 2 deletions
diff --git a/askbot/utils/category_tree.py b/askbot/utils/category_tree.py new file mode 100644 index 00000000..4406bdfd --- /dev/null +++ b/askbot/utils/category_tree.py @@ -0,0 +1,149 @@ +"""This is temporary code to parse category +tree, stored in the settings. +The tree is plain text, with levels of branching +reflected by indentation (2 spaces per level). +example of desired structure, when input is parsed + + cat_tree = [ + ['dummy', + [ + ['tires', [ + ['michelin', [ + ['trucks', []], + ['cars', []], + ['motorcycles', []] + ] + ], + ['good year', []], + ['honda', []], + ] + ], + ['abandonment', []], + ['chile', []], + ['vulcanization', []], + ] + ] + ] +""" +from askbot.conf import settings as askbot_settings +from django.utils import simplejson + +def get_leaf_index(tree, leaf_name): + children = tree[1] + for index, child in enumerate(children): + if child[0] == leaf_name: + return index + return None + +def _get_subtree(tree, path): + clevel = tree + for pace in path: + clevel = clevel[1][pace] + return clevel + +def get_subtree(tree, path): + """path always starts with 0, + and is a list of integers""" + assert(path[0] == 0) + if len(path) == 1:#special case + return tree[0] + else: + return _get_subtree(tree[0], path[1:]) + +def sort_tree(tree): + """sorts contents of the nodes alphabetically""" + tree = sorted(tree, lambda x,y: cmp(x[0], y[0])) + for item in tree: + item[1] = sort_tree(item[1]) + return tree + +def get_data(): + """returns category tree data structure encoded as json + or None, if category_tree is disabled + """ + if askbot_settings.TAG_SOURCE == 'category-tree': + return simplejson.loads(askbot_settings.CATEGORY_TREE) + else: + return None + +def _get_leaf_names(subtree): + leaf_names = set() + for leaf in subtree: + leaf_names.add(leaf[0]) + leaf_names |= _get_leaf_names(leaf[1]) + return leaf_names + +def get_leaf_names(tree = None): + """returns set of leaf names""" + data = tree or get_data() + if data is None: + return set() + return _get_leaf_names(data[0][1]) + +def path_is_valid(tree, path): + try: + get_subtree(tree, path) + return True + except IndexError: + return False + except AssertionError: + return False + +def add_category(tree, category_name, path): + subtree = get_subtree(tree, path) + children = subtree[1] + children.append([category_name, []]) + children = sorted(children, lambda x,y: cmp(x[0], y[0])) + subtree[1] = children + new_path = path[:] + #todo: reformulate all paths in terms of names? + new_item_index = get_leaf_index(subtree, category_name) + assert new_item_index != None + new_path.append(new_item_index) + return new_path + +def _has_category(tree, category_name): + for item in tree: + if item[0] == category_name: + return True + if _has_category(item[1], category_name): + return True + return False + +def has_category(tree, category_name): + """true if category is in tree""" + #skip the dummy + return _has_category(tree[0][1], category_name) + +def rename_category( + tree, from_name = None, to_name = None, path = None +): + if to_name == from_name: + return + subtree = get_subtree(tree, path[:-1]) + from_index = get_leaf_index(subtree, from_name) + #todo possibly merge if to_name exists on the same level + #to_index = get_leaf_index(subtree, to_name) + child = subtree[1][from_index] + child[0] = to_name + return sort_tree(tree) + +def _delete_category(tree, name): + for item in tree: + if item[0] == name: + tree.remove(item) + return True + if _delete_category(item[1], name): + return True + return False + +def delete_category(tree, name, path): + subtree = get_subtree(tree, path[:-1]) + del_index = get_leaf_index(subtree, name) + subtree[1].pop(del_index) + return sort_tree(tree) + +def save_data(tree): + assert(askbot_settings.TAG_SOURCE == 'category-tree') + tree_json = simplejson.dumps(tree) + askbot_settings.update('CATEGORY_TREE', tree_json) diff --git a/askbot/utils/html.py b/askbot/utils/html.py index 2e3c1913..44e3f1df 100644 --- a/askbot/utils/html.py +++ b/askbot/utils/html.py @@ -55,6 +55,11 @@ def sanitize_html(html): output_generator = s.serialize(stream) return u''.join(output_generator) +def site_url(url): + from askbot.conf import settings + base_url = urlparse(settings.APP_URL) + return base_url.scheme + '://' + base_url.netloc + url + def site_link(url_name, title): """returns html for the link to the given url todo: may be improved to process url parameters, keyword @@ -62,8 +67,8 @@ def site_link(url_name, title): """ from askbot.conf import settings base_url = urlparse(settings.APP_URL) - url = base_url.scheme + '://' + base_url.netloc + reverse(url_name) - return '<a href="%s">%s</a>' % (url, escape(title)) + url = site_url(reverse(url_name)) + return '<a href="%s">%s</a>' % (url, title) def unescape(text): """source: http://effbot.org/zone/re-sub.htm#unescape-html |