Exemple #1
0
 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)
Exemple #2
0
 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.user, action)
     if self.is_public():
         utils.notify(followers(self), action)
Exemple #3
0
 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)
Exemple #4
0
 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.')
Exemple #5
0
 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.')
Exemple #6
0
 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.')
Exemple #7
0
 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.')