def register(request): if request.method == "POST": email = request.POST.get('email') username = request.POST.get('username') password = request.POST.get('password') if not validators.email_validator( email) and not validators.username_validator(email): response = { 'msg': 'fail', 'info': 'please check your email address or username.' } elif not validators.password_validator(password): response = {'msg': 'fail', 'info': 'password too short.'} else: try: user = models.UserProfile() user.username = username user.email = email user.set_password(password) user.save() response = {"msg": "success"} except IntegrityError as e: response = {"msg": "fail", "info": str(e)} return HttpResponse(json.dumps(response)) return render(request, 'register.html')
def login(request): if request.method == "POST": email = request.POST.get('email') password = request.POST.get('password') if not validators.email_validator( email) and not validators.username_validator(email): response = { 'msg': 'fail', 'info': 'please check your email address or username.' } elif not validators.password_validator(password): response = {'msg': 'fail', 'info': 'password too short.'} else: response = {'msg': 'success', 'info': ''} user = auth.authenticate(request, username=email, password=password) if user is not None: auth.login(request, user) else: response = {'msg': 'fail', 'info': 'password is invalid.'} try: models.UserProfile.objects.get(username=email) except models.UserProfile.DoesNotExist: try: models.UserProfile.objects.get(email=email) except models.UserProfile.DoesNotExist: response['info'] = 'username or email not exist.' return HttpResponse(json.dumps(response)) return render(request, 'login.html')
def change_username(request): if request.is_ajax(): username = request.POST.get('username') if not validators.username_validator(username): res = {'msg': 'fail', 'info': 'invalid input.'} else: try: models.UserProfile.objects.get(username=username) res = {'msg': 'fail', 'info': 'username already taken.'} except models.UserProfile.DoesNotExist: user = get_user(request) user.username = username user.save() res = {'msg': 'success'} return HttpResponse(json.dumps(res))