Example #1
0
 def ship_charfield_clean(self, field_name):
     if self.cleaned_data.get('copy_address'):
         self.cleaned_data['ship_' + field_name] = clean_field(self, field_name)
         return self.cleaned_data['ship_' + field_name]
     else:
         val = clean_field(self, 'ship_' + field_name)
         # REQUIRED_SHIPPING_DATA doesn't contain 'ship_' prefix
         if (not val) and field_name in self.required_shipping_data:
             raise forms.ValidationError(_('This field is required.'))
         return val
Example #2
0
 def ship_charfield_clean(self, field_name):
     if self.cleaned_data.get('copy_address'):
         self.cleaned_data['ship_' + field_name] = clean_field(self, field_name)
         return self.cleaned_data['ship_' + field_name]
     else:
         val = clean_field(self, 'ship_' + field_name)
         # REQUIRED_SHIPPING_DATA doesn't contain 'ship_' prefix
         if (not val) and field_name in self.required_shipping_data:
             raise forms.ValidationError(_('This field is required.'))
         return val
Example #3
0
    def clean_ship_postal_code(self):
        code = self.ship_charfield_clean('postal_code')
        if not self._shippable:
            return code

        if clean_field(self, 'copy_address'):
            # We take it that the country for shipping and billing is the same;
            # don't bother validating again
            return code

        country = None

        if self._local_only:
            shop_config = Config.objects.get_current()
            country = shop_config.sales_country
        else:
            country = self.ship_charfield_clean('country')

        if not country:
            # Either the store is misconfigured, or the country was
            # not supplied, so the country validation will fail and
            # we can defer the postcode validation until that's fixed.
            return code

        return self.validate_postcode_by_country(code, country)
Example #4
0
    def clean_ship_postal_code(self):
        code = self.ship_charfield_clean('postal_code')
        if not self._shippable:
            return code

        if clean_field(self, 'copy_address'):
            # We take it that the country for shipping and billing is the same;
            # don't bother validating again
            return code

        country = None

        if self._local_only:
            shop_config = Config.objects.get_current()
            country = shop_config.sales_country
        else:
            country = self.ship_charfield_clean('country')

        if not country:
            # Either the store is misconfigured, or the country was
            # not supplied, so the country validation will fail and
            # we can defer the postcode validation until that's fixed.
            return code

        return self.validate_postcode_by_country(code, country)
Example #5
0
    def clean_postal_code(self):
        print
        print ">>> in_clean_postal_code"
        postcode = self.cleaned_data.get('postal_code')
        print "postcode: %s" % postcode
        if not postcode and 'postal_code' not in self.required_billing_data:
            print "return_postcode_1"
            return postcode
        country = None

        if self._local_only:
            shop_config = Config.objects.get_current()
            country = shop_config.sales_country
        else:
            country = clean_field(self, 'country')

        if not country:
            # Either the store is misconfigured, or the country was
            # not supplied, so the country validation will fail and
            # we can defer the postcode validation until that's fixed.
            print "return_postcode_2"
            return postcode

        print "return_postcode_3"
        return self.validate_postcode_by_country(postcode, country)
Example #6
0
    def clean_postal_code(self):
        print
        print ">>> in_clean_postal_code"
        postcode = self.cleaned_data.get('postal_code')
        print "postcode: %s" % postcode
        if not postcode and 'postal_code' not in self.required_billing_data:
            print "return_postcode_1"
            return postcode
        country = None

        if self._local_only:
            shop_config = Config.objects.get_current()
            country = shop_config.sales_country
        else:
            country = clean_field(self, 'country')

        if not country:
            # Either the store is misconfigured, or the country was
            # not supplied, so the country validation will fail and
            # we can defer the postcode validation until that's fixed.
            print "return_postcode_2"
            return postcode

        print "return_postcode_3"
        return self.validate_postcode_by_country(postcode, country)
Example #7
0
 def clean_state(self):
     data = self.cleaned_data.get('state')
     if self._local_only:
         country = self._default_country
     else:
         country = clean_field(self, 'country')
         if country == None:
             raise forms.ValidationError(_('This field is required.'))
     self._check_state(data, country)
     return data
