def get_notification_timer(self, name): """ Will return a single NotificationCallbackTimer """ try: obj = SQLNotificationCallbackTimer.objects.get(name=name) except ObjectDoesNotExist: raise ItemNotFoundError() return obj.to_data_object()
def get_notification_preference(self, name): """ Will return a single NotificationPreference if exists else raises exception ItemNotFoundError """ try: obj = SQLNotificationPreference.objects.get(name=name) except ObjectDoesNotExist: raise ItemNotFoundError() return obj.to_data_object()
def get_user_preference(self, user_id, name): """ Will return a single UserNotificationPreference if exists else raises exception ItemNotFoundError """ try: obj = SQLUserNotificationPreferences.objects.get( user_id=user_id, preference__name=name) except ObjectDoesNotExist: raise ItemNotFoundError() return obj.to_data_object()
def get_notification_for_user(self, user_id, msg_id): """ Get a single UserNotification for the user_id/msg_id pair """ try: item = SQLUserNotification.objects.select_related().get( user_id=user_id, msg_id=msg_id) return item.to_data_object() except ObjectDoesNotExist: msg = ("Could not find msg_id '{msg_id}' for user_id '{user_id}'!" ).format(msg_id=msg_id, user_id=user_id) raise ItemNotFoundError(msg)
def _get_notification_by_id(self, msg_id, options=None): """ Helper method to get Notification Message by id """ # pylint/pep8 seem to complain if defaults are set to empty dicts _options = options if options else {} select_related = _options.get('select_related', True) try: query = SQLNotificationMessage.objects if select_related: query = query.select_related() obj = query.get(id=msg_id) except ObjectDoesNotExist: raise ItemNotFoundError() return obj.to_data_object(options=options)
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_notification_message(self, msg): """ Saves a passed in NotificationMsg data object. If 'id' is set by the caller it will try to update the object. If it does not exist it will throw an exception. If it is created, then the id property will be set on the NotificationMsg and returned """ if msg.id: try: obj = SQLNotificationMessage.objects.get(id=msg.id) obj.load_from_data_object(msg) except ObjectDoesNotExist: msg = f"Could not SQLNotificationMessage with ID {msg.id}" raise ItemNotFoundError() else: obj = SQLNotificationMessage.from_data_object(msg) obj.save() return obj.to_data_object()
def get_notification_type(self, name): # pylint: disable=no-self-use """ This returns a NotificationType object. NOTE: NotificationTypes are supposed to be immutable during the process lifetime. New Types can be added, but not updated. Therefore we can memoize this function """ data_object = None # pull from the cache, if we have it if name in self._msg_type_cache: data_object = self._msg_type_cache[name] return data_object try: obj = SQLNotificationType.objects.get(name=name) except ObjectDoesNotExist: raise ItemNotFoundError() data_object = obj.to_data_object() # refresh the cache self._msg_type_cache[name] = data_object return data_object