Skip to content

Commit

Permalink
Merge branch 'master' into feature/configurable-backend-#125
Browse files Browse the repository at this point in the history
  • Loading branch information
louise-davies committed Jun 12, 2020
2 parents 8a2f6a2 + 016ce7b commit 108256f
Show file tree
Hide file tree
Showing 7 changed files with 2,113 additions and 2,312 deletions.
22 changes: 0 additions & 22 deletions common/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,28 +128,6 @@ def update_with_id(self, session_id, entity_type, id, data):
"""
pass

@abstractmethod
def get_users_investigations_with_filters(self, session_id, user_id, filters):
"""
Given a user id and a list of filters, return a filtered list of all investigations that belong to that user
:param session_id: The session id of the requesting user
:param user_id: The id of the user
:param filters: The list of filters
:return: A list of dictionary representations of the investigation entities
"""
pass

@abstractmethod
def count_users_investigations_with_filters(self, session_id, user_id, filters):
"""
Given a user id and a list of filters, return the count of all investigations that belong to that user
:param session_id: The session id of the requesting user
:param user_id: The id of the user
:param filters: The list of filters
:return: The count
"""
pass

@abstractmethod
def get_instrument_facilitycycles_with_filters(self, session_id, instrument_id, filters):
"""
Expand Down
13 changes: 1 addition & 12 deletions common/database_backend.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from common.backend import Backend
from common.database_helpers import get_investigations_for_user, get_investigations_for_user_count, \
get_facility_cycles_for_instrument, get_facility_cycles_for_instrument_count, \
from common.database_helpers import get_facility_cycles_for_instrument, get_facility_cycles_for_instrument_count, \
get_investigations_for_instrument_in_facility_cycle, get_investigations_for_instrument_in_facility_cycle_count, \
get_rows_by_filter, create_rows_from_json, patch_entities, get_row_by_id, insert_row_into_table, \
delete_row_by_id, update_row_from_id, get_filtered_row_count, get_first_filtered_row
Expand Down Expand Up @@ -78,16 +77,6 @@ def delete_with_id(self, session_id, table, id):
def update_with_id(self, session_id, table, id, data):
return update_row_from_id(table, id, data)

@requires_session_id
@queries_records
def get_users_investigations_with_filters(self, session_id, user_id, filters):
return get_investigations_for_user(user_id, filters)

@requires_session_id
@queries_records
def count_users_investigations_with_filters(self, session_id, user_id, filters):
return get_investigations_for_user_count(user_id, filters)

@requires_session_id
@queries_records
def get_instrument_facilitycycles_with_filters(self, session_id, instrument_id, filters):
Expand Down
48 changes: 0 additions & 48 deletions common/database_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -516,54 +516,6 @@ def patch_entities(table, json_list):
return results


class UserInvestigationsQuery(ReadQuery):
"""
The query class used for the /users/<:id>/investigations endpoint
"""

def __init__(self, user_id):
super().__init__(INVESTIGATION)
self.base_query = self.base_query.join(INVESTIGATIONUSER).filter(
INVESTIGATIONUSER.USER_ID == user_id)


def get_investigations_for_user(user_id, filters):
"""
Given a user id and a list of filters, return a filtered list of all investigations that belong to that user
:param user_id: The id of the user
:param filters: The list of filters
:return: A list of dictionary representations of the investigation entities
"""
with UserInvestigationsQuery(user_id) as query:
filter_handler = FilterOrderHandler()
return get_filtered_read_query_results(filter_handler, filters, query)


class UserInvestigationsCountQuery(CountQuery):
"""
The query class used for /users/<:id>/investigations/count
"""

def __init__(self, user_id):
super().__init__(INVESTIGATION)
self.base_query = self.base_query.join(INVESTIGATIONUSER).filter(
INVESTIGATIONUSER.USER_ID == user_id)


def get_investigations_for_user_count(user_id, filters):
"""
Given a user id and a list of filters, return the count of all investigations that belong to that user
:param user_id: The id of the user
:param filters: The list of filters
:return: The count
"""
with UserInvestigationsCountQuery(user_id) as count_query:
filter_handler = FilterOrderHandler()
filter_handler.add_filters(filters)
filter_handler.apply_filters(count_query)
return count_query.get_count()


class InstrumentFacilityCyclesQuery(ReadQuery):
def __init__(self, instrument_id):
super().__init__(FACILITYCYCLE)
Expand Down
10 changes: 2 additions & 8 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@
get_find_one_endpoint
from src.resources.entities.entity_map import endpoints
from src.resources.non_entities.sessions_endpoints import *
from src.resources.table_endpoints.table_endpoints import UsersInvestigations, UsersInvestigationsCount, \
InstrumentsFacilityCycles, InstrumentsFacilityCyclesCount, InstrumentsFacilityCyclesInvestigations, \
InstrumentsFacilityCyclesInvestigationsCount
from src.resources.table_endpoints.table_endpoints import InstrumentsFacilityCycles, InstrumentsFacilityCyclesCount, \
InstrumentsFacilityCyclesInvestigations, InstrumentsFacilityCyclesInvestigationsCount
from common.exceptions import ApiError
from apispec import APISpec
from pathlib import Path
Expand Down Expand Up @@ -79,11 +78,6 @@ def handle_error(e):
spec.path(resource=Sessions, api=api)