Example #8
0
 def clean_state(self):
     data = self.cleaned_data.get('state')
     if self._local_only:
         country = self._default_country
     else:
         country = clean_field(self, 'country')
         if country == None:
             raise forms.ValidationError(_('This field is required.'))
     self._check_state(data, country)
     return data
Example #9
0
File: forms.py Project: 34/T
    def __init__(self, *args, **kwargs):
        initial = kwargs.get('initial', {})
        form_initialdata.send(self.__class__, form=self, initial=initial, contact = kwargs.get('contact', None))
        kwargs['initial'] = initial

        shop = kwargs.pop('shop', None)
        shippable = kwargs.pop('shippable', True)
        super(ContactInfoForm, self).__init__(*args, **kwargs)
        if not shop:
            shop = Config.objects.get_current()
        self._shop = shop
        self._shippable = shippable

        #self.required_billing_data = config_value('SHOP', 'REQUIRED_BILLING_DATA')
        self.required_shipping_data = config_value('SHOP', 'REQUIRED_SHIPPING_DATA')
        self._local_only = shop.in_country_only
        self.enforce_state = config_value('SHOP','ENFORCE_STATE')

        self._default_country = shop.sales_country
        shipping_country = (self._contact and getattr(self._contact.shipping_address, 'country', None)) or self._default_country
        #self.fields['country'] = forms.ModelChoiceField(shop.countries(), required=False, label=_(u'国家'), empty_label=None, initial=billing_country.pk)
        self.fields['country'] = forms.ModelChoiceField(shop.countries(), required=False, label=_(u'国家'), empty_label=None, initial=shipping_country.pk)

        if self.enforce_state:
            # if self.is_bound and not self._local_only:
            if self.is_bound and not self._local_only:
                # If the user has already chosen the country and submitted,
                # populate accordingly.
                #
                # We don't really care if country fields are empty;
                # area_choices_for_country() handles those cases properly.
                #billing_country_data = clean_field(self, 'country')
                shipping_country_data = clean_field(self, 'country')

                if shipping_country_data:
                    shipping_country = shipping_country_data

            # Get areas for the initial country selected.
            shipping_areas = area_choices_for_country(shipping_country)

            shipping_state = (self._contact and getattr(self._contact.shipping_address, 'state', None)) or selection
            self.fields['ship_state'] = forms.ChoiceField(choices=shipping_areas, initial=shipping_state, required=False, label=_(u'地区'))

        # slap a star on the required fields
        for f in self.fields:
            fld = self.fields[f]
            if fld.required:
                fld.label = (fld.label or f) + '*'
        log.info('Sending form_init signal: %s', self.__class__)
        form_init.send(self.__class__, form=self)
Example #10
0
 def clean_ship_country(self):
     copy_address = clean_field(self, 'copy_address')
     if copy_address:
         return self.cleaned_data.get('country')
     if self._local_only:
         return self._default_country
     if not self._shippable:
         return self.cleaned_data.get('country')
     shipcountry = self.cleaned_data.get('ship_country')
     if not shipcountry:
         raise forms.ValidationError(_('This field is required.'))
     if config_value('PAYMENT', 'COUNTRY_MATCH'):
         country = self.cleaned_data.get('country')
         if shipcountry != country:
             raise forms.ValidationError(_('Shipping and Billing countries must match'))
     return shipcountry
Example #11
0
 def clean_ship_country(self):
     copy_address = clean_field(self, 'copy_address')
     if copy_address:
         return self.cleaned_data.get('country')
     if self._local_only:
         return self._default_country
     if not self._shippable:
         return self.cleaned_data.get('country')
     shipcountry = self.cleaned_data.get('ship_country')
     if not shipcountry:
         raise forms.ValidationError(_('This field is required.'))
     if config_value('PAYMENT', 'COUNTRY_MATCH'):
         country = self.cleaned_data.get('country')
         if shipcountry != country:
             raise forms.ValidationError(_('Shipping and Billing countries must match'))
     return shipcountry
