def bulk_create_user_notification(self, user_msgs): """ This is an optimization for bulk creating *new* UserNotification objects in the database. Since we want to support fan-outs of messages, we may need to insert 10,000's (or 100,000's) of objects as optimized as possible. NOTE: this method will return None, the primary key of the user_msgs that was created will not be returned (limitation of Django ORM) NOTE: This method cannot update existing UserNotifications, only create them. NOTE: It is assumed that user_msgs is already chunked in an appropriate size. """ if len(user_msgs) > const.NOTIFICATION_BULK_PUBLISH_CHUNK_SIZE: msg = ( 'You have passed in a user_msgs list of size {length} but the size ' 'limit is {max}.'.format(length=len(user_msgs), max=const.NOTIFICATION_BULK_PUBLISH_CHUNK_SIZE) ) raise BulkOperationTooLarge(msg) objs = [] for user_msg in user_msgs: objs.append(SQLUserNotification.from_data_object(user_msg)) SQLUserNotification.objects.bulk_create(objs, batch_size=const.NOTIFICATION_BULK_PUBLISH_CHUNK_SIZE)
def bulk_create_user_notification(self, user_msgs): """ This is an optimization for bulk creating *new* UserNotification objects in the database. Since we want to support fan-outs of messages, we may need to insert 10,000's (or 100,000's) of objects as optimized as possible. NOTE: this method will return None, the primary key of the user_msgs that was created will not be returned (limitation of Django ORM) NOTE: This method cannot update existing UserNotifications, only create them. NOTE: It is assumed that user_msgs is already chunked in an appropriate size. """ if len(user_msgs) > const.NOTIFICATION_BULK_PUBLISH_CHUNK_SIZE: msg = ( 'You have passed in a user_msgs list of size {length} but the size ' 'limit is {max}.'.format( length=len(user_msgs), max=const.NOTIFICATION_BULK_PUBLISH_CHUNK_SIZE)) raise BulkOperationTooLarge(msg) objs = [] for user_msg in user_msgs: objs.append(SQLUserNotification.from_data_object(user_msg)) SQLUserNotification.objects.bulk_create( objs, batch_size=const.NOTIFICATION_BULK_PUBLISH_CHUNK_SIZE)
def test_archive_user_notification_model_fields(self): # pylint: disable=C0103 """ Test to check that the SQLUserNotificationArchive Model has all the fields (names) of the SQLUserNotification model. """ user_notification_orig = SQLUserNotification() user_notification_archive = SQLUserNotificationArchive() for orig_attr in user_notification_orig.__dict__: self.assertIn(orig_attr, list(user_notification_archive.__dict__.keys()))
def test_user_notification_model_fields(self): # pylint: disable=C0103 """ Test to check that the SQLUserNotification Model has all the fields (names) of the SQLUserNotificationArchive model. """ user_notification = SQLUserNotification() user_notification_archive = SQLUserNotificationArchive() for archive_attr in user_notification_archive.__dict__.keys(): self.assertIn(archive_attr, user_notification.__dict__.keys())
def test_archive_user_notification_model_has_all_fields_of_user_notification_model( self): """ Test to check that the SQLUserNotificationArchive Model has all the fields (names) of the SQLUserNotification model. """ user_notification_orig = SQLUserNotification() user_notification_archive = SQLUserNotificationArchive() for orig_attr in user_notification_orig.__dict__.keys(): self.assertIn(orig_attr, user_notification_archive.__dict__.keys())
def save_user_notification(self, user_msg): """ Create or Update the mapping of a user to a notification. """ if user_msg.id: try: obj = SQLUserNotification.objects.get(id=user_msg.id) obj.load_from_data_object(user_msg) except ObjectDoesNotExist: msg = f"Could not find SQLUserNotification with ID {user_msg.id}" raise ItemNotFoundError(msg) else: obj = SQLUserNotification.from_data_object(user_msg) obj.save() return obj.to_data_object()
def save_user_notification(self, user_msg): """ Create or Update the mapping of a user to a notification. """ if user_msg.id: try: obj = SQLUserNotification.objects.get(id=user_msg.id) obj.load_from_data_object(user_msg) except ObjectDoesNotExist: msg = "Could not find SQLUserNotification with ID {_id}".format(_id=user_msg.id) raise ItemNotFoundError(msg) else: obj = SQLUserNotification.from_data_object(user_msg) obj.save() return obj.to_data_object()