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])
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 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))
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)
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 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 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)
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 get_or_404(cls, pk): obj = session.query(cls).get(pk) if obj is None: raise Exception return obj
def place_get_all(): places = session.query(Place).all() return jsonify(PlaceSchema(many=True).dump(places))
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)
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))
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])