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
38 changes: 37 additions & 1 deletion dj_gui_api_server/DJConnector.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,43 @@ def insert_tuple(jwt_payload, schema_name, table_name, tuple_to_insert):
schema_virtual_module = dj.create_virtual_module(schema_name, schema_name)
getattr(schema_virtual_module, table_name).insert1(tuple_to_insert)

"""
Delete a specific tuple based on the restriction given (Can only delete 1 at a time)

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
tuple_to_restrict_by (dict): tuple to restrict the table by to delete

Returns:
None: (Assuming it was valid, otherwise it will raise an error)
"""
@staticmethod
def delete_tuple(jwt_payload, schema_name, table_name, tuple_to_restrict_by):
DJConnector.set_datajoint_config(jwt_payload)

schema_virtual_module = dj.create_virtual_module(schema_name, schema_name)
# Get all the table attributes and create a set
table_attributes = set(getattr(schema_virtual_module, table_name).heading.primary_key + getattr(schema_virtual_module, table_name).heading.secondary_attributes)

# Check to see if the restriction has at least one matching attribute, if not raise an error
if len(table_attributes & tuple_to_restrict_by.keys()) == 0:
raise Exception('Restriction is invalid: None of the attributes match')

# Compute restriction
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:
raise Exception('Nothing to delete')

# All check pass thus proceed to delete
tuple_to_delete.delete_quick()

"""
Method to set credentials for database

Expand All @@ -175,7 +212,6 @@ def set_datajoint_config(jwt_payload):

dj.conn(reset=True)


"""
Helper method for converting snake to camel case

Expand Down
25 changes: 24 additions & 1 deletion dj_gui_api_server/DJGUIAPIServer.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ def get_table_attributes(jwt_payload):
Parameter:
Parameters:
header: (html:GET:Authorization): Must include in format of: bearer <JWT-Token>
body: (html:POST:JSON): {"schemaName": <schema_name>, "tableName": <table_name>, "tuple", "tuple": <tuple_to_insert>} (NOTE: Table name must be in CamalCase)
body: (html:POST:JSON): {"schemaName": <schema_name>, "tableName": <table_name>, "tuple": <tuple_to_insert>} (NOTE: Table name must be in CamalCase)

Returns:
string: "Insert Successful" if the tuple was insert sucessfully
Expand All @@ -191,5 +191,28 @@ def insert_tuple(jwt_payload):
except Exception as e:
return str(e), 500

"""
Route to delete a specific tuple

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

Returns:
string: "Delete Successful" if the tuple was deleted sucessfully
or
string: With error message of why it failed, 500 error
"""
@app.route('/api/delete_tuple', methods=['POST'])
@protected_route
def delete_tuple(jwt_payload):
try:
# Attempt to delete tuple
DJConnector.delete_tuple(jwt_payload, request.json["schemaName"], request.json["tableName"], request.json["restrictionTuple"])
return "Delete Sucessful"
except Exception as e:
return str(e), 500

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