def tables_create(): engine = create_engine('postgres://{}:{}@{}/{}'.format( environ.get('DB_USER'), environ.get('DB_PASSWORD'), environ.get('DB_HOST'), environ.get('DB_NAME')), echo=True) Base = declarative_base() Suit.__table__.create(engine) # populate database with test data with session_scope() as session: suit = Suit() suit.model = 'mk1' suit.status = 'Destroyed in Escape from the Cave.' suit.first_appearance = 'Iron Man' suit.image_file = 'mk1.jpg' suit.video_file = 'test.gif' suit.description = '''The first Iron Man suit, the Mark I was created from Jericho missile parts while Tony Stark was being held captive in a cave in Afghanistan. The original Hall of Armors created by Tony after the first Iron Man film contained the armors Mark I to Mark IV, before being upgraded to house Mark I through Mark VII.''' session.add(suit) with session_scope() as session: suit = Suit() suit.model = 'mk2' suit.status = 'Destroyed by the Mandarin' suit.first_appearance = 'Iron Man' suit.image_file = 'mk2.jpg' suit.video_file = 'test.gif' suit.description = '''Designed as a prototype, this suit was used to explore flight potential and had almost no weapons, before quickly giving way to the Mark III. It was temporarily confiscated by the U.S. Military and was converted into the War Machine Mark I before Tony got it back and reverted it to the Mark II.''' session.add(suit) with session_scope() as session: suit = Suit() suit.model = 'mk3' suit.status = 'Destroyed by the Mandarin' suit.first_appearance = 'Iron Man' suit.image_file = 'mk3.jpg' suit.video_file = 'test2.gif' suit.description = '''This was the first armor to feature the iconic red and gold design.''' session.add(suit) with session_scope() as session: suit = Suit() suit.model = 'mk4' suit.status = 'Destroyed by the Mandarin' suit.first_appearance = 'Iron Man 2' suit.image_file = 'mk4.jpg' suit.video_file = 'test.gif' suit.description = '''After the Mark III took heavy damage in Tony’s fight with the Iron Mongor, this suit was built as a direct replacement, with only a few minor changes from the previous model.''' session.add(suit) with session_scope() as session: suit = Suit() suit.model = 'mk5' suit.status = 'Destroyed by the Mandarin' suit.first_appearance = 'Iron Man 2' suit.image_file = 'mk4.jpg' suit.video_file = 'test.gif' suit.description = '''The Mark V was an emergency suit, designed to be portable by collapsing into a briefcase.''' session.add(suit) return jsonify(engine.table_names())
def user_read_all(): try: if request.args['is_admin'] == 'true': is_admin = True else: is_admin = False except KeyError: with session_scope() as session: users = session.query(User).all() return jsonify({"users": [user for user in users]}) with session_scope() as session: users = session.query(User).filter(User.is_admin == is_admin) return jsonify({"users": [user for user in users]})
def user_register(): header = request.headers.get('Authorization') token = header.split()[1] jwt_user = jwt.decode(token, environ.get('FLASK_SECRET_KEY')) with session_scope() as session: user = session.query(User).get(jwt_user['id']) if not user: return jsonify(error='404 Not Found: user'), 404 try: user.name = request.get_json()['name'] except KeyError: return jsonify(error='422 Unprocessable Entity: no name'), 422 try: user.set_password(request.get_json()['password']) except KeyError: return jsonify(error='422 Unprocessable Entity: no password'), 422 user.role = 'authenticated' try: session.commit() except IntegrityError: return jsonify(error='409 Resource already exists: name'), 409 token = jwt.encode(user.as_dict(), environ.get('FLASK_SECRET_KEY')) return jsonify({ 'user': user.as_dict(), 'token': token.decode('utf-8') }), 200
def suit_status(suit_id): with session_scope() as session: suit = session.query(Suit).get(suit_id) if not suit: return jsonify(error='404 Not Found: suit not found'), 404 return jsonify({'status': suit.status}), 200
def suit_read(suit_id): with session_scope() as session: suit = session.query(Suit).get(suit_id) if not suit: return jsonify(error='404 Not Found: suit not found'), 404 return jsonify(suit.to_dict()), 200
def suit_read_video(suit_id): with session_scope() as session: suit = session.query(Suit).get(suit_id) if not suit: return jsonify(error='404 Not Found: suit not found'), 404 return jsonify({'description': suit.description}), 200
def user_delete(user_id): with session_scope() as session: user = session.query(User).get(user_id) if not user: return jsonify(error='404 Not Found: user not found'), 404 session.delete(user) return jsonify({'user': user}), 200
def user_update(user_id): with session_scope() as session: user = session.query(User).get(user_id) if not user: return jsonify(error='404 Not Found: user not found'), 404 try: email = request.get_json()['email'] user.email = email except KeyError: pass try: user.set_password(request.get_json()['password']) except KeyError: pass token = jwt.encode({ 'id': user.id }, environ.get('FLASK_SECRET_KEY')).decode('utf-8') return jsonify({'user': user, 'token': token}), 200
def user_create(): # TODO: check if request data has json ---> if(request.data): if not request.is_json: return jsonify(error='*** 422 Unprocessable Entity: is not JSON'), 422 try: email = request.get_json()['email'] except KeyError: return jsonify(error='422 Unprocessable Entity: no email'), 422 try: password = request.get_json()['password'] except KeyError: return jsonify(error='422 Unprocessable Entity: no password'), 422 try: is_admin = request.get_json()['is_admin'] except KeyError: return jsonify(error='422 Unprocessable Entity: no is_admin'), 422 try: with session_scope() as session: user = User() user.email = email user.set_password(password) user.is_admin = is_admin session.add(user) except IntegrityError: return jsonify({'message': 'User already exists.', 'info': 'User already exists.'}), 409 token = jwt.encode({'id': user.id}, environ.get('FLASK_SECRET_KEY')).decode('utf-8') return jsonify({'user': user, 'token': token}), 200
def index(): with session_scope() as session: suits = session.query(Suit).all() return render_template('index.html', suits=[suit.to_dict() for suit in suits])
def suit_read_all(): with session_scope() as session: suits = session.query(Suit).all() return jsonify({"suits": [suit.to_dict() for suit in suits]})