def get_schema_fields(self, view): field_dim = Field( name='dim', required=False, description=_( 'Set geometry dimension (2 by default for 2D, 3 for 3D)'), example=3, type='integer') field_language = Field( name='language', required=False, description=_("Set language for translation. 'all' by default"), example="fr") field_format = Field( name='format', required=False, description=_( "Set output format (json / geojson). JSON by default"), example="geojson") field_fields = Field( name='fields', required=False, description= _("Limit required fields to increase performances. Ex : id,url,geometry" )) field_omit = Field( name='omit', required=False, description= _("Omit specified fields to increase performance. Ex: url,category" )) return field_dim, field_language, field_format, field_fields, field_omit
def get_schema_fields(self, view): return ( Field( name='language', required=False, location='query', schema=coreschema.String( title=_("Language"), description= _("Set language for translation. Default: all. Example: fr" ))), Field( name='fields', required=False, location='query', schema=coreschema.String( title=_("Fields"), description= _("Limit required fields to increase performances. Example: id,url,geometry" ))), Field( name='omit', required=False, location='query', schema=coreschema.String( title=_("Omit"), description= _("Omit specified fields to increase performance. Example: url,category" ))), )
def get_schema_fields(self, view): return ( Field( name='period', required=False, location='query', schema=coreschema.String( title=_("Period"), description= _('Period of occupancy. Month numbers (1-12) separated by comas.' ' any = occupied at any time in the year. ignore = occupied or not.' ' Example: 7,8 for july and august'))), Field(name='practices', required=False, location='query', schema=coreschema.String( title=_("Practices"), description=_( 'Practices ids separated by comas. Example: 1,3'))), Field(name='structure', required=False, location='query', schema=coreschema.Integer( title=_("Structure"), description=_('Structure id. Example: 5'))), )
def test_get_expand_field(self): expected_field = Field(name='expand', required=False, location='query', schema=None, description='comma separated list of objects to expand (eg:<br/> '\ 'sequence,session,assembly,transcript_release_set,genes,translations,<br/>exons,)', type='string', example=None) expanded_field = DrfFields.get_expand_field(Transcript) self.assertEqual(expected_field, expanded_field) exptected_expand_all_field = Field(name='expand_all', required=False, location='query', schema=None, description='selecting true will expand all the related fields, ' \ 'to selectively expand, use expand above', type='boolean', example=None) expanded_all_field = DrfFields.get_expand_all_field() self.assertEqual(exptected_expand_all_field, expanded_all_field) common_query_set_fields = CommonFields.get_common_query_set(Transcript) self.assertIsNotNone(common_query_set_fields) query_set_fields = CommonFields.get_expand_query_set(Transcript) self.assertIsNotNone(query_set_fields) expand_all_mandatory_query_set_fields = CommonFields.get_expand_all_mandatory_query_set(Transcript) self.assertIsNotNone(expand_all_mandatory_query_set_fields)
def get_schema_fields(self, view): field_dist = Field(name=self.dist_param, required=False, description=_('Max distance in meters between point and elements'), type='number', example='XXX') field_point = Field(name=self.point_param, required=False, description=_('Reference point to compute distance LNG,LAT'), example='1.2563,46.5214', ) return field_dist, field_point
def get_schema_fields(self, view): field_period = Field(name='period', required=False, description=_('Period of occupancy. Month numbers (1-12) separated by comas. any = occupied at any time in the year. ignore = occupied or not.'), example=u'7,8 for july and august') field_practices = Field(name='practices', required=False, description=_('Practices ids separated by comas.'), example=u'1,3') field_structure = Field(name='structure', required=False, description=_('Structure id.'), example=u'5') return field_period, field_practices, field_structure
def _get_content(data, base_url, ref): content = {} links = _get_list(data, 'links') properties = _get_dict(data, 'properties') if properties: for key, value in properties.items(): if not isinstance(value, dict): continue if list(value.keys()) == ['$ref']: value = _dereference(value['$ref'], ref) sub_content = _get_content(value, base_url, ref) if sub_content: content[key] = sub_content if links: for link in get_dicts(links): rel = _get_string(link, 'rel') if rel: href = _get_string(link, 'href') method = _get_string(link, 'method') schema = _get_dict(link, 'schema') schema_type = _get_list(schema, 'type') schema_properties = _get_dict(schema, 'properties') schema_required = _get_list(schema, 'required') fields = [] url = urlparse.urljoin(base_url, href) templated = uritemplate.variables(url) for item in templated: orig = item if item.startswith('(') and item.endswith(')'): item = unquote(item.strip('(').rstrip(')')) if item.startswith('#/'): components = [ component for component in item.strip('#/').split('/') if component != 'definitions' ] item = '_'.join(components).replace('-', '_') url = url.replace(orig, item) fields.append( Field(name=item, location='path', required=True)) if schema_type == ['object'] and schema_properties: fields += [ Field(name=key, required=(key in schema_required)) for key in schema_properties.keys() ] if rel == 'self': rel = 'read' content[rel] = Link(url=url, action=method, fields=fields) return content
def get_schema_fields(self, view): field_dist = Field( name=self.dist_param, required=False, description='Max distance in meters between point and elements', type='number', example='XXX') field_point = Field( name=self.point_param, required=False, description='Reference point to compute distance', example='YES MAN', ) return field_dist, field_point
def get_schema_fields(self, view): field_in_bbox = Field( name=self.bbox_param, required=False, description= 'Filter elements contained in bbox formatted like SW-lng,SW-lat,NE-lng,NE-lat', example='1.15,46.1,1.56,47.6') return field_in_bbox,
def get_schema_fields(self, view): field_published = Field( name='published', required=False, description= 'Publication state. If language specified, only language published are filterted. true / false', type='boolean', example='true') return field_published,
def _primative_to_document(data, base_url=None): """ Take Python primatives as returned from parsing JSON content, and return a Core API document. """ if isinstance(data, dict) and data.get('_type') == 'document': # Document meta = _get_dict(data, '_meta') url = _get_string(meta, 'url') url = urlparse.urljoin(base_url, url) title = _get_string(meta, 'title') content = _get_content(data, base_url=url) return Document(url=url, title=title, content=content) if isinstance(data, dict) and data.get('_type') == 'error': # Error meta = _get_dict(data, '_meta') title = _get_string(meta, 'title') content = _get_content(data, base_url=base_url) return Error(title=title, content=content) elif isinstance(data, dict) and data.get('_type') == 'link': # Link url = _get_string(data, 'url') url = urlparse.urljoin(base_url, url) action = _get_string(data, 'action') encoding = _get_string(data, 'encoding') transform = _get_string(data, 'transform') description = _get_string(data, 'description') fields = _get_list(data, 'fields') fields = [ Field(name=_get_string(item, 'name'), required=_get_bool(item, 'required'), location=_get_string(item, 'location'), type=_get_string(item, 'type'), description=_get_string(item, 'description')) for item in fields if isinstance(item, dict) ] return Link(url=url, action=action, encoding=encoding, transform=transform, description=description, fields=fields) elif isinstance(data, dict): # Map content = _get_content(data, base_url=base_url) return Object(content) elif isinstance(data, list): # Array content = [_primative_to_document(item, base_url) for item in data] return Array(content) # String, Integer, Number, Boolean, null. return data
def get_schema_fields(self, view): return (Field( name='format', required=False, location='query', schema=coreschema.String( title=_("Format"), description= _("Set output format (json / geojson). Default: json. Example: geojson" ))), )
def get_schema_fields(self, view): return (Field( name=self.bbox_param, required=False, location='query', schema=coreschema.String( title=_("In bbox"), description=_( 'Filter elements contained in bbox formatted like SW-lng,SW-lat,NE-lng,NE-lat.' 'Example: 1.15,46.1,1.56,47.6'))), )
def _parse_link(data, base_url=None): url = _get_string(data, 'href') url = urlparse.urljoin(base_url, url) if _get_bool(data, 'templated'): fields = [ Field(name, location='path') for name in uritemplate.variables(url) ] else: fields = None return Link(url=url, fields=fields)
def get_schema_fields(self, view): return (Field( name='q', required=False, location='query', schema=coreschema.String( title=_("Query string"), description= _('Search field that returns sites containing data matching the string' ))), )
def get_schema_fields(self, view): return (Field( name='near_trek', required=False, location='query', schema=coreschema.Integer( title=_("Near trek"), description= _("Id of a trek. It will show only the touristics contents related to this trek" ))), )
def get_schema_fields(self, view): return ( Field(name='type', required=False, location='query', schema=coreschema.Integer( title=_("Type"), description=_( "Limit to POIs that contains a specific POI Type"))), Field( name='trek', required=False, location='query', schema=coreschema.Integer( title=_("Trek"), description= _("Id of a trek. It will show only the POIs related to this trek" ))), )
def get_schema_fields(self, view): return ( Field( name=self.dist_param, required=False, location='query', schema=coreschema.Integer( title=_("Distance"), description=_( 'Max distance in meters between point and elements'))), Field( name=self.point_param, required=False, location='query', schema=coreschema.String( title=_("Point"), description= _('Reference point to compute distance LNG,LAT. Example: 1.2563,46.5214' ), )), )
def test_link_encodings(json_codec): doc = Document( content={ 'link': Link(action='post', transform='inplace', fields=[ 'optional', Field('required', required=True, location='path') ]) }) bytes = json_codec.encode(doc, indent=True) assert bytes == b"""{
def doc(): return Document(url='http://example.org/', title='Example', content={ 'integer': 123, 'dict': { 'key': 'value' }, 'list': [1, 2, 3], 'link': Link(url='http://example.org/', fields=[ Field(name='noschema'), Field(name='string_example', schema=String()), Field(name='enum_example', schema=Enum(['a', 'b', 'c'])), ]), 'nested': { 'child': Link(url='http://example.org/123') }, '_type': 'needs escaping' })
def doc(): return Document(url='http://example.org/', title='Example', content={ 'integer': 123, 'dict': { 'key': 'value' }, 'list': [1, 2, 3], 'link': Link(url='http://example.org/', fields=[Field(name='example')]), 'nested': { 'child': Link(url='http://example.org/123') }, '_type': 'needs escaping' })
def get_schema_fields(self, view): return ( Field(name='duration_min', required=False, location='query', schema=coreschema.Number( title=_("Duration min"), description=_('Set minimum duration for a trek'))), Field(name='duration_max', required=False, location='query', schema=coreschema.Number( title=_("Duration max"), description=_('Set maximum duration for a trek'))), Field(name='length_min', required=False, location='query', schema=coreschema.Integer( title=_("Length min"), description=_('Set minimum length for a trek'))), Field(name='length_max', required=False, location='query', schema=coreschema.Integer( title=_("Length max"), description=_('Set maximum length for a trek'))), Field( name='difficulty_min', required=False, location='query', schema=coreschema.Integer( title=_("Difficulty min"), description= _('Set minimum difficulty for a trek. Difficulty usually goes from 1 (very easy) to 4 (difficult)' ))), Field( name='difficulty_max', required=False, location='query', schema=coreschema.Integer( title=_("Difficulty max"), description= _('Set maximum difficulty for a trek. Difficulty usually goes from 1 (very easy) to 4 (difficult)' ))), Field(name='ascent_min', required=False, location='query', schema=coreschema.Integer( title=_("Ascent min"), description=_('Set minimum ascent for a trek'))), Field(name='ascent_max', required=False, location='query', schema=coreschema.Integer( title=_("Ascent max"), description=_('Set maximum ascent for a trek'))), Field( name='city', required=False, location='query', schema=coreschema.String( title=_("City"), description= _('Id of a city to filter by. Can be multiple cities split by a comma. Example: 31006,31555,31017' ))), Field( name='district', required=False, location='query', schema=coreschema.String( title=_("District"), description= _('Id of a district to filter by. Can be multiple districts split by a comma. Example: 2273,2270' ))), Field(name='structure', required=False, location='query', schema=coreschema.Integer( title=_("Structure"), description=_('Id of a structure to filter by'))), Field( name='accessibility', required=False, location='query', schema=coreschema.String( title=_("Accessibility"), description= _('Id of the accessibilities to filter by, separated by commas. Example: 1,2' ))), Field( name='theme', required=False, location='query', schema=coreschema.String( title=_("Theme"), description= _('Id of the themes to filter by, separated by commas. Example: 9,14' ))), Field( name='portal', required=False, location='query', schema=coreschema.String( title=_("Portal"), description= _('Id of the portals to filter by, separated by commas. Example: 3,7' ))), Field(name='route', required=False, location='query', schema=coreschema.Integer( title=_("Route"), description=_('Id of the type of route to filter by'))), Field( name='label', required=False, location='query', schema=coreschema.String( title=_("Label"), description=_( 'Id of the trek label to filter by, separated by commas' ))), Field( name='q', required=False, location='query', schema=coreschema.String( title=_("Query string"), description= _('Search field that returns treks containing data matching the string' ))), )
def get_schema_fields(self, view): field_duration_min = Field( name='duration_min', required=False, description=_('Set minimum duration for a trek'), example=2.5, type='integer' ) field_duration_max = Field( name='duration_max', required=False, description=_('Set maximum duration for a trek'), example=7.5, type='integer' ) field_length_min = Field( name='length_min', required=False, description=_('Set minimum length for a trek'), example=5500, type='integer' ) field_length_max = Field( name='length_max', required=False, description=_('Set maximum length for a trek'), example=18000, type='integer' ) field_difficulty_min = Field( name='difficulty_min', required=False, description=_('Set minimum difficulty for a trek. Difficulty usually goes from 1 (very easy) to 4 (difficult)'), example=3, type='integer' ) field_difficulty_max = Field( name='difficulty_max', required=False, description=_('Set maximum difficulty for a trek. Difficulty usually goes from 1 (very easy) to 4 (difficult)'), example=4, type='integer' ) field_ascent_min = Field( name='ascent_min', required=False, description=_('Set minimum ascent for a trek'), example=250, type='integer' ) field_ascent_max = Field( name='ascent_max', required=False, description=_('Set maximum ascent for a trek'), example=1200, type='integer' ) field_city = Field( name='city', required=False, description=_('Code (pk) of a city to filter by. Can be multiple cities split by a comma'), example='31006,31555,31017', type='string' ) field_district = Field( name='district', required=False, description=_('Pk of a district to filter by. Can be multiple districts split by a comma'), example='2273,2270', type='string' ) field_structure = Field( name='structure', required=False, description=_('Pk of a structure to filter by'), example=4, type='integer' ) field_accessibilities = Field( name='accessibility', required=False, description=_('Pk of the accessibilities to filter by, separated by commas'), example='1,2', type='string' ) field_themes = Field( name='theme', required=False, description=_('Pk of the themes to filter by, separated by commas'), example='9,14', type='string' ) field_portals = Field( name='portal', required=False, description=_('Pk of the portals to filter by, separated by commas'), example='3,7', type='string' ) return field_duration_min, field_duration_max, field_length_min,\ field_length_max, field_difficulty_min, field_difficulty_max, \ field_ascent_min, field_ascent_max, field_city, field_district, \ field_structure, field_accessibilities, field_themes, field_portals
def _primitive_to_document(data, base_url=None): """ Take Python primitives as returned from parsing JSON content, and return a Core API document. """ if isinstance(data, dict) and data.get("_type") == "document": # Document meta = _get_dict(data, "_meta") url = _get_string(meta, "url") url = urlparse.urljoin(base_url, url) title = _get_string(meta, "title") description = _get_string(meta, "description") content = _get_content(data, base_url=url) return Document( url=url, title=title, description=description, media_type="application/coreapi+json", content=content, ) if isinstance(data, dict) and data.get("_type") == "error": # Error meta = _get_dict(data, "_meta") title = _get_string(meta, "title") content = _get_content(data, base_url=base_url) return Error(title=title, content=content) elif isinstance(data, dict) and data.get("_type") == "link": # Link url = _get_string(data, "url") url = urlparse.urljoin(base_url, url) action = _get_string(data, "action") encoding = _get_string(data, "encoding") transform = _get_string(data, "transform") title = _get_string(data, "title") description = _get_string(data, "description") fields = _get_list(data, "fields") fields = [ Field( name=_get_string(item, "name"), required=_get_bool(item, "required"), location=_get_string(item, "location"), schema=_get_schema(item, "schema"), ) for item in fields if isinstance(item, dict) ] return Link( url=url, action=action, encoding=encoding, transform=transform, title=title, description=description, fields=fields, ) elif isinstance(data, dict): # Map content = _get_content(data, base_url=base_url) return Object(content) elif isinstance(data, list): # Array content = [_primitive_to_document(item, base_url) for item in data] return Array(content) # String, Integer, Number, Boolean, null. return data
def build_field(name, location, required=False, description=''): return Field(name, required, location, '', description, '')