33import pytz
44
55from api .events .models import Events as EventsModel
6+ from api .location .models import Location as LocationModel
7+ from api .role .models import Role as RoleModel
8+ from api .room .models import Room as RoomModel
69
710utc = pytz .utc
811
912
10- def filter_events_by_date_range (query , start_date , end_date ):
13+ def filter_events_by_date_range_in_location (query , start_date , end_date , user ):
1114 """
1215 Return events that fall in the date range
16+ and matches the current_user's location with the events in that
17+ location and filters to return the events for that location
18+ but returns all locations for a user with super admin role
1319 """
1420 if start_date and not end_date :
1521 raise GraphQLError ("endDate argument missing" )
@@ -21,19 +27,29 @@ def filter_events_by_date_range(query, start_date, end_date):
2127 events = query .filter (
2228 EventsModel .state == 'active'
2329 ).all ()
24- if not events :
25- raise GraphQLError ('Events do not exist' )
2630 return events
2731
2832 start_date , end_date = format_range_dates (start_date , end_date )
2933
30- events = query .filter (
34+ admin_role = RoleModel .query .filter_by (
35+ id = user .roles [0 ].id ).first ()
36+ if admin_role .role == 'Super Admin' :
37+ events = query .filter (
3138 EventsModel .state == 'active' ,
3239 EventsModel .start_time >= start_date ,
3340 EventsModel .end_time <= end_date
3441 ).all ()
35- if not events :
36- raise GraphQLError ('Events do not exist for the date range' )
42+ return events
43+
44+ location = LocationModel .query .filter_by (
45+ name = user .location
46+ ).first ()
47+ events = query .join (RoomModel ).filter (
48+ EventsModel .state == 'active' ,
49+ EventsModel .start_time >= start_date ,
50+ EventsModel .end_time <= end_date ,
51+ RoomModel .location_id == location .id
52+ ).all ()
3753 return events
3854
3955
0 commit comments