from flask import Flask, Blueprint, jsonify, request
from flask_jwt_extended import jwt_required, get_jwt_identity
from sqlalchemy import exists
from app.models import db, User, Search, Posting

bp = Blueprint('user', __name__, url_prefix="/api/user")


@bp.route('/<int:id>')
def get_searches(id):
  print()
  print('********GETTING USER SEARCHES********')
  print()
  user_searches = Search.query.filter(Search.user_id == id).one()
  print(user_searches)

  searches = {
      'user_id': user_searches.user_id,
      'search_radius': user_searches.search_radius,
      'technologies': user_searches.technologies,
  }

  return {'user_searches': searches}, 200


@bp.route('/login', methods=['POST'])
def login():

  data = request.get_json()
  email = data['email']
  password = data['password']

  print()
  print('********GETTING TOKEN********')
  print()

  (ret, ), = db.session.query(exists().where(User.email == email))
  if ret:
    user = User.query.filter(User.email == email).one()
    return {'token': user.get_token(), 'username': user.user_name, 'id': user.id}\
        if user.check_password(password) else {'error': 'Login failed.'}
  else:
    return {'error': 'Login failed.'}


@bp.route('/register', methods=['POST'])
def register():
  data = request.get_json()

  (ret, ), = db.session.query(exists().where(User.email == data['email']))
  if ret:
    return {'error': 'User already exists. Please try again.'}

  new_user = {
      'user_name': data['userName'],
      'first_name': data['firstName'],
      'last_name': data['lastName'],
      'email': data['email'],
  }

  user = User(**new_user)
  user.set_password = data['password']
  search = Search(user=user)

  print()
  print('*****USER ADDED*****', {**new_user, 'password': user.hashed_password[:30]})
  print()
  db.session.add(user)
  db.session.commit()
  return {'token': user.get_token(), 'username': user.user_name, 'id': user.id}, 201


# @jwt_required
@bp.route('/users')
def get_users():
  print()
  print('********GETTING USERS********')
  print()
  users = User.query.all()
  res = [{
      'id': user.id,
      'user_name': user.user_name,
      'first_name': user.first_name,
      'last_name': user.last_name,
      'email': user.email,
      'password': user.password,
  } for user in users]
  return jsonify(res), 200


@bp.route('/bookmark', methods=['GET'])
@jwt_required
def get_bookmarks():
  print()
  print('********GETTING BOOKMARK********')
  print()

  current_user = get_jwt_identity()
  user = User.query.get(current_user['id'])
  res = [{
      'id': posting.id,
      'search_terms': posting.search_terms,
      'search_loc': posting.search_loc,
      'title': posting.title,
      'location': posting.location,
      'company': posting.company,
      'salary': posting.salary,
      'date': posting.date,
      'snippet': posting.snippet,
      'description': posting.description,
      'link': posting.link,
  } for posting in user.postings]
  return jsonify(res), 200


@bp.route('/bookmark', methods=['POST'])
@jwt_required
def add_bookmark():
  print()
  print('********ADDING BOOKMARK********')
  print()
  current_user = get_jwt_identity()
  data = request.get_json()

  user = User.query.get(current_user['id'])
  posting = Posting.query.get(data['row_id'])

  if data['checked']:
    user.postings.append(posting)
  else:
    user.postings.remove(posting)

  db.session.commit()

  res = [{
      'id': posting.id,
      'search_terms': posting.search_terms,
      'search_loc': posting.search_loc,
      'title': posting.title,
      'location': posting.location,
      'company': posting.company,
      'salary': posting.salary,
      'date': posting.date,
      'snippet': posting.snippet,
      'description': posting.description,
      'link': posting.link,
  } for posting in user.postings]

  return jsonify(res), 201

# @bp.route('/test')
# def test():
#   print()
#   print('********GETTING USER SETTINGS********')
#   print()
#   user_settings = UserSettings(user_id=1)
#   res = {
#       'user_id': user_settings.user_id,
#       'search_radius': user_settings.search_radius,
#       'tech_one': user_settings.tech_one,
#       'tech_two': user_settings.tech_two,
#       'tech_three': user_settings.tech_three,
#   }

