示例#1
0
    def handle_hide_comment(self, data, username):
        try:
            article_id = self.article_id
            a = Article.objects.get(id=article_id)
            id = data['id']
            explain = data['comment']
            req_user = self.scope["user"] if self.scope[
                "user"].is_authenticated else None

            comment = Comment.objects.get(id=id)
            if comment.is_replacement:
                action = 'delete_sum'
                self.recurse_down_post(comment)
                delete_node(comment.id)
                a.summary_num = a.summary_num - 1
                a.percent_complete = count_article(a)
                a.words_shown = count_words_shown(a)
                a.last_updated = datetime.datetime.now()
                a.save()
                affected = False
            else:
                action = 'hide_comment'
                if not comment.hidden:
                    comment.hidden = True
                    comment.save()
                    affected = True
                else:
                    affected = False

            if affected:
                parent = Comment.objects.filter(disqus_id=c.reply_to_disqus,
                                                article=a)
                if parent.count() > 0:
                    recurse_up_post(parent[0])

                a.comment_num = a.comment_num - 1
                words_shown = count_words_shown(a)
                percent_complete = count_article(a)
                h = History.objects.create(
                    user=req_user,
                    article=a,
                    action=action,
                    explanation=explain,
                    words_shown=words_shown,
                    current_percent_complete=percent_complete)
                c = Comment.objects.get(id=id)
                h.comments.add(c)
                a.percent_complete = percent_complete
                a.words_shown = words_shown
                a.last_updated = datetime.datetime.now()

                a.save()

            return {'d_id': data['id'], 'user': username, 'type': data['type']}
        except Exception as e:
            print(e)
            return {'user': username}
示例#2
0
    def handle_hide_replies(self, data, username):
        try:
            article_id = self.article_id
            a = Article.objects.get(id=article_id)
            id = data['id']
            explain = data['comment']
            req_user = self.scope["user"] if self.scope[
                "user"].is_authenticated else None

            c = Comment.objects.get(id=id)

            replies = Comment.objects.filter(reply_to_disqus=c.disqus_id,
                                             article=a)

            affected = self.recurse_down_hidden(replies, 0)

            if affected > 0:
                words_shown = count_words_shown(a)
                percent_complete = count_article(a)
                h = History.objects.create(
                    user=req_user,
                    article=a,
                    action='hide_replies',
                    explanation=explain,
                    words_shown=words_shown,
                    current_percent_complete=percent_complete)
                replies = Comment.objects.filter(reply_to_disqus=c.disqus_id,
                                                 article=a)
                for reply in replies:
                    h.comments.add(reply)

                recurse_up_post(c)

                ids = [reply.id for reply in replies]

                a.comment_num = a.comment_num - affected
                a.percent_complete = percent_complete
                a.words_shown = words_shown
                a.last_updated = datetime.datetime.now()

                a.save()

                return {
                    'd_id': data['id'],
                    'user': username,
                    'type': data['type'],
                    'ids': ids
                }
            else:
                return {'user': username}
        except Exception as e:
            print(e)
            return {'user': username}
示例#3
0
    def handle_hide_comments(self, data, username):
        try:
            article_id = self.article_id
            a = Article.objects.get(id=article_id)
            req_user = self.scope["user"] if self.scope[
                "user"].is_authenticated else None

            ids = data['ids']
            explain = data['comment']

            affected = Comment.objects.filter(id__in=ids,
                                              hidden=False).update(hidden=True)

            if affected > 0:
                words_shown = count_words_shown(a)
                percent_complete = count_article(a)
                h = History.objects.create(
                    user=req_user,
                    article=a,
                    action='hide_comments',
                    explanation=explain,
                    words_shown=words_shown,
                    current_percent_complete=percent_complete)
                for id in ids:
                    c = Comment.objects.get(id=id)
                    h.comments.add(c)

                    parent = Comment.objects.filter(
                        disqus_id=c.reply_to_disqus, article=a)
                    if parent.count() > 0:
                        recurse_up_post(parent[0])

                a.comment_num = a.comment_num - affected
                a.percent_complete = percent_complete
                a.words_shown = words_shown
                a.last_updated = datetime.datetime.now()
                a.save()

            return {
                'dids': data['ids'],
                'user': username,
                'type': data['type']
            }
        except Exception as e:
            print(e)
            return {'user': username}
