Exemplo n.º 1
0
def put_label_comment(request: Request, name: CommentLabelOption,
                      reason: str) -> Response:
    """Add a label to a comment."""
    comment = request.context

    if not request.user.is_label_available(name):
        raise HTTPUnprocessableEntity("That label is not available.")

    savepoint = request.tm.savepoint()

    weight = request.user.comment_label_weight
    if weight is None:
        weight = request.registry.settings[
            "tildes.default_user_comment_label_weight"]

    label = CommentLabel(comment, request.user, name, weight, reason)
    request.db_session.add(label)

    _mark_comment_read_from_interaction(request, comment)

    try:
        # manually flush before attempting to commit, to avoid having all objects
        # detached from the session in case of an error
        request.db_session.flush()
        request.tm.commit()
    except FlushError:
        savepoint.rollback()

    # re-query the comment to get complete data
    comment = (request.query(Comment).join_all_relationships().filter_by(
        comment_id=comment.comment_id).one())

    return {"comment": comment}
Exemplo n.º 2
0
def get_settings_theme_previews(request: Request) -> dict:
    """Generate the theme preview page."""
    # get the generic/unknown user and a random group to display on the example posts
    fake_user = request.query(User).filter(User.user_id == -1).one()
    group = request.query(Group).order_by(func.random()).limit(1).one()

    fake_link_topic = Topic.create_link_topic(group, fake_user,
                                              "Example Link Topic",
                                              "https://tildes.net/")

    fake_text_topic = Topic.create_text_topic(group, fake_user,
                                              "Example Text Topic",
                                              "No real text")
    fake_text_topic.content_metadata = {
        "excerpt": "Lorem ipsum dolor sit amet, consectetur adipiscing elit."
    }

    fake_topics = [fake_link_topic, fake_text_topic]

    # manually add other necessary attributes to the fake topics
    for fake_topic in fake_topics:
        fake_topic.topic_id = sys.maxsize
        fake_topic.tags = ["tag one", "tag two"]
        fake_topic.num_comments = 123
        fake_topic.num_votes = 12
        fake_topic.created_time = utc_now() - timedelta(hours=12)

    # create a fake top-level comment that appears to be written by the user
    markdown = (
        "This is what a regular comment written by yourself would look like.\n\n"
        "It has **formatting** and a [link](https://tildes.net).")
    fake_top_comment = Comment(fake_link_topic, request.user, markdown)
    fake_top_comment.comment_id = sys.maxsize
    fake_top_comment.created_time = utc_now() - timedelta(hours=12, minutes=30)

    child_comments_markdown = [
        ("This reply has received an Exemplary label. It also has a blockquote:\n\n"
         "> Hello World!"),
        ("This is a reply written by the topic's OP with a code block in it:\n\n"
         "```js\n"
         "function foo() {\n"
         "    ['1', '2', '3'].map(parseInt);\n"
         "}\n"
         "```"),
        ("This reply is new and has the *Mark New Comments* stripe on its left "
         "(even if you don't have that feature enabled)."),
    ]

    fake_comments = [fake_top_comment]

    # vary the ID and created_time on each fake comment so CommentTree works properly
    current_comment_id = fake_top_comment.comment_id
    current_created_time = fake_top_comment.created_time
    for markdown in child_comments_markdown:
        current_comment_id -= 1
        current_created_time += timedelta(minutes=5)

        fake_comment = Comment(fake_link_topic,
                               fake_user,
                               markdown,
                               parent_comment=fake_top_comment)
        fake_comment.comment_id = current_comment_id
        fake_comment.created_time = current_created_time
        fake_comment.parent_comment_id = fake_top_comment.comment_id

        fake_comments.append(fake_comment)

    # add other necessary attributes to all of the fake comments
    for fake_comment in fake_comments:
        fake_comment.num_votes = 0

    fake_tree = CommentTree(fake_comments, CommentTreeSortOption.NEWEST,
                            request.user)

    # add a fake Exemplary label to the first child comment
    fake_comments[1].labels = [
        CommentLabel(fake_comments[1], fake_user, CommentLabelOption.EXEMPLARY,
                     1.0)
    ]

    # the comment to mark as new is the last one, so set a visit time just before it
    fake_last_visit_time = fake_comments[-1].created_time - timedelta(
        minutes=1)

    return {
        "theme_options": THEME_OPTIONS,
        "fake_topics": fake_topics,
        "fake_comment_tree": fake_tree,
        "last_visit": fake_last_visit_time,
    }