Skip to content

Commit

Permalink
Add logic - user cant ad more than stock - vault
Browse files Browse the repository at this point in the history
  • Loading branch information
tomik-z-cech committed Feb 28, 2024
1 parent e960cf0 commit 06cf6d4
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 10 deletions.
7 changes: 1 addition & 6 deletions items/templates/items/item_detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,7 @@ <h3>
Price per unit :
&nbsp;
<strong>
{% with price_slice=item.price_per_unit|floatformat:0 %}
{{ price_slice }}
<sup>
{{ item.price_per_unit|floatformat:"2"|stringformat:"s"|slice:"-2:"|add:"" }}
</sup>
{% endwith %}
{{ item.price_per_unit}}
&nbsp;
</strong>
Expand Down
4 changes: 2 additions & 2 deletions landing/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class LandingPageView(generic.ListView):
def get(self, request, *args, **kwargs):
"""This method generates view of landing page"""
# Render template
postage_settings = PostageSettings.objects.filter(pk=1).first()
free_postage_treshold = PostageSettings.objects.filter(pk=1).first()
new_arrivals = Item.objects.all().order_by('-date_added')[:3]
all_items = Item.objects.all()
sorted_by_likes = sorted(all_items, key=lambda item: item.rating_counter(), reverse=True)
Expand All @@ -28,7 +28,7 @@ def get(self, request, *args, **kwargs):
request,
self.template_name,
{
"free_postage": postage_settings.free_postage,
"free_postage": free_postage_treshold.free_postage,
"newsletter_form": NewsletterForm(),
"new_arrivals": new_arrivals,
"favourites": favourites,
Expand Down
3 changes: 3 additions & 0 deletions vault/context_list.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from django.shortcuts import get_object_or_404
from items.models import Item
from owner.models import PostageSettings

def vault_content(request):
"""
Expand All @@ -11,6 +12,8 @@ def vault_content(request):
item_per_line = get_object_or_404(Item, pk=vault_item[0])
price_per_line = item_per_line.price_per_unit * int(vault_item[1]) * int(vault_item[3])
subtotal = subtotal + price_per_line
free_postage_treshold = PostageSettings.objects.filter(pk=1).first()
print(free_postage_treshold)
vault_context = {
"items_in_vault": len(vault),
"subtotal": subtotal,
Expand Down
14 changes: 12 additions & 2 deletions vault/views.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from django.shortcuts import redirect
from django.shortcuts import redirect, get_object_or_404
from django.contrib import messages
from items.models import Item

# Create your views here.
def add_to_vault(request, item_pk):
Expand All @@ -7,6 +9,7 @@ def add_to_vault(request, item_pk):
[item_pk, size, value, quantity]
"""
vault = request.session.get('vault', [])
item_selected = get_object_or_404(Item, pk=item_pk)
quantity = request.POST['quantity']
size = request.POST.get('size', 1)
value = request.POST.get('value', 0)
Expand All @@ -20,8 +23,15 @@ def add_to_vault(request, item_pk):
item_to_add = [item_pk, size, value, quantity]
if append_new:
vault.append(item_to_add)
for vault_item in vault:
if vault_item[0] == item_pk and vault_item[3] > item_selected.item_stock:
messages.error(request, f'The amount of {item_selected.item_name} you trying to add to vault is higher than the stock amount.')
else:
request.session['vault'] = vault
messages.success(request, f'Item {item_selected.item_name} was added to vault.')
else:
item_to_add = [item_pk, size, value, quantity]
vault.append(item_to_add)
request.session['vault'] = vault
request.session['vault'] = vault
messages.success(request, f'Item {item_selected.item_name} was added to vault.')
return redirect('item-detail', item_pk=item_pk)

0 comments on commit 06cf6d4

Please sign in to comment.