Example #1
0
def broadcast_post():

    text = request.form["text"].strip()
    if not text:
        abort(400)

    if request.form["color"][0] == "#":
        color = request.form["color"][1:]
    else:
        color = request.form["color"]
    if not color_validator.match(color):
        abort(400)

    g.db.add(AdminLogEntry(
        action_user=g.user,
        type="broadcast",
        description=text,
    ))

    message_json = json.dumps({
        "messages": [{
            "id": None,
            "user_number": None,
            "posted": time.time(),
            "type": "global",
            "color": color,
            "acronym": "",
            "name": "",
            "text": text,
        }]
    })

    next_index = 0
    while True:
        next_index, keys = g.redis.scan(next_index,"chat:*:online")
        for key in keys:
            chat_id = key[5:-7]
            g.redis.publish("channel:%s" % chat_id, message_json)
        if int(next_index) == 0:
            break

    return redirect(url_for("admin_broadcast"))
Example #2
0
def validate_character_form(form):

    try:
        search_character_id = int(form["search_character_id"])
        g.db.query(SearchCharacter).filter(SearchCharacter.id == search_character_id).one()
    except (KeyError, ValueError, NoResultFound):
        # id 1 always exists so fall back to that.
        search_character_id = 1

    shortcut = form.get("shortcut", "").strip()
    if shortcut and not username_validator.match(shortcut):
        abort(400)

    # Don't allow a blank name.
    if form["name"] == "":
        abort(400)

    # Validate color.
    # <input type="color"> always prefixes with a #.
    if form["color"][0] == "#":
        color = form["color"][1:]
    else:
        color = form["color"]
    if not color_validator.match(color):
        abort(400)

    # Validate case.
    if form["case"] not in case_options:
        abort(400)

    # XXX PUT LENGTH LIMIT ON REPLACEMENTS?
    # Zip replacements.
    replacements = zip(
        form.getlist("quirk_from"),
        form.getlist("quirk_to"),
    )
    # Strip out any rows where from is blank or the same as to.
    replacements = [_ for _ in replacements if _[0] != "" and _[0] != _[1]]
    # And encode as JSON.
    json_replacements = json.dumps(replacements)

    # XXX PUT LENGTH LIMIT ON REGEXES?
    # Zip regexes.
    regexes = zip(
        form.getlist("regex_from"),
        form.getlist("regex_to"),
    )
    # Strip out any rows where from is blank or the same as to.
    regexes = [_ for _ in regexes if _[0] != "" and _[0] != _[1]]
    # And encode as JSON.
    json_regexes = json.dumps(regexes)

    return {
        # There are length limits on the front end so silently truncate these.
        "title": form["title"][:50] if "title" in form else "",
        "search_character_id": search_character_id,
        "shortcut": shortcut if len(shortcut) != 0 else None,
        "name": form["name"][:50],
        "acronym": form["acronym"][:15],
        "color": color,
        "quirk_prefix": form["quirk_prefix"][:100],
        "quirk_suffix": form["quirk_suffix"][:100],
        "case": form["case"],
        "replacements": json_replacements,
        "regexes": json_regexes,
    }