Skip to content

Commit 25781ff

Browse files
authored
166374529-feat(query events): restrict event query by location (#419)
-query events by location [Delivers #166374529]
1 parent 7a87bfa commit 25781ff

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

api/events/schema.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from helpers.calendar.credentials import get_single_calendar_event
1010
from helpers.auth.authentication import Auth
1111
from helpers.calendar.analytics_helper import CommonAnalytics
12+
from helpers.auth.user_details import get_user_from_db
1213
import pytz
1314
from dateutil import parser
1415
from datetime import datetime
@@ -226,17 +227,20 @@ class Query(graphene.ObjectType):
226227

227228
@Auth.user_roles('Admin', 'Default User')
228229
def resolve_all_events(self, info, **kwargs):
230+
user = get_user_from_db()
229231
start_date, end_date = CommonAnalytics.all_analytics_date_validation(
230232
self, kwargs['start_date'], kwargs['end_date']
231233
)
232234
query = Events.get_query(info)
233235
all_events, all_dates = CommonAnalytics.get_all_events_and_dates(
234236
query, start_date, end_date
235237
)
238+
events_in_location = CalendarEvents().get_events_in_location(
239+
user, all_events)
236240
all_days_events = []
237241
for date in set(all_dates):
238242
daily_events = []
239-
for event in all_events:
243+
for event in events_in_location:
240244
CommonAnalytics.format_date(event.start_time)
241245
event_start_date = parser.parse(
242246
event.start_time).astimezone(pytz.utc)

helpers/calendar/events.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
from api.events.models import Events as EventsModel
1010
from api.room.models import Room as RoomModel
11+
from api.location.models import Location as LocationModel
1112
from .analytics_helper import CommonAnalytics
1213
from .credentials import Credentials, get_google_calendar_events
1314

@@ -213,3 +214,14 @@ def sync_all_events(self):
213214
rooms = RoomModel.query.filter_by(state='active')
214215
for room in rooms:
215216
self.sync_single_room_events(room)
217+
218+
def get_events_in_location(self, user, events):
219+
events_in_location = []
220+
location = LocationModel.query.filter_by(
221+
name=user.location).first()
222+
for event in events:
223+
room = RoomModel.query.filter_by(
224+
calendar_id=event.room.calendar_id).first()
225+
if room.location_id == location.id:
226+
events_in_location.append(event)
227+
return events_in_location

tests/test_events/test_query_all_events.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def test_query_events(self):
1111
"""
1212
Test a user can query for all events
1313
"""
14-
CommonTestCases.user_token_assert_equal(
14+
CommonTestCases.admin_token_assert_equal(
1515
self,
1616
query_events,
1717
event_query_response

0 commit comments

Comments
 (0)