示例#4
0
    def handle_delete_comment_summary(self, data, username):
        try:
            article_id = self.article_id
            article = Article.objects.get(id=article_id)
            comment_id = data['id']
            explain = data['comment']
            req_user = self.scope["user"] if self.scope[
                "user"].is_authenticated else None

            comment = Comment.objects.get(id=comment_id)
            if not comment.is_replacement:
                comment.summary = ""
                comment.save()
                recurse_up_post(comment)
                words_shown = count_words_shown(article)
                percent_complete = count_article(article)
                h = History.objects.create(
                    user=req_user,
                    article=article,
                    action='delete_comment_sum',
                    explanation=explain,
                    words_shown=words_shown,
                    current_percent_complete=percent_complete)

                h.comments.add(comment)

                article.percent_complete = percent_complete
                article.words_shown = words_shown
                article.last_updated = datetime.datetime.now()
                article.save()

            return {'d_id': data['id'], 'user': username, 'type': data['type']}

        except Exception as e:
            print(e)
            return {'user': username}
示例#5
0
    def handle_summarize_comments(self, data, username):
        try:
            article_id = self.article_id
            a = Article.objects.get(id=article_id)
            id = data['id']
            summary = data['comment']
            top_summary, bottom_summary = get_summary(summary)

            delete_nodes = data['delete_nodes']

            req_user = self.scope["user"] if self.scope[
                "user"].is_authenticated else None

            c = Comment.objects.get(id=id)
            percent_complete = a.percent_complete
            words_shown = a.words_shown

            if not c.is_replacement:
                new_id = random_with_N_digits(10)

                new_comment = Comment.objects.create(
                    article=a,
                    is_replacement=True,
                    reply_to_disqus=c.reply_to_disqus,
                    summary=top_summary,
                    summarized=True,
                    extra_summary=bottom_summary,
                    disqus_id=new_id,
                    points=c.points,
                    text_len=len(summary),
                    import_order=c.import_order)

                c.reply_to_disqus = new_id
                c.save()

                d_id = new_comment.id

                self.mark_children_summarized(new_comment)

                recurse_up_post(new_comment)

                recurse_down_num_subtree(new_comment)
                words_shown = count_words_shown(a)
                percent_complete = count_article(a)
                h = History.objects.create(
                    user=req_user,
                    article=a,
                    action='sum_nodes',
                    to_str=summary,
                    explanation='initial summary of subtree',
                    words_shown=words_shown,
                    current_percent_complete=percent_complete)
                h.comments.add(new_comment)

            else:
                from_summary = c.summary + '\n----------\n' + c.extra_summary
                c.summary = top_summary
                c.extra_summary = bottom_summary
                c.save()

                d_id = c.id

                new_comment = c
                recurse_down_num_subtree(new_comment)
                recurse_up_post(c)
                words_shown = count_words_shown(a)
                h = History.objects.create(
                    user=req_user,
                    article=a,
                    action='edit_sum_nodes',
                    from_str=from_summary,
                    to_str=summary,
                    explanation='edit summary of subtree',
                    words_shown=words_shown,
                    current_percent_complete=a.percent_complete)

            for node in delete_nodes:
                new_h = History.objects.create(
                    user=req_user,
                    article=a,
                    action='delete_node',
                    from_str=node,
                    to_str=c.id,
                    explanation='promote summary',
                    words_shown=a.words_shown,
                    current_percent_complete=a.percent_complete)
                delete_node(node)

            h.comments.add(c)
            if not c.is_replacement:
                a.summary_num = a.summary_num + 1
                a.percent_complete = percent_complete
                a.words_shown = words_shown
            a.last_updated = datetime.datetime.now()
            a.save()

            res = {
                'user': username,
                'type': data['type'],
                'd_id': new_comment.id,
                'node_id': data['node_id'],
                'orig_did': data['id']
            }
            res['subtype'] = data['subtype']
            res['delete_summary_node_dids'] = data['delete_summary_node_dids']
            if 'wikipedia.org' in a.url:
                if top_summary.strip() != '':
                    res['top_summary'] = clean_parse(top_summary)
                else:
                    res['top_summary'] = ''

                res['top_summary_wiki'] = top_summary

                if bottom_summary.strip() != '':
                    res['bottom_summary'] = clean_parse(bottom_summary)
                else:
                    res['bottom_summary'] = ''

                res['bottom_summary_wiki'] = bottom_summary
                return res
            else:
                res['top_summary'] = top_summary
                res['bottom_summary'] = bottom_summary
                return res

        except Exception as e:
            print(e)
            import traceback
            print(traceback.format_exc())
            return {'user': username}
