Exemple #1
0
def delete_character_post(character_id):
    character = character_query(character_id)
    character_id = character.id
    if g.user.default_character_id == character_id:
        g.user.default_character_id = None
        g.db.flush()
    g.db.query(CharacterTag).filter(CharacterTag.character_id == character_id).delete()
    g.db.query(Request).filter(Request.character_id==character_id).update({ "character_id": None })
    # Don't use g.db.delete(character) because it does a load of extra queries
    # for foreign keys and stuff.
    g.db.query(Character).filter(Character.id == character_id).delete()
    return redirect(url_for("rp_character_list"))
Exemple #2
0
def character(character_id, fmt=None):

    character = character_query(character_id, join_tags=True)

    search_character_groups = g.db.query(SearchCharacterGroup).order_by(
        SearchCharacterGroup.order,
    ).options(joinedload(SearchCharacterGroup.characters)).all()

    if fmt == "json":
        return jsonify(character.to_dict(include_default=True, include_options=True))

    return render_template(
        "rp/characters/character.html",
        character=character,
        replacements=json.loads(character.replacements),
        regexes=json.loads(character.regexes),
        character_tags={
            tag_type: ", ".join(tag["alias"] for tag in tags)
            for tag_type, tags in character.tags_by_type().iteritems()
        },
        search_character_groups=search_character_groups,
        case_options=case_options,
    )
Exemple #3
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))
Exemple #4
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"))
Exemple #5
0
def delete_character_get(character_id):
    character = character_query(character_id)
    return render_template("rp/characters/delete_character.html", character=character)
Exemple #6
0
def set_default_character(character_id):
    character = character_query(character_id)
    g.user.default_character = character
    return redirect(url_for("rp_character_list"))