def plan_validation(user, plan=None, on_activation=False): """ Validates validator that represents quotas in a given system :param user: :param plan: :return: """ if plan is None: # if plan is not given, the default is to use current plan of the user plan = user.userplan.plan quota_dict = plan.get_quota_dict() validators = getattr(settings, 'PLANS_VALIDATORS', {}) validators = import_name(validators) errors = { 'required_to_activate': [], 'other': [], } for quota in validators: validator = import_name(validators[quota]) if on_activation: validator.on_activation(user, quota_dict) else: try: validator(user, quota_dict) except ValidationError as e: if validator.required_to_activate: errors['required_to_activate'].extend(e.messages) else: errors['other'].extend(e.messages) return errors
def recalculate(self, amount, billing_info): """ Calculates and return pre-filled Order """ order = Order(pk=-1) order.amount = amount order.currency = self.get_currency() country = getattr(billing_info, 'country', None) if country is None: country = get_country_code(self.request) else: country = country.code tax_number = getattr(billing_info, 'tax_number', None) # Calculating tax can be complex task (e.g. VIES webservice call) # To ensure that tax calculated on order preview will be the same on final order # tax rate is cached for a given billing data (as this value only depends on it) tax_session_key = "tax_%s_%s" % (tax_number, country) tax = self.request.session.get(tax_session_key) if tax is None: taxation_policy = getattr(settings, 'PLANS_TAXATION_POLICY', None) if not taxation_policy: raise ImproperlyConfigured('PLANS_TAXATION_POLICY is not set') taxation_policy = import_name(taxation_policy) tax = str(taxation_policy.get_tax_rate(tax_number, country)) # Because taxation policy could return None which clutters with saving this value # into cache, we use str() representation of this value self.request.session[tax_session_key] = tax order.tax = Decimal(tax) if tax != 'None' else None return order
def recalculate(self, amount, billing_info): """ Calculates and return pre-filled Order """ order = Order(pk=-1) order.amount = amount order.currency = self.get_currency() country = getattr(billing_info, 'country', None) if not country is None: country = country.code tax_number = getattr(billing_info, 'tax_number', None) # Calculating session can be complex task (e.g. VIES webservice call) # To ensure that once we get what tax we display to confirmation it will # not change, tax rate is cached for a given billing data (as it mainly depends on it) tax_session_key = "tax_%s_%s" % (tax_number, country) tax = self.request.session.get(tax_session_key) if tax: order.tax = tax[0] #retreiving tax as a tuple to avoid None problems else: taxation_policy = getattr(settings, 'TAXATION_POLICY' , None) if not taxation_policy: raise ImproperlyConfigured('TAXATION_POLICY is not set') taxation_policy = import_name(taxation_policy) order.tax = taxation_policy.get_tax_rate(tax_number, country) self.request.session[tax_session_key] = (order.tax, ) return order
def recalculate(self, amount, billing_info): """ Calculates and return pre-filled Order """ order = Order(pk=-1) order.amount = amount order.currency = self.get_currency() country = getattr(billing_info, "country", None) if not country is None: country = country.code tax_number = getattr(billing_info, "tax_number", None) # Calculating tax can be complex task (e.g. VIES webservice call) # To ensure that tax calculated on order preview will be the same on # final order tax rate is cached for a given billing data (as this # value only depends on it) tax_session_key = "tax_%s_%s" % (tax_number, country) tax = self.request.session.get(tax_session_key) if tax is None: tax = getattr(settings, "PLANS_TAX", None) if tax is None: taxation_policy = getattr(settings, "PLANS_TAXATION_POLICY", None) if not taxation_policy: raise ImproperlyConfigured("PLANS_TAXATION_POLICY is not set") taxation_policy = import_name(taxation_policy) tax = str(taxation_policy.get_tax_rate(tax_number, country)) # Because taxation policy could return None which clutters with saving this value # into cache, we use str() representation of this value self.request.session[tax_session_key] = tax order.tax = Decimal(tax) if tax != "None" else None return order
def recalculate(self, amount, billing_info): """ Calculates and return pre-filled Order """ order = Order(pk=-1) order.amount = amount order.currency = self.get_currency() country = getattr(billing_info, 'country', None) if not country is None: country = country.code tax_number = getattr(billing_info, 'tax_number', None) # Calculating session can be complex task (e.g. VIES webservice call) # To ensure that once we get what tax we display to confirmation it will # not change, tax rate is cached for a given billing data (as it mainly depends on it) tax_session_key = "tax_%s_%s" % (tax_number, country) tax = self.request.session.get(tax_session_key) if tax: order.tax = tax[ 0] # retreiving tax as a tuple to avoid None problems else: taxation_policy = getattr(settings, 'TAXATION_POLICY', None) if not taxation_policy: raise ImproperlyConfigured('TAXATION_POLICY is not set') taxation_policy = import_name(taxation_policy) order.tax = taxation_policy.get_tax_rate(tax_number, country) self.request.session[tax_session_key] = (order.tax, ) return order
def recalculate(self, amount, billing_info): """ Calculates and return pre-filled Order """ order = Order(pk=-1) order.amount = amount order.currency = self.get_currency() country = getattr(billing_info, 'country', None) if not country is None: country = country.code tax_number = getattr(billing_info, 'tax_number', None) # Calculating session can be complex task (e.g. VIES webservice call) # To ensure that once we get what tax we display to confirmation it will # not change, tax rate is cached for a given billing data (as it mainly depends on it) tax_session_key = "tax_%s_%s" % (tax_number, country) tax = self.request.session.get(tax_session_key) if tax is None: taxation_policy = getattr(settings, 'TAXATION_POLICY', None) if not taxation_policy: raise ImproperlyConfigured('TAXATION_POLICY is not set') taxation_policy = import_name(taxation_policy) tax = str(taxation_policy.get_tax_rate(tax_number, country)) # Because taxation policy could return None which clutters with saving this value # into cache, we use str() representation of this value self.request.session[tax_session_key] = tax order.tax = Decimal(tax) if tax != 'None' else None return order
def recalculate(self, amount, billing_info): """ Method calculates order details """ self.amount = amount country = getattr(billing_info, 'country', None) if not country is None: country = country.code tax_number = getattr(billing_info, 'tax_number', None) tax_session_key = "tax_%s_%s" % (tax_number, country) tax = self.request.session.get(tax_session_key) if tax: self.tax = tax[0] #retreiving tax as a tuple to avoid None problems else: taxation_policy = getattr(settings, 'TAXATION_POLICY' , None) if not taxation_policy: raise ImproperlyConfigured('TAXATION_POLICY is not set') taxation_policy = import_name(taxation_policy)() self.tax = taxation_policy.get_tax_rate(tax_number, country) self.request.session[tax_session_key] = (self.tax, ) self.tax_total = self.amount * (self.tax or 0) / 100 self.tax_total = self.tax_total.quantize(Decimal('1.00')) self.total = self.amount + self.tax_total
def test_import_name_imports_module_by_path(self): """ import_name should be able to import a module with its python path """ module_name = 'abc.ABCMeta' self.assertEqual(type(import_name(module_name)), type(TypeError))
def post(self, request, *args, **kwargs): plan = get_object_or_404(Plan, Q(pk=kwargs['pk']) & Q(available=True) & ( Q(customized = request.user) | Q(customized__isnull=True))) if request.user.userplan.plan != plan: policy = import_name(getattr(settings, 'PLAN_CHANGE_POLICY', 'plans.plan_change.StandardPlanChangePolicy'))() period = request.user.userplan.days_left() price = policy.get_change_price(request.user.userplan.plan, plan, period) if price is None: request.user.userplan.extend_account(plan, None) messages.success(request, _("Your plan has been successfully changed")) else: return HttpResponseForbidden() return HttpResponseRedirect(reverse('upgrade_plan'))
def account_full_validation(user): """ Validates validator that represents quotas in a given system :param user: :return: """ quotas = get_user_quota(user) validators = getattr(settings, 'PLAN_ACTIVATION_VALIDATORS', {}) errors = [] for quota in quotas: if quota in validators: validator = import_name(validators[quota]) try: validator(user) except ValidationError, e: errors.append(e)
def post(self, request, *args, **kwargs): plan = get_object_or_404(Plan, Q(pk=kwargs['pk']) & Q(available=True, visible=True) & ( Q(customized=request.user) | Q(customized__isnull=True))) if request.user.userplan.plan != plan: policy = import_name( getattr(settings, 'PLANS_CHANGE_POLICY', 'plans.plan_change.StandardPlanChangePolicy'))() period = request.user.userplan.days_left() price = policy.get_change_price(request.user.userplan.plan, plan, period) if price is None: request.user.userplan.extend_account(plan, None) messages.success(request, _("Your plan has been successfully changed")) else: return HttpResponseForbidden() return HttpResponseRedirect(reverse('upgrade_plan'))
def account_full_validation(user): """ Validates validator that represents quotas in a given system :param user: :return: """ quotas = get_user_quota(user) validators = getattr(settings, 'PLAN_ACTIVATION_VALIDATORS', {}) errors = [] for quota in quotas: if validators.has_key(quota): validator = import_name(validators[quota]) try: validator(user) except ValidationError, e: errors.append(e)
def validate_plan(user, plan=None, **kwargs): if plan is None: if user.is_authenticated(): # if plan is not given, the default is to use current plan of the user plan = user.userplan.plan else: plan = Plan.objects.get(default=True) quota_dict = plan.get_quota_dict() validators = getattr(settings, 'PLAN_VALIDATORS', {}) errors = [] for quota in quota_dict: if quota in validators: validator = import_name(validators[quota]) try: validator(user, quota_dict, **kwargs) except ValidationError as e: errors.extend(e.messages) if len(errors): raise ValidationError(errors) return True
def get_policy(self): policy_class = getattr(settings, 'PLANS_CHANGE_POLICY', 'plans.plan_change.StandardPlanChangePolicy') return import_name(policy_class)()
def get_policy(self): policy_class = getattr(settings, 'PLAN_CHANGE_POLICY', 'plans.plan_change.StandardPlanChangePolicy') return import_name(policy_class)()