| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
from django import newforms as forms |
|---|
| 19 |
from django.contrib.contenttypes.models import ContentType |
|---|
| 20 |
|
|---|
| 21 |
from models import FixedPrice, FloatingPrice |
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
class PriceForm(forms.Form): |
|---|
| 25 |
def __init__(self, pricing_object, *args, **kwargs): |
|---|
| 26 |
super(PriceForm, self).__init__(*args, **kwargs) |
|---|
| 27 |
self.pricing_object = pricing_object |
|---|
| 28 |
|
|---|
| 29 |
def _get_final_price(self): |
|---|
| 30 |
return self.pricing_object.price |
|---|
| 31 |
final_price = property(_get_final_price) |
|---|
| 32 |
|
|---|
| 33 |
|
|---|
| 34 |
class FloatingPriceForm(PriceForm): |
|---|
| 35 |
desired_price = forms.DecimalField(required=True, max_digits=7, decimal_places=2) |
|---|
| 36 |
|
|---|
| 37 |
def __init__(self, pricing_object, *args, **kwargs): |
|---|
| 38 |
super(FloatingPriceForm, self).__init__(pricing_object, *args, **kwargs) |
|---|
| 39 |
self.fields['desired_price'].min_value = pricing_object.minimum |
|---|
| 40 |
|
|---|
| 41 |
def _get_final_price(self): |
|---|
| 42 |
return self.cleaned_data['desired_price'] |
|---|
| 43 |
final_price = property(_get_final_price) |
|---|
| 44 |
|
|---|
| 45 |
|
|---|
| 46 |
def create_price_form(product, data=None): |
|---|
| 47 |
price_model_type = product.pricing_type |
|---|
| 48 |
if price_model_type == ContentType.objects.get_for_model(FixedPrice): |
|---|
| 49 |
return PriceForm(product.pricing_object, data, prefix=product.id) |
|---|
| 50 |
elif price_model_type == ContentType.objects.get_for_model(FloatingPrice): |
|---|
| 51 |
return FloatingPriceForm(product.pricing_object, data, prefix=product.id, |
|---|
| 52 |
initial={'desired_price' : product.pricing_object.recommended}) |
|---|
| 53 |
|
|---|
| 54 |
def create_price_forms(products, data=None, selected=None): |
|---|
| 55 |
product_forms = [] |
|---|
| 56 |
selected_form = None |
|---|
| 57 |
for product in products: |
|---|
| 58 |
if product == selected: |
|---|
| 59 |
selected_form = create_price_form(product, data=data) |
|---|
| 60 |
product_forms.append((product, selected_form)) |
|---|
| 61 |
else: |
|---|
| 62 |
product_forms.append((product, create_price_form(product))) |
|---|
| 63 |
return (product_forms, selected_form) |
|---|