예제 #1
0
    def del_config(self, uid, city_ids):
        """Return result in string"""
        city_ids = ext.to_int(city_ids)

        if not bot.line_api_wrapper.is_valid_user_id(uid):
            return error.error.line_bot_api.illegal_user_id(uid)

        if city_ids is None:
            return error.error.main.invalid_thing_with_correct_format(
                u'城市ID', u'整數', city_ids)

        if not isinstance(city_ids, (list, tuple)):
            city_ids = [city_ids]

        mod_result = self.update_one(
            {weather_report_config_data.USER_ID: uid}, {
                '$pull': {
                    weather_report_config_data.CONFIG: {
                        '$or': [{
                            weather_report_child_config.CITY_ID: city_id
                            for city_id in city_ids
                        }]
                    }
                }
            }, True)

        if mod_result.modified_count > 0:
            return u'已刪除常用城市。\n城市ID: {}'.format(u'、'.join(
                [u'#{}'.format(id) for id in city_ids]))
        else:
            return u'沒有更動任何常用城市。'
예제 #2
0
    def valid_int(obj, allow_null):
        base = param_validator.base_null(obj, allow_null)
        if base is not None:
            return base

        new_int = ext.to_int(obj)

        if new_int is not None:
            return param_validation_result(obj, True)
        else:
            return param_validation_result(error.sys_command.must_int(obj),
                                           False)
예제 #3
0
    def valid_int_arr(obj, allow_null):
        base = param_validator.base_null(obj, allow_null)
        if base is not None:
            return base

        sp = ext.to_list(obj.split(param_validator.ARRAY_SEPARATOR))
        new_int = ext.to_int(sp)

        if new_int is not None:
            return param_validation_result(sp, True)
        else:
            return param_validation_result(error.sys_command.must_int(obj),
                                           False)
예제 #4
0
    def add_config(self,
                   uid,
                   city_ids,
                   mode=tool.weather.output_type.SIMPLE,
                   interval=3,
                   data_range=120):
        """Return result in string"""
        city_ids = ext.to_int(city_ids)

        if not bot.line_api_wrapper.is_valid_user_id(uid):
            return error.error.line_bot_api.illegal_user_id(uid)
        if data_range < 0 or data_range % 3 != 0 or data_range > 120:
            return error.error.main.invalid_thing_with_correct_format(
                u'資料範圍(小時內)', u'0~120之間,並且是3的倍數的整數。', data_range)
        if interval < 0 or interval % 3 != 0 or interval > data_range:
            return error.error.main.invalid_thing_with_correct_format(
                u'資料頻率', u'0~{}(資料範圍)之間,並且是3的倍數的整數。'.format(data_range),
                interval)
        if city_ids is None:
            return error.error.main.invalid_thing_with_correct_format(
                u'城市ID', u'整數', city_ids)

        mod_result = self.update_one(
            {weather_report_config_data.USER_ID: uid}, {
                '$push': {
                    weather_report_config_data.CONFIG: {
                        '$each': [
                            weather_report_child_config.init_by_field(
                                city_id, mode, interval, data_range)
                            for city_id in city_ids
                        ]
                    }
                }
            }, True)

        if mod_result.modified_count > 0:
            return u'已新增常用城市。\n{}'.format(u'\n'.join([
                u'城市ID: {} ({})\n查看{}小時內每{}小時的資料。'.format(
                    city_id, unicode(mode), data_range, interval)
                for city_id in city_ids
            ]))
        else:
            return u'沒有更動任何常用城市。'