Working asset edit/create template

This commit is contained in:
Harry Bridge
2019-01-05 20:00:00 +00:00
parent 9f3c1f7f3c
commit fbfd8f272b
7 changed files with 428 additions and 15 deletions

9
filters.py Normal file
View File

@@ -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']

9
forms.py Normal file
View File

@@ -0,0 +1,9 @@
from django import forms
from assets import models
class AssetForm(forms.ModelForm):
class Meta:
model = models.Asset
fields = '__all__'

73
static/js/ajax_form.js Normal file
View File

@@ -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)}
});
}

23
static/js/csrf.js Normal file
View File

@@ -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'));
}
}
});

View File

@@ -1,15 +0,0 @@
{% extends 'base.html' %}
{% block main %}
<h1>Asset {{ form_header|default:'Create' }}</h1>
<form method="post">
{{ form }}
{% csrf_token %}
<button type="submit">Submit</button>
</form>
{% endblock %}

298
templates/asset_update.html Normal file
View File

@@ -0,0 +1,298 @@
{% extends 'base.html' %}
{% load widget_tweaks %}
{% load asset_templatetags %}
{% block title %}Asset {{ object.asset_id }}{% endblock %}
{% block main %}
<h4>
Asset
{% if object %}
| {{ object.asset_id }}
{% else %}
Create
{% endif %}
</h4>
<div class="divider"></div>
<br>
<form method="post" id="asset_update_form">
{% csrf_token %}
<input type="hidden" name="id" value="{{ object.id|default:0 }}">
<div class="row">
<div class="col s12">
<div class="button-group right">
{% if edit %}
{# <input type="submit" class="btn" value="Save">#}
<button type="button" class="btn" onclick="updateAsset()">Save</button>
{% else %}
<a href="{% url 'asset_update' object.pk %}" class="btn">Edit</a>
{% endif %}
{% if object %}
<a class="waves-effect waves-light btn modal-trigger" href="#confirm_delete_modal">Delete</a>
{% endif %}
</div>
</div>
</div>
<div class="row">
<div class="panel">
<div class="panel-heading">
Asset Details
</div>
<div class="panel-body">
<dl class="dl-horizontal">
{% if edit %}
<div class="row">
<div class="input-field col s12">
{% if object.asset_id %}
{% render_field form.asset_id|attr:'readonly'|add_class:'disabled_input' value=object.asset_id %}
{% else %}
{% render_field form.asset_id %}
{% endif %}
<label for="{{ form.asset_id.id_for_label }}" class="bold-text">Asset ID</label>
</div>
<div class="input-field col s12">
{% render_field form.description value=object.description %}
<label for="{{ form.description.id_for_label }}" class="bold-text">Description</label>
</div>
<div class="input-field col s12">
<select name="{{ form.category.name }}" id="{{ form.category.id_for_label }}" required>
{% for id, choice in form.category.field.choices %}
<option value="{{ id }}" {% if object.category.id == id %}selected{% endif %}>{{ choice }}</option>
{% endfor %}
</select>
<label for="{{ form.category.id_for_label }}" class="bold-text">Category</label>
</div>
<div class="input-field col s12">
<select name="{{ form.status.name }}" id="{{ form.status.id_for_label }}" required>
{% for id, choice in form.status.field.choices %}
<option value="{{ id }}" {% if object.status.id == id %}selected{% endif %}>{{ choice }}</option>
{% endfor %}
</select>
<label for="{{ form.status.id_for_label }}" class="bold-text">Status</label>
</div>
<div class="input-field col s12">
{% render_field form.serial_number value=object.serial_number %}
<label for="{{ form.serial_number.id_for_label }}">Serial Number</label>
</div>
<div class="input-field col s12">
{% render_field form.comments|add_class:'materialize-textarea' %}
<label for="{{ form.comments.id_for_label }}">Comments</label>
</div>
</div>
{% else %}
<dt>Asset ID</dt>
<dd>{{ object.asset_id }}</dd>
<dt>Description</dt>
<dd>{{ object.description }}</dd>
<dt>Category</dt>
<dd>{{ object.category }}</dd>
<dt>Status</dt>
<dd>{{ object.status }}</dd>
<dt>Serial Number</dt>
<dd>{{ object.serial_number|default:'-' }}</dd>
<dt>Comments</dt>
<dd>{{ object.comments|default:'-'|linebreaksbr }}</dd>
{% endif %}
</dl>
</div>
</div>
</div>
<div class="row">
<div class="panel">
<div class="panel-heading">
Purchase Details
</div>
<div class="panel-body">
<dl class="dl-horizontal">
{% if edit %}
<div class="row">
<div class="input-field col s12">
<select name="{{ form.purchased_from.name }}" id="{{ form.purchased_from.id_for_label }}">
{% for id, choice in form.purchased_from.field.choices %}
<option value="{{ id }}" {% if object.purchased_from.id == id %}selected{% endif %}>{{ choice }}</option>
{% endfor %}
</select>
<label for="{{ form.purchased_from.id_for_label }}">Purchased From</label>
</div>
<div class="input-field col s12">
{% render_field form.purchase_price value=object.purchase_price %}
<label for="{{ form.purchase_price.id_for_label }}">Purchase Price</label>
</div>
<div class="input-field col s12">
{% render_field form.salvage_value value=object.salvage_value %}
<label for="{{ form.salvage_value.id_for_label }}">Salvage Value</label>
</div>
<div class="input-field col s12">
{% render_field form.date_acquired|add_class:'datepicker' value=object.date_acquired|date %}
<label for="{{ form.date_acquired.id_for_label }}" class="bold-text">Date Acquired</label>
</div>
<div class="input-field col s12">
{% render_field form.date_sold|add_class:'datepicker' value=object.date_sold|date %}
<label for="{{ form.date_sold.id_for_label }}">Date Sold</label>
</div>
</div>
{% else %}
<dt>Purchased From</dt>
<dd>{{ object.purchased_from|default_if_none:'-' }}</dd>
<dt>Purchase Price</dt>
<dd>£{{ object.purchase_price|default_if_none:'-' }}</dd>
<dt>Salvage Value</dt>
<dd>£{{ object.salvage_value|default_if_none:'-' }}</dd>
<dt>Date Acquired</dt>
<dd>{{ object.date_acquired|default_if_none:'-' }}</dd>
<dt>Date Sold</dt>
<dd>{{ object.date_sold|default_if_none:'-' }}</dd>
{% endif %}
</dl>
</div>
</div>
</div>
{% if object.is_cable %}
<div class="row">
<div class="panel">
<div class="panel-heading">
Cable Details
</div>
<div class="panel-body">
<dl class="dl-horizontal">
<dt>Length</dt>
<dd>{{ object.length }}m</dd>
<dt>Type</dt>
<dd>TODO</dd>
<dt>Required CSA</dt>
<dd>TODO</dd>
</dl>
</div>
</div>
</div>
{% endif %}
<div class="row">
<div class="panel">
<div class="panel-heading">
Collection Details
</div>
<div class="panel-body">
<dl class="dl-horizontal">
{% if edit %}
<div class="row">
<div class="input-field col s10">
<input type="hidden" name="{{ form.parent.html_name }}" id="hidden_parent_id" value="{{ object.parent.id }}">
<input type="text" id="parent_id" value="{{ object.parent|default:'' }}" disabled="">
<label for="parent_id">Parent</label>
</div>
<div class="col s2">
<button type="button" class="btn btn-flat" onclick="clearParent()">
<i class="material-icons">clear</i>
</button>
</div>
<div class="input-field col s10">
<input type="text" id="parent_search">
<label for="parent_search">Search for asset</label>
{# {% render_field form.parent value=object.parent %}#}
</div>
<div class="col s2">
<button type="button" class="btn btn-flat" onclick="formAssetSearch()">
<i class="material-icons">search</i>
</button>
<br>
</div>
<div class="col s12" id="formAssetSearchResult">
<!--Placeholder for search results-->
</div>
</div>
{% else %}
<dt>Parent</dt>
<dd>
{% if object.parent %}
<a href="{% url 'asset_detail' object.parent.pk %}">
{{ object.parent.asset_id }} - {{ object.parent.description }}
</a>
{% else %}
<span>-</span>
{% endif %}
</dd>
<dt>Children</dt>
{% if object.asset_parent.all %}
{% for child in object.asset_parent.all %}
<dd>
<a href="{% url 'asset_detail' child.pk %}" >
{{ child.asset_id }} - {{ child.description }}
</a>
</dd>
{% endfor %}
{% else %}
<dd><span>-</span></dd>
{% endif %}
{% endif %}
</dl>
</div>
</div>
</div>
</form>
{% include 'confirm_delete.html' with object=object %}
{% endblock %}
{% block script %}
{# <script>#}
{# $('#asset_update_form').on('submit', function(event){#}
{#console.log($('#asset_update_form').serialize());#}
{# event.preventDefault();#}
{# updateAsset();#}
{# return false;#}
{# });#}
{# </script>#}
<script>
function clearParent() {
$('#hidden_parent_id').val('');
$('#parent_id').val('');
M.updateTextFields()
}
</script>
<script>
$(document).ready(function() {
{% if edit %}
var comments_id = '#{{ form.comments.id_for_label }}';
$(comments_id).val('{{ object.comments|linebreaksn }}');
M.textareaAutoResize($(comments_id));
{% endif %}
M.updateTextFields();
})
</script>
{% endblock %}

View File

@@ -0,0 +1,16 @@
{% for asset in object_list %}
<a href="javascript:void(0);" onclick="insertInParentField({{ asset.pk }}, '{{ asset }}')">
{{ asset.asset_id }} - {{ asset.description }}
</a>
<br>
{% empty %}
No assets match given ID
{% endfor %}
<script>
function insertInParentField(pk, str) {
$('#hidden_parent_id').val(pk);
$('#parent_id').val(str);
M.updateTextFields();
}
</script>