Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 25 additions & 8 deletions dj_gui_api_server/DJConnector.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,14 @@ def attempt_login(database_address, username, password):
jwt_payload (dict): Dictionary containing databaseAddress, username and password strings

Returns:
list<strings>: List of schemas names
list<strings>: List of schemas names in alphabetical order excluding information_schema
"""
@staticmethod
def list_schemas(jwt_payload):
DJConnector.set_datajoint_config(jwt_payload)

# Attempt to connect return true if successful, false is failed
schemas = dj.list_schemas()
return schemas
return [row[0] for row in dj.conn().query('SELECT SCHEMA_NAME FROM information_schema.schemata WHERE SCHEMA_NAME != "information_schema" ORDER BY SCHEMA_NAME')]

"""
List all tables and their type give a schema
Expand Down Expand Up @@ -94,14 +93,14 @@ def list_tables(jwt_payload, schema_name):
table_name (string): Table name under the given schema, must be in camel case

Returns:
list<tuples_as_dicts>: a list of tuples in dict form
list<tuples_rows>: a list of tuples in dict form
"""
@staticmethod
def fetch_tuples(jwt_payload, schema_name, table_name):
DJConnector.set_datajoint_config(jwt_payload)

schema_virtual_module = dj.create_virtual_module(schema_name, schema_name)
return getattr(schema_virtual_module, table_name).fetch(as_dict=True)
return getattr(schema_virtual_module, table_name).fetch().tolist()

"""
Method to get primary and secondary attributes of a table
Expand All @@ -112,14 +111,33 @@ def fetch_tuples(jwt_payload, schema_name, table_name):
table_name (string): Table name under the given schema, must be in camel case

Returns:
dict(primary_keys=[<primary_key_names>], secondary_attributes=[<secondary_key_names])
dict(primary_attributes=[[attribute_name, type, nullable, default, autoincrement]], secondary_attributes=[[attribute_name, type, nullable, default, autoincrement]])
"""
@staticmethod
def get_table_attributes(jwt_payload, schema_name, table_name):
DJConnector.set_datajoint_config(jwt_payload)

schema_virtual_module = dj.create_virtual_module(schema_name, schema_name)
return dict(primary_keys=getattr(schema_virtual_module, table_name).heading.primary_key, secondary_attributes=getattr(schema_virtual_module, table_name).heading.secondary_attributes)
table_attributes = dict(primary_attributes=[], secondary_attributes=[])
for attribute_name, attribute_info in getattr(schema_virtual_module, table_name).heading.attributes.items():
if attribute_info.in_key:
table_attributes['primary_attributes'].append((
attribute_name,
attribute_info.type,
attribute_info.nullable,
attribute_info.default,
attribute_info.autoincrement
))
else:
table_attributes['secondary_attributes'].append((
attribute_name,
attribute_info.type,
attribute_info.nullable,
attribute_info.default,
attribute_info.autoincrement
))

return table_attributes

"""
Get the table definition
Expand Down Expand Up @@ -186,7 +204,6 @@ def delete_tuple(jwt_payload, schema_name, table_name, tuple_to_restrict_by):
tuple_to_delete = getattr(schema_virtual_module, table_name) & tuple_to_restrict_by

# Check if there is only 1 tuple to delete otherwise raise error

if len(tuple_to_delete) > 1:
raise Exception('Cannot delete more than 1 tuple at a time. Please update the restriction accordingly')
elif len(tuple_to_delete) == 0:
Expand Down
4 changes: 2 additions & 2 deletions dj_gui_api_server/DJGUIAPIServer.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def list_tables(jwt_payload):
body: (html:POST:JSON): {"schemaName": <schema_name>, "tableName": <table_name>} (NOTE: Table name must be in CamalCase)

Returns:
dict(table_definition=table_definition): table_definition will be in string format
dict(tuples=tuples): Tuples will be represented as a list
or
string: With error message of why it failed, 500 error
"""
Expand Down Expand Up @@ -156,7 +156,7 @@ def get_table_definition(jwt_payload):
body: (html:POST:JSON): {"schemaName": <schema_name>, "tableName": <table_name>} (NOTE: Table name must be in CamalCase)

Returns:
dict(primary_keys=[<primary_key_names>], secondary_attributes=[<secondary_key_names])
dict(primary_attributes=[tuple(attribute_name, type, nullable, default, autoincrement)], secondary_attributes=[tuple(attribute_name, type, nullable, default, autoincrement)])
or
string: With error message of why it failed, 500 error
"""
Expand Down