class TestTLEntryNotice(BaseTestNotification): """ Test case for the TLEntryNotice object """ def setUp(self): super(TestTLEntryNotice, self).setUp() self.notice = TLEntryNotice() # test timeline entry self.entry = TimelineEntry.objects.create( title="Changes to ", changes="Test Changes", status="Draft", entry_type="Draft", module_code=self.module.module_code, object_id=self.module.module_code, content_object=self.module, approved_by=self.module_leader) self.watcher = WatcherWrapper(self.user) self.watcher.add_module(self.module) def test_create_notification(self): """ Test the creation of the notification """ sample_data = {'entry': self.entry} # expected output expected_msg = self.notice.content_template.format( self.module.module_code) expected_link = reverse(self.notice.link_name, kwargs={'module_pk': self.module.module_code}) # create the notification self.notice.create(**sample_data) # get the notification notification = self.model.objects.filter(recipient=self.user) self.assertEquals(notification.count(), 1) notice = notification.first() self.assertFalse(notice.seen) self.assertEquals(notice.content, expected_msg) self.assertEquals(notice.link, expected_link) self.assertEquals(notice.recipient, self.user) def test_create_notification_invalid_params(self): """ Test exception is raised when invalid params are provided """ # test with explict None in kwarg sample_data = {'entry': None} with self.assertRaises(ValueError): self.notice.create(**sample_data) # test with no kwargs with self.assertRaises(ValueError): self.notice.create()
def __process_watchers(self): # add modules that the user needs to review # to thier notification watch list user = self.object.user # get all the modules modules = list(self.object.modules.all()) watcher = WatcherWrapper(user) watcher.bulk_module_add(*modules)
def post(self, request, *args, **kwargs): # remove all modules from list that were being watched obj = self.get_object() modules = list(obj.modules.all()) watcher = WatcherWrapper(obj.user) watcher.bulk_module_remove(*modules) return super(AdminReviewerDeleteView, self).post(request, *args, **kwargs)
def __process_watchers(self): """ Private method to add modules to tutor watch list """ user = self.object.programme_tutor_user # get the modules modules = self.object.modules.all() # add the modules to user's watch list tutor_watcher = WatcherWrapper(user) tutor_watcher.bulk_module_add(*list(modules))
def __process_watchers(self): """ Private method that removes the modules that are being watched by these tutor. """ obj = self.get_object() modules = obj.modules.all() # remove the modules from watch list watcher = WatcherWrapper(obj.programme_tutor_user) watcher.bulk_module_remove(*list(modules))
def post(self, request, *args, **kwargs): original_module_leader = self.get_object().module_leader response = super( AdminModuleUpdateView, self).post(request, *args, **kwargs) # check to see that the module leader may have changed. current_module_leader = self.object.module_leader if current_module_leader != original_module_leader: # create watcher objects for module leaders original_ml_watcher = WatcherWrapper(original_module_leader) current_ml_watcher = WatcherWrapper(current_module_leader) # update module status by removing or adding module original_ml_watcher.remove_module(self.object) current_ml_watcher.add_module(self.object) # push the notification to the new module leader push_notification( 'module_leader', module_code=self.object.module_code, module_leader=current_module_leader ) # publish changes to timeline publish_changes(self.object, request.user) return response
def get_success_url(self): # before sending success url, connect the user # to recieve notifiations obj = self.object publish_changes(obj, self.request.user) watcher = WatcherWrapper(obj.module_leader) # add the module created to thier list watcher.add_module(obj) # push notification to module leader push_notification( 'module_leader', module_code=obj.module_code, module_leader=obj.module_leader ) return reverse('all_modules')
def setUp(self): super(TestTLEntryNotice, self).setUp() self.notice = TLEntryNotice() # test timeline entry self.entry = TimelineEntry.objects.create( title="Changes to ", changes="Test Changes", status="Draft", entry_type="Draft", module_code=self.module.module_code, object_id=self.module.module_code, content_object=self.module, approved_by=self.module_leader) self.watcher = WatcherWrapper(self.user) self.watcher.add_module(self.module)
def get(self, request, *args, **kwargs): username = request.user.username user_id = request.user.pk # remove old notifications self.__remove_old_notifications(username) unseen = self.model.objects.get_unseen_notifications(username) all_notifications = self.model.objects.get_all_notifications(username) # get all the modules the user is watching user = User.objects.get(pk=user_id) watcher = WatcherWrapper(user) watching = watcher.modules() context = { 'unseen': unseen, 'all': all_notifications, 'watching': watching, } return render(request, self.template, context)
def test_wrapper_incorrect_types(self): """ Test for ensuring that incorrect data types are not processed for certain methods. """ # incorrect data for the constructor incorrect_data_list = ["user", 1234, self.module] for data in incorrect_data_list: with self.assertRaises(ValueError): WatcherWrapper(data) watcher = self.obj(self.user) # test incorrect data for add and remove single modules incorrect_data_list = [234646, 2.334, self.user] for data in incorrect_data_list: with self.assertRaises(ValueError): watcher.add_module(data) with self.assertRaises(ValueError): watcher.remove_module(data)
def __process_watchers(self, request): """ Private method to update the tutor's watch list by removing and adding relevant modules """ # get the list of new module codes module_codes = request.POST.getlist('modules', None) obj = self.get_object() watcher = WatcherWrapper(obj.programme_tutor_user) # get the current and new modules new_modules = set(Module.objects.filter(module_code__in=module_codes)) current_modules = set(obj.modules.all()) # determine what needs to be removed modules_to_remove = list(current_modules.difference(new_modules)) # update the modules this tutor is watching watcher.bulk_module_remove(*modules_to_remove) watcher.bulk_module_add(*list(new_modules))
def __process_watchers(self, request): """ Private method that updates notification watcher for a reviewer. """ # get the new list of module codes module_codes = request.POST.getlist('modules', None) obj = self.get_object() watcher = WatcherWrapper(obj.user) # get the new modules new_modules = set(Module.objects.filter(module_code__in=module_codes)) old_modules = set(obj.modules.all()) # compare the old modules to new to determine which # modules are no longer being reviewed by reviewer modules_to_remove = list(old_modules.difference(new_modules)) # remove modules that are not being reviewed by user watcher.bulk_module_remove(*modules_to_remove) # add any new modules that are being reviewed watcher.bulk_module_add(*list(new_modules))
def setUp(self): super(BaseTestNotification, self).setUp() self.model = Notification self.watcher = WatcherWrapper(self.user)