# Table specific endpoints
api.add_resource(UsersInvestigations, "/users/<int:id>/investigations")
spec.path(resource=UsersInvestigations, api=api)
api.add_resource(UsersInvestigationsCount,
"/users/<int:id>/investigations/count")
spec.path(resource=UsersInvestigationsCount, api=api)
api.add_resource(InstrumentsFacilityCycles,
"/instruments/<int:id>/facilitycycles")
spec.path(resource=InstrumentsFacilityCycles, api=api)
Expand Down
5 changes: 3 additions & 2 deletions src/resources/entities/entity_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ def delete(self, id):
parameters:
- in: path
required: true
name: ID
name: id
description: The id of the entity to delete
schema:
type: integer
Expand Down Expand Up @@ -227,7 +227,7 @@ def patch(self, id):
parameters:
- in: path
required: true
name: ID
name: id
description: The id of the entity to update
schema:
type: integer
Expand Down Expand Up @@ -283,6 +283,7 @@ def get(self):
parameters:
- WHERE_FILTER
- DISTINCT_FILTER
- INCLUDE_FILTER
responses:
200:
description: Success - The count of the {table.__name__} objects
Expand Down
95 changes: 8 additions & 87 deletions src/resources/table_endpoints/table_endpoints.py
Original file line number Diff line number Diff line change
@@ -1,90 +1,11 @@
from flask_restful import Resource

from common.database_helpers import get_investigations_for_user, get_investigations_for_user_count, \
get_facility_cycles_for_instrument, get_facility_cycles_for_instrument_count, \
from common.database_helpers import get_facility_cycles_for_instrument, get_facility_cycles_for_instrument_count, \
get_investigations_for_instrument_in_facility_cycle, get_investigations_for_instrument_in_facility_cycle_count
from common.helpers import get_session_id_from_auth_header, get_filters_from_query_string
from common.backends import backend


class UsersInvestigations(Resource):
def get(self, id):
"""
---
summary: Get a user's Investigations
description: Retrieve the investigations that a user of a given ID is an InvestigationUser on, subject to filters.
tags:
- Investigations
parameters:
- in: path
required: true
name: ID
description: The id of the user to retrieve the investigations of
schema:
type: integer
- WHERE_FILTER
- ORDER_FILTER
- LIMIT_FILTER
- SKIP_FILTER
- DISTINCT_FILTER
- INCLUDE_FILTER
responses:
200:
description: Success - returns a list of the user's investigations that satisfy the filters
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/INVESTIGATION'
400:
description: Bad request - Something was wrong with the request
401:
description: Unauthorized - No session ID was found in the HTTP Authorization header
403:
description: Forbidden - The session ID provided is invalid
404:
description: No such record - Unable to find a record in the database
"""
return backend.get_investigations_for_user(get_session_id_from_auth_header(), id, get_filters_from_query_string()), 200


class UsersInvestigationsCount(Resource):
def get(self, id):
"""
---
summary: Count a user's Investigations
description: Return the count of the Investigations that belong to a given user that would be retrieved given the filters provided
tags:
- Investigations
parameters:
- in: path
required: true
name: ID
description: The id of the user to count the investigations of
schema:
type: integer
- WHERE_FILTER
- DISTINCT_FILTER
responses:
200:
description: Success - The count of the user's investigations that satisfy the filters
content:
application/json:
schema:
type: integer
400:
description: Bad request - Something was wrong with the request
401:
description: Unauthorized - No session ID was found in the HTTP Authorization header
403:
description: Forbidden - The session ID provided is invalid
404:
description: No such record - Unable to find a record in the database
"""
return backend.get_investigations_for_user_count(get_session_id_from_auth_header(), id, get_filters_from_query_string()), 200


class InstrumentsFacilityCycles(Resource):
def get(self, id):
"""
Expand All @@ -96,7 +17,7 @@ def get(self, id):
parameters:
- in: path
required: true
name: ID
name: id
description: The id of the instrument to retrieve the facility cycles of
schema:
type: integer
Expand Down Expand Up @@ -138,7 +59,7 @@ def get(self, id):
parameters:
- in: path
required: true
name: ID
name: id
description: The id of the instrument to count the facility cycles of
schema:
type: integer
Expand Down Expand Up @@ -174,13 +95,13 @@ def get(self, instrument_id, cycle_id):
parameters:
- in: path
required: true
name: Instrument ID
name: instrument_id
description: The id of the instrument to retrieve the investigations of
schema:
type: integer
- in: path
required: true
name: Facility Cycle ID
name: cycle_id
description: The id of the facility cycles to retrieve the investigations of
schema:
type: integer
Expand All @@ -198,7 +119,7 @@ def get(self, instrument_id, cycle_id):
schema:
type: array
items:
$ref: '#/components/schemas/INVESTIGATIONS'
$ref: '#/components/schemas/INVESTIGATION'
400:
description: Bad request - Something was wrong with the request
401:
Expand All @@ -223,13 +144,13 @@ def get(self, instrument_id, cycle_id):
parameters:
- in: path
required: true
name: Instrument ID
name: instrument_id
description: The id of the instrument to retrieve the investigations of
schema:
type: integer
- in: path
required: true
name: Facility Cycle ID
name: cycle_id
description: The id of the facility cycles to retrieve the investigations of
schema:
type: integer
Expand Down
Loading

0 comments on commit 108256f

Please sign in to comment.