def _get_and_validate_course_access(user, course_id): """ Check if course_id exists and is accessible by the user. Returns a course_module object """ course_key = CourseKey.from_string(course_id) course_module = get_course_and_check_access(course_key, user) if not course_module: raise NotFound( 'Course with course_id {} does not exist.'.format(course_id)) return course_module
def update(self, instance, validated_data): course_run_key = instance.id _id = validated_data.pop('id') team = validated_data.pop('team', []) user = self.context['request'].user fields = { 'display_name': instance.display_name } fields.update(validated_data) new_course_run_key = rerun_course(user, course_run_key, course_run_key.org, course_run_key.course, _id['run'], fields, async=False) course_run = get_course_and_check_access(new_course_run_key, user) self.update_team(course_run, team) return course_run
def update(self, instance, validated_data): course_run_key = instance.id _id = validated_data.pop('id') team = validated_data.pop('team', []) user = self.context['request'].user fields = {'display_name': instance.display_name} fields.update(validated_data) new_course_run_key = rerun_course(user, course_run_key, course_run_key.org, course_run_key.course, _id['run'], fields, False) course_run = get_course_and_check_access(new_course_run_key, user) self.update_team(course_run, team) return course_run
def get_object(self): lookup_url_kwarg = self.lookup_url_kwarg or self.lookup_field assert lookup_url_kwarg in self.kwargs, ( 'Expected view %s to be called with a URL keyword argument ' 'named "%s". Fix your URL conf, or set the `.lookup_field` ' 'attribute on the view correctly.' % (self.__class__.__name__, lookup_url_kwarg)) course_run_key = CourseKey.from_string(self.kwargs[lookup_url_kwarg]) course_run = get_course_and_check_access(course_run_key, self.request.user) if course_run: return course_run raise Http404
def course_custom_settings(request, course_key_string): """ Course custom settings configuration GET html: get the page PUT, POST json: update the Course's custom settings. """ course_key = CourseKey.from_string(course_key_string) with modulestore().bulk_operations(course_key): course_module = get_course_and_check_access(course_key, request.user) try: settings = CustomSettings.objects.get(id=course_key) except ObjectDoesNotExist as exc: return HttpResponseBadRequest(django.utils.html.escape( exc.message), content_type="text/plain") if 'text/html' in request.META.get('HTTP_ACCEPT', '') and request.method == 'GET': return render_to_response( 'custom_settings.html', { 'context_course': course_module, 'custom_dict': { 'is_featured': settings.is_featured, 'tags': settings.tags }, 'custom_settings_url': reverse('custom_settings', kwargs={'course_key_string': unicode(course_key)}), }) elif 'application/json' in request.META.get( 'HTTP_ACCEPT', '') and request.method in ['POST', 'PUT']: body = json.loads(request.body) settings.is_featured = body.get('is_featured') settings.tags = body.get('tags') settings.save() return JsonResponse(body) else: return HttpResponseNotAllowed("Bad Request", content_type="text/plain")
def update(self, instance, validated_data): def _execute_method_and_log_time(func, *args): """ Call func passed in method with logging the time it took to complete. Temporarily added for EDUCATOR-4013, we will remove this once we get the required information. """ course_run_key = args[1] if 'MITx+7.00x' not in unicode(course_run_key): return func(*args) start_time = time.time() output = func(*args) log.info(u'[%s] [%s] completed in [%f]', func.__name__, course_run_key, (time.time() - start_time)) return output course_run_key = instance.id _id = validated_data.pop('id') team = validated_data.pop('team', []) user = self.context['request'].user fields = { 'display_name': instance.display_name } fields.update(validated_data) new_course_run_key = _execute_method_and_log_time( rerun_course, user, course_run_key, course_run_key.org, course_run_key.course, _id['run'], fields, False ) course_run = get_course_and_check_access(new_course_run_key, user) self.update_team(course_run, team) return course_run