示例#6
0
    def handle_summarize_selected(self, data, username):
        try:
            article_id = self.article_id
            a = Article.objects.get(id=article_id)
            ids = data['ids']
            children_ids = data['children']
            children_ids = [int(x) for x in children_ids]
            child_id = data['child']

            delete_nodes = data['delete_nodes']

            summary = data['comment']

            top_summary, bottom_summary = get_summary(summary)

            req_user = self.scope["user"] if self.scope[
                "user"].is_authenticated else None

            comments = Comment.objects.filter(id__in=ids)
            children = [c for c in comments if c.id in children_ids]
            child = Comment.objects.get(id=child_id)

            lowest_child = children[0]
            for c in children:
                if c.import_order < lowest_child.import_order:
                    lowest_child = c

            new_id = random_with_N_digits(10)

            new_comment = Comment.objects.create(
                article=a,
                is_replacement=True,
                reply_to_disqus=child.reply_to_disqus,
                summarized=True,
                summary=top_summary,
                extra_summary=bottom_summary,
                disqus_id=new_id,
                points=child.points,
                text_len=len(summary),
                import_order=lowest_child.import_order)

            for node in delete_nodes:
                delete_node(node)

            self.mark_children_summarized(new_comment)

            recurse_up_post(new_comment)

            recurse_down_num_subtree(new_comment)

            a.summary_num = a.summary_num + 1
            words_shown = count_words_shown(a)
            percent_complete = count_article(a)
            h = History.objects.create(
                user=req_user,
                article=a,
                action='sum_selected',
                to_str=summary,
                explanation='initial summary of group of comments',
                words_shown=words_shown,
                current_percent_complete=percent_complete)

            for c in children:
                c.reply_to_disqus = new_id
                c.save()
                h.comments.add(c)

            h.comments.add(new_comment)
            a.percent_complete = percent_complete
            a.words_shown = words_shown
            a.last_updated = datetime.datetime.now()

            a.save()

            res = {
                'user': username,
                'type': data['type'],
                'd_id': new_comment.id,
                'lowest_d': child_id,
                'children': children_ids
            }
            res['size'] = data['size']
            res['delete_summary_node_dids'] = data['delete_summary_node_dids']
            if 'wikipedia.org' in a.url:
                if top_summary.strip() != '':
                    res['top_summary'] = clean_parse(top_summary)
                else:
                    res['top_summary'] = ''

                res['top_summary_wiki'] = top_summary

                if bottom_summary.strip() != '':
                    res['bottom_summary'] = clean_parse(bottom_summary)
                else:
                    res['bottom_summary'] = ''

                res['bottom_summary_wiki'] = bottom_summary
                res['user'] = username
                res['type'] = data['type']
                return res
            else:
                res['top_summary'] = top_summary
                res['bottom_summary'] = bottom_summary
                return res

        except Exception as e:
            print(e)
            return {'user': username}
