Example #1
0
def search_save():

    new_details = validate_character_form(request.form)
    g.user.search_character_id = new_details["search_character_id"]
    g.user.name = new_details["name"]
    g.user.acronym = new_details["acronym"]
    g.user.color = new_details["color"]
    g.user.quirk_prefix = new_details["quirk_prefix"]
    g.user.quirk_suffix = new_details["quirk_suffix"]
    g.user.case = new_details["case"]
    g.user.replacements = new_details["replacements"]
    g.user.regexes = new_details["regexes"]

    if request.form["style"] in User.search_style.type.enums:
        g.user.search_style = request.form["style"]

    if request.form["level"] in User.search_level.type.enums:
        g.user.search_level = request.form["level"]

    search_filters = set()
    for search_filter in request.form.getlist("search_filter"):
        search_filter = search_filter.strip().lower()
        if search_filter:
            search_filters.add(search_filter)
        if len(search_filters) == 100:
            break
    g.user.search_filters = sorted(list(search_filters))

    all_character_ids = set(_[0] for _ in g.db.query(SearchCharacter.id).all())

    # Picky checkboxes
    g.db.query(SearchCharacterChoice).filter(SearchCharacterChoice.user_id == g.user.id).delete()
    if "use_picky" in request.form:
        for key in request.form.keys():
            if not key.startswith("picky_"):
                continue
            try:
                character_id = int(key[6:])
            except:
                continue
            if not character_id in all_character_ids:
                continue
            g.db.add(SearchCharacterChoice(user_id=g.user.id, search_character_id=character_id))

    return redirect(url_for("rp_search"))
Example #2
0
def save_search_character(id):
    character = search_character_query(id)
    new_details = validate_character_form(request.form)
    # Ignore a blank title.
    if new_details["title"] != "":
        character.title = new_details["title"]
    character.name = new_details["name"]
    character.acronym = new_details["acronym"]
    character.color = new_details["color"]
    character.quirk_prefix = new_details["quirk_prefix"]
    character.quirk_suffix = new_details["quirk_suffix"]
    character.case = new_details["case"]
    character.replacements = new_details["replacements"]
    character.regexes = new_details["regexes"]
    character.text_preview = request.form["text_preview"]
    # Remember to clear the cache
    g.redis.delete("search_character:%s" % id)
    return redirect(url_for("rp_search_character_list"))
Example #3
0
def save():

    # Remember old values so we can check if they've changed later.
    old_name = g.chat_user.name
    old_acronym = g.chat_user.acronym
    old_color = g.chat_user.color

    new_details = validate_character_form(request.form)
    g.chat_user.search_character_id = new_details["search_character_id"]
    g.chat_user.name = new_details["name"]
    g.chat_user.acronym = new_details["acronym"]
    g.chat_user.color = new_details["color"]
    g.chat_user.quirk_prefix = new_details["quirk_prefix"]
    g.chat_user.quirk_suffix = new_details["quirk_suffix"]
    g.chat_user.case = new_details["case"]
    g.chat_user.replacements = new_details["replacements"]
    g.chat_user.regexes = new_details["regexes"]

    # Send a message if name or acronym has changed.
    if g.chat_user.name != old_name or g.chat_user.acronym != old_acronym:
        if g.chat_user.computed_group == "silent":
            send_userlist(g.db, g.redis, g.chat)
        else:
            send_message(g.db, g.redis, Message(
                chat_id=g.chat.id,
                user_id=g.user.id,
                type="user_info",
                name=g.chat_user.name,
                text=("%s [%s] is now %s [%s].") % (
                    old_name, old_acronym,
                    g.chat_user.name, g.chat_user.acronym,
                ),
            ))
    # Just refresh the user list if the color has changed.
    elif g.chat_user.color != old_color:
        send_userlist(g.db, g.redis, g.chat)

    return jsonify(g.chat_user.to_dict(include_options=True))
Example #4
0
def new_search_character_post():
    new_details = validate_character_form(request.form)
    del new_details["search_character_id"]
    del new_details["shortcut"]
    try:
        group = g.db.query(SearchCharacterGroup).filter(
            SearchCharacterGroup.id == request.form["group_id"],
        ).one()
    except NoResultFound:
        abort(404)
    order = (
        g.db.query(func.max(SearchCharacter.order))
        .filter(SearchCharacter.group_id == group.id)
        .scalar() or 0
    ) + 1
    new_search_character = SearchCharacter(
        group_id=group.id,
        order=order,
        text_preview=request.form["text_preview"],
        **new_details
    )
    g.db.add(new_search_character)
    return redirect(url_for("rp_search_character_list"))
