diff --git a/filters.py b/filters.py new file mode 100644 index 00000000..c7efcd7f --- /dev/null +++ b/filters.py @@ -0,0 +1,9 @@ +import django_filters + +from assets import models + + +class AssetFilter(django_filters.FilterSet): + class Meta: + model = models.Asset + fields = ['asset_id', 'description', 'category', 'status'] diff --git a/forms.py b/forms.py new file mode 100644 index 00000000..90a02b9a --- /dev/null +++ b/forms.py @@ -0,0 +1,9 @@ +from django import forms + +from assets import models + + +class AssetForm(forms.ModelForm): + class Meta: + model = models.Asset + fields = '__all__' diff --git a/static/js/ajax_form.js b/static/js/ajax_form.js new file mode 100644 index 00000000..d90363a3 --- /dev/null +++ b/static/js/ajax_form.js @@ -0,0 +1,73 @@ +function filterAssetTable() { + $.ajax({ + url : "/asset/filter/", // the endpoint + type : "POST", // http method + data : { + form: $('#asset-filter-form').serialize() + }, + traditional: true, + + success : function(data) { + // console.log(data); + $('#asset_table_body').html(data) + }, + + error : function(xhr) {console.log(xhr.status + ": " + xhr.responseText)} + }); +} + +function updateAsset() { + $.ajax({ + url : "/asset/update/", // the endpoint + type : "POST", // http method + data : { + form: $('#asset_update_form').serialize() + }, + traditional: true, + + success : function(data) { + // console.log(data); + window.location.href = data['url']; + }, + + error : function(xhr) {console.log(xhr.status + ": " + xhr.responseText)} + }); +} + +function formAssetSearch() { + $.ajax({ + url : "/asset/filter/", // the endpoint + type : "POST", // http method + data : { + sender: 'asset_update', + form: "csrfmiddlewaretoken=" + $('input[name=csrfmiddlewaretoken]').val() + "&asset_id=" + $('#parent_search').val() + }, + traditional: true, + + success : function(data) { + // console.log(data); + $('#formAssetSearchResult').html(data); + // window.location.href = data['url']; + }, + + error : function(xhr) {console.log(xhr.status + ": " + xhr.responseText)} + }); +} + +function deleteAsset(asset_id) { + $.ajax({ + url : "/asset/delete/", // the endpoint + type : "POST", // http method + data : { + asset_id: asset_id + }, + traditional: true, + + success : function(data) { + // console.log(data); + window.location.href = data['url']; + }, + + error : function(xhr) {console.log(xhr.status + ": " + xhr.responseText)} + }); +} \ No newline at end of file diff --git a/static/js/csrf.js b/static/js/csrf.js new file mode 100644 index 00000000..895ce31e --- /dev/null +++ b/static/js/csrf.js @@ -0,0 +1,23 @@ +$.ajaxSetup({ + beforeSend: function(xhr, settings) { + function getCookie(name) { + var cookieValue = null; + if (document.cookie && document.cookie != '') { + var cookies = document.cookie.split(';'); + for (var i = 0; i < cookies.length; i++) { + var cookie = jQuery.trim(cookies[i]); + // Does this cookie string begin with the name we want? + if (cookie.substring(0, name.length + 1) == (name + '=')) { + cookieValue = decodeURIComponent(cookie.substring(name.length + 1)); + break; + } + } + } + return cookieValue; + } + if (!(/^http:.*/.test(settings.url) || /^https:.*/.test(settings.url))) { + // Only send the token to relative URLs i.e. locally. + xhr.setRequestHeader("X-CSRFToken", getCookie('csrftoken')); + } + } +}); \ No newline at end of file diff --git a/templates/asset_form.html b/templates/asset_form.html deleted file mode 100644 index 65c21816..00000000 --- a/templates/asset_form.html +++ /dev/null @@ -1,15 +0,0 @@ -{% extends 'base.html' %} - - -{% block main %} - -