#   db.session.add(user_settings)
#   db.session.commit()

#   return {'user_settings': res}, 200

8e9518f08e33e20f8f817e574f64058853d67c14
<mask token>


@bp.route('/<int:id>')
def get_searches(id):
    print()
    print('********GETTING USER SEARCHES********')
    print()
    user_searches = Search.query.filter(Search.user_id == id).one()
    print(user_searches)
    searches = {'user_id': user_searches.user_id, 'search_radius':
        user_searches.search_radius, 'technologies': user_searches.technologies
        }
    return {'user_searches': searches}, 200


<mask token>


@bp.route('/register', methods=['POST'])
def register():
    data = request.get_json()
    (ret,), = db.session.query(exists().where(User.email == data['email']))
    if ret:
        return {'error': 'User already exists. Please try again.'}
    new_user = {'user_name': data['userName'], 'first_name': data[
        'firstName'], 'last_name': data['lastName'], 'email': data['email']}
    user = User(**new_user)
    user.set_password = data['password']
    search = Search(user=user)
    print()
    print('*****USER ADDED*****', {**new_user, 'password': user.
        hashed_password[:30]})
    print()
    db.session.add(user)
    db.session.commit()
    return {'token': user.get_token(), 'username': user.user_name, 'id':
        user.id}, 201


@bp.route('/users')
def get_users():
    print()
    print('********GETTING USERS********')
    print()
    users = User.query.all()
    res = [{'id': user.id, 'user_name': user.user_name, 'first_name': user.
        first_name, 'last_name': user.last_name, 'email': user.email,
        'password': user.password} for user in users]
    return jsonify(res), 200


<mask token>


@bp.route('/bookmark', methods=['POST'])
@jwt_required
def add_bookmark():
    print()
    print('********ADDING BOOKMARK********')
    print()
    current_user = get_jwt_identity()
    data = request.get_json()
    user = User.query.get(current_user['id'])
    posting = Posting.query.get(data['row_id'])
    if data['checked']:
        user.postings.append(posting)
    else:
        user.postings.remove(posting)
    db.session.commit()
    res = [{'id': posting.id, 'search_terms': posting.search_terms,
        'search_loc': posting.search_loc, 'title': posting.title,
        'location': posting.location, 'company': posting.company, 'salary':
        posting.salary, 'date': posting.date, 'snippet': posting.snippet,
        'description': posting.description, 'link': posting.link} for
        posting in user.postings]
    return jsonify(res), 201

<mask token>


@bp.route('/<int:id>')
def get_searches(id):
    print()
    print('********GETTING USER SEARCHES********')
    print()
    user_searches = Search.query.filter(Search.user_id == id).one()
    print(user_searches)
    searches = {'user_id': user_searches.user_id, 'search_radius':
        user_searches.search_radius, 'technologies': user_searches.technologies
        }
    return {'user_searches': searches}, 200


<mask token>


@bp.route('/register', methods=['POST'])
def register():
    data = request.get_json()
    (ret,), = db.session.query(exists().where(User.email == data['email']))
    if ret:
        return {'error': 'User already exists. Please try again.'}
    new_user = {'user_name': data['userName'], 'first_name': data[
        'firstName'], 'last_name': data['lastName'], 'email': data['email']}
    user = User(**new_user)
    user.set_password = data['password']
    search = Search(user=user)
    print()
    print('*****USER ADDED*****', {**new_user, 'password': user.
        hashed_password[:30]})
    print()
    db.session.add(user)
    db.session.commit()
    return {'token': user.get_token(), 'username': user.user_name, 'id':
        user.id}, 201


@bp.route('/users')
def get_users():
    print()
    print('********GETTING USERS********')
    print()
    users = User.query.all()
    res = [{'id': user.id, 'user_name': user.user_name, 'first_name': user.
        first_name, 'last_name': user.last_name, 'email': user.email,
        'password': user.password} for user in users]
    return jsonify(res), 200


