def get_schedule_days(): full_response = [] for i in range(7): cur_response = {} movie_sessions = [] cur_date = datetime.datetime.today() + datetime.timedelta(days=i) date = f"{cur_date:%Y-%m-%d}" cur_response['date'] = date cur_response['day'] = calendar.day_name[cur_date.weekday()] for movie_schedule in session.query(MovieSchedule).filter_by( date=date): reservation_num = session.query(Reservation).filter_by( movie_schedule_id=movie_schedule.id).count() movie_sessions.append({ "time": movie_schedule.time, "movie": movie_schedule.movie.serialize, "reservation_num": reservation_num }) movie_sessions.sort(key=lambda x: x["time"]) cur_response["movie_sessions"] = movie_sessions full_response.append(cur_response) return jsonify(full_response), 200
def create_movie_schedule(): if g.user.role != 'admin': return jsonify({"description": "Not have required privileges."}), 400 date = request.json.get('date', '') time = request.json.get('time', '') movie_id = request.json.get('movie_id', '') if not date or not time or not movie_id: return jsonify(meta={ "code": 400, "type": "Bad Request", "message": "Missing arguments" }), 400 if session.query(Movie.id).filter_by(id=movie_id).scalar() is None: return jsonify( meta={ "code": 404, "type": "Not Found", "message": "Movie with specified id was not found" }), 404 if session.query(MovieSchedule).filter_by(date=date, time=time).first() is not None: return jsonify( meta={ "code": 409, "type": "Conflict", "message": "This date and time is already reserved" }), 409 movie_schedule = MovieSchedule(movie_id=movie_id, date=date, time=time) session.add(movie_schedule) session.commit() return jsonify(movie_schedule.serialize), 201
def get_movie_by_id(id): if session.query(Movie.id).filter_by(id=id).scalar() is None: return jsonify( meta={ "code": 404, "type": "Not Found", "message": "Movie with specified id was not found" }), 404 movie = session.query(Movie).filter_by(id=id).one() return jsonify(movie.serialize), 200
def get_movie_schedule_by_id(id): if session.query(MovieSchedule.id).filter_by(id=id).scalar() is None: return jsonify( meta={ "code": 404, "type": "Not Found", "message": "Specified schedule was not found" }), 404 movie_schedule = session.query(MovieSchedule).get(id) return jsonify(movie_schedule.serialize), 200
def get_schedule_by_movie(id): movie_schedules = session.query(MovieSchedule).all() proper_movie_schedule = [] for item in movie_schedules: if item.movie_id == id: proper_movie_schedule.append(item) return jsonify([i.serialize for i in proper_movie_schedule]), 200
def reserve(): movie_schedule_id = request.json.get('movie_schedule_id', '') user_id = request.json.get('user_id', '') if user_id != g.user.id: return jsonify(meta={ "code": 406, "type": "Not Acceptable", "message": "No permission" }), 406 if not movie_schedule_id or not user_id: return jsonify(meta={ "code": 400, "type": "Bad Request", "message": "Missing arguments" }), 400 if session.query(MovieSchedule).get(movie_schedule_id) is None: return jsonify( meta={ "code": 400, "type": "Bad Request", "message": "No such schedule on affiche" }), 400 reservation = Reservation(movie_schedule_id=movie_schedule_id, user_id=user_id) session.add(reservation) session.commit() return jsonify(reservation.serialize), 201
def user_reservations(id): res = [] reservations = {} movies = {} for item in session.query(Movie): movies[item.id] = item for item in session.query(Reservation): if item.user_id == id: reservations[item.movie_schedule_id] = item for item in session.query(MovieSchedule): if item.id in reservations: res.append({ "movie_name": movies[item.movie_id].name, "date": item.date, "time": item.time }) return jsonify([item for item in res]), 200
def modify_movie(id): if g.user.role != 'admin': return jsonify({"description": "Not have required privileges."}), 400 if session.query(Movie.id).filter_by(id=id).scalar() is None: return jsonify( meta={ "code": 404, "type": "Not Found", "message": "Movie with specified id was not found" }), 404 movie = session.query(Movie).filter_by(id=id).one() if request.method == 'PUT': name = request.json.get('name', '') picture = request.json.get('picture', '') info = request.json.get('info', '') actors = request.json.get('actors', '') duration = request.json.get('duration', '') if name: movie.name = name if picture: movie.picture = picture if info: movie.info = info if actors: movie.actors = actors if duration: movie.duration = duration session.add(movie) session.commit() return jsonify(movie.serialize), 200 elif request.method == 'DELETE': movie_schedule = session.query(MovieSchedule).filter_by( movie_id=id).first() if movie_schedule is not None: return jsonify(movie.serialize), 409 session.delete(movie) session.commit() return jsonify(movie.serialize), 200
def modify_movie_schedule(id): if g.user.role != 'admin': return jsonify({"description": "Not have required privileges."}), 400 if session.query(MovieSchedule.id).filter_by(id=id).scalar() is None: return jsonify( meta={ "code": 404, "type": "Not Found", "message": "Specified schedule was not found" }), 404 movie_schedule = session.query(MovieSchedule).get(id) if request.method == 'PUT': movie_id = request.json.get('movie_id', '') date = request.json.get('date', '') time = request.json.get('time', '') if movie_id: if session.query(Movie).filter_by(id=movie_id).first() is not None: movie_schedule.movie_id = movie_id else: return jsonify( meta={ "code": 400, "type": "Bad Request", "message": "No such movie id" }), 400 if date: movie_schedule.date = date if time: movie_schedule.time = time session.add(movie_schedule) session.commit() return jsonify(movie_schedule.serialize), 200 if request.method == 'DELETE': session.delete(movie_schedule) session.commit() return jsonify(movie_schedule.serialize), 200
def register(): print("Request Method:", request.method) username = request.json.get('username', '') firstname = request.json.get('firstname', '') lastname = request.json.get('lastname', '') email = request.json.get('email', '') password = request.json.get('password', '') phone_number = request.json.get('phone_number', '') photo = request.json.get('photo', '') # checking if all required arguments were passed if not username or not firstname or not lastname or not email or not password: return jsonify(meta={ "code": 400, "type": "Bad Request", "message": "Missing arguments" }), 400 # checking if such username or email already exist if session.query(User).filter_by(username=username).first() is not None or \ session.query(User).filter_by(username=email).first() is not None: return jsonify( meta={ "code": 409, "type": "Conflict", "message": "Such username or email already exist" }), 409 new_user = User(username=username, firstname=firstname, lastname=lastname, email=email, password_hash=User.hash_password(password), phone_number=phone_number, photo=photo) session.add(new_user) session.commit() return jsonify(new_user.serialize), 201
def login(): username = request.json.get('username', '') email = request.json.get('email', '') password = request.json.get('password', '') user = None if username: user = session.query(User).filter_by(username=username).first() elif email: user = session.query(User).filter_by(email=email).first() if user is None: return jsonify(meta={ "code": 404, "type": "Not Found", "message": "Try again" }), 404 if user.verify_password(password): return jsonify(user.serialize), 200 return jsonify(meta={ "code": 406, "type": "Not Acceptable", "message": "Invalid data supplied" }), 406
def user_handler_by_id(id): # if session.query(User).filter_by(id=id).scalar() is None: # return jsonify(meta={"code": 404, "type": "Not Found", "message": "Specified user was not found"}), 404 user = session.query(User).get(id) # if g.user.id != id: # return jsonify(meta={"code": 403, "type": "Forbidden", "message": "No permission"}), 403 if request.method == 'GET': return jsonify(user.serialize), 200 if request.method == 'PUT': username = request.json.get('username', '') firstname = request.json.get('firstname', '') lastname = request.json.get('lastname', '') email = request.json.get('email', '') password = request.json.get('password', '') phone_number = request.json.get('phone_number', '') photo = request.json.get('photo', '') if username: user.username = username if firstname: user.firstname = firstname if lastname: user.lastname = lastname if email: user.email = email if password: user.password_hash = User.hash_password(password) if phone_number: user.phone_number = phone_number if photo: user.photo = photo session.add(user) session.commit() return jsonify(user.serialize), 200 elif request.method == 'DELETE': session.delete(user) session.commit() return jsonify(user.serialize), 200
def get_movies(): movies = session.query(Movie).all() return jsonify([i.serialize for i in movies]), 200
def get_movie_schedules(): movie_schedules = session.query(MovieSchedule).all() return jsonify([i.serialize for i in movie_schedules])
def get_all_user(): if g.user.role != 'admin': return jsonify({"description": "Not have required privileges."}), 400 users = session.query(User).all() return jsonify([i.serialize for i in users]), 200