示例#1
0
class NotificationsFilterSchema(ma.Schema):
    class Meta:
        strict = True

    template_type = fields.Nested(BaseTemplateSchema,
                                  only=['template_type'],
                                  many=True)
    status = fields.Nested(NotificationModelSchema, only=['status'], many=True)
    page = fields.Int(required=False)
    page_size = fields.Int(required=False)
    limit_days = fields.Int(required=False)
    include_jobs = fields.Boolean(required=False)
    include_from_test_key = fields.Boolean(required=False)
    older_than = fields.UUID(required=False)
    format_for_csv = fields.String()
    to = fields.String()
    include_one_off = fields.Boolean(required=False)
    count_pages = fields.Boolean(required=False)

    @pre_load
    def handle_multidict(self, in_data):
        if isinstance(in_data, dict) and hasattr(in_data, 'getlist'):
            out_data = dict([(k, in_data.get(k)) for k in in_data.keys()])
            if 'template_type' in in_data:
                out_data['template_type'] = [{
                    'template_type': x
                } for x in in_data.getlist('template_type')]
            if 'status' in in_data:
                out_data['status'] = [{
                    "status": x
                } for x in in_data.getlist('status')]

        return out_data

    @post_load
    def convert_schema_object_to_field(self, in_data):
        if 'template_type' in in_data:
            in_data['template_type'] = [
                x.template_type for x in in_data['template_type']
            ]
        if 'status' in in_data:
            in_data['status'] = [x.status for x in in_data['status']]
        return in_data

    @validates('page')
    def validate_page(self, value):
        _validate_positive_number(value)

    @validates('page_size')
    def validate_page_size(self, value):
        _validate_positive_number(value)
示例#2
0
class PrecipitationSchema(ma.Schema):
    city_id = fields.Int()
    city = fields.Str()
    state = fields.Str()
    mean = fields.Float()