Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
19 changes: 19 additions & 0 deletions dj_gui_api_server/DJConnector.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,24 @@ def fetch_tuples(jwt_payload, schema_name, table_name):

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

"""
Method to get primary and secondary attributes of a table

Parameters:
jwt_payload (dict): Dictionary containing databaseAddress, username and password strings
schema_name (string): Schema name where to find the table under
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])
"""
@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)

"""
Method to set credentials for database
Expand All @@ -119,6 +137,7 @@ def set_datajoint_config(jwt_payload):
dj.config['database.password'] = jwt_payload['password']

dj.conn(reset=True)

"""
Helper method for converting snake to camel case

Expand Down
20 changes: 20 additions & 0 deletions dj_gui_api_server/DJGUIAPIServer.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,5 +127,25 @@ def fetch_tuples(jwt_payload):
except Exception as e:
return str(e), 500

"""
Route to fetch all tuples for a given table

Parameters:
header: (html:GET:Authorization): Must include in format of: bearer <JWT-Token>
body: (html:POST:JSON): {"schemaName": <schema_name>, "tableName": <table_name>} (NOTE: Table name must be in CamalCase)

Returns:
dict(tuples=[tuples_as_dicts])
or
dict(error=<error_message>): With error message of why it failed
"""
@app.route('/api/get_table_attributes', methods=['POST'])
@protected_route
def get_table_attributes(jwt_payload):
try:
return DJConnector.get_table_attributes(jwt_payload, request.json["schemaName"], request.json["tableName"])
except Exception as e:
return str(e), 500

if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)