def execute(request, func_args): args = [arg for arg in func_args.split('/') if arg] func_name = args[0] # first arg is function name args = args[1:] # second to last args are parameters try: func = GraphFunction.objects.get(name=func_name) except GraphFunction.DoesNotExist: response = draw_message( 'Error! "{0}" is not a valid function!'.format(func_name)) response.status_code = 500 return response except MultipleObjectsReturned: # This should not happen, since the "name" field # has the "unique=True" attribute response = draw_message('Error! Something REALLY wrong happened.') response.status_code = 500 return response # A TypeNotSupported exception will be raised if # an expected_type is not valid. # I won't cover this possibility (I trust you, admin) args = check_types(func.params, args) if args is None: response = draw_message('Error! Invalid params type.') response.status_code = 500 return response cursor = connections[func.database.name].cursor() cursor.callproc('plr_set_display', [':5.0']) # some preliminary stuff res = cursor.fetchall() if not res[0][0].upper() == 'OK': response = draw_message('Error! Something wrong happened.') response.status_code = 500 return response try: cursor.callproc(func_name, args) buff = cursor.fetchall()[0] cursor.callproc('plr_get_raw', buff) buff = cursor.fetchall()[0][0] except DataError: response = draw_message('Error! Invalid data.') response.status_code = 500 return response except ProgrammingError: response = draw_message( 'Error! No function matches the given name and argument types.') response.status_code = 500 return response response = HttpResponse(buff, content_type="image/png", status=200) return response
def execute(request, func_args): args = [arg for arg in func_args.split('/') if arg] func_name = args[0] # first arg is function name args = args[1:] # second to last args are parameters try: func = GraphFunction.objects.get(name=func_name) except GraphFunction.DoesNotExist: response = draw_message('Error! "{0}" is not a valid function!' .format(func_name)) response.status_code = 500 return response except MultipleObjectsReturned: # This should not happen, since the "name" field # has the "unique=True" attribute response = draw_message('Error! Something REALLY wrong happened.') response.status_code = 500 return response # A TypeNotSupported exception will be raised if # an expected_type is not valid. # I won't cover this possibility (I trust you, admin) args = check_types(func.params, args) if args is None: response = draw_message('Error! Invalid params type.') response.status_code = 500 return response cursor = connections[func.database.name].cursor() cursor.callproc('plr_set_display', [':5.0']) # some preliminary stuff res = cursor.fetchall() if not res[0][0].upper() == 'OK': response = draw_message('Error! Something wrong happened.') response.status_code = 500 return response try: cursor.callproc(func_name, args) buff = cursor.fetchall()[0] cursor.callproc('plr_get_raw', buff) buff = cursor.fetchall()[0][0] except DataError: response = draw_message('Error! Invalid data.') response.status_code = 500 return response except ProgrammingError: response = draw_message( 'Error! No function matches the given name and argument types.') response.status_code = 500 return response response = HttpResponse(buff, content_type="image/png", status=200) return response
def test_check_types(self): i = ['42', 'c', 'test', '42.0'] self.assertEqual(check_types(self.func1.params, i), [42, 'c', 'test', 42.0]) i = ['c', 'c', 'test', '42.0'] self.assertEqual(check_types(self.func1.params, i), None) i = ['42', '42', 'test', '42.0'] self.assertEqual(check_types(self.func1.params, i), None) i = ['42', 'c', 23, '42.0'] self.assertEqual(check_types(self.func1.params, i), [42, 'c', 23, 42.0]) i = ['42', 'c', 'test', 'test'] self.assertEqual(check_types(self.func1.params, i), None) i = ['42.0', '1.2,3.4,5.6', '1,2,3,4', '"lorem","ipsum","dolor,sit"'] exp_out = [ 42.0, [1.2, 3.4, 5.6], [1, 2, 3, 4], ['lorem', 'ipsum', 'dolor,sit'] ] self.assertEqual(check_types(self.func2.params, i), exp_out) self.assertIsNone(check_type('float_array', '1.2a,2.3')) self.assertEqual(check_type('float_array', '12, 23.0'), [12.0, 23.0]) self.assertIsNone(check_type('int_array', '1,2,3a')) self.assertIsNone(check_type('char_array', 'a,,b')) self.assertIsNone(check_type('string_array', '"asd",asd,"asd"')) self.assertEqual(check_type('string_array', r'"lorem\"ipsum","dolor"'), ['lorem"ipsum', 'dolor']) self.assertEqual( check_type('string_array', '"lorem, ipsum","dolor","sit123","amet",'), ['lorem, ipsum', 'dolor', 'sit123', 'amet']) self.assertIsNone(check_type('string_array', '"lorem",ipsum,"dolor"')) self.assertIsNone(check_type('string_array', '"lor"em,"ipsum"')) self.assertEqual(check_type('string_array', '"lorem ipsum"'), ['lorem ipsum']) self.assertIsNone(check_type('string_array', 'lorem,ipsum'))
def test_check_types(self): i = ['42', 'c', 'test', '42.0'] self.assertEqual(check_types(self.func1.params, i), [42, 'c', 'test', 42.0]) i = ['c', 'c', 'test', '42.0'] self.assertEqual(check_types(self.func1.params, i), None) i = ['42', '42', 'test', '42.0'] self.assertEqual(check_types(self.func1.params, i), None) i = ['42', 'c', 23, '42.0'] self.assertEqual(check_types(self.func1.params, i), [42, 'c', 23, 42.0]) i = ['42', 'c', 'test', 'test'] self.assertEqual(check_types(self.func1.params, i), None) i = ['42.0', '1.2,3.4,5.6', '1,2,3,4', '"lorem","ipsum","dolor,sit"'] exp_out = [42.0, [1.2, 3.4, 5.6], [1, 2, 3, 4], ['lorem', 'ipsum', 'dolor,sit']] self.assertEqual(check_types(self.func2.params, i), exp_out) self.assertIsNone(check_type('float_array', '1.2a,2.3')) self.assertEqual(check_type('float_array', '12, 23.0'), [12.0, 23.0]) self.assertIsNone(check_type('int_array', '1,2,3a')) self.assertIsNone(check_type('char_array', 'a,,b')) self.assertIsNone(check_type('string_array', '"asd",asd,"asd"')) self.assertEqual(check_type('string_array', r'"lorem\"ipsum","dolor"'), ['lorem"ipsum', 'dolor']) self.assertEqual(check_type('string_array', '"lorem, ipsum","dolor","sit123","amet",'), ['lorem, ipsum', 'dolor', 'sit123', 'amet']) self.assertIsNone(check_type('string_array', '"lorem",ipsum,"dolor"')) self.assertIsNone(check_type('string_array', '"lor"em,"ipsum"')) self.assertEqual(check_type('string_array', '"lorem ipsum"'), ['lorem ipsum']) self.assertIsNone(check_type('string_array', 'lorem,ipsum'))