Skip to content

Commit

Permalink
Placed the user_info underneath the UNS service and not the email eve…
Browse files Browse the repository at this point in the history
…nt processor it launches. Makes the code more direct. Also fixed unit tests. All unit tests passing
  • Loading branch information
swarbhanu committed Nov 29, 2012
1 parent 341087d commit 20b9bf5
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 30 deletions.
4 changes: 2 additions & 2 deletions ion/services/dm/presentation/test/user_notification_test.py
Expand Up @@ -101,7 +101,7 @@ def test_create_notification(self):
self.user_notification.notifications = {}

self.user_notification.event_processor.add_notification_for_user = mocksignature(self.user_notification.event_processor.add_notification_for_user)

self.user_notification.update_user_info_dictionary = mocksignature(self.user_notification.update_user_info_dictionary)
self.user_notification.event_publisher.publish_event = mocksignature(self.user_notification.event_publisher.publish_event)

self.user_notification._notification_in_notifications = mocksignature(self.user_notification._notification_in_notifications)
Expand Down Expand Up @@ -219,8 +219,8 @@ def test_delete_user_notification(self):

notification_id = 'notification_id_1'

self.user_notification.event_processor.stop_notification_subscriber = mocksignature(self.user_notification.event_processor.stop_notification_subscriber)
self.user_notification.event_publisher.publish_event = mocksignature(self.user_notification.event_publisher.publish_event)
self.user_notification.user_info = {}

#-------------------------------------------------------------------------------------------------------------------
# Create a notification object
Expand Down
56 changes: 28 additions & 28 deletions ion/services/dm/presentation/user_notification_service.py
Expand Up @@ -149,6 +149,8 @@ def on_start(self):
#---------------------------------------------------------------------------------------------------

self.notifications = {}
self.user_info = {}
self.reverse_user_info = {}

#---------------------------------------------------------------------------------------------------
# Get the clients
Expand Down Expand Up @@ -270,6 +272,9 @@ def create_notification(self, notification=None, user_id=''):

user = self.event_processor.add_notification_for_user(notification_request=notification, user_id=user_id)

# Update the user info object with the notification
self.update_user_info_dictionary(user_id=user_id, new_notification=notification, old_notification=None)

#-------------------------------------------------------------------------------------------------------------------
# Generate an event that can be picked by a notification worker so that it can update its user_info dictionary
#-------------------------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -370,8 +375,6 @@ def delete_notification(self, notification_id=''):
notification_request = self.clients.resource_registry.read(notification_id)
old_notification = notification_request

self.event_processor.stop_notification_subscriber(notification_request=notification_request)

#-------------------------------------------------------------------------------------------------------------------
# Update the resource registry
#-------------------------------------------------------------------------------------------------------------------
Expand All @@ -384,7 +387,7 @@ def delete_notification(self, notification_id=''):
# Update the user info dictionaries
#-------------------------------------------------------------------------------------------------------------------

for user_id in self.event_processor.user_info.iterkeys():
for user_id in self.user_info.iterkeys():
self.update_user_info_dictionary(user_id, notification_request, old_notification)

#-------------------------------------------------------------------------------------------------------------------
Expand All @@ -411,14 +414,14 @@ def delete_notification_from_user_info(self, notification_id):

for user_id in user_ids:

value = self.event_processor.user_info[user_id]
value = self.user_info[user_id]

for notif in value['notifications']:
if notification_id == notif._id:
# remove the notification
value['notifications'].remove(notif)

self.event_processor.reverse_user_info = calculate_reverse_user_info(self.event_processor.user_info)
self.reverse_user_info = calculate_reverse_user_info(self.user_info)

def find_events(self, origin='', type='', min_datetime=0, max_datetime=0, limit= -1, descending=False):
"""
Expand Down Expand Up @@ -581,8 +584,8 @@ def get_user_notifications(self, user_id=''):
@retval notifications list of NotificationRequest objects
"""

if self.event_processor.user_info.has_key(user_id):
notifications = self.event_processor.user_info[user_id]['notifications']
if self.user_info.has_key(user_id):
notifications = self.user_info[user_id]['notifications']
ret = IonObject(OT.ComputedListValue)

if notifications:
Expand Down Expand Up @@ -668,7 +671,7 @@ def process_batch(self, start_time = 0, end_time = 0):
if end_time <= start_time:
return

for user_id, value in self.event_processor.user_info.iteritems():
for user_id, value in self.user_info.iteritems():

notifications = value['notifications']

Expand Down Expand Up @@ -755,7 +758,7 @@ def format_and_send_email(self, events_for_message, user_id):

self.send_batch_email( msg_body = msg_body,
msg_subject = msg_subject,
msg_recipient=self.event_processor.user_info[user_id]['user_contact'].email,
msg_recipient=self.user_info[user_id]['user_contact'].email,
smtp_client=self.smtp_client )

def send_batch_email(self, msg_body, msg_subject, msg_recipient, smtp_client):
Expand Down Expand Up @@ -823,38 +826,35 @@ def update_user_info_object(self, user_id, new_notification, old_notification):

def update_user_info_dictionary(self, user_id, new_notification, old_notification):

notifications = []

#------------------------------------------------------------------------------------
# Remove the old notifications
# If there was a previous notification which is being updated, check the dictionaries and update there
#------------------------------------------------------------------------------------
if old_notification:
# Remove the old notifications
if old_notification in self.user_info[user_id]['notifications']:

if old_notification in self.event_processor.user_info[user_id]['notifications']:

# remove from notifications list
self.event_processor.user_info[user_id]['notifications'].remove(old_notification)
# remove from notifications list
self.user_info[user_id]['notifications'].remove(old_notification)

#------------------------------------------------------------------------------------
# update the notification subscription object
#------------------------------------------------------------------------------------
# update that old notification subscription
notification_subscription._res_obj = new_notification

# update that old notification subscription
notification_subscription._res_obj = new_notification
# find the already existing notifications for the user
notifications = self.user_info[user_id]['notifications']

#------------------------------------------------------------------------------------
# find the already existing notifications for the user
# update the user info - contact information, notifications
#------------------------------------------------------------------------------------

notifications = self.event_processor.user_info[user_id]['notifications']
notifications.append(new_notification)

#------------------------------------------------------------------------------------
# update the user info - contact information, notifications
#------------------------------------------------------------------------------------
user = self.clients.resource_registry.read(user_id)

self.event_processor.user_info[user_id]['user_contact'] = user.contact
self.event_processor.user_info[user_id]['notifications'] = notifications
self.user_info[user_id]['user_contact'] = user.contact
self.user_info[user_id]['notifications'] = notifications

self.event_processor.reverse_user_info = calculate_reverse_user_info(self.event_processor.user_info)
self.reverse_user_info = calculate_reverse_user_info(self.user_info)

def get_subscriptions(self, resource_id='', include_nonactive=False):
"""
Expand Down

0 comments on commit 20b9bf5

Please sign in to comment.