Skip to content

Commit 7af1b4e

Browse files
authored
Merge pull request #26 from Synicix/list_schema_ensure_order
Change list_schema to direct query with ORDER BY to ensure alphabetic…
2 parents deb1848 + 53a5cf2 commit 7af1b4e

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

dj_gui_api_server/DJConnector.py

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,14 @@ def attempt_login(database_address, username, password):
3333
jwt_payload (dict): Dictionary containing databaseAddress, username and password strings
3434
3535
Returns:
36-
list<strings>: List of schemas names
36+
list<strings>: List of schemas names in alphabetical order excluding information_schema
3737
"""
3838
@staticmethod
3939
def list_schemas(jwt_payload):
4040
DJConnector.set_datajoint_config(jwt_payload)
4141

4242
# Attempt to connect return true if successful, false is failed
43-
schemas = dj.list_schemas()
44-
return schemas
43+
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')]
4544

4645
"""
4746
List all tables and their type give a schema
@@ -94,14 +93,14 @@ def list_tables(jwt_payload, schema_name):
9493
table_name (string): Table name under the given schema, must be in camel case
9594
9695
Returns:
97-
list<tuples_as_dicts>: a list of tuples in dict form
96+
list<tuples_rows>: a list of tuples in dict form
9897
"""
9998
@staticmethod
10099
def fetch_tuples(jwt_payload, schema_name, table_name):
101100
DJConnector.set_datajoint_config(jwt_payload)
102101

103102
schema_virtual_module = dj.create_virtual_module(schema_name, schema_name)
104-
return getattr(schema_virtual_module, table_name).fetch(as_dict=True)
103+
return getattr(schema_virtual_module, table_name).fetch().tolist()
105104

106105
"""
107106
Method to get primary and secondary attributes of a table
@@ -112,14 +111,33 @@ def fetch_tuples(jwt_payload, schema_name, table_name):
112111
table_name (string): Table name under the given schema, must be in camel case
113112
114113
Returns:
115-
dict(primary_keys=[<primary_key_names>], secondary_attributes=[<secondary_key_names])
114+
dict(primary_attributes=[[attribute_name, type, nullable, default, autoincrement]], secondary_attributes=[[attribute_name, type, nullable, default, autoincrement]])
116115
"""
117116
@staticmethod
118117
def get_table_attributes(jwt_payload, schema_name, table_name):
119118
DJConnector.set_datajoint_config(jwt_payload)
120119

121120
schema_virtual_module = dj.create_virtual_module(schema_name, schema_name)
122-
return dict(primary_keys=getattr(schema_virtual_module, table_name).heading.primary_key, secondary_attributes=getattr(schema_virtual_module, table_name).heading.secondary_attributes)
121+
table_attributes = dict(primary_attributes=[], secondary_attributes=[])
122+
for attribute_name, attribute_info in getattr(schema_virtual_module, table_name).heading.attributes.items():
123+
if attribute_info.in_key:
124+
table_attributes['primary_attributes'].append((
125+
attribute_name,
126+
attribute_info.type,
127+
attribute_info.nullable,
128+
attribute_info.default,
129+
attribute_info.autoincrement
130+
))
131+
else:
132+
table_attributes['secondary_attributes'].append((
133+
attribute_name,
134+
attribute_info.type,
135+
attribute_info.nullable,
136+
attribute_info.default,
137+
attribute_info.autoincrement
138+
))
139+
140+
return table_attributes
123141

124142
"""
125143
Get the table definition
@@ -186,7 +204,6 @@ def delete_tuple(jwt_payload, schema_name, table_name, tuple_to_restrict_by):
186204
tuple_to_delete = getattr(schema_virtual_module, table_name) & tuple_to_restrict_by
187205

188206
# Check if there is only 1 tuple to delete otherwise raise error
189-
190207
if len(tuple_to_delete) > 1:
191208
raise Exception('Cannot delete more than 1 tuple at a time. Please update the restriction accordingly')
192209
elif len(tuple_to_delete) == 0:

dj_gui_api_server/DJGUIAPIServer.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ def list_tables(jwt_payload):
114114
body: (html:POST:JSON): {"schemaName": <schema_name>, "tableName": <table_name>} (NOTE: Table name must be in CamalCase)
115115
116116
Returns:
117-
dict(table_definition=table_definition): table_definition will be in string format
117+
dict(tuples=tuples): Tuples will be represented as a list
118118
or
119119
string: With error message of why it failed, 500 error
120120
"""
@@ -156,7 +156,7 @@ def get_table_definition(jwt_payload):
156156
body: (html:POST:JSON): {"schemaName": <schema_name>, "tableName": <table_name>} (NOTE: Table name must be in CamalCase)
157157
158158
Returns:
159-
dict(primary_keys=[<primary_key_names>], secondary_attributes=[<secondary_key_names])
159+
dict(primary_attributes=[tuple(attribute_name, type, nullable, default, autoincrement)], secondary_attributes=[tuple(attribute_name, type, nullable, default, autoincrement)])
160160
or
161161
string: With error message of why it failed, 500 error
162162
"""

0 commit comments

Comments
 (0)