Example #5
0
def edit_request_post(request_id):

    search_request = _own_request_query(request_id)

    search_request.scenario = request.form["scenario"].strip()
    search_request.prompt = request.form["prompt"].strip()

    # At least one of prompt or scenario must be filled in.
    if len(search_request.scenario) == 0 and len(search_request.prompt) == 0:
        abort(400)

    if "draft" in request.form:
        search_request.status = "draft"
    elif search_request.status != "posted":
        search_request.status = "posted"
        # Bump the date if the request is being re-posted.
        search_request.posted = datetime.datetime.now()

    new_or_saved_character = request.form["new_or_saved_character"]
    edit_character = "edit_character" in request.form
    save_character_as = request.form["save_character_as"]

    g.db.query(RequestTag).filter(RequestTag.request_id == search_request.id).delete()

    # Use saved character verbatim.
    if new_or_saved_character == "saved" and not edit_character:

        character = character_query(request.form["character_id"], join_tags=True)

        search_request.character = character
        search_request.name = character.name
        search_request.alias = character.alias
        search_request.color = character.color
        search_request.quirk_prefix = character.quirk_prefix
        search_request.quirk_suffix = character.quirk_suffix
        search_request.case = character.case
        search_request.replacements = character.replacements
        search_request.regexes = character.regexes

        search_request.tags += request_tags_from_character(character)
        search_request.tags += request_tags_from_form(request.form)

    # Otherwise rely on the form data.
    else:

        character = None
        character_details = validate_character_form(request.form)

        # Update existing character.
        if new_or_saved_character == "saved" and save_character_as == "update":
            character = save_character_from_form(request.form["character_id"], request.form)

        # Create new character.
        elif save_character_as == "new":
            if character_details["title"] == "":
                character_details["title"] == "Untitled character"
            character = Character(user=g.user, **character_details)
            g.db.add(character)
            g.db.flush()

        # Neither.
        elif save_character_as == "temp":
            pass

        # oh god how did this get here I am not good with computer
        else:
            abort(400)

        # The title and tag attributes from the character aren't needed on the
        # request, so only copy the attributes Requests actually have.
        for key, value in character_details.iteritems():
            if hasattr(search_request, key):
                setattr(search_request, key, value)

        if character is not None:
            search_request.tags += request_tags_from_character(character)
            search_request.tags += request_tags_from_form(request.form)
        else:
            search_request.tags += request_tags_from_form(request.form, include_character_tags=True)

    return redirect(url_for("rp_edit_request_get", request_id=search_request.id))
Example #6
0
def new_request_post():

    scenario = request.form["scenario"].strip()
    prompt = request.form["prompt"].strip()

    # At least one of prompt or scenario must be filled in.
    if len(scenario) == 0 and len(prompt) == 0:
        abort(400)

    status = "draft" if "draft" in request.form else "posted"

    new_or_saved_character = request.form["new_or_saved_character"]
    edit_character = "edit_character" in request.form
    save_character_as = request.form["save_character_as"]

    # Use saved character verbatim.
    if new_or_saved_character == "saved" and not edit_character:

        character = character_query(request.form["character_id"], join_tags=True)
        # XXX consider making a classmethod for this like with user_chat
        new_request = Request(
            user=g.user,
            status=status,
            character=character,
            name=character.name,
            alias=character.alias,
            color=character.color,
            quirk_prefix=character.quirk_prefix,
            quirk_suffix=character.quirk_suffix,
            case=character.case,
            replacements=character.replacements,
            regexes=character.regexes,
            scenario=scenario,
            prompt=prompt,
        )

        new_request.tags = request_tags_from_character(character)
        new_request.tags += request_tags_from_form(request.form)

    # Otherwise rely on the form data.
    else:

        character = None
        character_details = validate_character_form(request.form)

        # Update existing character.
        if new_or_saved_character == "saved" and save_character_as == "update":
            character = save_character_from_form(request.form["character_id"], request.form)

        # Create new character.
        elif save_character_as == "new":
            if character_details["title"] == "":
                character_details["title"] == "Untitled character"
            character = Character(user=g.user, **character_details)
            g.db.add(character)
            g.db.flush()

        # Neither.
        elif save_character_as == "temp":
            pass

        # oh god how did this get here I am not good with computer
        else:
            abort(400)

        new_request = Request(user=g.user, status=status, character=character, scenario=scenario, prompt=prompt)

        # The title and tag attributes from the character aren't needed on the
        # request, so only copy the attributes Requests actually have.
        for key, value in character_details.iteritems():
            if hasattr(new_request, key):
                setattr(new_request, key, value)

        if character is not None:
            new_request.tags = request_tags_from_character(character)
            new_request.tags += request_tags_from_form(request.form)
        else:
            new_request.tags = request_tags_from_form(request.form, include_character_tags=True)

    g.db.add(new_request)

    return redirect(url_for("rp_your_request_list"))
Example #7
0
def new_character_post():
    new_details = validate_character_form(request.form)
    g.db.add(Character(user_id=g.user.id, **new_details))
    return redirect(url_for("rp_character_list"))