def exe_add(request):
    if not request.user:
        return redirect(reverse('club:login'))
    print(request.method)
    sid = request.POST.get('sid', None)
    name = request.POST.get('name', None)
    join_club_id = request.POST.get('club_id', None)
    member = get_member(sid=sid, name=name)
    club = Club.objects.filter(pk=join_club_id).first()

    member_id = request.user.id
    # 添加失败
    if not member:
        if not (join_club_id and member_id and verify_permission(
                join_club_id, member_id) and join_club_id):
            return render(request, 'club/404.html')
        add_fail = True
        add_fail_message = '学号/姓名填写不规范,请重新填写'
        data = {
            'manage_club': club,
            'add_fail': add_fail,
            'add_fail_message': add_fail_message,
        }
        data = merge_dict(data, user_data(request))
        return render(request, 'club/add_member.html', context=data)

    # 确认是否添加成功。
    try:
        member.club.add(join_club_id)
        member.save()
        temp = True
    except Exception:
        temp = False

    # 该社员已经加入了该社团
    if not temp:
        add_fail = True
        add_fail_message = '该成员已经加入过此社团,请勿重复操作'
        data = {
            'manage_club': club,
            'add_fail': add_fail,
            'add_fail_message': add_fail_message,
        }
        data = merge_dict(data, user_data(request))
        return render(request, 'club/add_member.html', context=data)

    # 添加成功
    add_success = True
    add_success_message = '添加社员' + member.name + '成功'
    data = {
        'manage_club': club,
        'add_success': add_success,
        'add_success_message': add_success_message,
    }
    # data = merge_dict(data, user_data(request))
    print(add_success)
    print(add_success_message)
    print(data)
    return render(request, 'club/add_member.html', context=data)
def show_join_club(request, join_club_id):
    if not request.user:
        return redirect(reverse('club:login'))
    join_club = Club.objects.filter(pk=join_club_id).first()

    if not join_club:
        return render(request, 'club/404.html', status=404)
    key_word = query(request)
    if key_word:
        join_club_admin1 = join_club.member_club.all().filter(
            admin=join_club_id).filter(sid__contains=key_word)
        join_club_admin2 = join_club.member_club.all().filter(
            admin=join_club_id).filter(name__contains=key_word)
        join_club_member1 = join_club.member_club.all().exclude(
            admin=join_club_id).filter(sid__contains=key_word)
        join_club_member2 = join_club.member_club.all().exclude(
            admin=join_club_id).filter(name__contains=key_word)
        join_club_admin = join_club_admin1 | join_club_admin2
        join_club_member = join_club_member1 | join_club_member2

    else:
        join_club_admin = join_club.member_club.all().filter(
            admin=join_club_id)
        join_club_member = join_club.member_club.all().exclude(
            admin=join_club_id)

    data = {
        'key': key_word,
        'join_club': join_club,
        'join_club_admin': join_club_admin,
        'join_club_member': join_club_member,
    }
    data = merge_dict(data, user_data(request))
    return render(request, 'club/join_list.html', context=data)
def all(request, club_id):
    if not request.user:
        return redirect(reverse('club:login'))
    club_show = Club.objects.all().filter(pk=club_id).first()
    if not club_show:
        return render(request, 'club/404.html', status=404)

    key_word = query(request)

    if key_word:
        club_admin1 = club_show.member_club.all().filter(admin=club_id).filter(
            sid__contains=key_word)
        club_admin2 = club_show.member_club.all().filter(admin=club_id).filter(
            name__contains=key_word)
        club_member1 = club_show.member_club.all().exclude(
            admin=club_id).filter(sid__contains=key_word)
        club_member2 = club_show.member_club.all().exclude(
            admin=club_id).filter(name__contains=key_word)
        club_admin = club_admin1 | club_admin2
        club_member = club_member1 | club_member2

    else:
        club_admin = club_show.member_club.all().filter(admin=club_id)
        club_member = club_show.member_club.all().exclude(admin=club_id)

    data = {
        'key': key_word,
        'club': club_show,
        'club_admin': club_admin,
        'club_member': club_member,
    }
    data = merge_dict(data, user_data(request))
    return render(request, 'club/all_list.html', context=data)
