Beispiel #1
0
def threaded_comment_json(comment, viewer):
    coll = ContentType.objects.get_for_model(Collaboration)
    all = ThreadedComment.objects.filter(
        content_type=coll, object_pk=comment.content_object.pk, site__pk=settings.SITE_ID
    )
    all = fill_tree(all)
    all = annotate_tree_properties(all)

    return {
        "type": "discussion",
        "form": comments.get_form()(comment.content_object).__unicode__(),
        "editing": True,
        "can_edit": True,
        "discussion": {
            "id": comment.id,
            "max_length": COMMENT_MAX_LENGTH,
            "thread": [
                {
                    "open": obj.open if hasattr(obj, "open") else None,
                    "close": [i for i in obj.close] if hasattr(obj, "close") else None,
                    "id": obj.id,
                    "author": obj.name,
                    "author_username": obj.user.username,
                    "submit_date": pretty_date(obj.submit_date),
                    "title": obj.title,
                    "content": obj.comment,
                    "can_edit": True if obj.user == viewer else False,
                }
                for obj in all
            ],
        },
    }
Beispiel #2
0
def threaded_comment_json(comment, viewer):
    coll = ContentType.objects.get_for_model(Collaboration)
    all = ThreadedComment.objects.filter(content_type=coll, object_pk=comment.content_object.pk, site__pk=settings.SITE_ID)
    all = fill_tree(all)
    all = annotate_tree_properties(all)

    return {
        'type': 'discussion',
        'form': comments.get_form()(comment.content_object).__unicode__(),
        'editing': True,
        'can_edit': True,
        'discussion': {
            'id': comment.id,
            'max_length': COMMENT_MAX_LENGTH,
            'thread': [{ 'open': obj.open if hasattr(obj, "open") else None,
                         'close': [ i for i in obj.close ] if hasattr(obj, "close") else None,
                         'id': obj.id,
                         'author': obj.name,
                         'author_username': obj.user.username,
                         'submit_date': pretty_date(obj.submit_date),
                         'title': obj.title,
                         'content': obj.comment,
                         'can_edit': True if obj.user == viewer else False
                       } for obj in all]
         }
    }
Beispiel #3
0
def threaded_comment_json(request, comment):
    viewer = request.user
    coll = ContentType.objects.get_for_model(Collaboration)
    all_comments = ThreadedComment.objects.filter(
        content_type=coll,
        object_pk=comment.content_object.pk,
        site__pk=settings.SITE_ID)
    all_comments = fill_tree(all_comments)
    all_comments = list(annotate_tree_properties(all_comments))

    rand = ''.join([choice(letters) for i in range(5)])
    citations = threaded_comment_citations(all_comments, viewer)

    asset_resource = AssetResource()
    sherd_resource = SherdNoteResource()

    return {
        'type':
        'discussion',
        'form':
        comments.get_form()(comment.content_object).__unicode__(),
        'editing':
        True,
        'can_edit':
        True,
        'discussion': {
            'id':
            comment.id,
            'max_length':
            COMMENT_MAX_LENGTH,
            'thread': [{
                'open':
                obj.open if hasattr(obj, "open") else None,
                'close':
                [i for i in obj.close] if hasattr(obj, "close") else None,
                'id':
                obj.id,
                'author':
                obj.name,
                'author_username':
                obj.user.username,
                'submit_date':
                pretty_date(obj.submit_date),
                'title':
                obj.title,
                'content':
                obj.comment,
                'can_edit':
                True if obj.user == viewer else False
            } for obj in all_comments]
        },
        'assets':
        dict([('%s_%s' % (rand, ann.asset.pk),
               asset_resource.render_one(request, ann.asset))
              for ann in citations if (ann.title != "Annotation Deleted"
                                       and ann.title != 'Asset Deleted')]),
        'annotations':
        [sherd_resource.render_one(request, ann, rand) for ann in citations],
    }
Beispiel #4
0
    def test_open_and_close_match(self):
        depth = 0
        for x in annotate_tree_properties(comments.get_model().objects.all()):
            depth += getattr(x, 'open', 0)
            self.assertEqual(x.depth, depth)
            depth -= len(getattr(x, 'close', []))

        self.assertEqual(0, depth)
    def test_open_and_close_match(self):
        depth = 0
        for x in annotate_tree_properties(django_comments.get_model().objects.all()):
            depth += getattr(x, 'open', 0)
            self.assertEqual(x.depth, depth)
            depth -= len(getattr(x, 'close', []))

        self.assertEqual(0, depth)
