Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ability to add new items to cart using CartItemList view
- Loading branch information
Showing
5 changed files
with
100 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
from django import forms | ||
from django.contrib.contenttypes.models import ContentType | ||
|
||
|
||
class CartItemForm(forms.Form): | ||
|
||
product_id = forms.IntegerField(min_value=1) | ||
product_type = forms.ModelChoiceField(queryset=ContentType.objects.all()) | ||
|
||
quantity = forms.IntegerField(min_value=1, initial=1, required=False) | ||
is_held = forms.BooleanField(initial=False, required=False) | ||
|
||
def clean_quantity(self): | ||
quantity = self.cleaned_data['quantity'] | ||
return quantity if quantity is not None else 1 | ||
|
||
def clean(self): | ||
product_id = self.cleaned_data.get('product_id') | ||
product_type = self.cleaned_data.get('product_type') | ||
if product_id and product_type: | ||
try: | ||
product = product_type.get_object_for_this_type(pk=product_id) | ||
except product_type.model_class().DoesNotExist: | ||
raise forms.ValidationError('Product does not exist') | ||
self.cleaned_data['product'] = product | ||
return self.cleaned_data | ||
|
||
def add_to_cart(self, cart): | ||
return cart.add(self.cleaned_data['product'], | ||
self.cleaned_data['quantity'], | ||
self.cleaned_data['is_held']) |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
from django.contrib.contenttypes.models import ContentType | ||
from django.template import Library | ||
|
||
|
||
register = Library() | ||
|
||
|
||
@register.filter | ||
def content_type_pk(value): | ||
return ContentType.objects.get_for_model(value.__class__).pk |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters