root/trunk/djedna/store/forms.py

Revision 409, 2.5 kB (checked in by thomas, 11 months ago)

First check in of initial store functionality

Line 
1 # (c) Copyright 2008 Thomas Bohmbach, Jr.
2 #
3 # This file is part of DJ Edna.
4 #
5 # DJ Edna is free software: you can redistribute it and/or modify it under the
6 # terms of the GNU General Public License as published by the Free
7 # Software Foundation, either version 3 of the License, or (at your option)
8 # any later version.
9 #
10 # DJ Edna is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 # or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
13 # more details.
14 #
15 # You should have received a copy of the GNU General Public License along with
16 # DJ Edna.  If not, see <http://www.gnu.org/licenses/>.
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)
Note: See TracBrowser for help on using the browser.