Skip to content

Commit

Permalink
Merge pull request #205 from ral-facilities/feature/camel-case-db-bac…
Browse files Browse the repository at this point in the history
…kend-#119

Make DB Backend use camelCase
  • Loading branch information
MRichards99 authored Apr 1, 2021
2 parents 671b33d + a0c966e commit 0601fb2
Show file tree
Hide file tree
Showing 27 changed files with 1,915 additions and 1,761 deletions.
6 changes: 5 additions & 1 deletion .flake8
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,9 @@ max-complexity = 17
max-line-length = 80
application-import-names = datagateway_api,test,util
import-order-style = google
per-file-ignores = test/*:S101,util/icat_db_generator.py:S311,datagateway_api/wsgi.py:E402,F401
per-file-ignores =
test/*: S101
util/icat_db_generator.py: S311
datagateway_api/wsgi.py:E402,F401
datagateway_api/common/database/models.py: N815,A003
enable-extensions=G
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
venv/
.idea/
*.pyc
logs.log
logs.log*
config.json
.vscode/
.nox/
Expand Down
26 changes: 13 additions & 13 deletions datagateway_api/common/database/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
requires_session_id,
update_row_from_id,
)
from datagateway_api.common.database.models import EntityHelper, SESSION
from datagateway_api.common.database.models import SESSION
from datagateway_api.common.exceptions import AuthenticationError
from datagateway_api.common.helpers import queries_records
from datagateway_api.common.helpers import get_entity_object_from_name, queries_records


log = logging.getLogger()
Expand All @@ -38,9 +38,9 @@ def login(self, credentials):
insert_row_into_table(
SESSION,
SESSION(
ID=session_id,
USERNAME=f"{credentials['mechanism']}/root",
EXPIREDATETIME=datetime.datetime.now() + datetime.timedelta(days=1),
id=session_id,
username=f"{credentials['mechanism']}/root",
expireDateTime=datetime.datetime.now() + datetime.timedelta(days=1),
),
)
return session_id
Expand All @@ -63,49 +63,49 @@ def logout(self, session_id):
@requires_session_id
@queries_records
def get_with_filters(self, session_id, entity_type, filters):
table = EntityHelper.get_entity_object_from_name(entity_type)
table = get_entity_object_from_name(entity_type)
return get_rows_by_filter(table, filters)

@requires_session_id
@queries_records
def create(self, session_id, entity_type, data):
table = EntityHelper.get_entity_object_from_name(entity_type)
table = get_entity_object_from_name(entity_type)
return create_rows_from_json(table, data)

@requires_session_id
@queries_records
def update(self, session_id, entity_type, data):
table = EntityHelper.get_entity_object_from_name(entity_type)
table = get_entity_object_from_name(entity_type)
return patch_entities(table, data)

@requires_session_id
@queries_records
def get_one_with_filters(self, session_id, entity_type, filters):
table = EntityHelper.get_entity_object_from_name(entity_type)
table = get_entity_object_from_name(entity_type)
return get_first_filtered_row(table, filters)

@requires_session_id
@queries_records
def count_with_filters(self, session_id, entity_type, filters):
table = EntityHelper.get_entity_object_from_name(entity_type)
table = get_entity_object_from_name(entity_type)
return get_filtered_row_count(table, filters)

@requires_session_id
@queries_records
def get_with_id(self, session_id, entity_type, id_):
table = EntityHelper.get_entity_object_from_name(entity_type)
table = get_entity_object_from_name(entity_type)
return get_row_by_id(table, id_).to_dict()

@requires_session_id
@queries_records
def delete_with_id(self, session_id, entity_type, id_):
table = EntityHelper.get_entity_object_from_name(entity_type)
table = get_entity_object_from_name(entity_type)
return delete_row_by_id(table, id_)

@requires_session_id
@queries_records
def update_with_id(self, session_id, entity_type, id_, data):
table = EntityHelper.get_entity_object_from_name(entity_type)
table = get_entity_object_from_name(entity_type)
return update_row_from_id(table, id_, data)

@requires_session_id
Expand Down
4 changes: 2 additions & 2 deletions datagateway_api/common/database/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
SkipFilter,
WhereFilter,
)
from datagateway_api.common.helpers import get_entity_object_from_name


log = logging.getLogger()
Expand Down Expand Up @@ -65,9 +66,8 @@ def apply_filter(self, query):
included_included_table,
)
field = getattr(included_included_table, self.included_included_field)

elif self.included_field:
included_table = getattr(models, self.field)
included_table = get_entity_object_from_name(self.field)
query.base_query = query.base_query.join(included_table)
field = getattr(included_table, self.included_field)

Expand Down
72 changes: 36 additions & 36 deletions datagateway_api/common/database/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def requires_session_id(method):
def wrapper_requires_session(*args, **kwargs):
log.info(" Authenticating consumer")
session = db.session
query = session.query(SESSION).filter(SESSION.ID == args[1]).first()
query = session.query(SESSION).filter(SESSION.id == args[1]).first()
if query is not None:
log.info(" Closing DB session")
session.close()
Expand Down Expand Up @@ -150,10 +150,10 @@ def execute_query(self):
else:
record = self.table()
record.update_from_dict(self.row)
record.CREATE_TIME = datetime.datetime.now()
record.MOD_TIME = datetime.datetime.now()
record.CREATE_ID = "user"
record.MOD_ID = "user" # These will need changing
record.createTime = datetime.datetime.now()
record.modTime = datetime.datetime.now()
record.createId = "user"
record.modId = "user"
self.session.add(record)
self.commit_changes()
self.session.refresh(record)
Expand Down Expand Up @@ -232,7 +232,7 @@ def get_row_by_id(table, id_):
"""
with ReadQuery(table) as read_query:
log.info(" Querying %s for record with ID: %s", table.__tablename__, id_)
where_filter = WhereFilter("ID", id_, "eq")
where_filter = WhereFilter("id", id_, "eq")
where_filter.apply_filter(read_query)
return read_query.get_single_result()

Expand Down Expand Up @@ -394,14 +394,14 @@ def __init__(self, instrument_id):
investigation_instrument = aliased(INSTRUMENT)
self.base_query = (
self.base_query.join(FACILITYCYCLE.FACILITY)
.join(FACILITY.INSTRUMENT)
.join(FACILITY.INVESTIGATION)
.join(INVESTIGATION.INVESTIGATIONINSTRUMENT)
.join(FACILITY.instruments)
.join(FACILITY.investigations)
.join(INVESTIGATION.investigationInstruments)
.join(investigation_instrument, INVESTIGATIONINSTRUMENT.INSTRUMENT)
.filter(INSTRUMENT.ID == instrument_id)
.filter(investigation_instrument.ID == INSTRUMENT.ID)
.filter(INVESTIGATION.STARTDATE >= FACILITYCYCLE.STARTDATE)
.filter(INVESTIGATION.STARTDATE <= FACILITYCYCLE.ENDDATE)
.filter(INSTRUMENT.id == instrument_id)
.filter(investigation_instrument.id == INSTRUMENT.id)
.filter(INVESTIGATION.startDate >= FACILITYCYCLE.startDate)
.filter(INVESTIGATION.startDate <= FACILITYCYCLE.endDate)
)


Expand All @@ -425,14 +425,14 @@ def __init__(self, instrument_id):
investigation_instrument = aliased(INSTRUMENT)
self.base_query = (
self.base_query.join(FACILITYCYCLE.FACILITY)
.join(FACILITY.INSTRUMENT)
.join(FACILITY.INVESTIGATION)
.join(INVESTIGATION.INVESTIGATIONINSTRUMENT)
.join(FACILITY.instruments)
.join(FACILITY.investigations)
.join(INVESTIGATION.investigationInstruments)
.join(investigation_instrument, INVESTIGATIONINSTRUMENT.INSTRUMENT)
.filter(INSTRUMENT.ID == instrument_id)
.filter(investigation_instrument.ID == INSTRUMENT.ID)
.filter(INVESTIGATION.STARTDATE >= FACILITYCYCLE.STARTDATE)
.filter(INVESTIGATION.STARTDATE <= FACILITYCYCLE.ENDDATE)
.filter(INSTRUMENT.id == instrument_id)
.filter(investigation_instrument.id == INSTRUMENT.id)
.filter(INVESTIGATION.startDate >= FACILITYCYCLE.startDate)
.filter(INVESTIGATION.startDate <= FACILITYCYCLE.endDate)
)


Expand All @@ -458,15 +458,15 @@ def __init__(self, instrument_id, facility_cycle_id):
investigation_instrument = aliased(INSTRUMENT)
self.base_query = (
self.base_query.join(INVESTIGATION.FACILITY)
.join(FACILITY.FACILITYCYCLE)
.join(FACILITY.INSTRUMENT)
.join(INVESTIGATION.INVESTIGATIONINSTRUMENT)
.join(FACILITY.facilityCycles)
.join(FACILITY.instruments)
.join(INVESTIGATION.investigationInstruments)
.join(investigation_instrument, INVESTIGATIONINSTRUMENT.INSTRUMENT)
.filter(INSTRUMENT.ID == instrument_id)
.filter(FACILITYCYCLE.ID == facility_cycle_id)
.filter(investigation_instrument.ID == INSTRUMENT.ID)
.filter(INVESTIGATION.STARTDATE >= FACILITYCYCLE.STARTDATE)
.filter(INVESTIGATION.STARTDATE <= FACILITYCYCLE.ENDDATE)
.filter(INSTRUMENT.id == instrument_id)
.filter(FACILITYCYCLE.id == facility_cycle_id)
.filter(investigation_instrument.id == INSTRUMENT.id)
.filter(INVESTIGATION.startDate >= FACILITYCYCLE.startDate)
.filter(INVESTIGATION.startDate <= FACILITYCYCLE.endDate)
)


Expand Down Expand Up @@ -495,15 +495,15 @@ def __init__(self, instrument_id, facility_cycle_id):
investigation_instrument = aliased(INSTRUMENT)
self.base_query = (
self.base_query.join(INVESTIGATION.FACILITY)
.join(FACILITY.FACILITYCYCLE)
.join(FACILITY.INSTRUMENT)
.join(INVESTIGATION.INVESTIGATIONINSTRUMENT)
.join(FACILITY.facilityCycles)
.join(FACILITY.instruments)
.join(INVESTIGATION.investigationInstruments)
.join(investigation_instrument, INVESTIGATIONINSTRUMENT.INSTRUMENT)
.filter(INSTRUMENT.ID == instrument_id)
.filter(FACILITYCYCLE.ID == facility_cycle_id)
.filter(investigation_instrument.ID == INSTRUMENT.ID)
.filter(INVESTIGATION.STARTDATE >= FACILITYCYCLE.STARTDATE)
.filter(INVESTIGATION.STARTDATE <= FACILITYCYCLE.ENDDATE)
.filter(INSTRUMENT.id == instrument_id)
.filter(FACILITYCYCLE.id == facility_cycle_id)
.filter(investigation_instrument.id == INSTRUMENT.id)
.filter(INVESTIGATION.startDate >= FACILITYCYCLE.startDate)
.filter(INVESTIGATION.startDate <= FACILITYCYCLE.endDate)
)


Expand Down
Loading

0 comments on commit 0601fb2

Please sign in to comment.