def update(self, instance, validated_data): lms_course_id = validated_data.get('lms_course_id') # Handle saving the preview URL as a slug first preview_url = validated_data.pop('preview_url', None) if preview_url: self.update_preview_url(preview_url, lms_course_id or instance.lms_course_id) instance = super(CourseRunSerializer, self).update(instance, validated_data) request = self.context['request'] if preview_url: # Change ownership to CourseTeam. instance.course_run_state.change_owner_role( PublisherUserRole.CourseTeam) if waffle.switch_is_active('enable_publisher_email_notifications'): if preview_url: send_email_preview_page_is_available(instance, site=request.site) elif lms_course_id: send_email_for_studio_instance_created(instance, site=request.site) return instance
def update(self, instance, validated_data): instance = super(UpdateCourseKeySerializer, self).update(instance, validated_data) if waffle.switch_is_active('enable_publisher_email_notifications'): send_email_for_studio_instance_created(instance) return instance
def test_email_with_error(self): """ Verify that emails for studio instance created.""" with LogCapture(emails.logger.name) as l: emails.send_email_for_studio_instance_created(self.course_run) l.check((emails.logger.name, 'ERROR', 'Failed to send email notifications for course_run [{}]'. format(self.course_run.id)))
def test_email_with_error(self): """ Verify that emails failure raise exception.""" with self.assertRaises(Exception) as ex: emails.send_email_for_studio_instance_created(self.course_run) error_message = 'Failed to send email notifications for course_run [{}]'.format( self.course_run.id) self.assertEqual(ex.message, error_message)
def update(self, instance, validated_data): instance = super(UpdateCourseKeySerializer, self).update(instance, validated_data) if waffle.switch_is_active('enable_publisher_email_notifications'): send_email_for_studio_instance_created(instance) return instance
def test_email_sent_successfully(self): """ Verify that emails sent successfully for studio instance created.""" emails.send_email_for_studio_instance_created(self.course_run) self.assert_email_sent( reverse('publisher:publisher_course_run_detail', kwargs={'pk': self.course_run.id}), 'Studio instance created', 'EdX has created a Studio instance for' )
def test_email_sent_successfully(self): """ Verify that emails sent successfully for studio instance created.""" emails.send_email_for_studio_instance_created(self.course_run) course_key = CourseKey.from_string(self.course_run.lms_course_id) self.assert_email_sent( reverse('publisher:publisher_course_run_detail', kwargs={'pk': self.course_run.id}), 'Studio URL created: {title} {run_number}'.format( title=self.course_run.course.title, run_number=course_key.run), 'created a Studio URL for the')
def test_email_sent_successfully(self): """ Verify that emails sent successfully for studio instance created.""" emails.send_email_for_studio_instance_created(self.course_run) # assert email sent self.assert_email_sent( reverse('publisher:publisher_course_run_detail', kwargs={'pk': self.course_run.id}), 'Studio instance created', 'Studio instance created for the following course run' )
def test_email_with_error(self): """ Verify that emails for studio instance created.""" with LogCapture(emails.logger.name) as l: emails.send_email_for_studio_instance_created(self.course_run) l.check( ( emails.logger.name, 'ERROR', 'Failed to send email notifications for course_run [{}]'.format(self.course_run.id) ) )
def update(self, instance, validated_data): instance = super(CourseRunSerializer, self).update(instance, validated_data) preview_url = validated_data.get('preview_url') lms_course_id = validated_data.get('lms_course_id') if preview_url: # Change ownership to CourseTeam. instance.course_run_state.change_owner_role(PublisherUserRole.CourseTeam) if waffle.switch_is_active('enable_publisher_email_notifications'): if preview_url: send_email_preview_page_is_available(instance) elif lms_course_id: send_email_for_studio_instance_created(instance) return instance
def post(self, request, *args, **kwargs): user = request.user context = self.get_context_data() course_run = context.get('course_run') lms_course_id = course_run.lms_course_id course_form = self.course_form( request.POST, request.FILES, instance=course_run.course, initial=context.get('initial'), organization=context.get('organization'), edit_mode=True) run_form = self.run_form(request.POST, instance=course_run) seat_form = self.seat_form(request.POST, instance=course_run.seats.first()) if course_form.is_valid() and run_form.is_valid( ) and seat_form.is_valid(): try: with transaction.atomic(): course = course_form.save(commit=False) course.changed_by = self.request.user course.save() course_run = run_form.save(commit=False) course_run.changed_by = self.request.user course_run.save() run_form.save_m2m() # If price-type comes with request then save the seat object. if request.POST.get('type'): seat_form.save(changed_by=user, course_run=course_run) # in case of any updating move the course-run state to draft. if course_run.course_run_state.name != CourseStateChoices.Draft: course_run.course_run_state.change_state( state=CourseStateChoices.Draft, user=user) if course_run.lms_course_id and lms_course_id != course_run.lms_course_id: emails.send_email_for_studio_instance_created( course_run, updated_text=_('updated')) # pylint: disable=no-member messages.success(request, _('Course run updated successfully.')) return HttpResponseRedirect( reverse(self.success_url, kwargs={'pk': course_run.id})) except Exception as e: # pylint: disable=broad-except # pylint: disable=no-member error_message = _( 'An error occurred while saving your changes. {error}' ).format(error=str(e)) messages.error(request, error_message) logger.exception( 'Unable to update course run and seat for course [%s].', course_run.id) if not messages.get_messages(request): messages.error(request, _('Please fill all required fields.')) context.update({ 'course_form': course_form, 'run_form': run_form, 'seat_form': seat_form }) return render(request, self.template_name, context, status=400)