Skip to content

Commit

Permalink
Merge pull request #12 from ral-facilities/1_add_patch_http_method_fo…
Browse files Browse the repository at this point in the history
…r_non_id_endpoints

Add patch method to entity endpoints
  • Loading branch information
keiranjprice101 authored Jun 20, 2019
2 parents e58f162 + 9870458 commit 2cfd027
Show file tree
Hide file tree
Showing 40 changed files with 111 additions and 80 deletions.
29 changes: 28 additions & 1 deletion common/database_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from sqlalchemy.orm import sessionmaker

from common.constants import Constants
from common.exceptions import MissingRecordError, BadFilterError
from common.exceptions import MissingRecordError, BadFilterError, BadRequestError


def get_record_by_id(table, id):
Expand Down Expand Up @@ -173,3 +173,30 @@ def get_first_filtered_row(table, filters):
:return: the first row matching the filter
"""
return get_rows_by_filter(table, filters)[0]


def patch_entities(table, json_list):
"""
Update one or more rows in the given table, from the given list containing json. Each entity must contain its ID
:param table: The table of the entities
:param json_list: the list of updated values or a dictionary
:return: The list of updated rows.
"""
results = []
if type(json_list) is dict:
for key in json_list:
if key.upper() == "ID":
update_row_from_id(table, json_list[key], json_list)
result = get_row_by_id(table, json_list[key])
results.append(result)
else:
for entity in json_list:
for key in entity:
if key.upper() == "ID":
update_row_from_id(table, entity[key], entity)
result = get_row_by_id(table, entity[key])
results.append(result)
if len(results) == 0:
raise BadRequestError()

return results
4 changes: 4 additions & 0 deletions common/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,7 @@ class BadFilterError(ApiError):

class AuthenticationError(ApiError):
pass


class BadRequestError(ApiError):
pass
8 changes: 5 additions & 3 deletions common/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from sqlalchemy.exc import IntegrityError

from common.database_helpers import get_icat_db_session
from common.exceptions import MissingRecordError, BadFilterError, AuthenticationError
from common.exceptions import MissingRecordError, BadFilterError, AuthenticationError, BadRequestError
from common.models.db_models import SESSION


Expand Down Expand Up @@ -52,9 +52,11 @@ def wrapper_gets_records(*args, **kwargs):
return "Bad request", 400
except TypeError:
return "Bad request", 400
except IntegrityError as e:
except IntegrityError:
return "Bad request", 400

except BadRequestError:
return "Bad request", 400

return wrapper_gets_records


Expand Down
4 changes: 2 additions & 2 deletions src/resources/entities/applications_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from flask_restful import Resource

from common.database_helpers import get_row_by_id, delete_row_by_id, update_row_from_id, get_rows_by_filter, \
get_filtered_row_count, get_first_filtered_row, create_row_from_json
get_filtered_row_count, get_first_filtered_row, create_row_from_json, patch_entities
from common.helpers import requires_session_id, queries_records, get_filters_from_query_string


Expand All @@ -22,7 +22,7 @@ def post(self):
@requires_session_id
@queries_records
def patch(self):
pass
return list(map(lambda x: x.to_dict(), patch_entities(APPLICATION, request.json))), 200


class ApplicationsWithID(Resource):
Expand Down
4 changes: 2 additions & 2 deletions src/resources/entities/datacollection_datafiles_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from flask_restful import Resource

from common.database_helpers import get_row_by_id, delete_row_by_id, update_row_from_id, get_rows_by_filter, \
get_filtered_row_count, get_first_filtered_row, create_row_from_json
get_filtered_row_count, get_first_filtered_row, create_row_from_json, patch_entities
from common.helpers import requires_session_id, queries_records, get_filters_from_query_string
from common.models.db_models import DATACOLLECTIONDATAFILE

Expand All @@ -22,7 +22,7 @@ def post(self):
@requires_session_id
@queries_records
def patch(self):
pass
return list(map(lambda x: x.to_dict(), patch_entities(DATACOLLECTIONDATAFILE, request.json))), 200


class DataCollectionDatafilesWithID(Resource):
Expand Down
4 changes: 2 additions & 2 deletions src/resources/entities/datacollection_datasets_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from flask_restful import Resource

from common.database_helpers import get_row_by_id, delete_row_by_id, update_row_from_id, get_rows_by_filter, \
get_filtered_row_count, get_first_filtered_row, create_row_from_json
get_filtered_row_count, get_first_filtered_row, create_row_from_json, patch_entities
from common.helpers import requires_session_id, queries_records, get_filters_from_query_string
from common.models.db_models import DATACOLLECTIONDATASET

Expand All @@ -22,7 +22,7 @@ def post(self):
@requires_session_id
@queries_records
def patch(self):
pass
return list(map(lambda x: x.to_dict(), patch_entities(DATACOLLECTIONDATASET, request.json))), 200


class DataCollectionDatasetsWithID(Resource):
Expand Down
4 changes: 2 additions & 2 deletions src/resources/entities/datacollection_parameters_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from flask_restful import Resource

from common.database_helpers import get_row_by_id, delete_row_by_id, update_row_from_id, get_rows_by_filter, \
get_filtered_row_count, get_first_filtered_row, create_row_from_json
get_filtered_row_count, get_first_filtered_row, create_row_from_json, patch_entities
from common.helpers import requires_session_id, queries_records, get_filters_from_query_string
from common.models.db_models import DATACOLLECTIONPARAMETER

Expand All @@ -22,7 +22,7 @@ def post(self):
@requires_session_id
@queries_records
def patch(self):
pass
return list(map(lambda x: x.to_dict(), patch_entities(DATACOLLECTIONPARAMETER, request.json))), 200


class DataCollectionParametersWithID(Resource):
Expand Down
4 changes: 2 additions & 2 deletions src/resources/entities/datacollections_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from flask_restful import Resource

from common.database_helpers import get_row_by_id, delete_row_by_id, update_row_from_id, get_rows_by_filter, \
get_filtered_row_count, get_first_filtered_row, create_row_from_json
get_filtered_row_count, get_first_filtered_row, create_row_from_json, patch_entities
from common.helpers import requires_session_id, queries_records, get_filters_from_query_string
from common.models.db_models import DATACOLLECTION

Expand All @@ -22,7 +22,7 @@ def post(self):
@requires_session_id
@queries_records
def patch(self):
pass
return list(map(lambda x: x.to_dict(), patch_entities(DATACOLLECTION, request.json))), 200


class DataCollectionsWithID(Resource):
Expand Down
4 changes: 2 additions & 2 deletions src/resources/entities/datafile_formats_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from flask_restful import Resource

from common.database_helpers import get_row_by_id, delete_row_by_id, update_row_from_id, get_rows_by_filter, \
get_filtered_row_count, get_first_filtered_row, create_row_from_json
get_filtered_row_count, get_first_filtered_row, create_row_from_json, patch_entities
from common.helpers import requires_session_id, queries_records, get_filters_from_query_string
from common.models.db_models import DATAFILEFORMAT

Expand All @@ -22,7 +22,7 @@ def post(self):
@requires_session_id
@queries_records
def patch(self):
pass
return list(map(lambda x: x.to_dict(), patch_entities(DATAFILEFORMAT, request.json))), 200


class DatafileFormatsWithID(Resource):
Expand Down
5 changes: 2 additions & 3 deletions src/resources/entities/datafile_parameters_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from flask_restful import Resource

from common.database_helpers import get_row_by_id, delete_row_by_id, update_row_from_id, get_rows_by_filter, \
get_filtered_row_count, get_first_filtered_row, create_row_from_json
get_filtered_row_count, get_first_filtered_row, create_row_from_json, patch_entities
from common.helpers import requires_session_id, queries_records, get_filters_from_query_string
from common.models.db_models import DATAFILEPARAMETER

Expand All @@ -22,8 +22,7 @@ def post(self):
@requires_session_id
@queries_records
def patch(self):
pass

return list(map(lambda x: x.to_dict(), patch_entities(DATAFILEPARAMETER, request.json))), 200

class DatafileParametersWithID(Resource):
@requires_session_id
Expand Down
4 changes: 2 additions & 2 deletions src/resources/entities/datafiles_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from flask_restful import Resource

from common.database_helpers import get_row_by_id, delete_row_by_id, update_row_from_id, get_rows_by_filter, \
get_filtered_row_count, get_first_filtered_row, create_row_from_json
get_filtered_row_count, get_first_filtered_row, create_row_from_json, patch_entities
from common.helpers import requires_session_id, queries_records, get_filters_from_query_string
from common.models.db_models import DATAFILE

Expand All @@ -22,7 +22,7 @@ def post(self):
@requires_session_id
@queries_records
def patch(self):
pass
return list(map(lambda x: x.to_dict(), patch_entities(DATAFILE, request.json))), 200


class DatafilesWithID(Resource):
Expand Down
4 changes: 2 additions & 2 deletions src/resources/entities/dataset_type_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from flask_restful import Resource

from common.database_helpers import get_row_by_id, delete_row_by_id, update_row_from_id, get_rows_by_filter, \
get_filtered_row_count, get_first_filtered_row, create_row_from_json
get_filtered_row_count, get_first_filtered_row, create_row_from_json, patch_entities
from common.helpers import requires_session_id, queries_records, get_filters_from_query_string
from common.models.db_models import DATASETTYPE

Expand All @@ -22,7 +22,7 @@ def post(self):
@requires_session_id
@queries_records
def patch(self):
pass
return list(map(lambda x: x.to_dict(), patch_entities(DATASETTYPE, request.json))), 200


class DatasetTypesWithID(Resource):
Expand Down
4 changes: 2 additions & 2 deletions src/resources/entities/datasets_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from flask_restful import Resource

from common.database_helpers import get_row_by_id, delete_row_by_id, update_row_from_id, get_rows_by_filter, \
get_filtered_row_count, get_first_filtered_row, create_row_from_json
get_filtered_row_count, get_first_filtered_row, create_row_from_json, patch_entities
from common.helpers import requires_session_id, queries_records, get_filters_from_query_string
from common.models.db_models import DATASET

Expand All @@ -22,7 +22,7 @@ def post(self):
@requires_session_id
@queries_records
def patch(self):
pass
return list(map(lambda x: x.to_dict(), patch_entities(DATASET, request.json))), 200


class DatasetsWithID(Resource):
Expand Down
4 changes: 2 additions & 2 deletions src/resources/entities/facilities_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from flask_restful import Resource

from common.database_helpers import get_row_by_id, delete_row_by_id, update_row_from_id, get_rows_by_filter, \
get_filtered_row_count, get_first_filtered_row, create_row_from_json
get_filtered_row_count, get_first_filtered_row, create_row_from_json, patch_entities
from common.helpers import requires_session_id, queries_records, get_filters_from_query_string
from common.models.db_models import FACILITY

Expand All @@ -22,7 +22,7 @@ def post(self):
@requires_session_id
@queries_records
def patch(self):
pass
return list(map(lambda x: x.to_dict(), patch_entities(FACILITY, request.json))), 200


class FacilitiesWithID(Resource):
Expand Down
4 changes: 2 additions & 2 deletions src/resources/entities/facility_cycles_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from flask_restful import Resource

from common.database_helpers import get_row_by_id, delete_row_by_id, update_row_from_id, get_rows_by_filter, \
get_filtered_row_count, get_first_filtered_row, create_row_from_json
get_filtered_row_count, get_first_filtered_row, create_row_from_json, patch_entities
from common.helpers import requires_session_id, queries_records, get_filters_from_query_string
from common.models.db_models import FACILITYCYCLE

Expand All @@ -22,7 +22,7 @@ def post(self):
@requires_session_id
@queries_records
def patch(self):
pass
return list(map(lambda x: x.to_dict(), patch_entities(FACILITYCYCLE, request.json))), 200


class FacilityCyclesWithID(Resource):
Expand Down
4 changes: 2 additions & 2 deletions src/resources/entities/groupings_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from flask_restful import Resource

from common.database_helpers import get_row_by_id, delete_row_by_id, update_row_from_id, get_rows_by_filter, \
get_filtered_row_count, get_first_filtered_row, create_row_from_json
get_filtered_row_count, get_first_filtered_row, create_row_from_json, patch_entities
from common.helpers import requires_session_id, queries_records, get_filters_from_query_string
from common.models.db_models import GROUPING

Expand All @@ -22,7 +22,7 @@ def post(self):
@requires_session_id
@queries_records
def patch(self):
pass
return list(map(lambda x: x.to_dict(), patch_entities(GROUPING, request.json))), 200


class GroupingsWithID(Resource):
Expand Down
5 changes: 2 additions & 3 deletions src/resources/entities/instrument_scientists_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from flask_restful import Resource

from common.database_helpers import get_row_by_id, delete_row_by_id, update_row_from_id, get_rows_by_filter, \
get_filtered_row_count, get_first_filtered_row, create_row_from_json
get_filtered_row_count, get_first_filtered_row, create_row_from_json, patch_entities
from common.helpers import requires_session_id, queries_records, get_filters_from_query_string
from common.models.db_models import INSTRUMENTSCIENTIST

Expand All @@ -22,8 +22,7 @@ def post(self):
@requires_session_id
@queries_records
def patch(self):
pass

return list(map(lambda x: x.to_dict(), patch_entities(INSTRUMENTSCIENTIST, request.json))), 200

class InstrumentScientistsWithID(Resource):
@requires_session_id
Expand Down
4 changes: 2 additions & 2 deletions src/resources/entities/instruments_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from flask_restful import Resource

from common.database_helpers import get_row_by_id, delete_row_by_id, update_row_from_id, get_rows_by_filter, \
get_filtered_row_count, get_first_filtered_row, create_row_from_json
get_filtered_row_count, get_first_filtered_row, create_row_from_json, patch_entities
from common.helpers import requires_session_id, queries_records, get_filters_from_query_string
from common.models.db_models import INSTRUMENT

Expand All @@ -22,7 +22,7 @@ def post(self):
@requires_session_id
@queries_records
def patch(self):
pass
return list(map(lambda x: x.to_dict(), patch_entities(INSTRUMENT, request.json))), 200


class InstrumentsWithID(Resource):
Expand Down
4 changes: 2 additions & 2 deletions src/resources/entities/investigation_groups_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from flask_restful import Resource

from common.database_helpers import get_row_by_id, delete_row_by_id, update_row_from_id, get_rows_by_filter, \
get_filtered_row_count, get_first_filtered_row, create_row_from_json
get_filtered_row_count, get_first_filtered_row, create_row_from_json, patch_entities
from common.helpers import requires_session_id, queries_records, get_filters_from_query_string
from common.models.db_models import INVESTIGATIONGROUP

Expand All @@ -22,7 +22,7 @@ def post(self):
@requires_session_id
@queries_records
def patch(self):
pass
return list(map(lambda x: x.to_dict(), patch_entities(INVESTIGATIONGROUP, request.json))), 200


class InvestigationGroupsWithID(Resource):
Expand Down
4 changes: 2 additions & 2 deletions src/resources/entities/investigation_instruments_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from flask_restful import Resource

from common.database_helpers import get_row_by_id, delete_row_by_id, update_row_from_id, get_rows_by_filter, \
get_filtered_row_count, get_first_filtered_row, create_row_from_json
get_filtered_row_count, get_first_filtered_row, create_row_from_json, patch_entities
from common.helpers import requires_session_id, queries_records, get_filters_from_query_string
from common.models.db_models import INVESTIGATIONINSTRUMENT

Expand All @@ -22,7 +22,7 @@ def post(self):
@requires_session_id
@queries_records
def patch(self):
pass
return list(map(lambda x: x.to_dict(), patch_entities(INVESTIGATIONINSTRUMENT, request.json))), 200


class InvestigationInstrumentsWithID(Resource):
Expand Down
4 changes: 2 additions & 2 deletions src/resources/entities/investigation_parameters_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from flask_restful import Resource

from common.database_helpers import get_row_by_id, delete_row_by_id, update_row_from_id, get_rows_by_filter, \
get_filtered_row_count, get_first_filtered_row, create_row_from_json
get_filtered_row_count, get_first_filtered_row, create_row_from_json, patch_entities
from common.helpers import requires_session_id, queries_records, get_filters_from_query_string
from common.models.db_models import INVESTIGATIONPARAMETER

Expand All @@ -22,7 +22,7 @@ def post(self):
@requires_session_id
@queries_records
def patch(self):
pass
return list(map(lambda x: x.to_dict(), patch_entities(INVESTIGATIONPARAMETER, request.json))), 200


class InvestigationParametersWithID(Resource):
Expand Down
Loading

0 comments on commit 2cfd027

Please sign in to comment.