def post(self, request): """Log in a user. You must send all required form fields with the request. You can optionally send an `analytics` param with a JSON-encoded object with additional info to include in the login analytics event. Currently, the only supported field is "enroll_course_id" to indicate that the user logged in while enrolling in a particular course. Arguments: request (HttpRequest) Returns: HttpResponse: 200 on success HttpResponse: 400 if the request is not valid. HttpResponse: 403 if authentication failed. 403 with content "third-party-auth" if the user has successfully authenticated with a third party provider but does not have a linked account. HttpResponse: 302 if redirecting to another page. Example Usage: POST /user_api/v1/login_session with POST params `email`, `password`, and `remember`. 200 OK """ # For the initial implementation, shim the existing login view # from the student Django app. from student.views import login_user return shim_student_view(login_user, check_logged_in=True)(request)
def post(self, request): """Create the user's account. You must send all required form fields with the request. You can optionally send an `analytics` param with a JSON-encoded object with additional info to include in the registration analytics event. Currently, the only supported field is "enroll_course_id" to indicate that the user registered while enrolling in a particular course. Arguments: request (HTTPRequest) Returns: HttpResponse: 200 on success HttpResponse: 400 if the request is not valid. HttpResponse: 302 if redirecting to another page. """ email = request.POST.get('email') username = request.POST.get('username') # Handle duplicate email/username conflicts = account_api.check_account_exists(email=email, username=username) if conflicts: if all(conflict in conflicts for conflict in ['email', 'username']): # Translators: This message is shown to users who attempt to create a new # account using both an email address and a username associated with an # existing account. error_msg = _( u"It looks like {email_address} and {username} belong to an existing account. Try again with a different email address and username." ).format(email_address=email, username=username) elif 'email' in conflicts: # Translators: This message is shown to users who attempt to create a new # account using an email address associated with an existing account. error_msg = _( u"It looks like {email_address} belongs to an existing account. Try again with a different email address." ).format(email_address=email) else: # Translators: This message is shown to users who attempt to create a new # account using a username associated with an existing account. error_msg = _( u"It looks like {username} belongs to an existing account. Try again with a different username." ).format(username=username) return HttpResponse(status=409, content=error_msg, content_type="text/plain") # For the initial implementation, shim the existing login view # from the student Django app. from student.views import create_account return shim_student_view(create_account)(request)
def post(self, request): """Create the user's account. You must send all required form fields with the request. You can optionally send an `analytics` param with a JSON-encoded object with additional info to include in the registration analytics event. Currently, the only supported field is "enroll_course_id" to indicate that the user registered while enrolling in a particular course. Arguments: request (HTTPRequest) Returns: HttpResponse: 200 on success HttpResponse: 400 if the request is not valid. HttpResponse: 302 if redirecting to another page. """ email = request.POST.get('email') username = request.POST.get('username') # Handle duplicate email/username conflicts = account_api.check_account_exists(email=email, username=username) if conflicts: if all(conflict in conflicts for conflict in ['email', 'username']): # Translators: This message is shown to users who attempt to create a new # account using both an email address and a username associated with an # existing account. error_msg = _( u"It looks like {email_address} and {username} belong to an existing account. Try again with a different email address and username." ).format(email_address=email, username=username) elif 'email' in conflicts: # Translators: This message is shown to users who attempt to create a new # account using an email address associated with an existing account. error_msg = _( u"It looks like {email_address} belongs to an existing account. Try again with a different email address." ).format(email_address=email) else: # Translators: This message is shown to users who attempt to create a new # account using a username associated with an existing account. error_msg = _( u"It looks like {username} belongs to an existing account. Try again with a different username." ).format(username=username) return HttpResponse( status=409, content=error_msg, content_type="text/plain" ) # For the initial implementation, shim the existing login view # from the student Django app. from student.views import create_account return shim_student_view(create_account)(request)
def _shimmed_view(self, response, check_logged_in=False): # pylint: disable=missing-docstring def stub_view(request): # pylint: disable=missing-docstring self.captured_request = request return response return shim_student_view(stub_view, check_logged_in=check_logged_in)