def __init__(self, *args, **kwargs): super(PaymentContactInfoForm, self).__init__(*args, **kwargs) if not self.cart: request = threadlocals.get_current_request() self.cart = Cart.objects.from_request(request) self.fields['discount'] = forms.CharField(max_length=30, required=False) self.payment_required_fields = {} if config_value('PAYMENT', 'USE_DISCOUNTS'): if not self.fields['discount'].initial: sale = _find_sale(self.cart) if sale: self.fields['discount'].initial = sale.code else: self.fields['discount'].widget = forms.HiddenInput() # Listeners of the form_init signal (below) may modify the dict of # payment_required_fields. For example, if your CUSTOM_PAYMENT requires # customer's city, put the following code in the listener: # # form.payment_required_fields['CUSTOM_PAYMENT'] = ['city'] # form_init.send(PaymentContactInfoForm, form=self)
def satchmo_language_selection_form(context): """ Display the set language form, if enabled in shop settings. """ request = threadlocals.get_current_request() enabled = get_l10n_setting('allow_translation_choice') languages = [] if enabled: try: url = urlresolvers.reverse('satchmo_set_language') languages = settings.LANGUAGES except urlresolvers.NoReverseMatch: url = "" log.warning('No url found for satchmo_set_language (OK if running tests)') else: url = "" media_url = context.get('media_url', None) return { 'enabled' : enabled, 'set_language_url' : url, 'languages' : languages, 'media_url' : media_url, 'django_language' : request.session.get('django_language', 'en'), }
def satchmo_language_selection_form(context): """ Display the set language form, if enabled in shop settings. """ request = threadlocals.get_current_request() enabled = get_l10n_setting('allow_translation_choice') languages = [] if enabled: try: url = urlresolvers.reverse('satchmo_set_language') languages = settings.LANGUAGES print "111" except urlresolvers.NoReverseMatch: url = "" log.warning( 'No url found for satchmo_set_language (OK if running tests)') print "112" else: url = "" return { 'enabled': enabled, 'set_language_url': url, 'languages': languages, 'STATIC_URL': context.get('STATIC_URL', ''), # for easy flag images 'django_language': request.session.get('django_language', 'en'), }
def satchmo_language_selection_form(context): """ Display the set language form, if enabled in shop settings. """ request = threadlocals.get_current_request() enabled = get_l10n_setting("allow_translation_choice") languages = [] if enabled: try: url = urlresolvers.reverse("satchmo_set_language") languages = settings.LANGUAGES print "111" except urlresolvers.NoReverseMatch: url = "" log.warning("No url found for satchmo_set_language (OK if running tests)") print "112" else: url = "" return { "enabled": enabled, "set_language_url": url, "languages": languages, "STATIC_URL": context.get("STATIC_URL", ""), # for easy flag images "django_language": request.session.get("django_language", "en"), }
def persist_query_string(url, from_url=None): """ Transfers GET parameters from ``from_url`` to ``url`` as *defaults* (if ``url`` has a different value for that GET parameter, it will remain as-is). Also has the side effect of sorting the query-string parameter keys. """ from_url = from_url or get_current_request().get_full_path() parsed_url = urlparse(from_url) querydict = parse_qs(parsed_url.query) if not querydict: return url else: parsed_new_url = urlparse(url) querydict.update(parse_qs(parsed_new_url.query)) newquery = urlencode(sorted(querydict.items()), doseq=True) new_url = "%s?%s%s" % ( parsed_new_url.path, newquery, parsed_new_url.fragment ) return new_url
def clean_discount(self): """ Check if discount exists and is valid. """ if not config_value('PAYMENT', 'USE_DISCOUNTS'): return '' data = self.cleaned_data['discount'] if data: try: discount = Discount.objects.get(code=data, active=True) except Discount.DoesNotExist: raise forms.ValidationError(_('Invalid discount code.')) request = threadlocals.get_current_request() try: contact = Contact.objects.from_request(request) except Contact.DoesNotExist: contact = None valid, msg = discount.isValid(self.cart, contact=contact) if not valid: raise forms.ValidationError(msg) # TODO: validate that it can work with these products return data
def __init__(self, *args, **kwargs): super(PaymentShippingAddressBookForm, self).__init__(*args, **kwargs) if not self.cart: request = threadlocals.get_current_request() self.cart = Cart.objects.from_request(request) shipping_choices_ = shipping_choices() if len(shipping_choices_) == 1: self.fields['shippingmethod'].widget = forms.HiddenInput(attrs={'value' : shipping_choices_[0][0]}) else: self.fields['shippingmethod'].widget = forms.RadioSelect(attrs={'value' : shipping_choices_[0][0]}) self.fields['shippingmethod'].choices = shipping_choices_ self.fields['discount'] = forms.CharField(max_length=30, required=False) if config_value('PAYMENT', 'USE_DISCOUNTS'): if not self.fields['discount'].initial: sale = _find_sale(self.cart) if sale: self.fields['discount'].initial = sale.code else: self.fields['discount'].widget = forms.HiddenInput() form_init.send(PaymentContactInfoForm, form=self)
def __init__(self, sender, instance, **kwargs): self.sender = sender self.instance = instance self.request = get_current_request() self.user = getattr(self.request, 'user', None) self.kwargs = kwargs
def by_host(host=None, id_only=False, called_recursive=None): """Get the current site by looking at the request stored in the thread. Returns the best match found in the `django.contrib.sites` app. If not found, then returns the default set as given in `settings.SITE_ID` Params: - `host`: optional, host to look up - `id_only`: if true, then do not retrieve the full site, just the id. """ global _WARNED if id_only: site = -1 else: site = None debug_domain = None debug_target = None if settings.DEBUG: raw = get_threadlocal_setting('DEBUG_DOMAIN') if raw: parts = raw.split('=') if len(parts) == 2: debug_domain = parts[0] debug_target = parts[1] else: debug_domain = raw debug_target = 'com' if not host: request = threadlocals.get_current_request() if request: host = request.get_host() else: log.debug('No request') site = by_settings(id_only=id_only) if host: if app_cache_ready(): try: site = keyedcache.cache_get('SITE', host=host, id_only=id_only) if id_only: site = site.id except keyedcache.NotCachedError, nce: try: log.debug('looking up site by host: %s', host) site = Site.objects.get(domain=host) except Site.DoesNotExist: if host.find(":") > -1: try: # strip the port host = host.split(":")[0] site = Site.objects.get(domain=host) except Site.DoesNotExist: pass if debug_domain and host.endswith(debug_domain): host = host[:-len(debug_domain)] + debug_target log.debug('Using debug domain: %s', host) try: site = Site.objects.get(domain=host) except Site.DoesNotExist: pass if not site and get_threadlocal_setting( 'AUTO_WWW') and not called_recursive: if host.startswith('www'): log.debug('trying site lookup without www') site = by_host(host=host[4:], id_only=id_only, called_recursive=True) else: log.debug('trying site lookup with www') site = by_host(host='www.%s' % host, id_only=id_only, called_recursive=True) if site: keyedcache.cache_set(nce.key, value=site) if id_only: site = site.id else: if not host in _WARNED: log.warn( "Site for '%s' is not configured on this server - add to sites in admin", host) _WARNED[host] = True site = by_settings(id_only=id_only) else: log.debug('app cache not ready') site = by_settings(id_only=id_only)
def by_host(host=None, id_only=False, called_recursive=None): """Get the current site by looking at the request stored in the thread. Returns the best match found in the `django.contrib.sites` app. If not found, then returns the default set as given in `settings.SITE_ID` Params: - `host`: optional, host to look up - `id_only`: if true, then do not retrieve the full site, just the id. """ global _WARNED if id_only: site = -1 else: site = None debug_domain = None debug_target = None if settings.DEBUG: raw = get_threadlocal_setting('DEBUG_DOMAIN') if raw: parts = raw.split('=') if len(parts) == 2: debug_domain = parts[0] debug_target = parts[1] else: debug_domain = raw debug_target = 'com' if not host: request = threadlocals.get_current_request() if request: host = request.get_host() else: log.debug('No request') site = by_settings(id_only=id_only) if host: if app_cache_ready(): try: site = keyedcache.cache_get('SITE', host=host, id_only=id_only) if id_only: site = site.id except keyedcache.NotCachedError, nce: try: log.debug('looking up site by host: %s', host) site = Site.objects.get(domain=host) except Site.DoesNotExist: if host.find(":") > -1: try: # strip the port host = host.split(":")[0] site = Site.objects.get(domain=host) except Site.DoesNotExist: pass if debug_domain and host.endswith(debug_domain): host = host[:-len(debug_domain)] + debug_target log.debug('Using debug domain: %s', host) try: site = Site.objects.get(domain=host) except Site.DoesNotExist: pass if not site and get_threadlocal_setting('AUTO_WWW') and not called_recursive: if host.startswith('www'): log.debug('trying site lookup without www') site = by_host(host=host[4:], id_only=id_only, called_recursive=True) else: log.debug('trying site lookup with www') site = by_host(host = 'www.%s' % host, id_only=id_only, called_recursive=True) if site: keyedcache.cache_set(nce.key, value=site) if id_only: site = site.id else: if not host in _WARNED: log.warn("Site for '%s' is not configured on this server - add to sites in admin", host) _WARNED[host] = True site = by_settings(id_only=id_only) else: log.debug('app cache not ready') site = by_settings(id_only=id_only)