update on the bulk update
This commit is contained in:
parent
2c0e40598a
commit
0c81022e7c
@ -279,6 +279,11 @@ urlpatterns = [
|
||||
views.CarFinanceUpdateView.as_view(),
|
||||
name="car_finance_update",
|
||||
),
|
||||
path(
|
||||
"htmx/cars/bulk_update_car_price/",
|
||||
views.bulk_update_car_price,
|
||||
name="bulk_update_car_price",
|
||||
),
|
||||
path("ajax/", views.AjaxHandlerView.as_view(), name="ajax_handler"),
|
||||
path(
|
||||
"cars/<slug:slug>/add-color/", views.CarColorCreate.as_view(), name="add_color"
|
||||
|
||||
@ -23,6 +23,7 @@ from inventory.models import Status as LeadStatus
|
||||
from django.db import IntegrityError
|
||||
from background_task.models import Task
|
||||
from django.views.generic import FormView
|
||||
from django.views.decorators.http import require_http_methods
|
||||
from django.db.models.deletion import RestrictedError
|
||||
from django.http.response import StreamingHttpResponse
|
||||
from django.core.exceptions import ImproperlyConfigured, ValidationError
|
||||
@ -9287,4 +9288,27 @@ def upload_cars(request,pk=None):
|
||||
|
||||
return render(request, 'csv_upload.html',{"make_data":models.CarMake.objects.all(),"form":form,"item":item})
|
||||
###############################################################
|
||||
###############################################################
|
||||
###############################################################
|
||||
|
||||
@require_http_methods(["POST"])
|
||||
def bulk_update_car_price(request):
|
||||
if request.method == "POST":
|
||||
cars = request.POST.getlist('car')
|
||||
price = request.POST.get('price')
|
||||
|
||||
if not price or int(price) <= 0:
|
||||
messages.error(request, "Please enter a valid price")
|
||||
elif not cars:
|
||||
messages.error(request, "No cars selected for price update")
|
||||
else:
|
||||
for car_pk in cars:
|
||||
car_finance , created = models.CarFinance.objects.get_or_create(car__pk=car_pk, cost_price=Decimal(price),selling_price=0)
|
||||
if not created:
|
||||
car_finance.cost_price = Decimal(price)
|
||||
car_finance.selling_price = 0
|
||||
car_finance.save()
|
||||
messages.success(request, "Price updated successfully")
|
||||
|
||||
response = HttpResponse()
|
||||
response['HX-Redirect'] = reverse('car_list')
|
||||
return response
|
||||
|
||||
@ -145,8 +145,14 @@
|
||||
hx-on::after-request="filter_after_request()">{{ _("Search") }}</button>
|
||||
</div>
|
||||
<div class="row">
|
||||
<form class="update-price-form d-flex flex-row align-items-center ms-auto w-25 d-none" action="" method="post" style="float:right;">
|
||||
<input class="form-control me-2" type="number" placeholder='{{ _("Search") }}' name="price" aria-label="Price">
|
||||
<form hx-boost='true' action="{% url 'bulk_update_car_price' %}" method="post"
|
||||
hx-include=".car-checkbox"
|
||||
class="update-price-form d-flex flex-row align-items-center ms-auto w-25 d-none" style="float:right;">
|
||||
{% csrf_token %}
|
||||
<div class="form-floating me-2">
|
||||
<input class="form-control" type="number" placeholder='{{ _("Search") }}' name="price" aria-label="Price" id="price">
|
||||
<label for="price">{{ _("Price") }}</label>
|
||||
</div>
|
||||
<button class="btn btn-outline-primary" type="submit">{{ _("Search") }}</button>
|
||||
</form>
|
||||
<div class="table-responsive scrollbar transition">
|
||||
@ -186,7 +192,7 @@
|
||||
<tr class="position-static">
|
||||
<td class="align-middle white-space-nowrap">
|
||||
<div class="form-check">
|
||||
<input class="form-check-input car-checkbox" type="checkbox" name="car" id="car-{{car.pk}}">
|
||||
<input class="form-check-input car-checkbox" type="checkbox" name="car" value="{{ car.pk }}" id="car-{{car.pk}}">
|
||||
</div>
|
||||
</td>
|
||||
<td class="align-middle white-space-nowrap ps-1">
|
||||
@ -313,18 +319,31 @@
|
||||
|
||||
document.getElementById('select-all').addEventListener('change', function() {
|
||||
const checkboxes = document.querySelectorAll('#project-list-table-body input[type="checkbox"]');
|
||||
checkboxes.forEach(checkbox => {
|
||||
checkbox.checked = this.checked;
|
||||
});
|
||||
});
|
||||
document.getElementById('car-checkbox').addEventListener('change', function() {
|
||||
const form = document.querySelector('.update-price-form');
|
||||
if (this.checked) {
|
||||
form.classList.remove('d-none');
|
||||
checkboxes.forEach(checkbox => checkbox.checked = true);
|
||||
} else {
|
||||
form.classList.add('d-none');
|
||||
checkboxes.forEach(checkbox => checkbox.checked = false);
|
||||
}
|
||||
updateFormVisibility();
|
||||
});
|
||||
|
||||
const cbox = document.querySelectorAll('.car-checkbox');
|
||||
cbox.forEach(checkbox => {
|
||||
checkbox.addEventListener('change', function() {
|
||||
updateFormVisibility();
|
||||
});
|
||||
});
|
||||
|
||||
function updateFormVisibility() {
|
||||
const form = document.querySelector('.update-price-form');
|
||||
const checkedCount = document.querySelectorAll('.car-checkbox:checked').length;
|
||||
const submitButton = form.querySelector('button[type="submit"]');
|
||||
if (checkedCount > 0) {
|
||||
form.classList.remove('d-none');
|
||||
submitButton.textContent = `Update Cost Price (${checkedCount})`;
|
||||
} else {
|
||||
form.classList.add('d-none');
|
||||
}
|
||||
}
|
||||
</script>
|
||||
{% endblock customJS %}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user