class ProviderDetailsHistorySchema(BaseSchema): created_by = fields.Nested(UserSchema, only=['id', 'name', 'email_address'], dump_only=True) class Meta(BaseSchema.Meta): model = models.ProviderDetailsHistory exclude = ("provider_rates", "provider_stats") strict = True
class JobSchema(BaseSchema): created_by_user = fields.Nested(UserSchema, attribute="created_by", dump_to="created_by", only=["id", "name"], dump_only=True) created_by = field_for(models.Job, 'created_by', required=True, load_only=True) job_status = field_for(models.JobStatus, 'name', required=False) scheduled_for = fields.DateTime() service_name = fields.Nested(ServiceSchema, attribute="service", dump_to="service_name", only=["name"], dump_only=True) template_name = fields.Method('get_template_name', dump_only=True) template_type = fields.Method('get_template_type', dump_only=True) contact_list_id = field_for(models.Job, 'contact_list_id') def get_template_name(self, job): return job.template.name def get_template_type(self, job): return job.template.template_type @validates('scheduled_for') def validate_scheduled_for(self, value): _validate_datetime_not_in_past(value) _validate_datetime_not_more_than_96_hours_in_future(value) class Meta(BaseSchema.Meta): model = models.Job exclude = ( 'notifications', 'notifications_delivered', 'notifications_failed', 'notifications_sent', ) strict = True
class AcquaintanceSchema(ma.Schema): "Description" excerpts = fields.Nested('ExcerptSchema', allow_none=True, many=True, only=["id"]) class Meta: additional = ("isa", "person", "acquainted")
class NotificationWithPersonalisationSchema(NotificationWithTemplateSchema): template_history = fields.Nested( TemplateHistorySchema, attribute="template", only=['id', 'name', 'template_type', 'content', 'subject', 'version'], dump_only=True) class Meta(NotificationWithTemplateSchema.Meta): # mark as many fields as possible as required since this is a public api. # WARNING: Does _not_ reference fields computed in handle_template_merge, such as # 'body', 'subject' [for emails], and 'content_char_count' fields = ( # db rows 'billable_units', 'created_at', 'id', 'job_row_number', 'notification_type', 'reference', 'sent_at', 'sent_by', 'status', 'template_version', 'to', 'updated_at', # computed fields 'personalisation', # relationships 'api_key', 'job', 'service', 'template_history', ) @pre_dump def handle_personalisation_property(self, in_data): self.personalisation = in_data.personalisation return in_data @post_dump def handle_template_merge(self, in_data): in_data['template'] = in_data.pop('template_history') template = get_template_instance(in_data['template'], in_data['personalisation']) in_data['body'] = template.content_with_placeholders_filled_in if in_data['template']['template_type'] != models.SMS_TYPE: in_data['subject'] = template.subject in_data['content_char_count'] = None else: in_data['content_char_count'] = template.content_count in_data.pop('personalisation', None) in_data['template'].pop('content', None) in_data['template'].pop('subject', None) return in_data
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() @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)
class PlaceSchema(ma.Schema): "Description" slug = fields.Method("get_slugify") excerpts = fields.Nested('ExcerptSchema', allow_none=True, many=True, only=["id"]) events = fields.Nested('EventSchema', allow_none=True, many=True, only=["id"]) owners = fields.Nested('PersonSchema', allow_none=True, many=True, only=["id"]) def get_slugify(self, obj): return (slugify(obj.name)) class Meta: additional = ("id", "unique", "name", "description", "address", "lat", "lon")
class OrganizationParameterQualityCheckSchema(ma.ModelSchema): class Meta: model = organizations.OrganizationParameterQualityChecks sqla_session = session # load_only = ('parameter_name','quality_check_operand_name','quality_check_action_name') organization_id = field_for(organizations.Organizations, 'organization_id', dump_only=False) parameter_id = field_for(domains.Parameters, 'parameter_id', dump_only=False) quality_check_operand_id = field_for(domains.QualityCheckOperands, 'quality_check_operand_id', dump_only=False) quality_check_action_id = field_for(domains.QualityCheckActions, 'quality_check_action_id', dump_only=False) parameter_name = fields.Nested(ParameterSchema, only=('parameter_name')) quality_check_operand_name = fields.Nested( QualityCheckOperandSchema, only=('quality_check_operand_name')) quality_check_action_name = fields.Nested( QualityCheckActionSchema, only=('quality_check_action_name'))
class SensorSchema(ma.ModelSchema): class Meta: model = sensors.Sensors sqla_session = session ingest_frequency = fields.Number(validate=lambda n: 1 <= n <= 60) organization_id = field_for(organizations.Organizations, 'organization_id', dump_only=False) data_qualifier_id = field_for(domains.DataQualifiers, 'data_qualifier_id', dump_only=False) medium_type_id = field_for(domains.MediumTypes, 'medium_type_id', dump_only=False) parameters = fields.Nested(SensorParameterSchema, many=True)
class TemplateHistorySchema(BaseSchema): reply_to = fields.Method("get_reply_to", allow_none=True) reply_to_text = fields.Method("get_reply_to_text", allow_none=True) created_by = fields.Nested(UserSchema, only=['id', 'name', 'email_address'], dump_only=True) created_at = field_for(models.Template, 'created_at', format='%Y-%m-%d %H:%M:%S.%f') def get_reply_to(self, template): return template.reply_to def get_reply_to_text(self, template): return template.get_reply_to_text() class Meta: model = models.TemplateHistory
class ParameterSchema(ma_sq.SQLAlchemySchema): class Meta: model = ParameterDao include_relationships = True include_fk = True load_instance = True sqla_session = orm_utils.orm_session() uid = fields.Integer() name = fields.String(required=True, allow_none=False, validate=validate.Length(min=1)) values = fields.Nested(ValueSchema, many=True) parameter_set = fields.Pluck('ParameterSetSchema', 'uid', allow_none=True) position = fields.Integer(allow_none=True) excluded = fields.Pluck("ParameterSchema", "uid", many=True) excluded_by = fields.Pluck("ParameterSchema", "uid", many=True)
class UpdateParameterSchema(Schema): uid = fields.Integer(load_only=True) name = fields.String(required=True, allow_none=False, validate=validate.Length(min=1)) values = fields.Nested('ValueSchema', many=True) parameter_set = fields.Pluck('ParameterSetSchema', 'uid', allow_none=True) position = fields.Integer(load_only=True) excluded = fields.Pluck("ParameterSchema", "uid", many=True) excluded_by = fields.Pluck("ParameterSchema", "uid", many=True) @validates('uid') def validate_uid(self, data, **kwargs): raise ValidationError("Cannot modify read-only field") @validates('position') def validate_position(self, data, **kwargs): raise ValidationError("Cannot modify read-only field")
class TemplateHistorySchema(BaseSchema): reply_to = fields.Method("get_reply_to", allow_none=True) reply_to_text = fields.Method("get_reply_to_text", allow_none=True) process_type = field_for(models.Template, 'process_type') created_by = fields.Nested(UserSchema, only=['id', 'name', 'email_address'], dump_only=True) created_at = field_for(models.Template, 'created_at', format=DATETIME_FORMAT_NO_TIMEZONE) def get_reply_to(self, template): return template.reply_to def get_reply_to_text(self, template): return template.get_reply_to_text() class Meta(BaseSchema.Meta): model = models.TemplateHistory
class PersonSchema(ma.Schema): "Description" slug = fields.Method("get_slugify") excerpts = fields.Nested('ExcerptSchema', allow_none=True, many=True, only=["id"]) events = fields.Nested('EventSchema', allow_none=True, many=True, only=["id"]) places = fields.Nested('PlaceSchema', allow_none=True, many=True, only=["id"]) possessions = fields.Nested('ItemSchema', allow_none=True, many=True, only=["id"]) properties = fields.Nested('PlaceSchema', allow_none=True, many=True, only=["id"]) groups = fields.Nested('GroupSchema', allow_none=True, many=True, only=["id"]) acquaintances = fields.Nested('PersonSchema', allow_none=True, many=True, only=["id"]) # TODO: this is a nested query # "encounters": [i.id for e in self.events for i in e.items], def get_slugify(self, obj): return (slugify(obj.name)) class Meta: additional = ("id", "name", "alias", "unique")
class CitizensSchema(Schema): citizens = fields.Nested(PersonSchema(many=True))
class ApiSchema(ApiSchemaRaw): endpoints = fields.List(fields.Nested(EndpointSchema))
class OrdersDetailSchema(ma.SQLAlchemySchema): class Meta: model = OrderDetail client_id = fields.Integer() products = fields.List(fields.Nested(ProductsMetadata))
class PictureAlbumSchema(PictureAlbumPhotoSchema): picture = fields.Nested(PictureAdminSchema, many=True)
class CategoryListAdminSchema(BaseMarshmallow): class Meta: fields = ("id", "title", "sort", "module", "create_time", "sub") sub = fields.Nested(SubCategorySchema, many=True) create_time = fields.DateTime('%Y-%m-%d %H:%M')
class CategoryListSchema(BaseMarshmallow): class Meta: fields = ("id", "title", "module", "sub") sub = fields.Nested(SubCategorySchema, many=True)