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)
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
def place_delete(pk): pk = int(pk) try: place = get_or_404(Place, pk) except Exception: return jsonify({'message': "Place not found"}), 404 session.delete(place) session.commit() return jsonify({'message': "Success"}), 200
def place_add(): data = request.get_json() try: place = PlaceSchema(partial=True).load(data, unknown=EXCLUDE) except Exception: return jsonify({'message': "Invalid input"}), 405 session.add(place) session.commit() return jsonify({'message': "Success"}), 200
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
def user_add(): data = request.get_json() try: data['password'] = bcrypt.generate_password_hash( data['password']).decode('utf-8') user = UserSchema(partial=True).load(data, unknown=EXCLUDE) user.place = get_or_404(Place, int(data['place_id'])) except Exception: return jsonify({'message': "Invalid input"}), 405 session.add(user) session.commit() return jsonify({'message': "Success"}), 200
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
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
def advertisement_add(): data = request.get_json() try: advertisement = AdvertisementSchema(partial=("name", "description", "quantity", "status", "tag")).load( data, unknown=EXCLUDE) advertisement.owner = get_or_404(User, int(data['owner_id'])) except Exception: return jsonify({'message': "Invalid owner id"}), 400 session.add(advertisement) session.commit() return jsonify({'message': "Success"}), 200