Example #12
0
    def __init__(self, *args, **kwargs):
        initial = kwargs.get('initial', {})
        form_initialdata.send(ContactInfoForm,
                              form=self,
                              initial=initial,
                              contact=kwargs.get('contact', None))
        kwargs['initial'] = initial

        shop = kwargs.pop('shop', None)
        shippable = kwargs.pop('shippable', True)
        super(ContactInfoForm, self).__init__(*args, **kwargs)
        if not shop:
            shop = Config.objects.get_current()
        self._shop = shop
        self._shippable = shippable

        self.required_billing_data = config_value('SHOP',
                                                  'REQUIRED_BILLING_DATA')
        self.required_shipping_data = config_value('SHOP',
                                                   'REQUIRED_SHIPPING_DATA')
        self._local_only = shop.in_country_only
        self.enforce_state = config_value('SHOP', 'ENFORCE_STATE')

        self._default_country = shop.sales_country
        billing_country = (self._contact and getattr(
            self._contact.billing_address, 'country',
            None)) or self._default_country
        shipping_country = (self._contact and getattr(
            self._contact.shipping_address, 'country',
            None)) or self._default_country
        self.fields['country'] = forms.ModelChoiceField(
            shop.countries(),
            required=False,
            label=_('Country'),
            empty_label=None,
            initial=billing_country.pk)
        self.fields['ship_country'] = forms.ModelChoiceField(
            shop.countries(),
            required=False,
            label=_('Country'),
            empty_label=None,
            initial=shipping_country.pk)

        if self.enforce_state:
            # if self.is_bound and not self._local_only:
            if self.is_bound and not self._local_only:
                # If the user has already chosen the country and submitted,
                # populate accordingly.
                #
                # We don't really care if country fields are empty;
                # area_choices_for_country() handles those cases properly.
                billing_country_data = clean_field(self, 'country')
                shipping_country_data = clean_field(self, 'ship_country')

                # Has the user selected a country? If so, use it.
                if billing_country_data:
                    billing_country = billing_country_data

                if clean_field(self, "copy_address"):
                    shipping_country = billing_country
                elif shipping_country_data:
                    shipping_country = shipping_country_data

            # Get areas for the initial country selected.
            billing_areas = area_choices_for_country(billing_country)
            shipping_areas = area_choices_for_country(shipping_country)

            billing_state = (self._contact and getattr(
                self._contact.billing_address, 'state', None)) or selection
            self.fields['state'] = forms.ChoiceField(
                choices=billing_areas,
                initial=billing_state,
                label=_('State'),
                # if there are not states, then don't make it required. (first
                # choice is always either "--Please Select--", or "Not
                # Applicable")
                required=len(billing_areas) > 1)

            shipping_state = (self._contact and getattr(
                self._contact.shipping_address, 'state', None)) or selection
            self.fields['ship_state'] = forms.ChoiceField(
                choices=shipping_areas,
                initial=shipping_state,
                required=False,
                label=_('State'))

        for fname in self.required_billing_data:
            if fname == 'country' and self._local_only:
                continue

            # ignore the user if ENFORCE_STATE is on; if there aren't any
            # states, we might have made the billing state field not required in
            # the enforce_state block earlier, and we don't want the user to
            # make it required again.
            if fname == 'state' and self.enforce_state:
                continue

            self.fields[fname].required = True

        # if copy_address is on, turn of django's validation for required fields
        if not (self.is_bound and clean_field(self, "copy_address")):
            for fname in self.required_shipping_data:
                if fname == 'country' and self._local_only:
                    continue
                self.fields['ship_%s' % fname].required = True

        # slap a star on the required fields
        for f in self.fields:
            fld = self.fields[f]
            if fld.required:
                fld.label = (fld.label or f) + '*'
        log.info('Sending form_init signal: %s', ContactInfoForm)
        form_init.send(ContactInfoForm, form=self)
