def test_region_subscription(self): user = get_user() r = Region.objects.get(name='1') Subscription.objects.create(user=user, region=r, lookup_id='r%s' % r.id) notify = Notify() emails = notify.gather_subscribers( Appeal.objects.filter(created_at__gte=notify.get_time_threshold()), RecordType.APPEAL, SubscriptionType.NEW, ) self.assertEqual(len(emails), 1) self.assertEqual(emails[0], user.email)
def response_change(self, request, obj): if "_notify-subscribers" in request.POST and request.user.is_superuser: notif_class = Notify() try: notif_class.notify(records=[obj], rtype=RecordType.FOLLOWED_EVENT, stype=SubscriptionType.NEW, uid=request.user.id) self.message_user(request, "Successfully notified subscribers.") except Exception: self.message_user(request, "Could not notify subscribers.", level=messages.ERROR) return HttpResponseRedirect(".") return super().response_change(request, obj)
def test_new_record_subscription(self): # Subscription to NEW_EMERGENCIES (orig: new field reports) user = get_user() Subscription.objects.create( user=user, rtype=RecordType.NEW_EMERGENCIES, #FIELD_REPORT, stype=SubscriptionType.NEW, ) notify = Notify() emails = notify.gather_subscribers( FieldReport.objects.filter(created_at__gte=notify.diff_5_minutes()), RecordType.NEW_EMERGENCIES, #FIELD_REPORT, SubscriptionType.NEW, ) self.assertEqual(len(emails), 1) self.assertEqual(emails[0], user.email)
def test_dtype_subscription(self): # Subscription to a disaster type user = get_user() d = DisasterType.objects.get(name='d1') Subscription.objects.create( user=user, dtype=d, lookup_id='d%s' % d.id, ) notify = Notify() emails = notify.gather_subscribers( FieldReport.objects.filter(created_at__gte=notify.diff_5_minutes()), RecordType.FIELD_REPORT, SubscriptionType.NEW, ) self.assertEqual(len(emails), 1) self.assertEqual(emails[0], user.email)
def test_region_subscription(self): # Subscription to a region user = get_user() r = Region.objects.get(name=1) Subscription.objects.create( user=user, region=r, lookup_id='r%s' % r.id, ) notify = Notify() emails = notify.gather_subscribers( FieldReport.objects.filter(created_at__gte=notify.diff_5_minutes()), RecordType.FIELD_REPORT, SubscriptionType.NEW, ) self.assertEqual(len(emails), 1) self.assertEqual(emails[0], user.email)
def test_country_subscription(self): # Subscription to a country user = get_user() c = Country.objects.get(name='c2') Subscription.objects.create( user=user, country=c, lookup_id='c%s' % c.id, ) notify = Notify() emails = notify.gather_subscribers( FieldReport.objects.filter(created_at__gte=notify.diff_5_minutes()), RecordType.FIELD_REPORT, SubscriptionType.NEW, ) self.assertEqual(len(emails), 1) self.assertEqual(emails[0], user.email)
def test_new_record_subscription(self): # Subscription to new field reports user = get_user() Subscription.objects.create( user=user, rtype=RecordType.FIELD_REPORT, stype=SubscriptionType.NEW, ) notify = Notify() emails = notify.gather_subscribers( FieldReport.objects.filter( created_at__gte=notify.get_time_threshold()), RecordType.FIELD_REPORT, SubscriptionType.NEW, ) self.assertEqual(len(emails), 1) self.assertEqual(emails[0], user.email)
def test_region_and_country_subscription(self): user = get_user() r = Region.objects.get(name='1') c = Country.objects.get(name='2') Subscription.objects.create( user=user, region=r, lookup_id='r%s' % r.id ) Subscription.objects.create( user=user, country=c, lookup_id='c%s' % c.id ) notify = Notify() emails = notify.gather_subscribers( Appeal.objects.filter(created_at__gte=notify.diff_5_minutes()), RecordType.APPEAL, SubscriptionType.NEW, ) self.assertEqual(len(emails), 1) self.assertEqual(emails[0], user.email)
def test_multiple_subscription(self): user1 = get_user() user2 = get_user() d = DisasterType.objects.get(name='d1') r = Region.objects.get(name=1) # User 1: Disaster type subscription Subscription.objects.create( user=user1, dtype=d, lookup_id='d%s' % d.id, ) # User 1: Region subscription Subscription.objects.create( user=user1, region=r, lookup_id='r%s' % r.id, ) # User 2: NEW_EMERGENCIES subscription (instead of the original New field report subscription) Subscription.objects.create( user=user2, rtype=RecordType.NEW_EMERGENCIES, #FIELD_REPORT, stype=SubscriptionType.NEW, ) notify = Notify() emails = notify.gather_subscribers( FieldReport.objects.filter( created_at__gte=notify.get_time_threshold()), RecordType.FIELD_REPORT, SubscriptionType.NEW, ) self.assertEqual(len(emails), 2) self.assertEqual(emails.sort(), [user1.email, user2.email].sort())
def test_filter_just_created(self): notify = Notify() filtered = notify.filter_just_created(Appeal.objects.filter(created_at__gte=notify.diff_5_minutes())) self.assertEqual(len(filtered), 1) self.assertEqual(filtered[0].aid, 'test2')
def test_filter_just_created(self): notify = Notify() filtered = notify.filter_just_created( Appeal.objects.filter(created_at__gte=notify.get_time_threshold())) self.assertEqual(len(filtered), 1) self.assertEqual(filtered[0].aid, 'test2')