feature/product_models_refactor
mtyton 2023-06-08 19:49:19 +02:00
rodzic 784aba95dd
commit a66b6c85bb
8 zmienionych plików z 35 dodań i 29 usunięć

Wyświetl plik

@ -66,14 +66,17 @@ class SessionCart(BaseCart):
# TODO - add logging
...
def update_item_quantity(self, item_id: int, change: int) -> None:
def update_item_quantity(self, item_id: int, new_quantity: int) -> None:
self.validate_item_id(item_id)
if new_quantity < 1:
self.remove_item(item_id)
return
try:
self.session[settings.CART_SESSION_ID][str(item_id)] += change
self.session[settings.CART_SESSION_ID][str(item_id)] = new_quantity
self.session.modified = True
except KeyError:
# TODO - add logging
self.add_item(item_id, change)
self.add_item(item_id, new_quantity)
def get_items(self) -> List[CartItem]:
_items = []

Wyświetl plik

@ -122,16 +122,18 @@ $(document).on('click', '.add-to-cart-button', function(event) {
const productID = $(this).data('product-id');
const newQuantity = input.val();
const csrfToken = $(this).data('csrf-token');
const url = $(this).data("update-cart-url")
formData.append('product_id', productID);
formData.append('quantity', newQuantity);
console.log(input.val())
$.ajax({
type: 'POST',
url: '/../../cart/item/',
type: 'PUT',
url: url,
data: formData, // Use the serialized form data
headers: { 'X-CSRFToken': csrfToken },
dataType: 'json',
success: location.reload(),
processData: false, // Prevent jQuery from processing the data
contentType: false, // Let the browser set the content type
});

Wyświetl plik

@ -6,7 +6,7 @@
<div class="container h-100 py-5">
<div class="row d-flex justify-content-center align-items-center">
<div class="d-flex justify-content-between align-items-center mb-4">
<h3 class="fw-normal mb-0 text-black">Shopping Cart</h3>
<h3 class="fw-normal mb-0 text-black">Koszyk</h3>
</div>
</div>
{% for item in cart.get_items %}
@ -17,10 +17,10 @@
<div class="card-body">
<div class="row">
<div class="col-sm-6">
<h5 class="fw-normal mb-0 text-black">To Pay: {{cart.total_price}}</h5>
<h5 class="fw-normal mb-0 text-black">Do zapłaty: {{cart.total_price}}</h5>
</div>
<div class="col-sm-6 text-end">
<a href="{% url 'order' %}" class="btn btn-success btn-block btn-lg">Proceed to Pay</a>
<a href="{% url 'order' %}" class="btn btn-success btn-block btn-lg">Dalej</a>
</div>
</div>

Wyświetl plik

@ -8,8 +8,7 @@
<div class="container pt-4">
<div class="row">
<div class="col-12 px-4">
<h1>Order Data</h1>
<span>We won't share your data</span>
<h1>Twoje dane</h1>
<hr class="mt-1" />
</div>
</div>
@ -84,7 +83,7 @@
<div class="col-12 text-end mt-3">
<div class="form-outline">
<input type="submit" value="Confirm" class="btn btn-success btn-lg">
<input type="submit" value="Dalej" class="btn btn-success btn-lg">
</div>
</div>
</div>

Wyświetl plik

@ -5,13 +5,13 @@
<section class="h-100">
<div class="container">
<div class="d-flex justify-content-between align-items-center mb-4">
<h3 class="fw-normal mb-0 text-black">Customer Data</h3>
<h3 class="fw-normal mb-0 text-black">Twoje dane:</h3>
</div>
<div class="card mb-2 py-5">
<div class="card-body">
<div class="row">
<div class="col-sm-3">
<p class="mb-0">Full Name</p>
<p class="mb-0">Imię i Nazwisko</p>
</div>
<div class="col-sm-9">
<p class="text-muted mb-0">{{customer_data.full_name}}</p>
@ -29,7 +29,7 @@
<hr>
<div class="row">
<div class="col-sm-3">
<p class="mb-0">Phone</p>
<p class="mb-0">Numer telefonu</p>
</div>
<div class="col-sm-9">
<p class="text-muted mb-0">{{customer_data.phone}}</p>
@ -38,7 +38,7 @@
<hr>
<div class="row">
<div class="col-sm-3">
<p class="mb-0">Address</p>
<p class="mb-0">Adres</p>
</div>
<div class="col-sm-9">
<p class="text-muted mb-0">{{customer_data.full_address}}</p>
@ -51,7 +51,7 @@
<div class="container mt-5">
<div class="row d-flex justify-content-center align-items-center">
<div class="d-flex justify-content-between align-items-center mb-4">
<h3 class="fw-normal mb-0 text-black">Order Items</h3>
<h3 class="fw-normal mb-0 text-black">Zamówione przedmioty</h3>
</div>
</div>
{% for item in cart.get_items %}
@ -62,12 +62,12 @@
<div class="card-body">
<div class="row">
<div class="col-sm-6">
<h5 class="fw-normal mb-0 text-black">To Pay: {{cart.total_price}}</h5>
<h5 class="fw-normal mb-0 text-black">Do zapłaty: {{cart.total_price}}</h5>
</div>
<div class="col-sm-6 text-end">
<form action="" method="POST">
{% csrf_token %}
<input type="submit" class="btn btn-success btn-block btn-lg" value="Confirm">
<input type="submit" class="btn btn-success btn-block btn-lg" value="Zamawiam">
</form>
</div>
</div>

Wyświetl plik

@ -18,7 +18,10 @@
</button>
<input id="form1" min="0" name="quantity" value="{{item.quantity}}" type="number"
class="form-control form-control-sm" />
class="form-control form-control-sm quantity-input"
data-product-id="{{item.product.id}}"
data-csrf-token="{{csrf_token}}"
data-update-cart-url="{% url 'cart-action-update-product' item.product.id %}"/>
<button class="btn btn-link px-2"
onclick="this.parentNode.querySelector('input[type=number]').stepUp()">

Wyświetl plik

@ -7,17 +7,17 @@
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Added to cart</h5>
<h5 class="modal-title">Dodano do koszyka</h5>
<button type="button" class="close" data-bs-dismiss="modal" aria-label="Close" >
<span aria-hidden="true">&times;</span>
</button>
</div>
<div class="modal-body">
<p>Item has been added to cart.</p>
<p>Przedmiot został dodany do koszyka.</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Continue shopping</button>
<a href="{% url 'cart' %}" class="btn btn-primary">Go to cart</a>
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Kontynuuj zakupy</button>
<a href="{% url 'cart' %}" class="btn btn-primary">Idź do koszyka</a>
</div>
</div>
</div>
@ -36,13 +36,13 @@
<nav aria-label="Page navigation example">
<ul class="pagination justify-content-center">
<li class="page-item {% if not page.has_previous %} disabled {% endif %}">
<a class="page-link" href="?page={{page.number|add:-1}}" tabindex="-1" {% if not page.has_previous %}aria-disabled="true"{% endif %}>Previous</a>
<a class="page-link" href="?page={{page.number|add:-1}}" tabindex="-1" {% if not page.has_previous %}aria-disabled="true"{% endif %}>Poprzedni</a>
</li>
{% for pg in page.paginator.get_elided_page_range %}
<li class="page-item {% if pg == page.number %}active{% endif %}"><a class="page-link" href="?page={{pg}}">{{pg}}</a></li>
{% endfor %}
<li class="page-item {% if not page.has_next %} disabled {% endif %}">
<a class="page-link" {% if not page.has_next %}aria-disabled="true"{% endif %} href="?page={{page.number|add:1}}">Next</a>
<a class="page-link" {% if not page.has_next %}aria-disabled="true"{% endif %} href="?page={{page.number|add:1}}">Następny</a>
</li>
</ul>
</nav>

Wyświetl plik

@ -74,10 +74,9 @@ class CartActionView(ViewSet):
return Response(serializer.data, status=201)
@action(detail=True, methods=["put"])
def update_product(self, request, product_id):
def update_product(self, request, pk):
cart = SessionCart(self.request)
product_id = request.POST.get("product_id")
cart.update_item_quantity(product_id, request.PUT["quantity"])
cart.update_item_quantity(pk, int(request.data["quantity"]))
items = cart.get_items()
serializer = CartProductSerializer(instance=items, many=True)
return Response(serializer.data, status=201)