~ubuntu-branches/ubuntu/utopic/python-django-threadedcomments/utopic

« back to all changes in this revision

Viewing changes to threadedcomments/util.py

  • Committer: Package Import Robot
  • Author(s): Bernhard Reiter, Bernhard Reiter, Jakub Wilk
  • Date: 2013-08-09 19:12:14 UTC
  • mfrom: (1.1.2)
  • Revision ID: package-import@ubuntu.com-20130809191214-p8482p4e1ctekffq
Tags: 0.9.0-1
[ Bernhard Reiter ]
* New upstream release
* debian/compat: Bump to 9.
* debian/control, debian/copyright: Update upstream links. (Closes: #713930)
* debian/control: 
  - Remove python-textile, python-markdown, python-docutils from
    Build-Depends: and Suggests:
  - Bump debhelper Build-Depends to >= 9.
  - Bump Standards-Version to 3.9.4.
  - Add Suggests: python-django-south
* debian/copyright: s/BSD-new/BSD-3-clause/
* debian/docs, debian/doc-base: Update/remove to correspond with
  upstream sources.
* debian/patches/series, debian/patches/add_markup_deps_to_install,
  debian/patches/gravatar_default_url, debian/patches/django14:
  Remove patches, as obsolete.
* debian/rules: Add a SECRET_KEY for tests during building. (Closes: #711369)
* debian/NEWS.Debian: Add.

[ Jakub Wilk ]
* Use canonical URIs for Vcs-* fields.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from itertools import chain, imap
 
2
 
 
3
__all__ = ['fill_tree', 'annotate_tree_properties', ]
 
4
 
 
5
def _mark_as_root_path(comment):
 
6
    """
 
7
    Mark on comment as Being added to fill the tree.
 
8
    """
 
9
    setattr(comment, 'added_path', True)
 
10
    return comment
 
11
 
 
12
def fill_tree(comments):
 
13
    """
 
14
    Insert extra comments in the comments list, so that the root path of the first comment is always visible.
 
15
    Use this in comments' pagination to fill in the tree information.
 
16
 
 
17
    The inserted comments have an ``added_path`` attribute.
 
18
    """
 
19
    if not comments:
 
20
        return
 
21
 
 
22
    it = iter(comments)
 
23
    first = it.next()
 
24
    extra_path_items = imap(_mark_as_root_path, first.root_path)
 
25
    return chain(extra_path_items, [first], it)
 
26
 
 
27
def annotate_tree_properties(comments):
 
28
    """
 
29
    iterate through nodes and adds some magic properties to each of them
 
30
    representing opening list of children and closing it
 
31
    """
 
32
    if not comments:
 
33
        return
 
34
 
 
35
    it = iter(comments)
 
36
 
 
37
    # get the first item, this will fail if no items !
 
38
    old = it.next()
 
39
 
 
40
    # first item starts a new thread
 
41
    old.open = True
 
42
    last = set()
 
43
    for c in it:
 
44
        # if this comment has a parent, store its last child for future reference
 
45
        if old.last_child_id:
 
46
            last.add(old.last_child_id)
 
47
 
 
48
        # this is the last child, mark it
 
49
        if c.pk in last:
 
50
            c.last = True
 
51
 
 
52
        # increase the depth
 
53
        if c.depth > old.depth:
 
54
            c.open = True
 
55
 
 
56
        else: # c.depth <= old.depth
 
57
            # close some depths
 
58
            old.close = range(old.depth - c.depth)
 
59
 
 
60
            # new thread
 
61
            if old.root_id != c.root_id:
 
62
                # close even the top depth
 
63
                old.close.append(len(old.close))
 
64
                # and start a new thread
 
65
                c.open = True
 
66
                # empty the last set
 
67
                last = set()
 
68
        # iterate
 
69
        yield old
 
70
        old = c
 
71
 
 
72
    old.close = range(old.depth)
 
73
    yield old