def test_news_label_filtering(self):
        actions.login(self.STUDENT_EMAIL)
        actions.register(self, 'John Smith')

        label_foo = models.LabelDAO.save(models.LabelDTO(
            None, {'title': 'Foo',
                   'descripton': 'foo',
                   'type': models.LabelDTO.LABEL_TYPE_COURSE_TRACK}))
        label_bar = models.LabelDAO.save(models.LabelDTO(
            None, {'title': 'Bar',
                   'descripton': 'bar',
                   'type': models.LabelDTO.LABEL_TYPE_COURSE_TRACK}))

        now_ts = utc.now_as_timestamp() + 3  # Avoid filtering in-past items
        news.CourseNewsDao.add_news_item(news.NewsItem(
            'test:no_labels', 'url_no_labels',
            when=utc.timestamp_to_datetime(now_ts)))
        news.CourseNewsDao.add_news_item(news.NewsItem(
            'test:with_labels', 'url_with_labels',
            labels=common_utils.list_to_text([label_foo]),
            when=utc.timestamp_to_datetime(now_ts - 1)))

        # Student starts life with no labels, so should match both items.
        response = self.get('course')
        soup = self.parse_html_string_to_soup(response.body)
        self.assertEquals(
            [news_tests_lib.NewsItem(
                'Test Item no_labels', 'url_no_labels', True),
             news_tests_lib.NewsItem(
                 'Test Item with_labels', 'url_with_labels', True)],
            news_tests_lib.extract_news_items_from_soup(soup))

        # Apply non-matching label to Student; should not see labeled news.
        models.Student.set_labels_for_current(
            common_utils.list_to_text([label_bar]))
        response = self.get('course')
        soup = self.parse_html_string_to_soup(response.body)
        self.assertEquals(
            [news_tests_lib.NewsItem(
                'Test Item no_labels', 'url_no_labels', True)],
            news_tests_lib.extract_news_items_from_soup(soup))

        # Apply matching label to Student; should again see labeled news.
        models.Student.set_labels_for_current(
            common_utils.list_to_text([label_foo, label_bar]))
        response = self.get('course')
        soup = self.parse_html_string_to_soup(response.body)
        self.assertEquals(
            [news_tests_lib.NewsItem(
                'Test Item no_labels', 'url_no_labels', True),
             news_tests_lib.NewsItem(
                 'Test Item with_labels', 'url_with_labels', True)],
            news_tests_lib.extract_news_items_from_soup(soup))
Пример #2
0
    def post(self):
        student = self.personalize_page_and_get_enrolled()
        if not student:
            return
        if not self.assert_xsrf_token_or_fail(self.request, 'student-edit'):
            return

        all_track_label_ids = models.LabelDAO.get_set_of_ids_of_type(
            models.LabelDTO.LABEL_TYPE_COURSE_TRACK)
        new_track_label_ids = set([
            int(label_id) for label_id in self.request.get_all('labels')
            if label_id and int(label_id) in all_track_label_ids
        ])
        student_label_ids = set([
            int(label_id)
            for label_id in common_utils.text_to_list(student.labels)
            if label_id
        ])

        # Remove all existing track (and only track) labels from student,
        # then merge in selected set from form.
        student_label_ids = student_label_ids.difference(all_track_label_ids)
        student_label_ids = student_label_ids.union(new_track_label_ids)
        models.Student.set_labels_for_current(
            common_utils.list_to_text(list(student_label_ids)))

        self.redirect('/student/home')
