我无法在数据库中添加多个用户

时间:2016-11-25 04:02:38

标签: python flask

我想在组表中添加用户,我要检查所有组成员中的用户是否然后如果他已经在表中我闪烁消息,如果不是我将用户添加到组中。但它只适用于第一个用户。其余的逻辑评估错误

   @main.route('/join-classroom/<int:classroom_id>/<int:user_id>/')
@login_required
def join_classroom(classroom_id, user_id):
    classroom = Classroom.query.filter_by(id=classroom_id).first()

if classroom is None:
    flash('Invalid classname')
    return redirect(url_for('main.classrooms'))
else:
# check if user aleady added or on request
    classroomMembers = ClassroomMember.query.filter_by(user_id=user_id).all()
    for classroomMember in classroomMembers:
        if (classroomMember.user_id == user_id) and (classroomMember.classroom_id == classroom_id):
            flash('You are already in Group ')
            return redirect(url_for('main.classrooms'))

        elif (classroomMember.user_id != user_id) and (classroomMember.classroom_id != classroom_id):
            classroomMember = ClassroomMember(classroom_id=classroom_id, user_id=user_id)
            db.session.add(classroomMember)
            db.session.commit()
            flash('Your request has been sent successfully')
            return redirect(url_for('main.classrooms'))

        elif classroomMember.user_id is None:
            classroomMember = ClassroomMember(classroom_id=classroom_id, user_id=user_id)
            db.session.add(classroomMember)
            db.session.commit()
            flash('Your request has been sent successfully')
            return redirect(url_for('main.classrooms'))     

2 个答案:

答案 0 :(得分:0)

当您提出要求时

classroomMembers = ClassroomMember.query.filter_by(user_id=user_id).all()

您使用classroomMembers获取user_id=user_id条记录并且仅包含此类记录。 这种方式只有第一个if子句可以True,其他两个elif中的代码永远不会被执行。

因此,您需要检查组中是否有人,如果没有则添加。如果一个人不存在,那就创建一个新人。例如:

classroomMembers = ClassroomMember.query.filter_by(user_id=user_id).all()
if classroomMembers:
    classroomMember = classroomMembers[0]
    if classroomMember.classroom_id == classroom_id:
        flash('You are already in Group ')
        return redirect(url_for('main.classrooms'))
    else:
        classroomMember.classroom_id = classroom_id
        db.session.add(classroomMember)
        db.session.commit()
        flash('You were added to the Group')
        return redirect(url_for('main.classrooms'))
else:
    classroomMember = ClassroomMember(classroom_id=classroom_id, user_id=user_id)
    db.session.add(classroomMember)
    db.session.commit()
    flash('Your request has been sent successfully')
    return redirect(url_for('main.classrooms'))

答案 1 :(得分:0)

我用

解决了这个问题
@main.route('/join-classroom/<int:classroom_id>/<int:user_id>/')

@login_required def join_classroom(classroom_id,user_id):     classroom = Classroom.query.filter_by(id = classroom_id).first()

if classroom is None:
    flash('Invalid classname')
    return redirect(url_for('main.classrooms'))

else:
    classroomMember = ClassroomMember.query.filter_by(user_id=user_id).filter_by(classroom_id=classroom_id).first()

    if classroomMember is None:
        classroomMemberUser = ClassroomMember(classroom_id=classroom_id, user_id=user_id)
        db.session.add(classroomMemberUser)
        db.session.commit()
        flash('Your request has been sent successfully')
        return redirect(url_for('main.classrooms'))

    else:
        flash("You are already in the Group")
        return redirect(url_for('main.classrooms'))