コード例 #1
0
def join_channel():
    channel_name = request.form.get("channel_name")

    # Get channel object
    channel = Channel.query.filter_by(name=channel_name).first()

    current_user.join(channel)
    return jsonify({"success": True})
コード例 #2
0
def join_action(group_id, action):
    group = Group.query.filter_by(id=group_id).first_or_404()
    if action == 'join':
        current_user.join(group)
        db.session.commit()
    elif action == 'leave':
        current_user.leave(group)
        db.session.commit()
    return redirect(request.referrer)
コード例 #3
0
def group_post(group_id):
    group = Group.query.get(group_id)
    action = request.form.get("action")
    return_url = request.form.get("return_url")
    user_id = request.form.get("user", "").strip()

    membership_actions = frozenset(
        ("add", "remove", "add-admin", "remove-admin"))

    if action not in {"join", "leave"}.union(membership_actions):
        raise ValueError("Unknown action: {}".format(action))

    if action not in ("join", "leave"):
        assert is_admin(group)

    if action == "join":
        current_user.join(group)
    elif action == "leave":
        current_user.leave(group)

    elif action in membership_actions:
        try:
            user_id = int(user_id)
        except ValueError:
            flash(_("Error: No user selected"), "error")
            return redirect(url_for(".group_home", group_id=group_id))

        user = User.query.get(user_id)

        if action == "add":
            user.join(group)
        elif action == "remove":
            user.leave(group)
        elif action == "add-admin":
            if user not in group.admins:
                group.admins.append(user)
        elif action == "remove-admin":
            if user in group.admins:
                group.admins.remove(user)

    db.session.commit()

    if not return_url:
        return_url = url_for(".group_home", group_id=group_id)

    # TODO: security check
    return redirect(return_url)
コード例 #4
0
ファイル: groups.py プロジェクト: abilian/abilian-sbe
def group_post(group_id):
    group = Group.query.get(group_id)
    action = request.form.get("action")
    return_url = request.form.get("return_url")
    user_id = request.form.get("user", "").strip()

    membership_actions = frozenset(("add", "remove", "add-admin", "remove-admin"))

    if action not in {"join", "leave"}.union(membership_actions):
        raise ValueError(f"Unknown action: {action}")

    if action not in ("join", "leave"):
        assert is_admin(group)

    if action == "join":
        current_user.join(group)
    elif action == "leave":
        current_user.leave(group)

    elif action in membership_actions:
        try:
            user_id = int(user_id)
        except ValueError:
            flash(_("Error: No user selected"), "error")
            return redirect(url_for(".group_home", group_id=group_id))

        user = User.query.get(user_id)

        if action == "add":
            user.join(group)
        elif action == "remove":
            user.leave(group)
        elif action == "add-admin":
            if user not in group.admins:
                group.admins.append(user)
        elif action == "remove-admin":
            if user in group.admins:
                group.admins.remove(user)

    db.session.commit()

    if not return_url:
        return_url = url_for(".group_home", group_id=group_id)

    # TODO: security check
    return redirect(return_url)
