def test_clone_from_class(self): parent = Model('Parent', { 'name': fields.String, 'age': fields.Integer, 'birthdate': fields.DateTime, }) child = Model.clone('Child', parent, { 'extra': fields.String, }) assert child.__schema__ == { 'properties': { 'name': { 'type': 'string' }, 'age': { 'type': 'integer' }, 'birthdate': { 'type': 'string', 'format': 'date-time' }, 'extra': { 'type': 'string' } }, 'type': 'object' }
def test_clone_from_class_with_multiple_parents(self): grand_parent = Model('GrandParent', { 'grand_parent': fields.String, }) parent = Model('Parent', { 'name': fields.String, 'age': fields.Integer, 'birthdate': fields.DateTime, }) child = Model.clone('Child', grand_parent, parent, { 'extra': fields.String, }) self.assertEqual(child.__schema__, { 'properties': { 'grand_parent': { 'type': 'string' }, 'name': { 'type': 'string' }, 'age': { 'type': 'integer' }, 'birthdate': { 'type': 'string', 'format': 'date-time' }, 'extra': { 'type': 'string' } } })
def test_clone_from_instance(self): parent = Model('Parent', { 'name': fields.String, 'age': fields.Integer, 'birthdate': fields.DateTime, }) child = parent.clone('Child', { 'extra': fields.String, }) self.assertEqual(child.__schema__, { 'properties': { 'name': { 'type': 'string' }, 'age': { 'type': 'integer' }, 'birthdate': { 'type': 'string', 'format': 'date-time' }, 'extra': { 'type': 'string' } }, 'type': 'object' })
def test_clone_from_instance(self): parent = Model( 'Parent', { 'name': fields.String, 'age': fields.Integer, 'birthdate': fields.DateTime, }) child = parent.clone('Child', { 'extra': fields.String, }) assert child.__schema__ == { 'properties': { 'name': { 'type': 'string' }, 'age': { 'type': 'integer' }, 'birthdate': { 'type': 'string', 'format': 'date-time' }, 'extra': { 'type': 'string' } }, 'type': 'object' }
def test_clone_from_class(self): parent = Model('Parent', { 'name': fields.String, 'age': fields.Integer, 'birthdate': fields.DateTime, }) child = Model.clone('Child', parent, { 'extra': fields.String, }) self.assertEqual(child.__schema__, { 'properties': { 'name': { 'type': 'string' }, 'age': { 'type': 'integer' }, 'birthdate': { 'type': 'string', 'format': 'date-time' }, 'extra': { 'type': 'string' } }, 'type': 'object' })
def test_clone_from_instance_with_multiple_parents(self): grand_parent = Model('GrandParent', { 'grand_parent': fields.String, }) parent = Model('Parent', { 'name': fields.String, 'age': fields.Integer, 'birthdate': fields.DateTime, }) child = grand_parent.clone('Child', parent, { 'extra': fields.String, }) assert child.__schema__ == { 'properties': { 'grand_parent': { 'type': 'string' }, 'name': { 'type': 'string' }, 'age': { 'type': 'integer' }, 'birthdate': { 'type': 'string', 'format': 'date-time' }, 'extra': { 'type': 'string' } }, 'type': 'object' }
# "string" # } # }, # "reference":{"type": { # "string" # } # }, # }) #name_collection_model = api.schema_model("NameCollection", # {"name_collection" : fields.List(fields.Nested(name_model)), # # }) name_response = response_model.clone("NameResponse", {"personname": fields.Nested(name_model) }) #name_collection_response = name_response.clone("NameCollectionResponse", # { # "namelist": fields.List(fields.Nested(name_collection_model), # description="List of names") # }) """ target_model = api.schema_model("AnnotationTarget", { "properties": { "id": { "type": "string" }, "type": {
from utils import ServiceDAO import custom_fields as fields # ############# # DEFINE MODELS # ############# CUSTOM_FORM = Model( 'CustomForm', { 'id': fields.Integer(), 'speaker_form': fields.String(), 'session_form': fields.String() }) CUSTOM_FORM_POST = CUSTOM_FORM.clone('CustomFormPost') del CUSTOM_FORM_POST['id'] # ########## # DEFINE DAO # ########## class CFDAO(ServiceDAO): def create(self, event_id, data, url): data = self.validate(data) return update_or_create(self.model, event_id=event_id, **data) CustomFormDAO = CFDAO(CustomForms, CUSTOM_FORM_POST)
return "http://{host}/discovery/v1/schemas/{id}".format( host=current_app.config['HOST'], id=value) class SchemaPayload(fields.Raw): def format(self, value): return "http://{host}/discovery/v1/schemas/{id}/payload".format( host=current_app.config['HOST'], id=value) schema_base = Model( 'SchemaBase', { 'name': SpecName, 'version': fields.String, 'format': fields.String, 'description': fields.String, 'id': fields.String, 'title': fields.String, 'updated': UpdateTime(attribute='m_time') }) schema = schema_base.clone( 'Schema', { 'href': SchemaHref(attribute='id'), 'payload_href': SchemaPayload(attribute='id') }) schema_detail = schema_base.clone('SchemaDetail', { 'schema': JsonContent(), })
__all__ = ['login_model', 'registration_model', 'auth_model'] from flask_restplus import Model, fields login_model = Model('login_model', { 'username': fields.String, 'password': fields.String }) registration_model = login_model.clone('registration_model', { 'email': fields.String }) auth_model = Model('auth_model', { 'Authorization': fields.String(example='Bearer token') })
import custom_fields as fields from app.helpers.data import update_or_create from app.models.custom_forms import CustomForms from utils import ServiceDAO # ############# # DEFINE MODELS # ############# CUSTOM_FORM = Model('CustomForm', { 'id': fields.Integer(), 'speaker_form': fields.String(), 'session_form': fields.String() }) CUSTOM_FORM_POST = CUSTOM_FORM.clone('CustomFormPost') del CUSTOM_FORM_POST['id'] # ########## # DEFINE DAO # ########## class CFDAO(ServiceDAO): def create(self, event_id, data, url): data = self.validate(data) return update_or_create(self.model, event_id=event_id, **data) CustomFormDAO = CFDAO(CustomForms, CUSTOM_FORM_POST)
fields.Integer(required=True, description='The file size', example=100), 'reversion': fields.Integer(description='The file reversion', example=1), }, mask='file_name,file_size,reversion') file_output = file_input.clone( 'File', { 'uuid': fields.String( required=True, description='The file uuid and the unique identifier of file', example="12345678-1234-5678-1234-567812345678"), 'status': fields.String(required=True, description='The file status', example="new_create"), 'creator_id': fields.Integer( required=True, description="The creaator's id", example=1), 'create_time': fields.DateTime( required=True, description="The document create time", example=""), 'last_modify_time': fields.String(required=True, description="The document last update time"), }) files_output = paginate_model.clone( 'FileList', {'items': fields.List(fields.Nested(file_output))})
required=True, description= 'The ID of the library that has digitized the book (if applicable)' ), 'incoming_references': fields.List(fields.Nested(reference_fields), required=False) }) citing_publication_list = Model( 'CitingPublications', { 'articles': fields.List(fields.Nested(article_fields)), 'books': fields.List(fields.Nested(book_fields)), }) cited_publication_list = Model.clone( 'CitedPublications', citing_publication_list, {'primary_sources': fields.List(fields.Nested(primary_source_fields))}) keywords_with_tfidf = Model( 'KeywordsWithTfidf', { 'keyword': fields.String(required=True), 'tf': fields.Integer(required=True), 'df': fields.Integer(required=True), 'tfidf': fields.Float(required=True), }) europeana_result = Model( 'EuropeanaResult', { 'direct_url': fields.String(required=False), 'europeana_url': fields.String(required=False), 'provider': fields.String(required=False),
"group_id": fields.Integer, }, ) platform_group = Model( "Platform Group", { "id": fields.Integer, "name": fields.String, "platforms": fields.List(fields.Nested(platform_base)), }, mask="id,name,platforms{id,type,slug,name,country}", ) platform = platform_base.clone( "Platform", {"group": fields.Nested(platform_group, allow_null=True)} ) # It works, alright? platform.__mask__ = Mask("id,type,slug,name,country,group{id,name}") scrap = Model( "Scrap", { "id": fields.Integer, "date": fields.DateTime, "status": fields.String(choice=[str(s) for s in ScrapStatus]), "platform": fields.Nested(platform), "stats": fields.Raw(), },
from flask_restplus import Model, fields from .commons import paginate_model user_basic = Model('User', { 'username': fields.String(required=True, description='The user name', attribute='username'), 'email': fields.String(required=True, description='The user email'), }) user_input = user_basic.clone('User Input', { 'password': fields.String(required=True, description='The user password'), }) user_output = user_basic.clone('User', { 'id': fields.String(required=True, description='The user identifier'), }) users_output = paginate_model.clone('Users', { 'items': fields.List(fields.Nested(user_output)) })
}) person_model = api.clone( 'Person', resp_model, { # clone继承(api方式继承) 'weight': fields.Float(), # 体重 }) m_model = Model('m model', { 'name': fields.String, 'address': fields.String, }) c_model = m_model.clone( 'c model', { # 这种方式和api.clone有一些不同 'age': fields.Integer, }) @marshal_with(c_model, skip_none=True) def show_my(): """ 自带的Model和marshal_with同api.Model, api.marshal_with的区别 就是后者会自动添加到swagger文档中 """ return {'name': 'bifeng', 'address': 'hangzhou', 'age': 19} @api.route('/todo/format', endpoint='todo_ep') class TodoFormat(Resource): # 跳过返回 None的字段, 而不是返回null(该设置也可以在Nested中使用)
from flask_restplus import Model, fields from .commons import paginate_model document_input = Model( 'Document Input', { 'title': fields.String(required=True, description='The document title'), 'content': fields.String(required=True, description='The document content'), }) document_output = document_input.clone( 'Document', { 'id': fields.String(required=True, description='The document identifier'), 'creator_id': fields.String(required=True, description="The creaator's id"), 'create_time': fields.String(required=True, description="The document create time"), 'last_modify_time': fields.String(required=True, description="The document last update time"), 'reversion': fields.String(required=True, description="The document reversion"), }) documents_output = paginate_model.clone( 'DocumentList', {'items': fields.List(fields.Nested(document_output))})