mirror of
https://github.com/nottinghamtec/PyRIGS.git
synced 2026-01-17 05:22:16 +00:00
Started work on improving asset form update and validation - requires de-spaghettifying of asset_update.html before continuing
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
from django import forms
|
||||
from django.core.exceptions import ValidationError
|
||||
|
||||
from assets import models
|
||||
|
||||
@@ -7,7 +8,15 @@ class AssetForm(forms.ModelForm):
|
||||
class Meta:
|
||||
model = models.Asset
|
||||
fields = '__all__'
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(AssetForm, self).__init__(*args, **kwargs)
|
||||
self.fields['asset_id'].disabled = True #You should not be able to change the asset ID, either in update or create
|
||||
|
||||
def clean_date_sold(self):
|
||||
if self.cleaned_data["date_sold"] and self.cleaned_data["date_acquired"] > self.cleaned_data["date_sold"]:
|
||||
raise ValidationError("Cannot sell an item before it is acquired")
|
||||
return self.cleaned_data["date_sold"]
|
||||
|
||||
class SupplierForm(forms.ModelForm):
|
||||
class Meta:
|
||||
|
||||
@@ -19,7 +19,11 @@
|
||||
{% endif %}
|
||||
</h1>
|
||||
</div>
|
||||
|
||||
{% if create %}
|
||||
<form method="post" id="asset_update_form" action="{% url 'asset_create'%}">
|
||||
{% else %}
|
||||
<form method="post" id="asset_update_form" action="{% url 'asset_update' pk=object.pk%}">
|
||||
{% endif %}
|
||||
<div class="row" style="padding-bottom: 1em">
|
||||
<div class="col-sm-12">
|
||||
<div class="pull-right">
|
||||
@@ -27,7 +31,6 @@
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<form method="post" id="asset_update_form">
|
||||
{% csrf_token %}
|
||||
<input type="hidden" name="id" value="{{ object.id|default:0 }}" hidden=true>
|
||||
<div class="row">
|
||||
@@ -76,7 +79,7 @@
|
||||
<label for="{{ form.serial_number.id_for_label }}">Serial Number</label>
|
||||
{% render_field form.serial_number|add_class:'form-control' value=object.serial_number %}
|
||||
</div>
|
||||
<!---TODO: Lower default number of lines in comments box--->
|
||||
<!---TODO: Lower default number of lines in comments box-->
|
||||
<div class="form-group">
|
||||
<label for="{{ form.comments.id_for_label }}">Comments</label>
|
||||
{% render_field form.comments|add_class:'form-control' %}
|
||||
@@ -143,16 +146,20 @@
|
||||
<label for="{{ form.date_acquired.id_for_label }}" >Date
|
||||
Acquired</label>
|
||||
{% if object.date_acquired %}
|
||||
{% render_field form.date_acquired|add_class:'form-control'|attr:'type="date"' value=object.date_acquired|date %}
|
||||
{% with date_acq=object.date_acquired|date:"Y-m-d" %}
|
||||
{% render_field form.date_acquired|add_class:'form-control'|attr:'type="date"' value=date_acq %}
|
||||
{% endwith %}
|
||||
{% else %}
|
||||
<input type="date" name="date_acquired" value="{% now "DATE_FORMAT" %}"
|
||||
<input type="date" name="date_acquired" value="{% now "%Y-%m-%d%" %}"
|
||||
class="form-control" id="id_date_acquired">
|
||||
{% endif %}
|
||||
</div>
|
||||
|
||||
<div class="form-group">
|
||||
<label for="{{ form.date_sold.id_for_label }}">Date Sold</label>
|
||||
{% render_field form.date_sold|add_class:'form-control'|attr:'type="date"' value=object.date_sold|date %}
|
||||
{% with date_sol=object.form.date_sold|date:"Y-m-d" %}
|
||||
{% render_field form.date_sold|add_class:'form-control'|attr:'type="date"' value=date_sol %}
|
||||
{% endwith %}
|
||||
</div>
|
||||
{% else %}
|
||||
<dl>
|
||||
@@ -242,71 +249,15 @@
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="row">
|
||||
<div class="col-md-12">
|
||||
{% include 'partials/asset_buttons.html' %}
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
|
||||
<div class="row">
|
||||
<div class="col-md-12">
|
||||
{% include 'partials/asset_buttons.html' %}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
{% include 'partials/confirm_delete.html' with object=object %}
|
||||
|
||||
{% endblock %}
|
||||
|
||||
{% block js %}
|
||||
<script>
|
||||
function updateAsset() {
|
||||
$.ajax({
|
||||
url: "{% url 'ajax_asset_update' %}", // the endpoint
|
||||
type: "POST", // http method
|
||||
data: {
|
||||
form: $('#asset_update_form').serialize()
|
||||
},
|
||||
traditional: true,
|
||||
headers: {
|
||||
'X-CSRFToken': document.getElementById("asset_update_form").csrfmiddlewaretoken.value
|
||||
},
|
||||
|
||||
success: function(data) {
|
||||
// console.log(data);
|
||||
window.location.href = data['url'];
|
||||
},
|
||||
|
||||
error: function(xhr) {
|
||||
console.log(xhr.status + ": " + xhr.responseText)
|
||||
}
|
||||
});
|
||||
}
|
||||
</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('');
|
||||
}
|
||||
</script>
|
||||
|
||||
<script>
|
||||
$(document).ready(function() {
|
||||
{
|
||||
%
|
||||
if edit or duplicate %
|
||||
}
|
||||
var comments_id = '#{{ form.comments.id_for_label }}';
|
||||
$(comments_id).val('{{ object.comments|linebreaksn }}'); {
|
||||
%
|
||||
endif %
|
||||
}
|
||||
})
|
||||
</script>
|
||||
{% endblock %}
|
||||
{% endblock %}
|
||||
@@ -1,16 +1,16 @@
|
||||
<div class="btn-group">
|
||||
{% if edit and object %}
|
||||
<!--edit-->
|
||||
<button type="button" class="btn btn-success" onclick="updateAsset()"><i class="glyphicon glyphicon-floppy-disk"></i> Save</button>
|
||||
<button type="submit" class="btn btn-success"><i class="glyphicon glyphicon-floppy-disk"></i> Save</button>
|
||||
<a class="btn btn-default" href="{% url 'asset_update' object.pk %}?duplicate=true"><i class="glyphicon glyphicon-duplicate"></i> Duplicate</a>
|
||||
<a class="btn btn-danger" data-toggle="modal" data-target="#confirm_delete_modal"><i class="glyphicon glyphicon-trash"></i> Delete</a>
|
||||
{% elif duplicate %}
|
||||
<!--duplicate-->
|
||||
<button type="button" class="btn btn-default" onclick="updateAsset()"><i class="glyphicon glyphicon-ok-sign"></i> Create Duplicate</button>
|
||||
<button type="submit" class="btn btn-default"><i class="glyphicon glyphicon-ok-sign"></i> Create Duplicate</button>
|
||||
<a href="{% url 'asset_detail' previous_asset_pk %}" class="btn btn-warning"><i class="glyphicon glyphicon-remove"></i> Cancel</a>
|
||||
{% elif not object %}
|
||||
{% elif create %}
|
||||
<!--create-->
|
||||
<button type="button" class="btn btn-success" onclick="updateAsset()"><i class="glyphicon glyphicon-floppy-disk"></i> Save</button>
|
||||
<button type="submit" class="btn btn-success"><i class="glyphicon glyphicon-floppy-disk"></i> Save</button>
|
||||
{% else %}
|
||||
<!--detail view-->
|
||||
<a href="{% url 'asset_update' object.pk %}" class="btn btn-default"><i class="glyphicon glyphicon-edit"></i> Edit</a>
|
||||
|
||||
@@ -10,7 +10,7 @@ urlpatterns = [
|
||||
path('', views.AssetList.as_view(), name='index'),
|
||||
path('asset/list/', views.AssetList.as_view(), name='asset_list'),
|
||||
path('asset/<int:pk>/', views.AssetDetail.as_view(), name='asset_detail'),
|
||||
path('asset/create/', views.AssetEdit.as_view(), name='asset_create'),
|
||||
path('asset/create/', views.AssetCreate.as_view(), name='asset_create'),
|
||||
path('asset/<int:pk>/edit/', views.AssetEdit.as_view(), name='asset_update'),
|
||||
path('asset/delete/', views.asset_delete, name='ajax_asset_delete'),
|
||||
path('asset/update/', views.asset_update, name='ajax_asset_update'),
|
||||
|
||||
@@ -53,8 +53,10 @@ class AssetDetail(LoginRequiredMixin, generic.DetailView):
|
||||
# template_name = 'asset_update.html'
|
||||
# # success_url = reverse_lazy('asset_list')
|
||||
|
||||
class AssetEdit(LoginRequiredMixin, generic.TemplateView):
|
||||
class AssetEdit(LoginRequiredMixin, generic.UpdateView):
|
||||
template_name = 'asset_update.html'
|
||||
model = models.Asset
|
||||
form_class = forms.AssetForm
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
context = super(AssetEdit, self).get_context_data(**kwargs)
|
||||
@@ -75,6 +77,19 @@ class AssetEdit(LoginRequiredMixin, generic.TemplateView):
|
||||
|
||||
return context
|
||||
|
||||
def get_success_url(self):
|
||||
return reverse("asset_detail", kwargs={"pk":self.object.id})
|
||||
|
||||
class AssetCreate(LoginRequiredMixin, generic.CreateView):
|
||||
template_name = 'asset_update.html'
|
||||
model = models.Asset
|
||||
form_class = forms.AssetForm
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
context = super(AssetCreate, self).get_context_data(**kwargs)
|
||||
context["create"] = True
|
||||
return context
|
||||
|
||||
@login_required()
|
||||
def asset_update(request):
|
||||
context = dict()
|
||||
|
||||
Reference in New Issue
Block a user