class UAVBatteryAdd(Resource):
    def __init__(self):
        self.dao = BatteryDAO()
        self.userDao = UserDAO()

    def post(self):
        if (request.data != ""):
            data = json.loads(request.data)
            token = data['token']
            battery = data['battery']
            battery_dict = json.loads(json.dumps(battery))
            battery_obj = Battery()
            battery_obj.battery_id = battery_dict[0]['battery_id']
            battery_obj.battery_ver = battery_dict[0]['battery_ver']
            battery_obj.battery_type = battery_dict[0]['battery_type']
            battery_obj.battery_fact = battery_dict[0]['battery_fact']
            battery_obj.battery_date = battery_dict[0]['battery_date']
            battery_obj.user_team = battery_dict[0]['user_team']
            battery_obj.battery_status = '在库'
            battery_obj.battery_use_number = 0
            user = self.userDao.verify_token(token, '')
            if (not user):
                return make_response(jsonify({'error': 'Unauthorized access'}),
                                     401)

            rs = self.dao.add_battery(user, battery_obj)
            if rs == 1:
                return make_response(
                    jsonify({'success': 'add device success'}), 200)
            else:
                return make_response(jsonify({'failed': 'add device failed'}),
                                     401)
        else:
            return make_response(jsonify({'error': 'Unauthorized access'}),
                                 401)

    def get(self):
        return self.post()
Exemple #2
0
class UAVBatteryAdd(Resource):
    def __init__(self):
        self.dao = BatteryDAO()
        self.userDao = UserDAO()

    def post(self):
        if (request.data != ""):
            data = json.loads(request.data)
            token = data['token']
            battery = data['battery']
            battery_dict = json.loads(json.dumps(battery))
            battery_obj = Battery()
            battery_obj.battery_id = battery_dict[0]['battery_id']
            battery_obj.battery_ver = battery_dict[0]['battery_ver']
            battery_obj.battery_type = battery_dict[0]['battery_type']
            battery_obj.battery_fact = battery_dict[0]['battery_fact']
            battery_obj.battery_date = datetime.strptime(
                battery_dict[0]['battery_date'], '%Y-%m-%d').date()
            battery_obj.user_team = battery_dict[0]['user_team']
            battery_obj.battery_use_dpartment = battery_dict[0][
                'use_department']
            battery_obj.battery_status = '在库'
            battery_obj.battery_use_number = 0
            user = self.userDao.verify_token(token, '')
            if (not user):
                return make_response(
                    jsonify({
                        'error': '用户不存在或登录过期',
                        'errorcode': 10000000
                    }), 400)
            if user == 1010301:
                return make_response(
                    jsonify({
                        'error': '登录过期',
                        'errorcode': user
                    }), 400)
            if user == 1010302:
                return make_response(
                    jsonify({
                        'error': '用户验证错误',
                        'errorcode': user
                    }), 400)

            rs = self.dao.add_battery(user, battery_obj)
            if rs == 1:
                return make_response(jsonify({'success': '添加电池成功'}), 200)
            elif rs == 2020901:
                return make_response(
                    jsonify({
                        'error': '待添加的电池已经存在,添加电池失败',
                        'errorcode': rs
                    }), 404)
            else:
                return make_response(
                    jsonify({
                        'error': '添加电池失败',
                        'errorcode': rs
                    }), 401)
        else:
            return make_response(
                jsonify({
                    'error': '输入参数错误',
                    'errorcode': 10000000
                }), 401)

    def get(self):
        return self.post()