@bp.route('/bookmark', methods=['GET'])
@jwt_required
def get_bookmarks():
    print()
    print('********GETTING BOOKMARK********')
    print()
    current_user = get_jwt_identity()
    user = User.query.get(current_user['id'])
    res = [{'id': posting.id, 'search_terms': posting.search_terms,
        'search_loc': posting.search_loc, 'title': posting.title,
        'location': posting.location, 'company': posting.company, 'salary':
        posting.salary, 'date': posting.date, 'snippet': posting.snippet,
        'description': posting.description, 'link': posting.link} for
        posting in user.postings]
    return jsonify(res), 200


@bp.route('/bookmark', methods=['POST'])
@jwt_required
def add_bookmark():
    print()
    print('********ADDING BOOKMARK********')
    print()
    current_user = get_jwt_identity()
    data = request.get_json()
    user = User.query.get(current_user['id'])
    posting = Posting.query.get(data['row_id'])
    if data['checked']:
        user.postings.append(posting)
    else:
        user.postings.remove(posting)
    db.session.commit()
    res = [{'id': posting.id, 'search_terms': posting.search_terms,
        'search_loc': posting.search_loc, 'title': posting.title,
        'location': posting.location, 'company': posting.company, 'salary':
        posting.salary, 'date': posting.date, 'snippet': posting.snippet,
        'description': posting.description, 'link': posting.link} for
        posting in user.postings]
    return jsonify(res), 201

<mask token>


@bp.route('/<int:id>')
def get_searches(id):
    print()
    print('********GETTING USER SEARCHES********')
    print()
    user_searches = Search.query.filter(Search.user_id == id).one()
    print(user_searches)
    searches = {'user_id': user_searches.user_id, 'search_radius':
        user_searches.search_radius, 'technologies': user_searches.technologies
        }
    return {'user_searches': searches}, 200


@bp.route('/login', methods=['POST'])
def login():
    data = request.get_json()
    email = data['email']
    password = data['password']
    print()
    print('********GETTING TOKEN********')
    print()
    (ret,), = db.session.query(exists().where(User.email == email))
    if ret:
        user = User.query.filter(User.email == email).one()
        return {'token': user.get_token(), 'username': user.user_name, 'id':
            user.id} if user.check_password(password) else {'error':
            'Login failed.'}
    else:
        return {'error': 'Login failed.'}


@bp.route('/register', methods=['POST'])
def register():
    data = request.get_json()
    (ret,), = db.session.query(exists().where(User.email == data['email']))
    if ret:
        return {'error': 'User already exists. Please try again.'}
    new_user = {'user_name': data['userName'], 'first_name': data[
        'firstName'], 'last_name': data['lastName'], 'email': data['email']}
    user = User(**new_user)
    user.set_password = data['password']
    search = Search(user=user)
    print()
    print('*****USER ADDED*****', {**new_user, 'password': user.
        hashed_password[:30]})
    print()
    db.session.add(user)
    db.session.commit()
    return {'token': user.get_token(), 'username': user.user_name, 'id':
        user.id}, 201


@bp.route('/users')
def get_users():
    print()
    print('********GETTING USERS********')
    print()
    users = User.query.all()
    res = [{'id': user.id, 'user_name': user.user_name, 'first_name': user.
        first_name, 'last_name': user.last_name, 'email': user.email,
        'password': user.password} for user in users]
    return jsonify(res), 200


@bp.route('/bookmark', methods=['GET'])
@jwt_required
def get_bookmarks():
    print()
    print('********GETTING BOOKMARK********')
    print()
    current_user = get_jwt_identity()
    user = User.query.get(current_user['id'])
    res = [{'id': posting.id, 'search_terms': posting.search_terms,
        'search_loc': posting.search_loc, 'title': posting.title,
        'location': posting.location, 'company': posting.company, 'salary':
        posting.salary, 'date': posting.date, 'snippet': posting.snippet,
        'description': posting.description, 'link': posting.link} for
        posting in user.postings]
    return jsonify(res), 200


