def guide_get(self, config): student, can_display, displayability = ( can_display_guide_to_current_user(self.get_course(), config)) if not can_display: self.error( 404, 'Negative displayability: %s' % str(displayability)) return unit_id = self.request.get('unit_id') if not unit_id: self.error(404, 'Bad unit_id') return view = unit_outline.StudentCourseView( self.get_course(), student=student) unit = common_utils.find( lambda u: str(u.unit_id) == str(unit_id), view.get_units()) if not unit: self.error(404, 'Unit not visible') return self.prepare(config, view, student, unit, view.get_lessons(unit.unit_id), self.template_value) template = jinja_utils.get_template( 'steps.html', TEMPLATE_DIRS, handler=self) self.response.write(template.render(self.template_value))
def remove_news_item(self, resource_key): news_items = self.get_news_items() item = common_utils.find(lambda i: i.resource_key == resource_key, news_items) if not item: return False news_items.remove(item) self._set_news_items(news_items) return True
def remove_news_item(self, resource_key): news_items = self.get_news_items() item = common_utils.find( lambda i: i.resource_key == resource_key, news_items) if not item: return False news_items.remove(item) self._set_news_items(news_items) return True
def get_i18n_title(cls, resource_key): locale = None app_context = sites.get_course_for_current_request() if (app_context and app_context.default_locale != app_context.get_current_locale()): locale = app_context.get_current_locale() announcements = AnnouncementEntity.get_announcements(locale) item = common_utils.find( lambda a: a.key().id() == int(resource_key.key), announcements) return item.title if item else None
def add_news_item(self, news_item, overwrite_existing): news_items = self.get_news_items() # Only one News item per course object. If user has not seen older # alert, no point retaining it. old_item = common_utils.find( lambda i: i.resource_key == news_item.resource_key, news_items) if old_item: if overwrite_existing and old_item.when < news_item.when: news_items.remove(old_item) news_items.append(news_item) else: news_items.append(news_item) self._set_news_items(news_items)
def _perform_checks(self): lesson = None progress = None success, key, student, course = ( super(LessonProgressRESTHandler, self)._perform_checks()) if success: progress = course.get_progress_tracker() lesson = common_utils.find(lambda l: str(l.lesson_id) == key, course.get_lessons_for_all_units()) if not lesson: success = False transforms.send_json_response( self, 400, 'Bad Request.', {'key': key}) return success, key, student, lesson, progress
def put(self): """Handles PUT REST verb to save lesson and associated activity.""" request = transforms.loads(self.request.get('request')) key = request.get('key') if not self.assert_xsrf_token_or_fail(request, 'lesson-edit', {'key': key}): return if not roles.Roles.is_course_admin(self.app_context): transforms.send_json_response(self, 401, 'Access denied.', {'key': key}) return course = courses.Course(self) lesson = course.find_lesson_by_id(None, key) if not lesson: transforms.send_json_response(self, 404, 'Object not found.', {'key': key}) return payload = request.get('payload') updates_dict = transforms.json_to_dict( transforms.loads(payload), self.get_schema(course, key).get_json_schema_dict()) lesson.title = updates_dict['title'] lesson.unit_id = common_utils.find(lambda unit: unit['selected'], updates_dict['unit_id'])['value'] lesson.scored = (updates_dict['scored'] == 'scored') lesson.objectives = updates_dict['objectives'] lesson.video = updates_dict['video'] lesson.notes = updates_dict['notes'] lesson.auto_index = updates_dict['auto_index'] lesson.activity_title = updates_dict['activity_title'] lesson.activity_listed = updates_dict['activity_listed'] lesson.manual_progress = updates_dict['manual_progress'] activity = updates_dict.get('activity', '').strip() errors = [] if activity: if lesson.has_activity: course.set_activity_content(lesson, activity, errors=errors) else: errors.append('Old-style activities are not supported.') else: lesson.has_activity = False fs = self.app_context.fs path = fs.impl.physical_to_logical( course.get_activity_filename(lesson.unit_id, lesson.lesson_id)) if fs.isfile(path): fs.delete(path) if not errors: common_utils.run_hooks(self.PRE_SAVE_HOOKS, lesson, updates_dict) assert course.update_lesson(lesson) course.save() common_utils.run_hooks(self.POST_SAVE_HOOKS, lesson) transforms.send_json_response(self, 200, 'Saved.') else: transforms.send_json_response(self, 412, '\n'.join(errors))