def _upgrade_2019_09_05_specification(specification): from flask_potion import fields from web.server.api.aqt.api_models import GranularityResource granularity_converter = fields.ToOne(GranularityResource).converter for query in list(specification['queries'].values()): is_advanced_query_item = query['isAdvancedQueryItem'] settings = specification['settings'][query['settingId']] grouping_settings_map = settings['groupBySettings']['groupings'] if is_advanced_query_item: # find the time granularity grouping (if there is one). There # can only be up to one. granularity = None for aqt_group in query['advancedGroups']: if aqt_group['type'] == 'GRANULARITY': granularity = granularity_converter(aqt_group['item']) break if granularity: for group_settings in list(grouping_settings_map.values()): # check if a group setting has an invalid displayValueFormat if (group_settings['id'] == 'timestamp' and group_settings['displayValueFormat'] == 'DEFAULT'): group_settings['displayValueFormat'] = granularity.id break upgraded_specification = related.to_model(DashboardSpecification_20190911, specification) upgraded_specification.version = VERSION_2019_09_11 return related.to_dict(upgraded_specification)
def test_property_key(self): class Foo(ModelResource): class Schema: name = fields.String() class Meta: natural_key = 'name' manager = MemoryManager model = 'foo' self.api.add_resource(Foo) foo_field = fields.ToOne(Foo) self.assertJSONEqual(FOO_REFERENCE, foo_field.response) self.assertJSONEqual( { "anyOf": [FOO_REFERENCE, { "type": "integer" }, { "type": "string" }] }, foo_field.request, ) response = self.client.post('/api/foo', data={"name": "Jane"}) self.assert200(response) response = self.client.post('/api/foo', data={"name": "John"}) self.assert200(response) self.assertEqual({'id': 2, 'name': 'John'}, foo_field.convert(2)) self.assertEqual({'id': 1, 'name': 'Jane'}, foo_field.convert('Jane')) with self.assertRaises(ValidationError): foo_field.convert(['John']) with self.assertRaises(ItemNotFound) as cx: foo_field.convert('Joanne') self.assertEqual( { "message": "Not Found", "status": 404, "item": { "$type": "foo", "$where": { "name": "Joanne" } }, }, cx.exception.as_dict(), )
def test_simple_key(self): class Foo(ModelResource): class Meta: key_converters = [RefKey()] model = 'foo' manager = MemoryManager class Schema: inception = fields.ToOne('foo') self.api.add_resource(Foo) foo_field = fields.ToOne('foo') self.assertJSONEqual(FOO_REFERENCE, foo_field.response) self.assertJSONEqual(FOO_REFERENCE, foo_field.request)
class Schema(object): textId = fields.String(description='The string id of an indicator', attribute='text_id') text = fields.String(description='Indicator description text', attribute='text') groupId = fields.Integer(description='Indicator group ID', attribute='group_id', io='w') indicatorGroup = fields.ToOne('indicator_groups', attribute='indicator_group', io='r') constituents = fields.ToMany( 'web.server.api.indicator_api_models.IndicatorResource')
class DrinkResource(ModelResource): recipe = ItemAttributeRoute( fields.Array( fields.Object( properties={ "ingredient": fields.ToOne("ingredient"), "volume": fields.Number() }))) class Meta: name = "drink" model = name manager = MemoryManager id_field_class = fields.Integer include_type = True class Schema: name = fields.String()
class Schema: author = fields.ToOne('user')
class Schema: article = fields.ToOne('article') author = fields.ToOne('user')
def _upgrade_2019_05_24_specification(specification): from flask_potion import fields from config.aggregation import DIMENSION_SLICES from web.server.api.aqt.api_models import DimensionResource dimension_converter = fields.ToOne(DimensionResource).converter # First, some pre-processing: any settings objects that are not # used by a layout_item can not have a GroupBySettings object generated for # them (because we need access to query data in order to create that object). # So we should remove those settings objects from the dashbaord for settings_obj in list(specification['settings'].values()): settings_id = settings_obj['id'] settings_is_used = False # check if this settings_id is found in any layout item for layout_item in list(specification['items'].values()): if layout_item['settingId'] == settings_id: settings_is_used = True break if not settings_is_used: del specification['settings'][settings_id] # Now, for each layout item, we need to create a groupBySettings object. # To do that we will have to look at its selected groupings from its # query object, and create a groupBy setting object from that. for layout_item in list(specification['items'].values()): query_id = layout_item['queryId'] setting_id = layout_item['settingId'] query = specification['queries'][query_id] settings = specification['settings'][setting_id] is_advanced_query_item = layout_item['isAdvancedQueryItem'] groupings = {} if is_advanced_query_item: # Set up GroupBySettings with AQT groupings advanced_groups = query['advancedGroups'] has_string_dimension = False for group in advanced_groups: if group['type'] == 'GRANULARITY': groupings['timestamp'] = { 'id': 'timestamp', 'type': 'DATE', 'displayValueFormat': 'DEFAULT', 'label': None, } elif group['type'] == 'GROUPING_DIMENSION': has_string_dimension = True dimension = dimension_converter(group['item']['dimension']) groupings[dimension.id] = { 'id': dimension.id, 'type': 'STRING', 'displayValueFormat': 'DEFAULT', 'label': None, } # if we have no string dimension, create a default Nation grouping if not has_string_dimension: groupings['nation'] = { 'id': 'nation', 'type': 'STRING', 'displayValueFormat': 'DEFAULT', 'label': None, } else: # Set up GroupBySettings with SQT's groupBy selection group_by_id = query['groupBy'] dimension_ids = [group_by_id] if group_by_id in DIMENSION_SLICES: dimension_ids += DIMENSION_SLICES[group_by_id] for dimension_id in dimension_ids: groupings[dimension_id] = { 'id': dimension_id, 'type': 'STRING', 'label': None, 'displayValueFormat': 'DEFAULT', } settings['groupBySettings'] = {'groupings': groupings} upgraded_specification = related.to_model(DashboardSpecification_20190608, specification) upgraded_specification.version = VERSION_2019_06_08 return related.to_dict(upgraded_specification)
class Schema: belongs_to = fields.ToOne('user', nullable=True)
class Schema: owner = fields.ToOne('user')
class Schema: network = fields.ToOne('networks') gateway = fields.ToOne('gateways') email = fields.Email() password = fields.String(min_length=6, max_length=20)
class Schema: network = fields.ToOne('networks') gateway = fields.ToOne('gateways')
class Schema: province = fields.ToOne('province')
class Schema: district = fields.ToOne('district')
class Schema: timing = fields.DateTimeString() session = fields.ToOne('session')
class Schema: id = fields.String(min_length=3, max_length=20) network = fields.ToOne('networks') title = fields.String(min_length=3) login_ask_name = fields.Boolean(default=False) login_require_name = fields.Boolean(default=False)
class Schema: network = fields.ToOne('networks') gateway = fields.ToOne('gateways') available_actions = fields.String() time_left = fields.Integer()
class Schema: type = fields.ToOne('type')
class Schema: network = fields.ToOne('networks') gateway = fields.ToOne('gateways') currency = fields.ToOne('currencies')
def test_properties_key(self): class Foo(ModelResource): class Schema: first_name = fields.String() last_name = fields.String() class Meta: natural_key = ['first_name', 'last_name'] manager = MemoryManager model = 'foo' name = 'foo' self.api.add_resource(Foo) foo_field = fields.ToOne(Foo, nullable=True) self.assertJSONEqual(FOO_REFERENCE_NULLABLE, foo_field.response) self.assertJSONEqual( { "anyOf": [ FOO_REFERENCE, { "type": "integer" }, { "type": "array", "items": [{ "type": "string" }, { "type": "string" }], "additionalItems": False }, { "type": "null" } ] }, foo_field.request) response = self.client.post('/api/foo', data={ "first_name": "Jane", "last_name": "Doe" }) self.assert200(response) response = self.client.post('/api/foo', data={ "first_name": "John", "last_name": "Doe" }) self.assert200(response) self.assertEqual({ 'first_name': 'Jane', 'id': 1, 'last_name': 'Doe' }, foo_field.convert(1)) self.assertEqual({ 'first_name': 'John', 'id': 2, 'last_name': 'Doe' }, foo_field.convert(['John', 'Doe'])) self.assertEqual({ 'first_name': 'John', 'id': 2, 'last_name': 'Doe' }, foo_field.convert({"$ref": "/api/foo/2"})) with self.assertRaises(ItemNotFound) as cx: foo_field.convert(['John', 'Joe']) self.assertEqual( { "message": "Not Found", "status": 404, "item": { "$type": "foo", "$where": { "first_name": "John", "last_name": "Joe" } } }, cx.exception.as_dict()) with self.assertRaises(ValidationError): foo_field.convert(['John', None])
class Schema: book = fields.ToOne('book') store = fields.ToOne('book_store')
class Schema: inception = fields.ToOne('foo')
class Schema: author = fields.ToOne('user', nullable=True)
class Schema: video = fields.ToOne('app.resources.VideoResource')
class Schema: name = fields.String() mother = fields.ToOne('person', nullable=True)
class Schema: belongs_to = fields.ToOne('user')