Ejemplo n.º 1
0
def jsonCatalog():
    ''' JSON API Route for the whole catalog collection'''

    category = session.query(Category).all()
    items = session.query(Item).all()

    return jsonify(items=[i.serialize for i in items])
Ejemplo n.º 2
0
def createUser(name, email):
    '''
    Check if User is in datbase
    if not
    It will add him/her
    '''
    u = session.query(User).filter_by(email=email).one_or_none()
    if u is None:
        # Create and ID for the user
        id = random.randint(0, 9)
        newUser = User(id=id, name=name, email=email)
        session.add(newUser)
        try:
            session.commit()
        except BaseException:
            session.rollback()
            raise
        finally:
            session.close()

        newUser = session.query(User).all()
        print(newUser)

    else:
        print(u)
Ejemplo n.º 3
0
def board_place():
    email = auth.current_user()
    user = session.query(User).filter(User.email == email).first()
    pk = user.place_id

    local_advetrisement = session.query(Advertisement) \
        .filter(Advertisement.tag == Tag.Local.name) \
        .filter(Advertisement.owner.has(place_id=pk)) \
        .order_by(Advertisement.createdate.desc())

    return jsonify(AdvertisementSchema(many=True).dump(local_advetrisement))
Ejemplo n.º 4
0
def get_advertisement(pk):
    email = auth.current_user()
    user = session.query(User).filter(User.email == email).first()
    user_id = user.id
    pk = int(pk)

    try:
        advertisement = session.query(Advertisement).get(pk)
        if advertisement.owner_id != user_id:
            raise Exception
    except Exception:
        return jsonify({'message': "Advertisement not found"}), 404

    return AdvertisementSchema().dump(advertisement)
Ejemplo n.º 5
0
def update_user():
    email = auth.current_user()
    user = session.query(User).filter(User.email == email).first()
    pk = user.id
    data = request.get_json()
    if "password" in data:
        data["password"] = bcrypt.generate_password_hash(
            data['password']).decode('utf-8')

    session.query(User).filter(User.id == pk).update(data)

    session.commit()

    return jsonify({'message': "Success"}), 200
Ejemplo n.º 6
0
def place_update(pk):
    data = request.get_json()
    try:
        pk = int(pk)
    except ValueError:
        return "Invalid ID supplied", 400

    try:
        place = get_or_404(Advertisement, pk)
    except Exception:
        return jsonify({'message': "Advertisement not found"}), 404

    session.query(Place).filter(Place.id == pk).update(data)
    session.commit()

    return jsonify({'message': "Success"}), 200
Ejemplo n.º 7
0
def delete_advertisement(pk):
    email = auth.current_user()
    user = session.query(User).filter(User.email == email).first()
    user_id = user.id

    pk = int(pk)

    try:
        advertisement = session.query(Advertisement).get(pk)
        if advertisement.owner_id != user_id:
            raise Exception
    except Exception:
        return jsonify({'message': "Advertisement not found"}), 404

    session.delete(advertisement)
    session.commit()
    return jsonify({'message': "Success"}), 200
Ejemplo n.º 8
0
def get_user():
    email = auth.current_user()
    user = session.query(User).filter(User.email == email).first()
    pk = user.id

    user = get_or_404(User, pk)

    return UserSchema().dump(user)
Ejemplo n.º 9
0
def delete_user():
    email = auth.current_user()
    user = session.query(User).filter(User.email == email).first()
    pk = user.id

    user = get_or_404(User, pk)

    session.delete(user)
    session.commit()
    return jsonify({'message': "Success"}), 200
Ejemplo n.º 10
0
def get_or_404(cls, pk):
    obj = session.query(cls).get(pk)
    if obj is None:
        raise Exception
    return obj
Ejemplo n.º 11
0
def place_get_all():
    places = session.query(Place).all()

    return jsonify(PlaceSchema(many=True).dump(places))
Ejemplo n.º 12
0
def verify_password(email, password):
    user = session.query(User).filter(User.email == email).first()
    if user is None:
        return False
    return bcrypt.check_password_hash(user.password, password)
Ejemplo n.º 13
0
def board():
    global_advetrisement = session.query(Advertisement).filter(
        Advertisement.tag == Tag.Global.name).order_by(
            Advertisement.createdate.desc()).all()

    return jsonify(AdvertisementSchema(many=True).dump(global_advetrisement))
Ejemplo n.º 14
0
def jsonItem(category, item):
    ''' JSON API Route for a specific Item '''
    category = session.query(Category).filter_by(name=category).one_or_none()
    item = session.query(Item).filter_by(name=item).one_or_none()
    return jsonify(item=[item.serialize])