Пример #3
0
    def post(self):
        student = self.personalize_page_and_get_enrolled()
        if not student:
            return
        if not self.assert_xsrf_token_or_fail(self.request, 'student-edit'):
            return

        all_track_label_ids = models.LabelDAO.get_set_of_ids_of_type(
            models.LabelDTO.LABEL_TYPE_COURSE_TRACK)
        new_track_label_ids = set(
            [int(label_id)
             for label_id in self.request.get_all('labels')
             if label_id and int(label_id) in all_track_label_ids])
        student_label_ids = set(
            [int(label_id)
             for label_id in common_utils.text_to_list(student.labels)
             if label_id])

        # Remove all existing track (and only track) labels from student,
        # then merge in selected set from form.
        student_label_ids = student_label_ids.difference(all_track_label_ids)
        student_label_ids = student_label_ids.union(new_track_label_ids)
        models.Student.set_labels_for_current(
            common_utils.list_to_text(list(student_label_ids)))

        self.redirect('/student/home')
Пример #4
0
    def put(self):
        """Handles REST PUT verb with JSON payload."""
        request = transforms.loads(self.request.get('request'))
        key = request.get('key')

        if not self.assert_xsrf_token_or_fail(request, 'announcement-put',
                                              {'key': key}):
            return

        if not AnnouncementsRights.can_edit(self):
            transforms.send_json_response(self, 401, 'Access denied.',
                                          {'key': key})
            return

        entity = AnnouncementEntity.get(key)
        if not entity:
            transforms.send_json_response(self, 404, 'Object not found.',
                                          {'key': key})
            return

        schema = AnnouncementsItemRESTHandler.SCHEMA(
            'Announcement',
            self.get_course().get_course_announcement_list_email())

        payload = request.get('payload')
        update_dict = transforms.json_to_dict(transforms.loads(payload),
                                              schema.get_json_schema_dict())

        entity.labels = utils.list_to_text(
            LabelGroupsHelper.decode_labels_group(
                update_dict.get('label_groups')))

        transforms.dict_to_entity(entity, update_dict)

        entity.put()
        AnnouncementUtil.update_course_with_last_announcement(
            self.app_context.get_namespace_name())

        email_sent = False
        if entity.send_email:
            email_manager = notify.EmailManager(self.get_course())
            email_sent = email_manager.send_announcement(
                entity.title, entity.html, intent=ANNOUNCEMENTS_INTENT)

        if entity.send_email and not email_sent:
            if not self.get_course().get_course_announcement_list_email():
                message = 'Saved. Announcement list not configured.'
            else:
                message = 'Saved, but there was an error sending email.'
        else:
            message = 'Saved.'
        transforms.send_json_response(self, 200, message)
Пример #5
0
    def put(self):
        """Handles REST PUT verb with JSON payload."""
        request = transforms.loads(self.request.get('request'))
        key = request.get('key')

        if not self.assert_xsrf_token_or_fail(
                request, 'announcement-put', {'key': key}):
            return

        if not AnnouncementsRights.can_edit(self):
            transforms.send_json_response(
                self, 401, 'Access denied.', {'key': key})
            return

        entity = AnnouncementEntity.get(key)
        if not entity:
            transforms.send_json_response(
                self, 404, 'Object not found.', {'key': key})
            return

        schema = AnnouncementsItemRESTHandler.SCHEMA(
            'Announcement',
            self.get_course().get_course_announcement_list_email())

        payload = request.get('payload')
        update_dict = transforms.json_to_dict(
            transforms.loads(payload), schema.get_json_schema_dict())

        entity.labels = utils.list_to_text(
            LabelGroupsHelper.decode_labels_group(
                update_dict.get('label_groups')))

        transforms.dict_to_entity(entity, update_dict)

        entity.put()

        email_sent = False
        if entity.send_email:
            email_manager = notify.EmailManager(self.get_course())
            email_sent = email_manager.send_announcement(
                entity.title, entity.html)

        if entity.send_email and not email_sent:
            if not self.get_course().get_course_announcement_list_email():
                message = 'Saved. Announcement list not configured.'
            else:
                message = 'Saved, but there was an error sending email.'
        else:
            message = 'Saved.'
        transforms.send_json_response(self, 200, message)
