Skip to content
Open
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
10 changes: 7 additions & 3 deletions api/events/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@
from helpers.pagination.paginate import ListPaginate
from helpers.devices.devices import update_device_last_seen
from helpers.events_filter.events_filter import (
filter_events_by_date_range,
filter_events_by_date_range_in_location,
validate_page_and_per_page
)
from helpers.auth.user_details import get_user_from_db

utc = pytz.utc

Expand Down Expand Up @@ -258,10 +259,13 @@ def resolve_all_events(self, info, **kwargs):
page = kwargs.get('page')
per_page = kwargs.get('per_page')
page, per_page = validate_page_and_per_page(page, per_page)
user = get_user_from_db()
query = Events.get_query(info)
response = filter_events_by_date_range(
query, start_date, end_date
response = filter_events_by_date_range_in_location(
query, start_date, end_date, user
)
if not response:
raise GraphQLError('Events do not exist for the date range')
response.sort(
key=lambda x: parser.parse(x.start_time).astimezone(utc),
reverse=True)
Expand Down
42 changes: 42 additions & 0 deletions fixtures/events/events_query_by_date_fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,48 @@
}
}

query_events_with_location = '''
query{
allEvents(startDate: "Jul 11 2018",
endDate: "Jul 11 2018",
page:1,
perPage: 1){
events {
id
roomId
room{
name
locationId
}
},
hasNext,
hasPrevious,
pages,
queryTotal
}
}
'''

event_query_with_location_response = {
'data': {
'allEvents': {
'events': [{
'id': '1',
'roomId': 1,
'room': {
'name': 'Entebbe',
'locationId': 1
}
}],
'hasNext': False,
'hasPrevious': False,
'pages': 1,
'queryTotal': 1
}
}
}


query_events_page_without_per_page = '''
query{
allEvents(
Expand Down
28 changes: 22 additions & 6 deletions helpers/events_filter/events_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,19 @@
import pytz

from api.events.models import Events as EventsModel
from api.location.models import Location as LocationModel
from api.role.models import Role as RoleModel
from api.room.models import Room as RoomModel

utc = pytz.utc


def filter_events_by_date_range(query, start_date, end_date):
def filter_events_by_date_range_in_location(query, start_date, end_date, user):
"""
Return events that fall in the date range
and matches the current_user's location with the events in that
location and filters to return the events for that location
but returns all locations for a user with super admin role
"""
if start_date and not end_date:
raise GraphQLError("endDate argument missing")
Expand All @@ -21,19 +27,29 @@ def filter_events_by_date_range(query, start_date, end_date):
events = query.filter(
EventsModel.state == 'active'
).all()
if not events:
raise GraphQLError('Events do not exist')
return events

start_date, end_date = format_range_dates(start_date, end_date)

events = query.filter(
admin_role = RoleModel.query.filter_by(
id=user.roles[0].id).first()
if admin_role.role == 'Super Admin':
events = query.filter(
EventsModel.state == 'active',
EventsModel.start_time >= start_date,
EventsModel.end_time <= end_date
).all()
if not events:
raise GraphQLError('Events do not exist for the date range')
return events

location = LocationModel.query.filter_by(
name=user.location
).first()
events = query.join(RoomModel).filter(
EventsModel.state == 'active',
EventsModel.start_time >= start_date,
EventsModel.end_time <= end_date,
RoomModel.location_id == location.id
).all()
return events


Expand Down
16 changes: 16 additions & 0 deletions tests/test_events/test_query_all_events_by_date.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
from tests.base import BaseTestCase, CommonTestCases

from fixtures.events.events_query_by_date_fixtures import (
query_events,
event_query_response,
query_events_with_start_date_before_end_date,
event_query_with_start_date_before_end_date_response,
query_events_with_pagination,
event_query_with_pagination_response,
query_events_with_location,
event_query_with_location_response,
query_events_page_without_per_page,
event_query_page_without_per_page_response,
query_events_per_page_without_page,
Expand All @@ -23,6 +26,7 @@
query_events_without_page_and_per_page,
event_query_without_page_and_per_page_response
)
from tests.base import change_user_role_to_super_admin


class TestEventsQuery(BaseTestCase):
Expand Down Expand Up @@ -56,6 +60,18 @@ def test_query_events_with_pagination(self):
self,
query_events_with_pagination,
event_query_with_pagination_response

)

@change_user_role_to_super_admin
def test_query_events_with_location(self):
"""
Test a super_user can query for all events in all locations
"""
CommonTestCases.admin_token_assert_equal(
self,
query_events_with_location,
event_query_with_location_response
)

def test_query_events_without_start_date(self):
Expand Down