您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

forms.py 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. from django import forms
  2. from django.conf import settings
  3. from django.db.models import get_model
  4. from django.utils.translation import gettext_lazy as _
  5. basketline_model = get_model('basket', 'line')
  6. basket_model = get_model('basket', 'basket')
  7. Product = get_model('catalogue', 'product')
  8. class BasketLineForm(forms.ModelForm):
  9. save_for_later = forms.BooleanField(initial=False, required=False)
  10. class Meta:
  11. model = basketline_model
  12. exclude = ('basket', 'product', 'line_reference', )
  13. class SavedLineForm(forms.ModelForm):
  14. move_to_basket = forms.BooleanField(initial=False, required=False)
  15. class Meta:
  16. model = basketline_model
  17. exclude = ('basket', 'product', 'line_reference', 'quantity', )
  18. class BasketVoucherForm(forms.Form):
  19. code = forms.CharField(max_length=128)
  20. def __init__(self, *args, **kwargs):
  21. return super(BasketVoucherForm, self).__init__(*args,**kwargs)
  22. class ProductSelectionForm(forms.Form):
  23. product_id = forms.IntegerField(min_value=1)
  24. def clean_product_id(self):
  25. id = self.cleaned_data['product_id']
  26. try:
  27. return Product.objects.get(pk=id)
  28. except Product.DoesNotExist:
  29. raise forms.ValidationError(_("This product is not available for purchase"))
  30. class AddToBasketForm(forms.Form):
  31. product_id = forms.IntegerField(widget=forms.HiddenInput(), min_value=1)
  32. quantity = forms.IntegerField(initial=1, min_value=1)
  33. def __init__(self, basket, instance, *args, **kwargs):
  34. super(AddToBasketForm, self).__init__(*args, **kwargs)
  35. self.basket = basket
  36. self.instance = instance
  37. if instance:
  38. if instance.is_group:
  39. self._create_group_product_fields(instance)
  40. else:
  41. self._create_product_fields(instance)
  42. def clean_product_id(self):
  43. id = self.cleaned_data['product_id']
  44. product = Product.objects.get(id=id)
  45. if not product.has_stockrecord or not product.stockrecord.is_available_to_buy:
  46. raise forms.ValidationError(_("This product is not available for purchase"))
  47. return id
  48. def clean_quantity(self):
  49. qty = self.cleaned_data['quantity']
  50. basket_threshold = settings.OSCAR_MAX_BASKET_QUANTITY_THRESHOLD
  51. if basket_threshold:
  52. total_basket_quantity = self.basket.num_items
  53. max_allowed = basket_threshold - total_basket_quantity
  54. if qty > max_allowed:
  55. raise forms.ValidationError(
  56. _("Due to technical limitations we are not able to ship"
  57. " more than %(threshold)d items in one order. Your basket"
  58. " currently has %(basket)d items.") % {
  59. 'threshold': basket_threshold,
  60. 'basket': total_basket_quantity,
  61. })
  62. return qty
  63. def _create_group_product_fields(self, item):
  64. """
  65. Adds the fields for a "group"-type product (eg, a parent product with a
  66. list of variants.
  67. """
  68. choices = []
  69. for variant in item.variants.all():
  70. if variant.has_stockrecord:
  71. summary = u"%s (%s) - %.2f" % (variant.get_title(), variant.attribute_summary(),
  72. variant.stockrecord.price_incl_tax)
  73. choices.append((variant.id, summary))
  74. self.fields['product_id'] = forms.ChoiceField(choices=tuple(choices))
  75. def _create_product_fields(self, item):
  76. u"""Add the product option fields."""
  77. for option in item.options:
  78. self._add_option_field(item, option)
  79. def _add_option_field(self, item, option):
  80. u"""
  81. Creates the appropriate form field for the product option.
  82. This is designed to be overridden so that specific widgets can be used for
  83. certain types of options.
  84. """
  85. self.fields[option.code] = forms.CharField()