2016-11-25 17 views
0

グループテーブルにユーザを追加したいのですが、すべてのグループメンバーでユーザをチェックしています。もしすでにテーブルにいたら、メッセージをフラッシュし、グループ。それは最初のユーザーのためだけです。あなたがuser_id=user_idだけな記録とclassroomMembersレコードに取得データベースに複数のユーザを追加することはできません

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

を要求すると、残りはロジックが

@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'))  

答えて

0

偽と評価します。 これは、最初のif句だけがTrueになり、他の2つのコードが決して実行されないようになります。

グループ内の人がいるかどうかを確認し、そうでない場合は追加する必要があります。人がいない場合は、新しい人を作成します。例えばこれと同様: 教室= Classroom.query.filter_by(ID = classroom_id)1次回:

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')) 
+0

私はこの問題を解決しました。 –

0

Iは

@main.route('/join-classroom/<int:classroom_id>/<int:user_id>/') 

@login_required DEF join_classroom(classroom_id、USER_ID)の問題を解決しました()

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')) 
関連する問題