def set_temporal_constraint(self, deployment_obj, start_time=None, end_time=None): tc = self.get_temporal_constraint(deployment_obj) if tc is None: tc = TemporalBounds() deployment_obj.constraint_list.append(tc) if start_time == "NOW": start_time = str(int(time.time())) if end_time == "NOW": end_time = str(int(time.time())) if start_time is not None: tc.start_datetime = start_time if end_time is not None: tc.end_datetime = end_time
def create_notification(self, notification=None, user_id=''): """ Persists the provided NotificationRequest object for the specified Origin id. Associate the Notification resource with the user_id string. returned id is the internal id by which NotificationRequest will be identified in the data store. @param notification NotificationRequest @param user_id str @retval notification_id str @throws BadRequest if object passed has _id or _rev attribute """ if not user_id: raise BadRequest("User id not provided.") log.debug("Create notification called for user_id: %s, and notification: %s", user_id, notification) #--------------------------------------------------------------------------------------------------- # Persist Notification object as a resource if it has already not been persisted #--------------------------------------------------------------------------------------------------- # if the notification has already been registered, simply use the old id notification_id = self._notification_in_notifications(notification, self.notifications) # since the notification has not been registered yet, register it and get the id temporal_bounds = TemporalBounds() temporal_bounds.start_datetime = get_ion_ts() temporal_bounds.end_datetime = '' if not notification_id: notification.temporal_bounds = temporal_bounds notification_id, _ = self.clients.resource_registry.create(notification) self.notifications[notification_id] = notification else: log.debug("Notification object has already been created in resource registry before. No new id to be generated. notification_id: %s", notification_id) # Read the old notification already in the resource registry notification = self.clients.resource_registry.read(notification_id) # Update the temporal bounds of the old notification resource notification.temporal_bounds = temporal_bounds # Update the notification in the resource registry self.clients.resource_registry.update(notification) log.debug("The temporal bounds for this resubscribed notification object with id: %s, is: %s", notification_id,notification.temporal_bounds) # Link the user and the notification with a hasNotification association assocs= self.clients.resource_registry.find_associations(subject=user_id, predicate=PRED.hasNotification, object=notification_id, id_only=True) if assocs: log.debug("Got an already existing association: %s, between user_id: %s, and notification_id: %s", assocs,user_id,notification_id) return notification_id else: log.debug("Creating association between user_id: %s, and notification_id: %s", user_id, notification_id ) self.clients.resource_registry.create_association(user_id, PRED.hasNotification, notification_id) # read the registered notification request object because this has an _id and is more useful notification = self.clients.resource_registry.read(notification_id) # Update the user info object with the notification self.event_processor.add_notification_for_user(new_notification=notification, user_id=user_id) #------------------------------------------------------------------------------------------------------------------- # Generate an event that can be picked by a notification worker so that it can update its user_info dictionary #------------------------------------------------------------------------------------------------------------------- log.debug("(create notification) Publishing ReloadUserInfoEvent for notification_id: %s", notification_id) self.event_publisher.publish_event( event_type= "ReloadUserInfoEvent", origin="UserNotificationService", description= "A notification has been created.", notification_id = notification_id) return notification_id
def create_notification(self, notification=None, user_id=''): """ Persists the provided NotificationRequest object for the specified Origin id. Associate the Notification resource with the user_id string. returned id is the internal id by which NotificationRequest will be identified in the data store. @param notification NotificationRequest @param user_id str @retval notification_id str @throws BadRequest if object passed has _id or _rev attribute """ if not user_id: raise BadRequest("User id not provided.") log.debug( "Create notification called for user_id: %s, and notification: %s", user_id, notification) #--------------------------------------------------------------------------------------------------- # Persist Notification object as a resource if it has already not been persisted #--------------------------------------------------------------------------------------------------- notification_id = None # if the notification has already been registered, simply use the old id existing_user_notifications = self.get_user_notifications( user_info_id=user_id) if existing_user_notifications: notification_id = self._notification_in_notifications( notification, existing_user_notifications) # since the notification has not been registered yet, register it and get the id temporal_bounds = TemporalBounds() temporal_bounds.start_datetime = get_ion_ts() temporal_bounds.end_datetime = '' if not notification_id: notification.temporal_bounds = temporal_bounds notification_id, rev = self.clients.resource_registry.create( notification) else: log.debug( "Notification object has already been created in resource registry before. No new id to be generated. notification_id: %s", notification_id) # Read the old notification already in the resource registry notification = self.clients.resource_registry.read(notification_id) # Update the temporal bounds of the old notification resource notification.temporal_bounds = temporal_bounds # Update the notification in the resource registry self.clients.resource_registry.update(notification) log.debug( "The temporal bounds for this resubscribed notification object with id: %s, is: %s", notification._id, notification.temporal_bounds) # Link the user and the notification with a hasNotification association assocs = self.clients.resource_registry.find_associations( subject=user_id, predicate=PRED.hasNotification, object=notification_id, id_only=True) if assocs: log.debug( "Got an already existing association: %s, between user_id: %s, and notification_id: %s", assocs, user_id, notification_id) return notification_id else: log.debug( "Creating association between user_id: %s, and notification_id: %s", user_id, notification_id) self.clients.resource_registry.create_association( user_id, PRED.hasNotification, notification_id) # read the registered notification request object because this has an _id and is more useful notification = self.clients.resource_registry.read(notification_id) #------------------------------------------------------------------------------------------------------------------- # Generate an event that can be picked by a notification worker so that it can update its user_info dictionary #------------------------------------------------------------------------------------------------------------------- log.debug( "(create notification) Publishing ReloadUserInfoEvent for notification_id: %s", notification_id) self.event_publisher.publish_event( event_type=OT.ReloadUserInfoEvent, origin="UserNotificationService", description="A notification has been created.", notification_id=notification_id) return notification_id