def request_freeze(self, request, month=None): if request.method == "POST": month = Month.from_int(int(month)) intern = self.request.user # TODO: Check that month is not frozen or disabled or there is a freeze request # TODO: Check that month has not current rotation or rotation request form = FreezeRequestForm(data=request.data, instance=FreezeRequest(intern=intern, month=month)) if form.is_valid(): freeze_request = form.save() # Subscribe user to receive update notifications on the request subscribe(intern.settings_set.first(), "freeze_request_approved", object_id=freeze_request.id) subscribe(intern.settings_set.first(), "freeze_request_declined", object_id=freeze_request.id) # Notify medical internship unit of the request notify( "A new freeze request has been submitted by %s" % (request.user.profile.get_en_full_name()), "freeze_request_submitted", url="/planner/%d/" % freeze_request.intern.profile.intern.internship.id, ) # Display success message to user messages.success(request._request, "Your freeze request has been submitted successfully.") response_data = {'errors': form.errors} return Response(response_data) return FreezeRequestFormView.as_view()(request._request, month)
def request_freeze_cancel(self, request, month=None): intern = request.user month = Month.from_int(int(month)) current_freeze = intern.freezes.current_for_month(month) if not current_freeze: raise ObjectDoesNotExist("This month has no freeze to cancel.") if intern.freeze_cancel_requests.current_for_month(month): raise PermissionDenied("There is a pending freeze cancel request for this month already.") # TODO: Check that there are no rotations, leaves, freezes, or related requests in the month to be disabled cancel_request = FreezeCancelRequest.objects.create( intern=intern, month=month, ) # --notifications-- # Subscribe user to receive update notifications on the request subscribe(request.user.settings_set.first(), "freeze_cancel_request_approved", object_id=cancel_request.id) subscribe(request.user.settings_set.first(), "freeze_cancel_request_declined", object_id=cancel_request.id) # Notify medical internship unit of the request notify( "A freeze cancellation request has been submitted by %s" % (request.user.profile.get_en_full_name()), "freeze_cancel_request_submitted", url="/planner/%d/" % cancel_request.intern.profile.intern.internship.id, ) messages.success(request._request, "Your freeze cancellation request has been submitted successfully.") return Response(status=HTTP_201_CREATED)
def test_notify(self): # Subscribe User 1 to test key utils.subscribe(self.user1_settings, self.TEST_KEY) utils.notify("Test is a test", self.TEST_KEY) # Check that there is exactly 1 notification self.assertEqual(models.Notification.objects.all().count(), 1)
def set_up_staff_notification_subscriptions(sender, instance, **kwargs): if instance.role == Profile.STAFF: settings = instance.user.settings_set.first() subscribe(settings, "rotation_request_submitted") subscribe(settings, "leave_request_submitted") subscribe(settings, "leave_cancel_request_submitted") subscribe(settings, "freeze_request_submitted") subscribe(settings, "freeze_cancel_request_submitted")
def test_simple(self): TEST_KEY = 'test_key' user = User.objects.create_user('lalala') user_settings = models.Settings.objects.create(user=user) utils.subscribe(user_settings, TEST_KEY) utils.notify("Test Is a Test", TEST_KEY) self.assertEqual(models.Notification.objects.all().count(), 1)
def subscribeu(request): if not request.user.is_authenticated(): return HttpResponse("not authoride") from django_nyt.models import Settings settings = Settings.get_default_setting(request.user) subscribe(settings, "test") return HttpResponse("Ok")
def test_mark_read(self): utils.subscribe(self.user_settings, self.TEST_KEY) utils.notify("Test Is a Test", self.TEST_KEY) self.assertEqual( models.Notification.objects.filter(is_viewed=False).count(), 1) nid = models.Notification.objects.get().id self.client.login(username=self.user.username, password='******') self.client.get(reverse('nyt:json_mark_read', args=(nid, ))) self.assertEqual( models.Notification.objects.filter(is_viewed=False).count(), 0)
def test_mark_read(self): utils.subscribe(self.user_settings, self.TEST_KEY) utils.notify("Test Is a Test", self.TEST_KEY) self.assertEqual(models.Notification.objects.filter(is_viewed=False).count(), 1) nid = models.Notification.objects.get().id self.client.login(username=self.user.username, password='******') self.client.get(reverse('nyt:json_mark_read', args=(nid,))) self.assertEqual(models.Notification.objects.filter(is_viewed=False).count(), 0)
def test_simple(self): TEST_KEY = 'test_key' user = User.objects.create_user( 'lalala' ) user_settings = models.Settings.objects.create(user=user) utils.subscribe(user_settings, TEST_KEY) utils.notify("Test Is a Test", TEST_KEY) self.assertEqual(models.Notification.objects.all().count(), 1)
def create_users(apps, schema_editor): from django.contrib.auth import get_user_model user_model = get_user_model() for username, email in FIXTURES: user = user_model.objects.create( username=username, email=email, is_active=True, ) from django_nyt.models import Settings settings = Settings.get_default_setting(user) subscribe(settings, NOTIFICATION_TEST_KEY)
def notify_and_message(self, request, rotation_request): # Subscribe user to receive update notifications on the request subscribe(request.user.settings_set.first(), "rotation_request_approved", object_id=rotation_request.id) subscribe(request.user.settings_set.first(), "rotation_request_declined", object_id=rotation_request.id) # Notify medical internship unit of the request notify( "A new rotation request has been submitted by %s" % (request.user.profile.get_en_full_name()), "rotation_request_submitted", url="/interns/%d/" % rotation_request.internship.id, ) # FIXME: avoid sending a lot of simultaneous notifications # Display success message to user messages.success(request._request, "Your request has been submitted successfully.")
def test_notify_two_users(self): # Subscribe User 2 to test key utils.subscribe(self.user2_settings, self.TEST_KEY) utils.subscribe(self.user1_settings, self.TEST_KEY) utils.notify("Another test", self.TEST_KEY) self.assertEqual(models.Notification.objects.all().count(), 2) # Now create the same notification again, this should not create new # objects in the DB but instead increase the count of that notification! utils.notify("Another test", self.TEST_KEY) self.assertEqual(models.Notification.objects.filter(occurrences=2).count(), 2)
def test_notify_two_users(self): # Subscribe User 2 to test key utils.subscribe(self.user2_settings, self.TEST_KEY) utils.subscribe(self.user1_settings, self.TEST_KEY) utils.notify("Another test", self.TEST_KEY) self.assertEqual(models.Notification.objects.all().count(), 2) # Now create the same notification again, this should not create new # objects in the DB but instead increase the count of that notification! utils.notify("Another test", self.TEST_KEY) self.assertEqual( models.Notification.objects.filter(occurrences=2).count(), 2)
def subscribe_to_article(article, user): if user not in settings_map: settings_map[user], __ = Settings.objects.get_or_create(user=user) return subscribe( settings_map[user], ARTICLE_EDIT, content_type=ContentType.objects.get_for_model(article), object_id=article.id, )
def subscribe_to_article(article, user): if user not in settings_map: settings_map[user], __ = Settings.objects.get_or_create( user=user) return subscribe( settings_map[user], ARTICLE_EDIT, content_type=ContentType.objects.get_for_model(article), object_id=article.id)
def cancel_rotation(self, request, month=None): internship = request.user.profile.intern.internship month = Month.from_int(int(month)) current_rotation = internship.rotations.current_for_month(month) if not current_rotation: raise ObjectDoesNotExist("This month has no rotation to cancel.") if internship.rotation_requests.current_for_month(month): raise PermissionDenied("There is a pending rotation request for this month already.") requested_department = current_rotation.rotation_request.requested_department requested_department.id = None requested_department.save() rr = internship.rotation_requests.create( month=month, specialty=requested_department.department.specialty, requested_department=requested_department, is_delete=True, ) # --notifications-- # Subscribe user to receive update notifications on the request subscribe(request.user.settings_set.first(), "rotation_request_approved", object_id=rr.id) subscribe(request.user.settings_set.first(), "rotation_request_declined", object_id=rr.id) # Notify medical internship unit of the request notify( "A cancellation request has been submitted by %s" % (request.user.profile.get_en_full_name()), "rotation_request_submitted", url="/planner/%d/" % rr.internship.id, ) # FIXME: avoid sending a lot of simultaneous notifications messages.success(request._request, "Your cancellation request has been submitted successfully.") return Response(status=HTTP_201_CREATED)
def handle(self, *args, **options): user_settings = {} content_type = ContentType.objects.get_for_model(Article) for revision in ArticleRevision.objects.filter(deleted=False).exclude(user=None): if not revision.user in user_settings: user_settings[revision.user], __ = Settings.objects.get_or_create(user=revision.user) subscription = subscribe(user_settings[revision.user], ARTICLE_EDIT, content_type=content_type) ArticleSubscription.objects.get_or_create(subscription=subscription, article=revision.article)
def handle(self, *args, **options): user_settings = {} content_type = ContentType.objects.get_for_model(Article) for revision in ArticleRevision.objects.filter(deleted=False).exclude( user=None): if not revision.user in user_settings: user_settings[ revision.user], __ = Settings.objects.get_or_create( user=revision.user) subscription = subscribe( user_settings[revision.user], ARTICLE_EDIT, content_type=content_type, ) ArticleSubscription.objects.get_or_create( subscription=subscription, article=revision.article)