Пример #6
0
 def test_register_with_labels(self):
     student_name = 'John Smith from Back East'
     actions.login(LABELS_STUDENT_EMAIL)
     user = users.get_current_user()
     response = actions.view_registration(self, COURSE_NAME)
     register_form = actions.get_form_by_action(response, 'register')
     self.post('/%s/%s' % (COURSE_NAME, register_form.action), {
         'form01': student_name,
         'xsrf_token': register_form['xsrf_token'].value,
         'labels': common_utils.list_to_text([self.bar_id, self.baz_id])})
     self._verify_labels(self.get(STUDENT_LABELS_URL), [self.bar_id,
                                                        self.baz_id])
     with common_utils.Namespace(NAMESPACE):
         student = models.Student.get_enrolled_student_by_user(user)
         self.assertEquals(student.name, student_name)
Пример #7
0
 def test_register_with_labels(self):
     user = self.make_test_user(LABELS_STUDENT_EMAIL)
     student_name = 'John Smith from Back East'
     actions.login(user.email())
     response = actions.view_registration(self, COURSE_NAME)
     register_form = actions.get_form_by_action(response, 'register')
     self.post('/%s/%s' % (COURSE_NAME, register_form.action), {
         'form01': student_name,
         'xsrf_token': register_form['xsrf_token'].value,
         'labels': common_utils.list_to_text([self.bar_id, self.baz_id])})
     self._verify_labels(self.get(STUDENT_LABELS_URL), [self.bar_id,
                                                        self.baz_id])
     with common_utils.Namespace(NAMESPACE):
         student = models.Student.get_enrolled_student_by_user(user)
         self.assertEquals(student.name, student_name)
Пример #8
0
    def _apply_updates_common(self, unit, updated_unit_dict, errors):
        """Apply changes common to all unit types."""
        unit.title = updated_unit_dict.get('title')
        unit.description = updated_unit_dict.get('description')
        unit.now_available = not updated_unit_dict.get('is_draft')

        labels = LabelGroupsHelper.decode_labels_group(
            updated_unit_dict['label_groups'])

        if self._course.get_parent_unit(unit.unit_id):
            track_label_ids = models.LabelDAO.get_set_of_ids_of_type(
                models.LabelDTO.LABEL_TYPE_COURSE_TRACK)
            if track_label_ids.intersection(labels):
                errors.append('Cannot set track labels on entities which '
                              'are used within other units.')

        unit.labels = common_utils.list_to_text(labels)
Пример #9
0
    def _apply_updates_common(self, unit, updated_unit_dict, errors):
        """Apply changes common to all unit types."""
        unit.title = updated_unit_dict.get('title')
        unit.description = updated_unit_dict.get('description')
        unit.now_available = not updated_unit_dict.get('is_draft')
        unit.shown_when_unavailable = updated_unit_dict.get(
            'shown_when_unavailable')

        labels = LabelGroupsHelper.decode_labels_group(
            updated_unit_dict['label_groups'])

        if self._course.get_parent_unit(unit.unit_id):
            track_label_ids = models.LabelDAO.get_set_of_ids_of_type(
                models.LabelDTO.LABEL_TYPE_COURSE_TRACK)
            if track_label_ids.intersection(labels):
                errors.append('Cannot set track labels on entities which '
                              'are used within other units.')

        unit.labels = common_utils.list_to_text(labels)
    def put(self):
        """Handles REST PUT verb with JSON payload."""
        request = transforms.loads(self.request.get('request'))
        key = request.get('key')

        if not self.assert_xsrf_token_or_fail(
                request, 'announcement-put', {'key': key}):
            return

        if not AnnouncementsRights.can_edit(self):
            transforms.send_json_response(
                self, 401, 'Access denied.', {'key': key})
            return

        entity = AnnouncementEntity.get(key)
        if not entity:
            transforms.send_json_response(
                self, 404, 'Object not found.', {'key': key})
            return

        schema = AnnouncementsItemRESTHandler.SCHEMA()

        payload = request.get('payload')
        update_dict = transforms.json_to_dict(
            transforms.loads(payload), schema.get_json_schema_dict())

        # The datetime widget returns a datetime object and we need a UTC date.
        update_dict['date'] = datetime.datetime.strptime(
            update_dict['date'], courses.ISO_8601_DATE_FORMAT).date()

        entity.labels = utils.list_to_text(
            LabelGroupsHelper.decode_labels_group(
                update_dict.get('label_groups')))

        transforms.dict_to_entity(entity, update_dict)

        entity.put()

        transforms.send_json_response(self, 200, 'Saved.')
