diff --git a/project/controllers/library.py b/project/controllers/library.py index 14b3de0ed1188bf981b35c3454b513d4fa92731a..c2ea94b47466b876c41f491af9db27f2ed8ce21b 100644 --- a/project/controllers/library.py +++ b/project/controllers/library.py @@ -16,13 +16,25 @@ def libraries(user = None): @app.route('/libraries/add', methods=['POST']) @security('user') def addLibrary(user = None): - name = request.get['name'] + name = request.form['name'] library = Library.objects(user=user,unit='Movie',name=name).first() if library: return jsonify(response='error',message='Library with name %s already exists' % library.name),404 library = Library(user=user,unit='Movie',name=name).save() return jsonify(response='success',type='redirect',path=url_for(endpoint='libraries',_external=True)) +@app.route('/libraries/remove', methods=['POST']) +@security('user') +def removeLibrary(user = None): + name = request.form['name'] + library = Library.objects(user=user,unit='Movie',name=name).first() + if not library: + return jsonify(response='error',message='Library requested does not exists'),404 + if library.name == 'Master' or library.name == 'Borrowed': + return jsonify(response='error',message='Library %s cannot be deleted' % library.name),404 + library.delete() + return jsonify(response='success',type='redirect',path=url_for(endpoint='libraries',_external=True)) + @app.route('/libraries/<name>') @security('user') def library(name,user=None): diff --git a/project/templates/library/addlibrary_modal.html b/project/templates/library/addlibrary_modal.html new file mode 100644 index 0000000000000000000000000000000000000000..dc62d12ac7098254916c4ca7c0d80f6efab4b420 --- /dev/null +++ b/project/templates/library/addlibrary_modal.html @@ -0,0 +1,20 @@ +{% extends "modal_base.html" %} +{% block modal_title %} Add Library {% endblock %} +{% block modal_content %} + <form class="form-horizontal" id="addLibrary" role="form" method="post" action="{{ url_for('addLibrary') }}"> + <div class="form-group"> + <label for="name" class="col-sm-3 control-label">Name</label> + <div class="col-xs-9 col-md-6"> + <input type="text" class="form-control" name="name" placeholder="New Library Name"> + </div> + </div> + </form> +{% endblock %} +{% block accept_button_text %}Submit{% endblock %} +{% block ajax_url %}'{{ url_for('addLibrary') }}'{% endblock %} +{% block form_id %}addLibrary{% endblock %} +{% block validation_rules %} +rules: { + name: "required", +} +{% endblock %} \ No newline at end of file diff --git a/project/templates/library/master.html b/project/templates/library/master.html index 729eb10b0e719331ff7fde023427c6de37145e5c..efab2f093f7c24ee862ccb07fe3536399191cb01 100644 --- a/project/templates/library/master.html +++ b/project/templates/library/master.html @@ -1,7 +1,12 @@ {% extends "usernav.html" %} {% block content %} <div class="col-sm-12"> - <h3>My Libraries</h3> + <div class="col-md-4"> + <h3>My Libraries</h3> + </div> + <div class="btn-group pull-right"> + <button type="button" class="btn btn-default add-movie" data-toggle="modal" data-target="#addLibrary-form">Add Library</button> + </div> <table class="table table-striped table-hover"> <thead> <tr> @@ -24,9 +29,7 @@ {{ library.collection|length }} </td> <td> - <button class="btn btn-default btn-sm"> - <span class="glyphicon glyphicon-pencil"></span> Edit - </button> + <button type="button" class="btn btn-default btn-small remove-library" data-name='{{ library.name }}'>Delete</button> </td> </tr> {% endfor %} @@ -35,6 +38,10 @@ </div> {% endblock %} +{% block modals %} + {% include 'library/addlibrary_modal.html' %} +{% endblock %} + {% block javascript %} <script> $(function(){ @@ -42,7 +49,42 @@ $('.clickable').on('click',function(){ var libname = $(this).data('name'); window.document.location = libraryPath.replace('__lib__',libname); - }) + }); + + var removePath = '{{ url_for("removeLibrary") }}'; + $('.remove-library').on('click',function(event){ + event.stopPropagation(); + event.preventDefault(); + var library_name = $(this).data('name'); + var message = "Are you sure you want to delete this Library?"; + if(library_name == "Master" || library_name == "Borrowed"){ + alert("You cannot delete the "+library_name+" library."); + return false; + } + if(confirm(message)){ + $.ajax({ + url: removePath, + data: {name:library_name}, + 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 %} \ No newline at end of file