示例#7
0
    def handle_summarize_comment(self, data, username):
        try:
            article_id = self.article_id
            a = Article.objects.get(id=article_id)
            id = data['id']
            summary = data['comment']
            top_summary, bottom_summary = get_summary(summary)

            req_user = self.scope["user"] if self.scope[
                "user"].is_authenticated else None

            c = Comment.objects.get(id=id)
            from_summary = c.summary + '\n----------\n' + c.extra_summary
            c.summary = top_summary
            c.extra_summary = bottom_summary
            c.save()

            if from_summary != '':
                action = 'edit_sum'
                explanation = 'edit summary'
            else:
                action = 'sum_comment'
                explanation = 'initial summary'

            recurse_up_post(c)
            words_shown = count_words_shown(a)
            percent_complete = count_article(a)
            h = History.objects.create(
                user=req_user,
                article=a,
                action=action,
                from_str=from_summary,
                to_str=summary,
                explanation=explanation,
                words_shown=words_shown,
                current_percent_complete=percent_complete)

            h.comments.add(c)
            if from_summary == '':
                a.summary_num = a.summary_num + 1
                a.percent_complete = percent_complete
            a.words_shown = words_shown
            a.last_updated = datetime.datetime.now()
            a.save()
            res = {'user': username, 'type': data['type'], 'd_id': data['id']}
            if 'wikipedia.org' in a.url:
                if top_summary.strip() != '':
                    res['top_summary'] = clean_parse(top_summary)
                else:
                    res['top_summary'] = ''

                res['top_summary_wiki'] = top_summary

                if bottom_summary.strip() != '':
                    res['bottom_summary'] = clean_parse(bottom_summary)
                else:
                    res['bottom_summary'] = ''

                res['bottom_summary_wiki'] = bottom_summary
                return res
            else:
                res['top_summary'] = top_summary
                res['bottom_summary'] = bottom_summary
                return res

        except Exception as e:
            print(e)
            return {'user': username}
示例#8
0
    def handle_message(self, data, username):
        article_id = self.article_id
        article = Article.objects.get(id=article_id)
        try:
            user = self.scope["user"]
            owner = data.get('owner', None)
            if not owner or owner == "None":
                owner = None
            else:
                owner = User.objects.get(username=owner)

            permission = None
            if user.is_authenticated:
                permission = Permissions.objects.filter(user=user,
                                                        article=article)
                if permission.exists():
                    permission = permission[0]
            if article.access_mode < 2 or (
                    user.is_authenticated and permission and
                (permission.access_level < 2)) or user == owner:
                comment = data['comment']
                req_user = user if user.is_authenticated else None
                req_username = user.username if user.is_authenticated else None
                # if commentauthor for username use it; otherwise create it
                author = CommentAuthor.objects.filter(username=req_username)
                if user.is_anonymous:
                    req_username = "******"
                    author = CommentAuthor.objects.create(
                        username=req_username, anonymous=True, is_wikum=True)
                else:
                    if author.exists():
                        author = author[0]
                        author.is_wikum = True
                        author.user = user
                    else:
                        # existing user who is not a comment author
                        author = CommentAuthor.objects.create(
                            username=req_username, is_wikum=True, user=user)
                new_id = random_with_N_digits(10)
                new_comment = None
                explanation = ''
                if data['type'] == 'new_node':
                    new_comment = Comment.objects.create(article=article,
                                                         author=author,
                                                         is_replacement=False,
                                                         disqus_id=new_id,
                                                         text=comment,
                                                         summarized=False,
                                                         text_len=len(comment))
                    explanation = 'new comment'
                elif data['type'] == 'reply_comment':
                    id = data['id']
                    c = Comment.objects.get(id=id)
                    new_comment = Comment.objects.create(
                        article=article,
                        author=author,
                        is_replacement=False,
                        reply_to_disqus=c.disqus_id,
                        disqus_id=new_id,
                        text=comment,
                        summarized=False,
                        text_len=len(comment),
                        import_order=c.import_order)
                    explanation = 'reply to comment'

                new_comment.save()
                action = data['type']

                recurse_up_post(new_comment)

                recurse_down_num_subtree(new_comment)

                # make_vector(new_comment, article)
                article.comment_num = article.comment_num + 1
                words_shown = count_words_shown(article)
                percent_complete = count_article(article)
                h = History.objects.create(
                    user=req_user,
                    article=article,
                    action=action,
                    explanation=explanation,
                    words_shown=words_shown,
                    current_percent_complete=percent_complete)
                h.comments.add(new_comment)
                article.percent_complete = percent_complete
                article.words_shown = words_shown
                article.last_updated = datetime.datetime.now(tz=timezone.utc)

                article.save()
                response_dict = {
                    'comment': comment,
                    'd_id': new_comment.id,
                    'author': req_username,
                    'type': data['type'],
                    'user': req_username
                }
                if data['type'] == 'reply_comment':
                    response_dict['parent_did'] = data['id']
                return response_dict
            else:
                return {'user': username}
        except Exception as e:
            print(e)
            return {}