Added functionality to allow filtering by multiple arguments.

Removed commented out code.
This commit is contained in:
Nicholas Staples
2016-04-04 13:13:29 +01:00
parent 42b9f13308
commit fac34aff10
4 changed files with 145 additions and 27 deletions

View File

@@ -2,7 +2,7 @@ from flask_marshmallow.fields import fields
from . import ma
from . import models
from app.dao.permissions_dao import permission_dao
from marshmallow import (post_load, ValidationError, validates, validates_schema)
from marshmallow import (post_load, ValidationError, validates, validates_schema, pre_load)
from marshmallow_sqlalchemy import field_for
from utils.recipients import (
validate_email_address, InvalidEmailError,
@@ -89,6 +89,11 @@ class ServiceSchema(BaseSchema):
raise ValidationError(error_msg, 'name')
class NotificationModelSchema(BaseSchema):
class Meta:
model = models.Notification
class TemplateSchema(BaseSchema):
class Meta:
model = models.Template
@@ -218,10 +223,29 @@ class EmailDataSchema(ma.Schema):
class NotificationsFilterSchema(ma.Schema):
template_type = field_for(models.Template, 'template_type', load_only=True, required=False)
status = field_for(models.Notification, 'status', load_only=True, required=False)
template_type = fields.Nested(TemplateSchema, only='template_type', many=True)
status = fields.Nested(NotificationModelSchema, only='status', many=True)
page = fields.Int(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):
try:
@@ -231,6 +255,7 @@ class NotificationsFilterSchema(ma.Schema):
except:
raise ValidationError("Not a positive integer")
user_schema = UserSchema()
user_schema_load_json = UserSchema(load_json=True)
service_schema = ServiceSchema()