def get_pending_notifications(): """Get notification data for all future notifications. The data dict that get's returned here contains notification data grouped by dates on which the notifications should be received. Returns list of Notifications, data: a tuple of notifications that were handled and corresponding data for those notifications. """ notifications = db.session.query( Notification).filter((Notification.sent_at.is_(None)) | (Notification.repeating == true())).all() notif_by_day = defaultdict(list) for notification in notifications: notif_by_day[notification.send_on.date()].append(notification) data = defaultdict(dict) today = date.today() for day, notif in notif_by_day.iteritems(): current_day = max(day, today) data[current_day] = merge_dict(data[current_day], get_notification_data(notif)) return notifications, data
def test_merge_dict(self): dict1 = { "a": { "b": { "c": 1, "d": { "e": 2, }, }, }, } dict2 = {"a": {"b": {"c": 1, "f": {"e": 2}}, "g": 3}, "h": 4} expected_result = { "a": { "b": { "c": 1, "d": { "e": 2, }, "f": { "e": 2, }, }, "g": 3, }, "h": 4, } result = utils.merge_dict(dict1, dict2) self.assertEqual(result, expected_result)
def get_notification_data(notifications): """Get notification data for all notifications. This function returns a filtered data for all notifications for the users that should receive it. Args: notifications (list of Notification): List of notification for which we want to get notification data. Returns: dict: Filtered dictionary containing all the data that should be sent for the given notification list. """ if not notifications: return {} aggregate_data = {} people_cache = {} tasks_cache = cycle_tasks_cache(notifications) deleted_rels_cache = deleted_task_rels_cache(tasks_cache.keys()) for notification in notifications: filtered_data = get_filter_data(notification, people_cache, tasks_cache=tasks_cache, del_rels_cache=deleted_rels_cache) aggregate_data = merge_dict(aggregate_data, filtered_data) # Remove notifications for objects without a contact (such as task groups) aggregate_data.pop("", None) return aggregate_data
def get_pending_notifications(): """Get notification data for all future notifications. The data dict that get's returned here contains notification data grouped by dates on which the notifications should be received. Returns list of Notifications, data: a tuple of notifications that were handled and corresponding data for those notifications. """ notifications = db.session.query(Notification).filter( (Notification.sent_at.is_(None)) | (Notification.repeating == true()) ).all() notif_by_day = defaultdict(list) for notification in notifications: notif_by_day[notification.send_on.date()].append(notification) data = defaultdict(dict) today = date.today() for day, notif in notif_by_day.iteritems(): current_day = max(day, today) data[current_day] = merge_dict(data[current_day], get_notification_data(notif)) return notifications, data
def get_notification_data(notifications): """Get notification data for all notifications. This function returns a filtered data for all notifications for the users that should receive it. Args: notifications (list of Notification): List of notification for which we want to get notification data. Returns: dict: Filtered dictionary containing all the data that should be sent for the given notification list. """ if not notifications: return {} aggregate_data = {} people_cache = {} tasks_cache = cycle_tasks_cache(notifications) deleted_rels_cache = deleted_task_rels_cache(tasks_cache.keys()) for notification in notifications: filtered_data = get_filter_data( notification, people_cache, tasks_cache=tasks_cache, del_rels_cache=deleted_rels_cache) aggregate_data = merge_dict(aggregate_data, filtered_data) # Remove notifications for objects without a contact (such as task groups) aggregate_data.pop("", None) sort_comments(aggregate_data) return aggregate_data
def get_daily_notifications(): """Get notification data for all future notifications. Returns list of Notifications, data: a tuple of notifications that were handled and corresponding data for those notifications. """ notifications = [] notifications_data = {} for notif_list, notif_data in generate_daily_notifications(): notifications.extend(notif_list.all()) notifications_data = merge_dict(notifications_data, notif_data) return notifications, notifications_data
def test_merge_dict(self): """Test merging two dictionaries.""" dict1 = { "a": { "b": { "c": 1, "d": { "e": 2, }, }, }, } dict2 = { "a": { "b": { "c": 1, "f": { "e": 2 } }, "g": 3 }, "h": 4 } expected_result = { "a": { "b": { "c": 1, "d": { "e": 2, }, "f": { "e": 2, }, }, "g": 3, }, "h": 4, } result = utils.merge_dict(dict1, dict2) self.assertEqual(result, expected_result)