Пример #11
0
    def put(self):
        """Handles REST PUT verb with JSON payload."""
        request = transforms.loads(self.request.get('request'))
        key = request.get('key')

        if not self.assert_xsrf_token_or_fail(
                request, 'announcement-put', {'key': key}):
            return

        if not AnnouncementsRights.can_edit(self):
            transforms.send_json_response(
                self, 401, 'Access denied.', {'key': key})
            return

        entity = AnnouncementEntity.get(key)
        if not entity:
            transforms.send_json_response(
                self, 404, 'Object not found.', {'key': key})
            return

        schema = AnnouncementsItemRESTHandler.SCHEMA()

        payload = request.get('payload')
        update_dict = transforms.json_to_dict(
            transforms.loads(payload), schema.get_json_schema_dict())

        # The datetime widget returns a datetime object and we need a UTC date.
        update_dict['date'] = update_dict['date'].date()

        entity.labels = common_utils.list_to_text(
            resources_display.LabelGroupsHelper.field_data_to_labels(
                update_dict))
        resources_display.LabelGroupsHelper.remove_label_field_data(update_dict)

        transforms.dict_to_entity(entity, update_dict)

        entity.put()

        transforms.send_json_response(self, 200, 'Saved.')
Пример #12
0
 def test_join_empty(self):
     self.assertEquals('', utils.list_to_text([]))
Пример #13
0
 def test_join_two(self):
     self.assertEquals("x y", utils.list_to_text(["x", "y"]))
Пример #14
0
 def test_join_split(self):
     l = ["a", "b", "c"]
     self.assertListEqual(l, utils.text_to_list(utils.list_to_text(l)))
 def _save_labels(self, student, labels):
     student.labels = common_utils.list_to_text(labels)
     student.put()
Пример #16
0
 def test_join_one(self):
     self.assertEquals("x", utils.list_to_text(["x"]))
Пример #17
0
 def test_split_join(self):
     text = 'a b c'
     self.assertEquals(text, utils.list_to_text(utils.text_to_list(text)))
Пример #18
0
 def test_join_split(self):
     l = ['a', 'b', 'c']
     self.assertListEqual(l, utils.text_to_list(utils.list_to_text(l)))
Пример #19
0
 def test_join_one(self):
     self.assertEquals('x', utils.list_to_text(['x']))
Пример #20
0
 def test_join_none(self):
     self.assertEquals('', utils.list_to_text(None))
Пример #21
0
 def test_join_none(self):
     self.assertEquals('', utils.list_to_text(None))
 def _save_labels(self, student, labels):
     student.labels = common_utils.list_to_text(labels)
     student.put()
Пример #23
0
 def test_join_empty(self):
     self.assertEquals('', utils.list_to_text([]))
Пример #24
0
 def test_join_one(self):
     self.assertEquals('x', utils.list_to_text(['x']))
Пример #25
0
 def test_join_two(self):
     self.assertEquals('x y', utils.list_to_text(['x', 'y']))
Пример #26
0
 def test_join_two(self):
     self.assertEquals('x y', utils.list_to_text(['x', 'y']))
Пример #27
0
 def test_split_join(self):
     text = 'a b c'
     self.assertEquals(text, utils.list_to_text(utils.text_to_list(text)))
Пример #28
0
 def test_join_split(self):
     l = ['a', 'b', 'c']
     self.assertListEqual(l, utils.text_to_list(utils.list_to_text(l)))