@bp.route('/bookmark', methods=['POST'])
@jwt_required
def add_bookmark():
    print()
    print('********ADDING BOOKMARK********')
    print()
    current_user = get_jwt_identity()
    data = request.get_json()
    user = User.query.get(current_user['id'])
    posting = Posting.query.get(data['row_id'])
    if data['checked']:
        user.postings.append(posting)
    else:
        user.postings.remove(posting)
    db.session.commit()
    res = [{'id': posting.id, 'search_terms': posting.search_terms,
        'search_loc': posting.search_loc, 'title': posting.title,
        'location': posting.location, 'company': posting.company, 'salary':
        posting.salary, 'date': posting.date, 'snippet': posting.snippet,
        'description': posting.description, 'link': posting.link} for
        posting in user.postings]
    return jsonify(res), 201

<mask token>
bp = Blueprint('user', __name__, url_prefix='/api/user')


@bp.route('/<int:id>')
def get_searches(id):
    print()
    print('********GETTING USER SEARCHES********')
    print()
    user_searches = Search.query.filter(Search.user_id == id).one()
    print(user_searches)
    searches = {'user_id': user_searches.user_id, 'search_radius':
        user_searches.search_radius, 'technologies': user_searches.technologies
        }
    return {'user_searches': searches}, 200


@bp.route('/login', methods=['POST'])
def login():
    data = request.get_json()
    email = data['email']
    password = data['password']
    print()
    print('********GETTING TOKEN********')
    print()
    (ret,), = db.session.query(exists().where(User.email == email))
    if ret:
        user = User.query.filter(User.email == email).one()
        return {'token': user.get_token(), 'username': user.user_name, 'id':
            user.id} if user.check_password(password) else {'error':
            'Login failed.'}
    else:
        return {'error': 'Login failed.'}


@bp.route('/register', methods=['POST'])
def register():
    data = request.get_json()
    (ret,), = db.session.query(exists().where(User.email == data['email']))
    if ret:
        return {'error': 'User already exists. Please try again.'}
    new_user = {'user_name': data['userName'], 'first_name': data[
        'firstName'], 'last_name': data['lastName'], 'email': data['email']}
    user = User(**new_user)
    user.set_password = data['password']
    search = Search(user=user)
    print()
    print('*****USER ADDED*****', {**new_user, 'password': user.
        hashed_password[:30]})
    print()
    db.session.add(user)
    db.session.commit()
    return {'token': user.get_token(), 'username': user.user_name, 'id':
        user.id}, 201


@bp.route('/users')
def get_users():
    print()
    print('********GETTING USERS********')
    print()
    users = User.query.all()
    res = [{'id': user.id, 'user_name': user.user_name, 'first_name': user.
        first_name, 'last_name': user.last_name, 'email': user.email,
        'password': user.password} for user in users]
    return jsonify(res), 200


@bp.route('/bookmark', methods=['GET'])
@jwt_required
def get_bookmarks():
    print()
    print('********GETTING BOOKMARK********')
    print()
    current_user = get_jwt_identity()
    user = User.query.get(current_user['id'])
    res = [{'id': posting.id, 'search_terms': posting.search_terms,
        'search_loc': posting.search_loc, 'title': posting.title,
        'location': posting.location, 'company': posting.company, 'salary':
        posting.salary, 'date': posting.date, 'snippet': posting.snippet,
        'description': posting.description, 'link': posting.link} for
        posting in user.postings]
    return jsonify(res), 200


@bp.route('/bookmark', methods=['POST'])
@jwt_required
def add_bookmark():
    print()
    print('********ADDING BOOKMARK********')
    print()
    current_user = get_jwt_identity()
    data = request.get_json()
    user = User.query.get(current_user['id'])
    posting = Posting.query.get(data['row_id'])
    if data['checked']:
        user.postings.append(posting)
    else:
        user.postings.remove(posting)
    db.session.commit()
    res = [{'id': posting.id, 'search_terms': posting.search_terms,
        'search_loc': posting.search_loc, 'title': posting.title,
        'location': posting.location, 'company': posting.company, 'salary':
        posting.salary, 'date': posting.date, 'snippet': posting.snippet,
        'description': posting.description, 'link': posting.link} for
        posting in user.postings]
    return jsonify(res), 201

from flask import Flask, Blueprint, jsonify, request
from flask_jwt_extended import jwt_required, get_jwt_identity
from sqlalchemy import exists
from app.models import db, User, Search, Posting