Example #13
0
    def __init__(self, *args, **kwargs):
        initial = kwargs.get('initial', {})
        form_initialdata.send(ContactInfoForm, form=self, initial=initial, contact = kwargs.get('contact', None))
        kwargs['initial'] = initial

        shop = kwargs.pop('shop', None)
        shippable = kwargs.pop('shippable', True)
        super(ContactInfoForm, self).__init__(*args, **kwargs)
        if not shop:
            shop = Config.objects.get_current()
        self._shop = shop
        self._shippable = shippable

        self.required_billing_data = config_value('SHOP', 'REQUIRED_BILLING_DATA')
        self.required_shipping_data = config_value('SHOP', 'REQUIRED_SHIPPING_DATA')
        self._local_only = shop.in_country_only
        self.enforce_state = config_value('SHOP','ENFORCE_STATE')

        organization_name = self._contact and getattr(self._contact.organization, 'name', '')
        self.fields['organization'] = forms.CharField(max_length=50, label=_('Organization'), required=False, initial=organization_name)
        self._default_country = shop.sales_country
        billing_country = (self._contact and getattr(self._contact.billing_address, 'country', None)) or self._default_country
        shipping_country = (self._contact and getattr(self._contact.shipping_address, 'country', None)) or self._default_country
        self.fields['country'] = forms.ModelChoiceField(shop.countries(), required=False, label=_('Country'), empty_label=None, initial=billing_country.pk)
        self.fields['ship_country'] = forms.ModelChoiceField(shop.countries(), required=False, label=_('Country'), empty_label=None, initial=shipping_country.pk)

        if self.enforce_state:
            # if self.is_bound and not self._local_only:
            if self.is_bound and not self._local_only:
                # If the user has already chosen the country and submitted,
                # populate accordingly.
                #
                # We don't really care if country fields are empty;
                # area_choices_for_country() handles those cases properly.
                billing_country_data = clean_field(self, 'country')
                shipping_country_data = clean_field(self, 'ship_country')

                # Has the user selected a country? If so, use it.
                if billing_country_data:
                    billing_country = billing_country_data

                if clean_field(self, "copy_address"):
                    shipping_country = billing_country
                elif shipping_country_data:
                    shipping_country = shipping_country_data

            # Get areas for the initial country selected.
            billing_areas = area_choices_for_country(billing_country)
            shipping_areas = area_choices_for_country(shipping_country)

            billing_state = (self._contact and getattr(self._contact.billing_address, 'state', None)) or selection
            self.fields['state'] = forms.ChoiceField(choices=billing_areas, initial=billing_state, label=_('State'),
                # if there are not states, then don't make it required. (first
                # choice is always either "--Please Select--", or "Not
                # Applicable")
                required=len(billing_areas)>1)

            shipping_state = (self._contact and getattr(self._contact.shipping_address, 'state', None)) or selection
            self.fields['ship_state'] = forms.ChoiceField(choices=shipping_areas, initial=shipping_state, required=False, label=_('State'))

        for fname in self.required_billing_data:
            if fname == 'country' and self._local_only:
                continue

            # ignore the user if ENFORCE_STATE is on; if there aren't any
            # states, we might have made the billing state field not required in
            # the enforce_state block earlier, and we don't want the user to
            # make it required again.
            if fname == 'state' and self.enforce_state:
                continue

            self.fields[fname].required = True

        # if copy_address is on, turn of django's validation for required fields
        if not (self.is_bound and clean_field(self, "copy_address")):
            for fname in self.required_shipping_data:
                if fname == 'country' and self._local_only:
                    continue
                self.fields['ship_%s' % fname].required = True

        # slap a star on the required fields
        for f in self.fields:
            fld = self.fields[f]
            if fld.required:
                fld.label = (fld.label or f) + '*'
        log.info('Sending form_init signal: %s', ContactInfoForm)
        form_init.send(ContactInfoForm, form=self)
Example #14
0
 def ship_charfield_clean(self, field_name):
     val = clean_field(self, 'ship_' + field_name)
     # REQUIRED_SHIPPING_DATA doesn't contain 'ship_' prefix
     if self._shippable and (not val) and field_name in self.required_shipping_data:
         raise forms.ValidationError(_('This field is required.'))
     return val