def get_password_reset_url(email=None): """ Returns the password reset url. """ reset_link = "" if email: try: prev_token = UserToken.objects.get(email=email, token_type=3, status=False) if prev_token: prev_token.status = True prev_token.save() except UserToken.DoesNotExist: pass passwd_reset_token = UserToken(email=email, token_type=3) passwd_reset_token.save() reset_link = "{0}/user/passwordreset/?questr_token={1}".format( settings.QUESTR_URL, passwd_reset_token.get_token()) return reset_link
def get_verification_url(user=None): """ Returns the verification url. """ verf_link = "" if user: try: prev_token = UserToken.objects.get(email=user.email, token_type=0, status=False) if prev_token: prev_token.status = True prev_token.save() except UserToken.DoesNotExist: pass verf_token = UserToken(email=user.email, token_type=0) verf_token.save() verf_link = "{0}/user/email/confirm/?questr_token={1}".format( settings.QUESTR_URL, verf_token.get_token()) return verf_link
def get_signup_invitation_url(email=None, token_type=None): """ Returns the verification url. """ signup_link = "" if email and token_type: try: prev_token = UserToken.objects.get(email=email, token_type=token_type, status=False) if prev_token: prev_token.status = True prev_token.save() except UserToken.DoesNotExist: pass signup_token = UserToken(email=email, token_type=token_type) signup_token.save() signup_link = "{0}/signup/invitation/?questr_token={1}".format( settings.QUESTR_URL, signup_token.get_token()) return signup_link
def post(self, request, *args, **kwargs): data = json.loads(request.body) username = data.get('email') password = data.get('password') try: credentials = { get_user_model().USERNAME_FIELD: username, 'password': password } user = authenticate(**credentials) try: token = user.usertoken.key except UserToken.DoesNotExist: obj = UserToken(user=user) obj.save() token = obj.key return JsonResponse({ "username": user.username, "token": token }, safe=False) except User.DoesNotExist: return JsonResponse({}, status=401)
def login(request): try: data = json.loads(request.POST["data"]) username = data.get("username") password = data.get("password") # -- user and pswd checking -- if (username is None and password is None) or (username == "" and password == ""): return JsonResponse({ "result": "error", "message": "user and password are empty or none" }) if username is None or username == "": return JsonResponse({ "result": "error", "message": "user is empty or none" }) if password is None or password == "": return JsonResponse({ "result": "error", "message": "password is empty or none" }) # -- end of user and pswd checking -- # login user = authenticate(username=username, password=password) if user is not None: if user.is_active: user_token = get_object_or_None(UserToken, user=user) if user_token is None: token_value = Token.generate_value() user_token = UserToken(token=token_value, user=user) user_token.save() return JsonResponse({ "result": "ok", "username": user.username, "email": user.email, "token": user_token.token }) else: return JsonResponse({ "result": "error", "message": "user is not active" }) else: return JsonResponse({ "result": "error", "message": "no user" }) except Exception as e: return JsonResponse({ "result": "error", "message": "something went wrong on the server", "traceback": str(e) })