Skip to content

Commit

Permalink
Add introspection (#219)
Browse files Browse the repository at this point in the history
This changes some objectinfo parameters to add introspection to
determine the field types.

Signed-off-by: David Brown <dmlb2000@gmail.com>
  • Loading branch information
dmlb2000 committed Apr 26, 2019
1 parent 7c37cec commit c72dfe4
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 20 deletions.
4 changes: 4 additions & 0 deletions pacifica/metadata/orm/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,11 +301,15 @@ def get_object_info(cls, where_clause=None):
}
where_clause = where_clause if where_clause else {}
# pylint: disable=no-value-for-parameter
field_types = {}
for field_name, field in cls._meta.fields.items():
field_types[field_name] = str(field.field_type)
js_object = {
'callable_name': cls.__module__.split('.').pop().lower(),
'last_changed_date': cls.last_change_date(),
'primary_keys': cls.get_primary_keys(),
'field_list': cls._meta.sorted_field_names,
'field_types': field_types,
'foreign_keys': cls.cls_foreignkeys(),
'related_models': related_model_info,
'related_names': cls.cls_revforeignkeys(),
Expand Down
32 changes: 14 additions & 18 deletions pacifica/metadata/rest/objectinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,20 @@
class ObjectInfoAPI(object):
"""ObjectInfoAPI API."""

lower_obj = {
obj.__module__.split('.').pop().lower(): obj.__name__ for obj in orm_obj_module.ORM_OBJECTS
}
exposed = True

@staticmethod
def get_class_object_from_name(object_class_name):
@classmethod
def get_class_object_from_name(cls, object_class_name):
"""Return a metadata model class for a given class name string."""
if object_class_name is not None:
lower_obj = {
obj.__module__.split('.').pop().lower(): obj.__name__ for obj in orm_obj_module.ORM_OBJECTS
}
try:
myclass = getattr(
orm_obj_module, lower_obj[object_class_name.lower()])
orm_obj_module,
cls.lower_obj[object_class_name.lower()]
)
except KeyError:
myclass = None
else:
Expand All @@ -32,20 +34,17 @@ def get_class_object_from_name(object_class_name):

# CherryPy requires these named methods.
# pylint: disable=invalid-name, protected-access
@staticmethod
@classmethod
@tools.json_out()
@db_connection_decorator
def GET(object_class_name=None, operation=None, **where_clause):
def GET(cls, object_class_name=None, operation=None, **where_clause):
"""
Implement the GET HTTP method.
Returns the json object based on fields passed into kwargs.
"""
if object_class_name == 'list':
lower_obj = {
obj.__name__: obj.__module__.split('.').pop().lower() for obj in orm_obj_module.ORM_OBJECTS
}
return {'available_objects': lower_obj}
return {'available_objects': cls.lower_obj}

myclass = ObjectInfoAPI.get_class_object_from_name(object_class_name)
if operation == 'hashlist':
Expand All @@ -56,13 +55,10 @@ def GET(object_class_name=None, operation=None, **where_clause):
}
else: # operation is None or operation == 'overview':
if myclass is None:
js_object = {}
if object_class_name is not None:
message = "'{0}' is not a valid class object name".format(
object_class_name)
else:
message = 'No object class name found'
raise cherrypy.HTTPError(404, message)
message = "'{0}' is not a valid class object name".format(object_class_name)
raise cherrypy.HTTPError(404, message)
return list(cls.lower_obj.keys())
else:
js_object = myclass.get_object_info(where_clause)
return js_object
4 changes: 2 additions & 2 deletions tests/rest/objectinfo_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,5 @@ def test_bad_objectinfo_api(self):
self.assertTrue(
"'DoesNotExist' is not a valid class object name" in req.text)
req = requests.get('{0}/objectinfo'.format(self.url))
self.assertEqual(req.status_code, 404)
self.assertTrue('No object class name found' in req.text)
self.assertEqual(req.status_code, 200)
self.assertTrue('users' in loads(req.text))

0 comments on commit c72dfe4

Please sign in to comment.