def mutate(self, info, **kwargs): # Validate if the country given is a valid country validate_country_field(**kwargs) validate_timezone_field(**kwargs) validate_url(**kwargs) validate_empty_fields(**kwargs) location = LocationModel(**kwargs) admin = get_user_from_db() email = admin.email username = email.split("@")[0] admin_name = username.split(".")[0] subject = 'A new location has been added' template = 'location_success.html' payload = { 'model': LocationModel, 'field': 'name', 'value': kwargs['name'] } with SaveContextManager(location, 'Location', payload): if not notification.send_email_notification( email=email, subject=subject, template=template, location_name=location.name, user_name=admin_name): raise GraphQLError("Location created but email not sent") return CreateLocation(location=location)
def update_delete_floor(self, floor_id): admin_details = get_user_from_db() location_query = location_join_floor() floor_location = location_query.filter_by(id=floor_id).first() if admin_details.location != floor_location.name: raise GraphQLError("You are not authorized to make changes in " + floor_location.name) # noqa: E501
def create_rooms_update_delete_office(self, office_id): admin_details = get_user_from_db() get_office = Office.query.filter_by(id=office_id).first() location = Location.query.filter_by(id=get_office.location_id).first() if admin_details.location != location.name: raise GraphQLError("You are not authorized to make changes in " + location.name) # noqa: E501
def create_floor(self, block_id): admin_details = get_user_from_db() location_query = location_join_block() block_location = location_query.filter_by(id=block_id).first() if admin_details.location != block_location.name: raise GraphQLError("You are not authorized to make changes in " + block_location.name) # noqa: E501
def mutate(self, info, **kwargs): validate_empty_fields(**kwargs) rating_values = ["Excellent", "Very Good", "Good", "Average", "Poor"] ratings = ["comments", "overall_rating", "cleanliness_rating"] query = Room.get_query(info) room = query.filter_by(id=kwargs['room_id']).first() if not room: raise GraphQLError("Non-existent room id") if not set(ratings).intersection(kwargs): raise GraphQLError("Ensure to give at least one feedback input") ratings.pop(0) for feedback in kwargs: if feedback in ratings: if kwargs[feedback] not in rating_values: raise GraphQLError("Invalid rating, only " + ', '.join(rating_values) + " ratings allowed") # noqa user = get_user_from_db() feedback = FeedbackModel(**kwargs, user_id=user.id) feedback.save() return CreateFeedback(feedback=feedback)
def resolve_all_remote_rooms(self, info, return_all=None): page_token = None filter = map_remote_room_location_to_filter() location = 'all' if return_all else get_user_from_db().location remote_rooms = [] while True: calendar_list = get_google_api_calendar_list(pageToken=page_token) for room_object in calendar_list['items']: if 'andela.com' in room_object['id'] and room_object[ 'id'].endswith( # noqa 'resource.calendar.google.com') and filter.get( location)(room_object['summary']): calendar_id = room_object['id'] room_name = room_object['summary'] remote_room = RemoteRoom(calendar_id=calendar_id, name=room_name) remote_rooms.append(remote_room) page_token = calendar_list.get('nextPageToken') if not page_token: break match = r'\bTest|\bDummy|\btest|\bdummy' if os.getenv("APP_SETTINGS") != "production": test_rooms = [ room for room in remote_rooms if re.search(match, room.name) ] return AllRemoteRooms(rooms=test_rooms) actual_rooms = [ room for room in remote_rooms if not re.search(match, room.name) ] return AllRemoteRooms(rooms=actual_rooms)
def mutate(self, info, email): query_user = User.get_query(info) user = query_user.filter(UserModel.email == email).first() if user: raise GraphQLError("User already joined Converge") admin = get_user_from_db() email_invite(email, admin.__dict__["name"]) return InviteToConverge(email=email)
def delete_resource(self, resource_id): admin_details = get_user_from_db() location_query = location_join_resources() resource_location = location_query.filter( ResourceModel.id == resource_id).first() # noqa: E501 if admin_details.location != resource_location.name: raise GraphQLError("You are not authorized to make changes in " + resource_location.name) # noqa: E501
def update_delete_rooms_create_resource(self, room_id): admin_details = get_user_from_db() location_query = location_join_room() room_location = location_query.filter( RoomModel.id == room_id, RoomModel.state == "active").first() if admin_details.location != room_location.name: raise GraphQLError("You are not authorized to make changes in " + room_location.name) # noqa: E501
def admin_location_for_analytics_view(self): """ Return admin's location for viewing analytics data """ admin_details = get_user_from_db() location = Location.query.filter_by( name=admin_details.location).first() return location.id
def resolve_get_user_notification_settings(self, info): user = get_user_from_db() query = Notification.get_query(info) notification = query.filter( NotificationModel.user_id == user.id).first() if notification is None: notification = NotificationModel(user_id=user.id) notification.save() return [notification] # makes the response iterable
def resolve_all_events(self, info, **kwargs): page = kwargs.get('page') per_page = kwargs.get('per_page') if page is not None and page < 1: raise GraphQLError("page must be at least 1") if per_page is not None and per_page < 1: raise GraphQLError("perPage must be at least 1") if page and not per_page: raise GraphQLError("perPage argument missing") if per_page and not page: raise GraphQLError("page argument missing") user = get_user_from_db() start_date, end_date = CommonAnalytics.all_analytics_date_validation( self, kwargs['start_date'], kwargs['end_date'] ) query = Events.get_query(info) all_events, all_dates = CommonAnalytics.get_all_events_and_dates( query, start_date, end_date ) events_in_location = CalendarEvents().get_events_in_location( user, all_events) all_days_events = [] for date in set(all_dates): daily_events = [] for event in events_in_location: CommonAnalytics.format_date(event.start_time) event_start_date = parser.parse( event.start_time).astimezone(pytz.utc) day_of_event = event_start_date.strftime("%a %b %d %Y") if date == day_of_event: daily_events.append(event) all_days_events.append( DailyRoomEvents( day=date, events=daily_events ) ) all_days_events.sort(key=lambda x: datetime.strptime(x.day, "%a %b %d %Y"), reverse=True) # noqa if page and per_page: paginated_events = ListPaginate( iterable=all_days_events, per_page=per_page, page=page) has_previous = paginated_events.has_previous has_next = paginated_events.has_next current_page = paginated_events.current_page pages = paginated_events.pages query_total = paginated_events.query_total return PaginatedDailyRoomEvents( DailyRoomEvents=current_page, has_previous=has_previous, has_next=has_next, query_total=query_total, pages=pages) return PaginatedDailyRoomEvents(DailyRoomEvents=all_days_events)
def check_admin_restriction(new_role): ''' Restricting users who is not a super admin from assigning the role 'super Admin.' ''' admin_details = get_user_from_db() admin_role = RoleModel.query.filter_by( id=admin_details.roles[0].id).first() if admin_role.role != 'Super Admin' and new_role == 'Super Admin': raise GraphQLError('You are not authorized to assign this role')
def resolve_all_analytics(self, info, **kwargs): start_date = kwargs.get('start_date') end_date = kwargs.get('end_date') location_id = admin_roles.user_location_for_analytics_view() admin_details = get_user_from_db() query = Role.get_query(info) admin_role = query.filter_by(id=admin_details.roles[0].id).first() # check that id is valid verify_location_id(kwargs) if admin_role.role == 'Super Admin' and kwargs.get( 'location_id', None): location_id = kwargs.get('location_id') unconverted_dates = { 'start': start_date, 'end': end_date, } start_date, end_date = CommonAnalytics.all_analytics_date_validation( self, start_date, end_date) query = Room.get_query(info) room_analytics, bookings, percentages_dict, bookings_count = AllAnalyticsHelper.get_all_analytics( # noqa self, query, start_date, end_date, location_id, unconverted_dates) analytics = [] for analytic in room_analytics: current_analytic = ConsolidatedAnalytics( room_name=analytic['room_name'], cancellations=analytic['cancellations'], cancellations_percentage=analytic['cancellations_percentage'], auto_cancellations=analytic['auto_cancellations'], number_of_bookings=analytic['number_of_meetings'], checkins=analytic['checkins'], checkins_percentage=analytic['checkins_percentage'], bookings_percentage_share=percentage_formater( analytic['num_of_events'], bookings), app_bookings=analytic['app_bookings'], app_bookings_percentage=analytic['app_bookings_percentage'], events=analytic['room_events'], ) analytics.append(current_analytic) return AllAnalytics(bookings=bookings, checkins_percentage=percentage_formater( percentages_dict['total_checkins'], bookings), auto_cancellations_percentage=percentage_formater( percentages_dict['total_auto_cancellations'], bookings), cancellations_percentage=percentage_formater( percentages_dict['total_cancellations'], bookings), app_bookings_percentage=percentage_formater( percentages_dict['total_app_bookings'], bookings), bookings_count=bookings_count, analytics=analytics)
def user_location_for_analytics_view(self): """ Return admin's location for viewing analytics data """ admin_details = get_user_from_db() location = Location.query.filter_by( name=admin_details.location).first() if location: if location.state != StateType.active: raise GraphQLError('Location is not active') return location.id raise GraphQLError('Your location does not exist')
def mutate(self, info, email, **kwargs): query_user = User.get_query(info) exact_query_user = query_user.filter(UserModel.email == email).first() user_from_db = get_user_from_db() if not verify_email(email): raise GraphQLError("Invalid email format") if not exact_query_user: raise GraphQLError("User not found") if user_from_db.email == email: raise GraphQLError("You cannot delete yourself") exact_query_user.delete() return DeleteUser(user=exact_query_user)
def mutate(self, info, email, **kwargs): query_user = User.get_query(info) active_user = query_user.filter(UserModel.state == "active") exact_query_user = active_user.filter(UserModel.email == email).first() user_from_db = get_user_from_db() if not verify_email(email): raise GraphQLError("Invalid email format") if not exact_query_user: raise GraphQLError("User not found") if user_from_db.email == email: raise GraphQLError("You cannot delete yourself") update_entity_fields(exact_query_user, state="archived", **kwargs) exact_query_user.save() return DeleteUser(user=exact_query_user)
def mutate(self, info, **kwargs): user = get_user_from_db() notification = NotificationModel.query.filter_by( user_id=user.id).first() if not notification: notification = NotificationModel(user_id=user.id) if 'device_health_notification' in kwargs: notification.device_health_notification = kwargs[ 'device_health_notification'] if 'meeting_update_notification' in kwargs: notification.meeting_update_notification = kwargs[ 'meeting_update_notification'] notification.save() return UpdateNotification(notification=notification)
def update_resource(self, resource_id, room_id): admin_details = get_user_from_db() location_query = location_join_resources() resource_location = location_query.filter( ResourceModel.id == resource_id).first() # noqa: E501 if admin_details.location != resource_location.name: raise GraphQLError("You are not authorized to make changes in " + resource_location.name) # noqa: E501 # check room_id incase the resource room is to be updated. location_query = location_join_room() room_location = location_query.filter( RoomModel.id == room_id).first() # noqa: E501 if admin_details.location != room_location.name: raise GraphQLError("You are not authorized to make changes in " + room_location.name) # noqa: E501
def mutate(self, info, **kwargs): room_location = location_join_room().filter( RoomModel.id == kwargs['room_id'], RoomModel.state == 'active' ).first() if not room_location: raise GraphQLError("Room not found") user = get_user_from_db() device = DevicesModel( **kwargs, date_added=datetime.now(), last_seen=datetime.now(), location=user.location ) device.save() return CreateDevice(device=device)
def mutate(self, info, **kwargs): location = Location.query.filter_by(id=kwargs['location_id']).first() if not location: raise GraphQLError("Location not found") admin_roles.verify_admin_location(location_id=kwargs['location_id']) office = OfficeModel(**kwargs) admin = get_user_from_db() email = admin.email username = email.split("@")[0] admin_name = username.split(".")[0] payload = { 'model': OfficeModel, 'field': 'name', 'value': kwargs['name'] } with SaveContextManager( office, 'Office', payload ): new_office = kwargs['name'] if not send_email_notification(email, new_office, location.name, admin_name): # noqa raise GraphQLError("Office created but Emails not Sent") return CreateOffice(office=office)
def resolve_all_remote_rooms(self, info, return_all=None): page_token = None filter = map_remote_room_location_to_filter() location = 'all' if return_all else get_user_from_db().location remote_rooms = [] while True: calendar_list = get_google_api_calendar_list(pageToken=page_token) for room_object in calendar_list['items']: if 'andela.com' in room_object['id'] and room_object[ 'id'].endswith( # noqa 'resource.calendar.google.com') and filter.get( location)(room_object['summary']): calendar_id = room_object['id'] room_name = room_object['summary'] remote_room = RemoteRoom(calendar_id=calendar_id, name=room_name) remote_rooms.append(remote_room) page_token = calendar_list.get('nextPageToken') if not page_token: break return AllRemoteRooms(rooms=remote_rooms)
def create_update_delete_wing(self): admin_details = get_user_from_db() if admin_details.location.lower() != 'lagos': raise GraphQLError( "This action is restricted to Lagos Office admin" ) # noqa: E501
def create_office(self, location_id): admin_details = get_user_from_db() location = Location.query.filter_by(id=location_id).first() if location.name != admin_details.location: raise GraphQLError("You are not authorized to make changes in " + location.name) # noqa: E501
def verify_admin_location(self, location_id): admin_details = get_user_from_db() location = Location.query.filter_by(id=location_id).first() if admin_details.location != location.name: raise GraphQLError("You are not authorized to make changes in " + location.name)
def create_rooms_update_delete_location(self, kwargs): admin_details = get_user_from_db() location = Location.query.filter_by(id=kwargs['location_id']).first() if admin_details.location != location.name: raise GraphQLError("You are not authorized to make changes in " + location.name) # noqa: E501