def login(request):
    if request.method == 'GET':
        try:
            HttpResponse.delete_cookie('token')
        except Exception:
            pass
        return render(request, 'club/login.html')

    sid = request.POST.get('sid', '') or ''
    password = request.POST.get('password', '') or ''
    user = Member.objects.filter(sid=sid).first()
    temp = False
    if user:
        user_password = user.password
        temp = check_password(password, user_password)
    if not (sid and password and user and temp):
        login_error = True
        login_error_message = '用户名/密码错误'
        data = {
            'login_error': login_error,
            'login_error_message': login_error_message,
        }
        data = merge_dict(data, user_data(request))
        return render(request, 'club/login.html', context=data)
    # 重新生成token
    token = generate_club_token()
    user.token = token
    user.save()

    response = redirect(reverse('club:index'))
    response.set_cookie('token', token)
    return response
def manage_club(request, club_id):
    if not request.user:
        return redirect(reverse('club:login'))
    manage_club = Club.objects.filter(pk=club_id).first()
    key_word = query(request)
    if key_word:
        manage_club_admin1 = manage_club.member_club.all().filter(
            admin=club_id).filter(sid__contains=key_word)
        manage_club_admin2 = manage_club.member_club.all().filter(
            admin=club_id).filter(name__contains=key_word)
        manage_club_member1 = manage_club.member_club.all().exclude(
            admin=club_id).filter(sid__contains=key_word)
        manage_club_member2 = manage_club.member_club.all().exclude(
            admin=club_id).filter(name__contains=key_word)
        manage_club_admin = manage_club_admin1 | manage_club_admin2
        manage_club_member = manage_club_member1 | manage_club_member2

    else:
        manage_club_admin = manage_club.member_club.all().filter(admin=club_id)
        manage_club_member = manage_club.member_club.all().exclude(
            admin=club_id)

    data = {
        'key': key_word,
        'manage_club': manage_club,
        'manage_club_admin': manage_club_admin,
        'manage_club_member': manage_club_member,
    }
    data = merge_dict(data, user_data(request))
    return render(request, 'club/manage_list.html', context=data)
def see_all(request, join_club_id, member_id):
    if not request.user:
        return redirect(reverse('club:login'))
    club_member = Member.objects.filter(pk=member_id).first()
    join_club = Club.objects.filter(pk=join_club_id).first()
    if not (club_member and join_club):
        return render(request, 'club/404.html', status=404)
    data = {
        'club_member': club_member,
        'join_club': join_club,
    }
    data = merge_dict(data, user_data(request))
    return render(request, 'club/see_all.html', context=data)
def see_admin(request, join_club_id, member_id):
    if not request.user:
        return redirect(reverse('club:login'))
    manage_club = Club.objects.filter(pk=join_club_id).first()
    manage_admin = Member.objects.filter(pk=member_id).first()
    if not (manage_club and manage_admin):
        return render(request, 'club/404.html')
    data = {
        'manage_admin': manage_admin,
        'manage_club': manage_club,
    }
    data = merge_dict(data, user_data(request))
    return render(request, 'club/see_admin.html', context=data)
def add(request):
    if not request.user:
        return redirect(reverse('club:login'))
    if request.method == 'POST':
        join_club_id = request.POST.get('club_id', None)
        member_id = request.POST.get('member_id', None)

        if not (join_club_id and member_id
                and verify_permission(join_club_id, member_id)):
            print(join_club_id)
            print(member_id)
            print(verify_permission(join_club_id, member_id))
            return render(request, 'club/404.html')
        club = Club.objects.filter(pk=join_club_id).first()
        data = {
            'manage_club': club,
        }
        data = merge_dict(data, user_data(request))
        return render(request, 'club/add_member.html', context=data)