def createLoan(user=None): from datetime import datetime borrower = request.form["email"] return_date = request.form["date"] or None movie_id = request.form["movie"] if return_date: return_date = datetime.strptime(return_date, "%m/%d/%Y") if return_date <= datetime.now(): return jsonify(response="error", message="Return date must be in the future"), 404 if not movie_id: return jsonify(response="error", message="Invalid Movie given"), 404 from project.model.Movie import Movie movie = Movie.objects(id=movie_id).first() if not movie: return jsonify(response="error", message="Invalid Movie given"), 404 from project.model.Loan import Loan loan = Loan.objects(user=user, movie=movie).first() if loan: return jsonify(response="error", message="A loan already exists for this movie"), 404 loan = Loan.create(user, movie, borrower, return_date) from project.model.Library import Library borrowed_lib = Library.objects(user=user, unit="Movie", name="Loaned").first() borrowed_lib.addUnit(movie) return jsonify(response="success", type="reload")
def createLoan(user=None): from datetime import datetime borrower = request.form['email'] return_date = request.form['date'] or None movie_id = request.form['movie'] if return_date: return_date = datetime.strptime(return_date, '%m/%d/%Y') if return_date <= datetime.now(): return jsonify(response='error', message='Return date must be in the future'), 404 if not movie_id: return jsonify(response='error', message='Invalid Movie given'), 404 from project.model.Movie import Movie movie = Movie.objects(id=movie_id).first() if not movie: return jsonify(response='error', message='Invalid Movie given'), 404 from project.model.Loan import Loan loan = Loan.objects(user=user, movie=movie).first() if loan: return jsonify(response='error', message='A loan already exists for this movie'), 404 loan = Loan.create(user, movie, borrower, return_date) from project.model.Library import Library borrowed_lib = Library.objects(user=user, unit="Movie", name="Loaned").first() borrowed_lib.addUnit(movie) return jsonify(response='success', type="reload")
def returnMovie(user=None): movie_id = request.form['id'] if not movie_id: return jsonify(response='error', message='Invalid Movie given'), 404 from project.model.Movie import Movie movie = Movie.objects(id=movie_id).first() if not movie: return jsonify(response='error', message='Invalid Movie given'), 404 from project.model.Loan import Loan loan = Loan.objects(user=user, movie=movie).first() if not loan: return jsonify( response='error', message='This movie has not been loaned out and cannot be returned' ), 404 from project.model.Library import Library borrowed_lib = Library.objects(user=user, unit="Movie", name="Loaned").first() borrowed_lib.removeUnit(movie) loan.delete() return jsonify(response='success', type="reload")
def returnMovie(user=None): movie_id = request.form["id"] if not movie_id: return jsonify(response="error", message="Invalid Movie given"), 404 from project.model.Movie import Movie movie = Movie.objects(id=movie_id).first() if not movie: return jsonify(response="error", message="Invalid Movie given"), 404 from project.model.Loan import Loan loan = Loan.objects(user=user, movie=movie).first() if not loan: return jsonify(response="error", message="This movie has not been loaned out and cannot be returned"), 404 from project.model.Library import Library borrowed_lib = Library.objects(user=user, unit="Movie", name="Loaned").first() borrowed_lib.removeUnit(movie) loan.delete() return jsonify(response="success", type="reload")