예제 #1
0
def Stu_Apply():
    global result_data
    if request.method == 'POST':
        # 学生提交申请
        json_data = request.get_json(silent=True)

        if json_data is None:
            return jsonify(code=-101, data=None)

        if not json_data.get('activity_name'):
            return jsonify(code=-101, data={'tip': '缺少活动名称参数'})

        '''
        if not json_data.get('applicant_id'):
            return jsonify(code=-101, data={'tip': '缺少申请人学号参数'})
        '''

        if not json_data.get('applicant_name'):
            return jsonify(code=-101, data={'tip': '缺少申请人姓名参数'})

        if not json_data.get('applicant_phone'):
            return jsonify(code=-101, data={'tip': '缺少申请人联系方式参数'})

        if not json_data.get('use_date'):
            return jsonify(code=-101, data={'tip': '缺少活动使用日期参数'})

        if not json_data.get('begin_time'):
            return jsonify(code=-101, data={'tip': '缺少教室开始使用时间参数'})

        if not json_data.get('end_time'):
            return jsonify(code=-101, data={'tip': '缺少教室结束使用时间参数'})

        if not json_data.get('people_count'):
            return jsonify(code=-101, data={'tip': '缺少参加人数参数'})

        if not json_data.get('teacher_name'):
            return jsonify(code=-101, data={'tip': '缺少负责教师姓名参数'})

        if not json_data.get('building'):
            return jsonify(code=-101, data={'tip': '缺少教室所在教学楼的参数'})

        if not json_data.get('floor'):
            return jsonify(code=-101, data={'tip': '缺少教室所在楼层参数'})

        if not json_data.get('room'):
            return jsonify(code=-101, data={'tip': '缺少教室名字参数'})

        request_flag = material_flag = 1  # 如果为0,表示post传递的json不含有request_或material字段

        if not json_data.get('request'):
            request_flag = 0
        if not json_data.get('material'):
            material_flag = 0

        building_posted = json_data.get('building')
        floor_posted = json_data.get('floor')
        room_posted = json_data.get('room')
        exist = Room.query.filter(
            Room.room_name == room_posted and Room.floor == floor_posted and Room.building == building_posted).first()
        if exist is None:
            return jsonify(code=-102, data={'tip': '未查询到该教室'})

        room = Apply()
        time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
        apply_id = str(0)
        room.apply_id = apply_id
        room.activity_name = json_data.get('activity_name')
        room.applicant_id = from_session_get_applicant_id()
        room.applicant_name = json_data.get('applicant_name')
        room.applicant_phone = json_data.get('applicant_phone')
        room.apply_time = time
        room.use_date = json_data.get('use_date')
        room.begin_time = json_data.get('begin_time')
        room.end_time = json_data.get('end_time')
        room.people_count = json_data.get('people_count')
        if request_flag:
            room.request = json_data.get('request')
        room.teacher_name = json_data.get('teacher_name')
        if material_flag:
            room.material = json_data.get('material')
        room.check_status = '待审核'
        room.building = json_data.get('building')
        room.floor = json_data.get('floor')
        room.room_name = json_data.get('room')

        db.session.add(room)
        db.session.commit()
        try:
            db.session.commit()
        except:
            db.session.rollback()
            return jsonify(code=101, data={'tip': '数据库异常'})

        return jsonify(code=0, data={'tip': '正常'})

    elif request.method == 'GET':
        # 获取所有申请列表
        records = Apply.query.all()
        result_data = list()

        status_map = {
            '待审核': 0,
            '审核通过': 1,
            '审核失败': 2,
        }

        for record in records:
            result_data.append(dict(
                apply_id=record.apply_id,
                building=record.building,
                floor=record.floor,
                room=record.room_name,
                date=record.use_date,
                begin_time=record.begin_time,
                end_time=record.end_time,
                check_status=status_map.get(record.check_status)
            ))

    return jsonify(code=0, data=result_data)