def test_send_notification(self): """The email should send if there are notifications.""" # generate an action on an actor the user follows agency = AgencyFactory() foia = FOIARequestFactory(agency=agency) action = new_action(agency, "completed", target=foia) notify(self.user, action) # generate the email, which should contain the generated action email = self.digest(user=self.user, interval=self.interval) eq_(email.activity["count"], 1, "There should be activity.") eq_(email.send(), 1, "The email should send.")
def test_digest_follow_requests(self): """Digests should include information on requests I follow.""" # generate an action on a request the user owns other_user = UserFactory() foia = FOIARequestFactory(composer__user=other_user) agency = AgencyFactory() action = new_action(agency, "rejected", target=foia) notify(self.user, action) # generate the email, which should contain the generated action email = self.digest(user=self.user, interval=self.interval) eq_(email.activity["count"], 1, "There should be activity.") eq_(email.send(), 1, "The email should send.")
def save(self, *args, **kwargs): """Creates an action if question is newly asked""" is_new = True if self.pk is None else False super(Question, self).save(*args, **kwargs) if is_new: action = new_action(self.user, 'asked', target=self) # Notify users who subscribe to new question notifications new_question_subscribers = Profile.objects.filter( new_question_notifications=True) users_to_notify = [ profile.user for profile in new_question_subscribers ] notify(users_to_notify, action)
def save(self, *args, **kwargs): """Creates an action if question is newly asked""" # pylint: disable=signature-differs is_new = self.pk is None super(Question, self).save(*args, **kwargs) if is_new: action = new_action(self.user, "asked", target=self) # Notify users who subscribe to new question notifications new_question_subscribers = Profile.objects.filter( new_question_notifications=True) users_to_notify = [ profile.user for profile in new_question_subscribers ] notify(users_to_notify, action)
def save(self, *args, **kwargs): """Update the questions answer date when you save the answer""" is_new = True if self.pk is None else False super(Answer, self).save(*args, **kwargs) self.question.answer_date = self.date self.question.save() if is_new: action = new_action(self.user, 'answered', action_object=self, target=self.question) # Notify the question's owner and its followers about the new answer notify(self.question.user, action) notify(followers(self.question), action)
def save(self, *args, **kwargs): """Update the questions answer date when you save the answer""" # pylint: disable=signature-differs is_new = self.pk is None super(Answer, self).save(*args, **kwargs) self.question.answer_date = self.date self.question.save() if is_new: action = new_action(self.user, "answered", action_object=self, target=self.question) # Notify the question's owner and its followers about the new answer notify(self.question.user, action) notify(followers(self.question), action)
def notify(self, action): """ Notify the owner of the request. Notify followers if the request is not under embargo. Mark any existing notifications with the same message as read, to avoid notifying users with duplicated information. """ identical_notifications = ( Notification.objects.for_object(self).get_unread().filter( action__actor_object_id=action.actor_object_id, action__verb=action.verb)) for notification in identical_notifications: notification.mark_read() utils.notify(self.composer.user, action) if self.is_public(): utils.notify(followers(self), action)
def test_single_user(self): """Notify a single user about an action.""" user = UserFactory() notifications = notify(user, self.action) ok_(isinstance(notifications, list), 'A list should be returned.') ok_(isinstance(notifications[0], Notification), 'The list should contain notification objects.')
def test_many_users(self): """Notify many users about an action.""" users = [UserFactory(), UserFactory(), UserFactory()] notifications = notify(users, self.action) eq_(len(notifications), len(users), 'There should be a notification for every user in the list.') for user in users: notification_for_user = any(notification.user == user for notification in notifications) ok_(notification_for_user, 'Each user in the list should be notified.')
def test_get_question(self): """Try getting the detail page for a Question with an unread notification.""" question = QuestionFactory() view = QuestionDetail.as_view() # Create a notification for the question action = new_action(UserFactory(), 'answered', target=question) notification = notify(self.user, action)[0] ok_(not notification.read, 'The notification should be unread.') # Try getting the view as the user response = http_get_response(question.get_absolute_url(), view, self.user, pk=question.pk, slug=question.slug) eq_(response.status_code, 200, 'The view should respond 200 OK.') # Check that the notification has been read. notification.refresh_from_db() ok_(notification.read, 'The notification should be marked as read.')
def test_get_foia(self): """Try getting the detail page for a FOIA Request with an unread notification.""" agency = AgencyFactory() foia = FOIARequestFactory(agency=agency) view = FOIARequestDetail.as_view() # Create a notification for the request action = new_action(agency, 'completed', target=foia) notification = notify(self.user, action)[0] ok_(not notification.read, 'The notification should be unread.') # Try getting the view as the user response = http_get_response(foia.get_absolute_url(), view, self.user, idx=foia.pk, slug=foia.slug, jidx=foia.jurisdiction.pk, jurisdiction=foia.jurisdiction.slug) eq_(response.status_code, 200, 'The view should response 200 OK.') # Check that the notification has been read. notification.refresh_from_db() ok_(notification.read, 'The notification should be marked as read.')