Beispiel #6
0
    def test_last_flags_set_correctly_only_on_last_sibling(self):
        # construct the tree
        nodes = {}
        for x in comments.get_model().objects.all():
            nodes[x.pk] = (x, [])
            if x.parent_id:
                nodes[x.parent_id][1].append(x.pk)

        # check all the comments
        for x in annotate_tree_properties(comments.get_model().objects.all()):
            if getattr(x, 'last', False):
                # last comments have a parent
                self.assertTrue(x.parent_id)
                par, siblings = nodes[x.parent_id]

                # and ar last in their child list
                self.assertTrue(x.pk in siblings)
                self.assertEqual(len(siblings) - 1, siblings.index(x.pk))
    def test_last_flags_set_correctly_only_on_last_sibling(self):
        # construct the tree
        nodes = {}
        for x in django_comments.get_model().objects.all():
            nodes[x.pk] = (x, [])
            if x.parent_id:
                nodes[x.parent_id][1].append(x.pk)

        # check all the comments
        for x in annotate_tree_properties(django_comments.get_model().objects.all()):
            if getattr(x, 'last', False):
                # last comments have a parent
                self.assertTrue(x.parent_id)
                par, siblings = nodes[x.parent_id]

                # and ar last in their child list
                self.assertTrue(x.pk in siblings)
                self.assertEqual(len(siblings) - 1, siblings.index(x.pk))
Beispiel #8
0
def threaded_comment_json(request, comment):
    viewer = request.user
    coll = ContentType.objects.get_for_model(Collaboration)
    all_comments = ThreadedComment.objects.filter(
        content_type=coll, object_pk=comment.content_object.pk,
        site__pk=settings.SITE_ID)
    all_comments = fill_tree(all_comments)
    all_comments = list(annotate_tree_properties(all_comments))

    rand = ''.join([choice(letters) for i in range(5)])
    citations = threaded_comment_citations(all_comments, viewer)

    asset_resource = AssetResource()
    sherd_resource = SherdNoteResource()

    return {
        'type': 'discussion',
        'form': django_comments.get_form()(
            comment.content_object).__unicode__(),
        'editing': True,
        'can_edit': True,
        'discussion': {
            'id': comment.id,
            'max_length': COMMENT_MAX_LENGTH,
            'thread': [{'open': obj.open if hasattr(obj, "open") else None,
                        'close': [i for i in obj.close]
                        if hasattr(obj, "close") else None,
                        'id': obj.id,
                        'author': obj.name,
                        'author_username': obj.user.username,
                        'submit_date': pretty_date(obj.submit_date),
                        'title': obj.title,
                        'content': obj.comment,
                        'can_edit': True if obj.user == viewer else False}
                       for obj in all_comments]
        },
        'assets': dict([('%s_%s' % (rand, ann.asset.pk),
                        asset_resource.render_one(request, ann.asset))
                        for ann in citations
                        if (ann.title != "Annotation Deleted" and
                            ann.title != 'Asset Deleted')]),
        'annotations': [sherd_resource.render_one(request, ann, rand)
                        for ann in citations],
    }
Beispiel #9
0
def annotate_tree(comments):
    """
    Add ``open``, ``close`` properties to the comments, to render the tree.

    Syntax::

        {% for comment in comment_list|annotate_tree %}
            {% ifchanged comment.parent_id %}{% else %}</li>{% endifchanged %}
            {% if not comment.open and not comment.close %}</li>{% endif %}
            {% if comment.open %}<ul>{% endif %}

            <li id="c{{ comment.id }}">
                ...
            {% for close in comment.close %}</li></ul>{% endfor %}
        {% endfor %}

    When the :func:`fill_tree` filter, place the ``annotate_tree`` code after it::

        {% for comment in comment_list|fill_tree|annotate_tree %}
            ...
        {% endfor %}
    """
    return annotate_tree_properties(comments)
def annotate_tree(comments):
    """
    Add ``open``, ``close`` properties to the comments, to render the tree.

    Syntax::

        {% for comment in comment_list|annotate_tree %}
            {% ifchanged comment.parent_id %}{% else %}</li>{% endifchanged %}
            {% if not comment.open and not comment.close %}</li>{% endif %}
            {% if comment.open %}<ul>{% endif %}

            <li id="c{{ comment.id }}">
                ...
            {% for close in comment.close %}</li></ul>{% endfor %}
        {% endfor %}

    When the :func:`fill_tree` filter, place the ``annotate_tree`` code after it::

        {% for comment in comment_list|fill_tree|annotate_tree %}
            ...
        {% endfor %}
    """
    return annotate_tree_properties(comments)
Beispiel #11
0
def annotate_tree(comments):
    return annotate_tree_properties(comments)
def annotate_tree(comments):
    return annotate_tree_properties(comments)