Exemplo n.º 1
0
def change_password(data):
    try:
        print("=" * 80, "Changing Password", sep="\n")
        user_id = g.user['id']
        user = User.query.get(user_id)
        if (user and user.check_hash_password(data['currentPassword'])):
            new_hashed_password = User.generate_hash_password(
                data['newPassword'])
            user.password = new_hashed_password
            user.commit()
            print("Password Changed", "=" * 80, sep="\n")
            return Response(mimetype="application/json",
                            response=json.dumps(
                                {'success': 'Password Changed'}),
                            status=200)
        else:
            print("Incorrect Password", "=" * 80, sep="\n")
            return Response(mimetype="application/json",
                            response=json.dumps(
                                {'error': 'Incorrect Password'}),
                            status=403)
    except Exception as e:
        return Response(mimetype="application/json",
                        response=json.dumps({'error': str(e)}),
                        status=400)
Exemplo n.º 2
0
def user_sign_up(data):
    try:
        if User.username_exists(data["username"]):
            return Response(mimetype="application/json",
                            response=json.dumps(
                                {'error': "Username already exists"}),
                            status=403)
        if User.email_exists(data["email"]):
            return Response(mimetype="application/json",
                            response=json.dumps(
                                {'error': "email already exists"}),
                            status=403)
        password_hash = User.generate_hash_password(data["password"])
        new_user = User(username=data["username"],
                        name=data["name"],
                        email=data["email"],
                        password=password_hash)
        new_user.save()

        # # Now we'll send the email confirmation link
        # subject = "Confirm your email"

        # token = ts.dumps(self.email, salt='email-confirm-key')

        # confirm_url = url_for(
        #     'confirm_email',
        #     token=token,
        #     _external=True)

        # html = render_template(
        #     'email/activate.html',
        #     confirm_url=confirm_url)

        # # We'll assume that send_email has been defined in myapp/util.py
        # send_email(user.email, subject, html)

        return Response(mimetype="application/json",
                        response=json.dumps(
                            {'success': "User created successfully"}),
                        status=201)
    except Exception as e:
        error_msg = get_error_msg(e)
        logger.error(error_msg)
        return Response(mimetype="application/json",
                        response=json.dumps({'error': error_msg}),
                        status=400)
Exemplo n.º 3
0
def create_new_user(data):
    try:
        print("=" * 80, "Creating New User:"******"\n")
        user = User.get_user_via_email(data['email'])
        if (user):
            print("User already exists")
            print("=" * 80)
            return Response(mimetype="application/json",
                            response=json.dumps({'error': 'user exsists'}),
                            status=403)
        hashed_password = User.generate_hash_password(data['password'])
        new_user = User(username=data['username'],
                        email=data['email'],
                        password=hashed_password)
        new_user.save()
        print("New User Created", "=" * 80, sep="\n")
        return Response(mimetype="application/json",
                        response=json.dumps({'success': "New User Created"}),
                        status=201)
    except Exception as e:
        return Response(mimetype="application/json",
                        response=json.dumps({'error': str(e)}),
                        status=400)