diff --git a/project/controllers/loan.py b/project/controllers/loan.py index 7b8c8ac66ecd6922ec5d5b75bea496819397ccf0..1644c61516797c1c06e0ca2799f6e973f2596964 100644 --- a/project/controllers/loan.py +++ b/project/controllers/loan.py @@ -45,7 +45,6 @@ def reminderEmail(user=None): @app.route('/loan-movie', methods=['POST']) @security('user') def createLoan(user=None): - import smtplib from datetime import datetime borrower = request.form['email'] return_date = request.form['date'] or None @@ -70,4 +69,28 @@ def createLoan(user=None): borrowed_lib = Library.objects(user=user,unit="Movie",name="Borrowed").first() borrowed_lib.addUnit(movie) - return jsonify(response='success',type="reload") \ No newline at end of file + return jsonify(response='success',type="reload") + +@app.route('/return-movie', methods=['POST']) +@security('user') +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="Borrowed").first() + borrowed_lib.removeUnit(movie) + loan.delete() + + return jsonify(response='success',type="reload") \ No newline at end of file diff --git a/project/templates/library/library.html b/project/templates/library/library.html index 728d19ed3312d1a51f5aff37e9f7ee077504f1d3..fd6595843946671f91cc7fe1688647c51ca5ae7d 100644 --- a/project/templates/library/library.html +++ b/project/templates/library/library.html @@ -33,6 +33,7 @@ <button type="button" class="btn btn-default btn-small lend-movie" data-toggle="modal" data-target="#loan-form" data-id='{{ movie.id }}'>Lend</button> {% else %} This movie is loaned to {{ loan.email }} and is due back on {{ loan.expected_return_date.strftime('%m-%d-%Y') }} + <button type="button" class="btn btn-default btn-small return-movie" data-toggle="modal" data-target="#return-form" data-id='{{ movie.id }}'>Return</button> {% endif %} {% if library.name != "Master" or loan == None %} <button type="button" class="btn btn-default btn-small remove-movie" data-id='{{ loop.index }}'>Remove</button> @@ -61,6 +62,7 @@ <button type="button" class="btn btn-default btn-small lend-movie" data-toggle="modal" data-target="#loan-form" data-id='{{ movie.id }}'>Lend</button> {% else %} This movie is loaned to {{ loan.borrower_email }} and is due back on {{ loan.expected_return_date.strftime('%m-%d-%Y') }} + <button type="button" class="btn btn-default btn-small return-movie" data-toggle="modal" data-target="#return-form" data-id='{{ movie.id }}'>Return</button> {% endif %} {% if library.name != "Master" or loan == None %} <button type="button" class="btn btn-default btn-small remove-movie" data-id='{{ loop.index }}'>Remove</button> @@ -120,6 +122,37 @@ } }); + + var returnPath = '{{ url_for("returnMovie") }}'; + $('.return-movie').on('click',function(event){ + var movie_id = $(this).data('id'); + var message = "Are you sure you want return this movie? "; + if(confirm(message)){ + event.stopPropagation(); + event.preventDefault(); + $.ajax({ + url: returnPath, + data: {id:movie_id}, + method: 'POST', + success: function(data,status,jqXHR){ + if(data['response'] == 'success'){ + if(data['type'] == 'redirect'){ + window.document.location = data['path']; + } else if (data['type'] == 'reload') { + window.document.location.reload(); + } + } + }, + error: function(jqXHR, textStatus, errorThrown){ + response = jqXHR.responseJSON; + if('message' in response){ + alert(response['message']); + } + } + }); + + } + }); }); </script> {% endblock %}