@require_POST
@login_required
@ensure_csrf_cookie
def change_email_settings(request):
 user = request.user
 course_id = request.POST.get('course_id')
 course_key = SlashSeparatedCourseKey.from_deprecated_string(course_id)
 receive_emails = request.POST.get('receive_emails')
 if receive_emails:
  optout_object = Optout.objects.filter(user=user, course_id=course_key)
  if optout_object:
   optout_object.delete()
  log.info(u'User %s (%s) opted in to receive emails from course %s', user.username, user.email, course_id)
  track.views.server_track(request, 'change-email-settings', {'receive_emails': 'yes', 'course': course_id}, page='dashboard')
 else:
  Optout.objects.get_or_create(user=user, course_id=course_key)
  log.info(u'User %s (%s) opted out of receiving emails from course %s', user.username, user.email, course_id)
  track.views.server_track(request, 'change-email-settings', {'receive_emails': 'no', 'course': course_id}, page='dashboard')
 return JsonResponse({'success': True})

@ require_POST @ login_required @ ensure_csrf_cookie def change_email_settings ( request ) : user = request . user course_id = request . POST . get ( 'course_id' ) course_key = SlashSeparatedCourseKey . from_deprecated_string ( course_id ) receive_emails = request . POST . get ( 'receive_emails' ) if receive_emails : optout_object = Optout . objects . filter ( user = user , course_id = course_key ) if optout_object : optout_object . delete ( ) log . info ( u'User %s (%s) opted in to receive emails from course %s' , user . username , user . email , course_id ) track . views . server_track ( request , 'change-email-settings' , { 'receive_emails' : 'yes' , 'course' : course_id } , page = 'dashboard' ) else : Optout . objects . get_or_create ( user = user , course_id = course_key ) log . info ( u'User %s (%s) opted out of receiving emails from course %s' , user . username , user . email , course_id ) track . views . server_track ( request , 'change-email-settings' , { 'receive_emails' : 'no' , 'course' : course_id } , page = 'dashboard' ) return JsonResponse ( { 'success' : True } )
modify logged-in users setting for receiving emails from a course .