class ClusterSchema(ma.SQLAlchemyAutoSchema): class Meta: model = Cluster load_instance = True include_fk = True window_start = ma.Function(lambda obj: obj.window_start()) offset = ma.Function(lambda obj: obj.nearest_window()) file = ma.Nested(AudioFileSchema)
class ProductSchema(ma.ModelSchema): class Meta: model = models.Product fields = ('id', 'name', 'description', 'price', 'category', 'status', 'tags') category = ma.Nested(CategorySchema) price = ma.Decimal(as_string=True, places=2) status = ma.Function(lambda v: v.status.value) tags = ma.Function(lambda v: list(v.tags))
class FeatureRequestCreateUpdateSchema(ma.ModelSchema): class Meta: model = FeatureRequest fields = ('title', 'description', 'priority', 'target_date', 'client', 'area', 'is_archived') client = ma.Function(deserialize=load_from_id(Client), required=True, validate=[validate.NoneOf([None])]) area = ma.Function(deserialize=load_from_id(Area), required=True, validate=[validate.NoneOf([None])])
class ProductDeserializeSchema(ProductSchema): class Meta: model = models.Product fields = ('name', 'description', 'price', 'category', 'status', 'tags') category = ma.Function(deserialize=lambda v: models.Category.query.get( v), required=True, validate=[validate.NoneOf([None])]) status = ma.Function(deserialize=models.ProductStatusEnum.find, required=True, validate=[validate.NoneOf([None])]) tags = ma.List(ma.String(), required=True)
class SingleMatchSchema(ma.ModelSchema): class Meta: fields = ('id', 'date', 'winner_id', 'loser_id', 'winner_name', 'loser_name', 'update_singles', 'delete_singles') model = SingleMatch winner_name = ma.Function( lambda match: Player.query.get_or_404(match.winner_id).name) loser_name = ma.Function( lambda match: Player.query.get_or_404(match.loser_id).name) delete_singles = ma.URLFor("delete_singlematch", id="<id>") update_singles = ma.URLFor("update_singlematch", id="<id>")
class SessionItemSchema(ma.Schema): class Meta: fields = ('id', 'project', 'created', 'closed', 'handler', 'start_msg_id', 'msg_id', 'msg', 'last_session_msg', 'sync_msg_id', 'handler_msg_id', 'msg_count', 'unsynced_count', 'unhandled_count') project = ma.Nested(ProjectDataSchema) created = ma.Function(lambda s: s.created.timestamp()) closed = ma.Function(lambda s: s.closed and s.closed.timestamp()) handler = ma.Nested(RawStaffSchema) msg = ma.Nested(MessageSchema) last_session_msg = ma.Nested(MessageSchema)
class PlayerSchema(ma.ModelSchema): class Meta: fields = ('id', 'password', 'name', 'rollno', 'pwin', 'plos', 'dwin', 'dlos', 'age', 'mobile', 'dob', 'image', 'delete_player', 'update_player', 'type') model = Player image = ma.URLFor("get_player_image", id="<id>") update_player = ma.URLFor("update_player", id="<id>") delete_player = ma.URLFor("delete_player", id="<id>") age = ma.Function(lambda player, context: calculate_age(player.dob)) pwin = ma.Function(lambda match, context: int(context['pwin'])) plos = ma.Function(lambda match, context: int(context['plos'])) dwin = ma.Function(lambda match, context: int(context['dwin'])) dlos = ma.Function(lambda match, context: int(context['dlos']))
class ParticipantSchema(ModelSchema): class Meta(ModelSchema.Meta): model = Participant fields = ('id', '_links', 'last_updated', 'name', 'relationship', 'user_id', 'avatar_icon', 'avatar_color', 'has_consented', 'contact', 'identification', 'percent_complete') id = fields.Integer(required=False, allow_none=True) name = ma.Function(lambda obj: missing if obj is None else obj.get_name()) relationship = EnumField(Relationship) user_id = fields.Integer(required=False, allow_none=True) percent_complete = ma.Function(lambda obj: missing if obj is None else obj.get_percent_complete()) contact = fields.Nested(ContactQuestionnaireSchema, dump_only=True) identification = fields.Nested(IdentificationQuestionnaireSchema, dump_only=True) _links = ma.Hyperlinks({ 'self': ma.URLFor('api.participantendpoint', id='<id>'), 'user': ma.URLFor('api.userendpoint', id='<user_id>') })
class OrdersCompactSchema(ma.ModelSchema): class Meta: model = Order fields = ('id', 'order_date', 'delivered_on', 'status', 'total_price', 'number_of_items') number_of_items = ma.Function(lambda obj: obj.get_number_of_items())
class CustomerDeserializeSchema(CustomerSchema): class Meta: model = models.Customer fields = ('email', 'firstname', 'lastname', 'country') country = ma.Function(deserialize=lambda v: models.Country.query.get( v), required=True, validate=[validate.NoneOf([None])])
class FeatureRequestListArgsSchema(ma.Schema): limit = ma.Integer(required=False, validate=[validate.Range(1, 100)], missing=20) offset = ma.Integer(required=False, validate=[validate.Range(0)], missing=0) include_archived = ma.Boolean(required=False, missing=False) area = ma.Function(deserialize=load_from_id(Area), required=False) client = ma.Function(deserialize=load_from_id(Client), required=True, validate=[validate.NoneOf([None])]) search = ma.String(required=False) sort = ma.String( required=False, validate=[validate.OneOf(['priority', 'id', 'target_date'])], missing='priority')
class ProjectLabelSchema(ma.SQLAlchemyAutoSchema): class Meta: model = ProjectLabel load_instance = True include_fk = True label = ma.Nested(LabelSchema) clip_count = ma.Function(lambda obj: obj.clip_count)
class OrderUpdateSchema(ma.ModelSchema): class Meta: model = models.Order fields = ('status',) status = ma.Function(deserialize=models.OrderStatusEnum.find, required=True, validate=[validate.NoneOf([None])])
class ModelOutputSchema(ma.SQLAlchemyAutoSchema): class Meta: model = ModelOutput load_instance = True include_fk = True window_start = ma.Function(lambda obj: obj.start_time()) file = ma.Nested(AudioFileSchema) label = ma.Nested(LabelSchema)
class BookSearchableSchema(ma.ModelSchema): class Meta: model = Book fields = Book.__searchable__ authors_names = ma.Nested(AuthorNameSchema, many=True) publishers = ma.Nested(PublisherSchema, many=True) genres = ma.Nested(GenreSchema, many=True) tags = ma.Nested(TagSchema, many=True) authors = ma.Function(get_authors, many=True)
class OrderSchema(ma.ModelSchema): class Meta: model = models.Order fields = ('id', 'created_at', 'total', 'status', 'customer', 'detail') status = ma.Function(lambda v: v.status.value) customer = ma.Nested(CustomerSchema, only=( 'id', 'email', 'firstname', 'lastname')) detail = ma.Nested(OrderDetailSchema, many=True) total = ma.Decimal(as_string=True, places=2, dump_only=True)
class OrderCreateSchema(ma.Schema): customer = ma.Function(deserialize=lambda v: models.Customer.query.get( v), required=True, validate=[validate.NoneOf([None])]) detail = ma.Nested(OrderDetailCreateSchema, many=True) @post_load def create_order(self, data): order = models.Order(customer=data['customer']) for detail in data['detail']: order.add_product(detail['product'], detail['quantity']) return order
class SingleSummarySchema(ma.ModelSchema): class Meta: fields = ('date', 'opponent', 'match_won') model = SingleMatch w = ma.Function( lambda match: Player.query.get_or_404(match.winner_id).name) l = ma.Function(lambda match: Player.query.get_or_404(match.loser_id).name) opponent = ma.Method('get_opponent') match_won = ma.Method('get_matchwon') def get_opponent(self, match): pid = int(self.context['player_id']) mp = {match.winner_id: match.l, match.loser_id: match.w} return mp[pid] def get_matchwon(self, match): pid = int(self.context['player_id']) mp = {match.winner_id: True, match.loser_id: False} return mp[pid]
class ProjectDataSchema(ma.Schema): class Meta: fields = ("id", "domain", "type", "biz_id", "is_online", "last_online_ts", "tags", "owner", "leader", "customers", "meta_data", "ext_data", "start_msg_id", "msg_id", "current_session_id") owner = ma.Nested(RawCustomerSchema) leader = ma.Nested(RawStaffSchema) customers = ma.List(ma.Nested(RawCustomerSchema)) last_online_ts = ma.Function( lambda s: s.last_online_ts and s.last_online_ts.timestamp())
class UserExportSchema(ModelSchema): """ Used exclusively for data export, removes identifying information""" class Meta(ModelSchema.Meta): model = User fields = ('id', 'last_updated', 'role', 'email_verified', 'email', '_links') role = EnumField(Role) email = ma.Function(lambda obj: missing if obj is None else str(obj.id)) _links = ma.Hyperlinks({ 'self': ma.URLFor('api.userendpoint', id='<id>'), })
class OrdersListSchema(OrderSchema): class Meta: model = models.Order fields = ('id', 'created_at', 'status', 'customer', 'total') status = ma.Function(lambda row: row.Order.status.value) @staticmethod def get_attribute(obj, attr, default): if attr == 'total': return obj.total return getattr(obj.Order, attr, default)
class ModelSchema(ma.Schema): class Meta: model = Model fields = ( "id", "user", "project_id", "name", "visibility", "dataset", "hyperparameters", "parameters", "metrics", "tags", "git", "created", "updated", "_links", ) ordered = True visibility = ma.Function(lambda obj: "private" if obj.private else "public") dataset = ma.Method("get_dataset_details") git = ma.Method("get_version_control_details") user = ma.Nested(UserSchema(exclude=("created", "updated", "email"))) tags = ma.Nested(TagSchema(exclude=("models", )), many=True) _links = ma.Hyperlinks({ "self": ma.URLFor("api.model", id="<id>", _external=True), "user": ma.URLFor("api.user", id="<user_id>", _external=True), "project": ma.URLFor("api.project", id="<project_id>", _external=True), "download": ma.URLFor("storage.download_model", id="<id>", _external=True), }) def get_dataset_details(self, obj): return { "name": obj.dataset_name, "description": obj.dataset_description } def get_version_control_details(self, obj): return { "active_branch": obj.git_active_branch, "commit_hash": obj.git_commit_hash, }
class MessageSchema(ma.Schema): class Meta: fields = ("channel", "user_type", "user_id", "tx_key", "rx_key", "msg_id", "domain", "type", "content", "ts", "session_id") ts = ma.Function(lambda msg: msg.ts.timestamp())
class RawStaffSchema(ma.Schema): class Meta: fields = ("uid", "name", "is_online", "updated") updated = ma.Function(lambda x: x.updated.timestamp())
class OrderDetailCreateSchema(ma.Schema): product = ma.Function(deserialize=lambda v: models.Product.query.get( v), required=True, validate=[validate.NoneOf([None])]) quantity = ma.Integer(required=True, validate=[validate.Range(1)])
class OrdersByStatusSchema(ma.Schema): count = ma.Integer() status = ma.Function(lambda row: row.status.value)