コード例 #5
0
ファイル: main.py プロジェクト: tckmn/psetpartners
def save_student():
    raw_data = request.form
    session["ctx"] = {
        x[4:]: raw_data[x]
        for x in raw_data if x.startswith("ctx-")
    }  # return ctx-XXX values to
    if not 'submit' in session["ctx"]:
        flash_error(
            "Invalid operation, no submit value (please report this as a bug)."
        )
    submit = [x for x in session["ctx"]["submit"].split(' ') if x]
    if not submit:
        flash_error(
            "Unrecognized submit value, no changes made (please report this as a bug)."
        )
        return redirect(url_for(".student"))
    if submit[0] == "cancel":
        flash_info("Changes discarded.")
        return redirect(url_for(".student"))
    if submit[0] == "save":
        # update the full_name supplied by Touchstone (in case this changes)
        if session.get("displayname", ""):
            current_user.full_name = session["displayname"]
        if not save_changes(raw_data):
            if submit != "save":
                flash_error("The action you requested was not performed.")
            return redirect(url_for(".student"))
        submit = submit[1:]
    if not submit:
        return redirect(url_for(".student"))
    if submit[0] == "join":
        try:
            gid = int(submit[1])
            flash_info(current_user.join(gid))
        except Exception as err:
            msg = "Error joining group: {0}{1!r}".format(
                type(err).__name__, err.args)
            log_event(current_user.kerb,
                      'join',
                      status=-1,
                      detail={
                          'group_id': gid,
                          'msg': msg
                      })
            if debug_mode():
                raise
            flash_error(msg)
    elif submit[0] == "leave":
        try:
            gid = int(submit[1])
            flash_info(current_user.leave(gid))
        except Exception as err:
            msg = "Error leaving group: {0}{1!r}".format(
                type(err).__name__, err.args)
            log_event(current_user.kerb,
                      'leave',
                      status=-1,
                      detail={
                          'group_id': gid,
                          'msg': msg
                      })
            if debug_mode():
                raise
            flash_error(msg)
    elif submit[0] == "pool":
        try:
            cid = int(submit[1])
            flash_info(current_user.pool(cid))
        except Exception as err:
            msg = "Error adding you to the match pool: {0}{1!r}".format(
                type(err).__name__, err.args)
            log_event(current_user.kerb,
                      'pool',
                      status=-1,
                      detail={
                          'class_id': cid,
                          'msg': msg
                      })
            if debug_mode():
                raise
            flash_error(msg)
    elif submit[0] == "unpool":
        try:
            cid = int(submit[1])
            flash_info(current_user.unpool(cid))
        except Exception as err:
            msg = "Error removing you from the match pool: {0}{1!r}".format(
                type(err).__name__, err.args)
            log_event(current_user.kerb,
                      'unpool',
                      status=-1,
                      detail={
                          'class_id': cid,
                          'msg': msg
                      })
            if debug_mode():
                raise
            flash_error(msg)
    elif submit[0] == "matchasap":
        try:
            gid = int(submit[1])
            flash_info(current_user.matchasap(gid))
        except Exception as err:
            msg = "Error submitting match me asap request: {0}{1!r}".format(
                type(err).__name__, err.args)
            log_event(current_user.kerb,
                      'matchasap',
                      status=-1,
                      detail={
                          'group_id': gid,
                          'msg': msg
                      })
            if debug_mode():
                raise
            flash_error()
    elif submit[0] == "matchnow":
        try:
            gid = int(submit[1])
            flash_info(current_user.matchnow(gid))
        except Exception as err:
            msg = "Error submitting match me now request: {0}{1!r}".format(
                type(err).__name__, err.args)
            log_event(current_user.kerb,
                      'matchnow',
                      status=-1,
                      detail={
                          'group_id': gid,
                          'msg': msg
                      })
            if debug_mode():
                raise
            flash_error()
    elif submit[0] == "createprivate":
        try:
            cid = int(submit[1])
            flash_info(
                current_user.create_group(
                    cid,
                    {k: raw_data.get(k, '').strip()
                     for k in group_options},
                    public=False))
        except Exception as err:
            msg = "Error creating private group: {0}{1!r}".format(
                type(err).__name__, err.args)
            log_event(current_user.kerb,
                      'create',
                      status=-1,
                      detail={
                          'class_id': cid,
                          'public': False,
                          'msg': msg
                      })
            if debug_mode():
                raise
            flash_error(msg)
    elif submit[0] == "createpublic":
        try:
            cid = int(submit[1])
            flash_info(
                current_user.create_group(
                    cid,
                    {k: raw_data.get(k, '').strip()
                     for k in group_options},
                    public=True))
        except Exception as err:
            msg = "Error creating public group: {0}{1!r}".format(
                type(err).__name__, err.args)
            log_event(current_user.kerb,
                      'create',
                      status=-1,
                      detail={
                          'class_id': cid,
                          'public': True,
                          'msg': msg
                      })
            if debug_mode():
                raise
            flash_error(msg)
    elif submit[0] == "editgroup":
        try:
            gid = int(submit[1])
            flash_info(
                current_user.edit_group(
                    gid,
                    {k: raw_data.get(k, '').strip()
                     for k in group_options}))
        except Exception as err:
            msg = "Error editing group: {0}{1!r}".format(
                type(err).__name__, err.args)
            log_event(current_user.kerb,
                      'edit',
                      status=-1,
                      detail={
                          'group_id': gid,
                          'public': True,
                          'msg': msg
                      })
            if debug_mode():
                raise
            flash_error(msg)
    else:
        flash_error("Unrecognized submit command: " + submit[0])
    return redirect(url_for(".student"))