bp = Blueprint('user', __name__, url_prefix="/api/user")


@bp.route('/<int:id>')
def get_searches(id):
  print()
  print('********GETTING USER SEARCHES********')
  print()
  user_searches = Search.query.filter(Search.user_id == id).one()
  print(user_searches)

  searches = {
      'user_id': user_searches.user_id,
      'search_radius': user_searches.search_radius,
      'technologies': user_searches.technologies,
  }

  return {'user_searches': searches}, 200


@bp.route('/login', methods=['POST'])
def login():

  data = request.get_json()
  email = data['email']
  password = data['password']

  print()
  print('********GETTING TOKEN********')
  print()

  (ret, ), = db.session.query(exists().where(User.email == email))
  if ret:
    user = User.query.filter(User.email == email).one()
    return {'token': user.get_token(), 'username': user.user_name, 'id': user.id}\
        if user.check_password(password) else {'error': 'Login failed.'}
  else:
    return {'error': 'Login failed.'}


@bp.route('/register', methods=['POST'])
def register():
  data = request.get_json()

  (ret, ), = db.session.query(exists().where(User.email == data['email']))
  if ret:
    return {'error': 'User already exists. Please try again.'}

  new_user = {
      'user_name': data['userName'],
      'first_name': data['firstName'],
      'last_name': data['lastName'],
      'email': data['email'],
  }

  user = User(**new_user)
  user.set_password = data['password']
  search = Search(user=user)

  print()
  print('*****USER ADDED*****', {**new_user, 'password': user.hashed_password[:30]})
  print()
  db.session.add(user)
  db.session.commit()
  return {'token': user.get_token(), 'username': user.user_name, 'id': user.id}, 201


# @jwt_required
@bp.route('/users')
def get_users():
  print()
  print('********GETTING USERS********')
  print()
  users = User.query.all()
  res = [{
      'id': user.id,
      'user_name': user.user_name,
      'first_name': user.first_name,
      'last_name': user.last_name,
      'email': user.email,
      'password': user.password,
  } for user in users]
  return jsonify(res), 200


@bp.route('/bookmark', methods=['GET'])
@jwt_required
def get_bookmarks():
  print()
  print('********GETTING BOOKMARK********')
  print()

  current_user = get_jwt_identity()
  user = User.query.get(current_user['id'])
  res = [{
      'id': posting.id,
      'search_terms': posting.search_terms,
      'search_loc': posting.search_loc,
      'title': posting.title,
      'location': posting.location,
      'company': posting.company,
      'salary': posting.salary,
      'date': posting.date,
      'snippet': posting.snippet,
      'description': posting.description,
      'link': posting.link,
  } for posting in user.postings]
  return jsonify(res), 200


@bp.route('/bookmark', methods=['POST'])
@jwt_required
def add_bookmark():
  print()
  print('********ADDING BOOKMARK********')
  print()
  current_user = get_jwt_identity()
  data = request.get_json()

  user = User.query.get(current_user['id'])
  posting = Posting.query.get(data['row_id'])

  if data['checked']:
    user.postings.append(posting)
  else:
    user.postings.remove(posting)

  db.session.commit()

  res = [{
      'id': posting.id,
      'search_terms': posting.search_terms,
      'search_loc': posting.search_loc,
      'title': posting.title,
      'location': posting.location,
      'company': posting.company,
      'salary': posting.salary,
      'date': posting.date,
      'snippet': posting.snippet,
      'description': posting.description,
      'link': posting.link,
  } for posting in user.postings]

  return jsonify(res), 201

# @bp.route('/test')
# def test():
#   print()
#   print('********GETTING USER SETTINGS********')
#   print()
#   user_settings = UserSettings(user_id=1)
#   res = {
#       'user_id': user_settings.user_id,
#       'search_radius': user_settings.search_radius,
#       'tech_one': user_settings.tech_one,
#       'tech_two': user_settings.tech_two,
#       'tech_three': user_settings.tech_three,
#   }

#   db.session.add(user_settings)
#   db.session.commit()

#   return {'user_settings': res}, 200
