def __init__(self, *args, **kwargs): currency = kwargs.pop('currency', getattr(settings, 'DEFAULT_CURRENCY', 'GBP') ) self.challenge_words = kwargs.pop('challenge_words', None) super(SignupForm, self).__init__(*args, **kwargs) if Plan.objects.count() == 1: plan = Plan.objects.get(pk = settings.SAAS_DEFAULT_PLAN) del self.fields['plan'] else: plan = Plan.objects.get( pk = self.initial.get('plan') or settings.SAAS_DEFAULT_PLAN ) tax_rate = TaxRate.objects.get( chargeable_percent = settings.PAYMENTS_DEFAULT_TAXRATE ) symbol = get_currency_symbol(currency) price = plan.prices.get(currency = currency) price_monthly = format_price(symbol, price.monthly) price_yearly = format_price(symbol, price.yearly) self.fields['period'].choices = ( (1, _('Monthly (%s + %s)' % (price_monthly, tax_rate.shorthand))), (12, _('Annually (%s + %s)' % (price_yearly, tax_rate.shorthand))) ) if getattr(settings, 'SAAS_NEWSLETTER_OPTIN', False): self.fields['newsletter_optin'] = forms.BooleanField( label = getattr(settings, 'SAAS_NEWSLETTER_OPTIN_PROMPT', u'Subscribe to our newsletter' ), required = False, initial = True ) if getattr(settings, 'SAAS_TERMS_URL', None): self.fields['terms'] = forms.BooleanField( label = mark_safe( u'I agree to be bound by the <a href="%s">terms and conditions</a>' % settings.SAAS_TERMS_URL ) ) if getattr(settings, 'SAAS_SIGNUP_CHALLENGE'): if self.challenge_words is None: right_words = settings.SAAS_SIGNUP_CHALLENGE.get('CORRECT_WORDS', []) wrong_words = settings.SAAS_SIGNUP_CHALLENGE.get('INCORRECT_WORDS', []) word_count = min(4, len(wrong_words)) if word_count > 0: self.challenge_words = [random.choice(right_words)] + random.sample(wrong_words, word_count) random.shuffle(self.challenge_words) self.fields['challenge'] = forms.ChoiceField( label = settings.SAAS_SIGNUP_CHALLENGE.get('LABEL', u'Pick the correct word'), choices = [(w, w) for w in self.challenge_words], )
def _get_plan_prices(current_plan = None): currency = getattr(settings, 'DEFAULT_CURRENCY', 'GBP') symbol = get_currency_symbol(currency) tax_rate = TaxRate.objects.get( chargeable_percent = settings.PAYMENTS_DEFAULT_TAXRATE ) plans = Plan.objects.all() if current_plan: plans = plans.exclude( pk = current_plan.pk ).filter( order__gte = current_plan.order ) prices = {} for p in plans: try: price = p.prices.get(currency = currency) price_monthly = format_price(symbol, price.monthly) price_yearly = format_price(symbol, price.yearly) except Price.DoesNotExist: price_monthly = format_price(symbol, 0) price_yearly = format_price(symbol, 0) prices[p.pk] = ( (1, _(u'Monthly (%s + %s)' % (price_monthly, tax_rate.shorthand)), float(price.monthly)), (12, _(u'Annually (%s + %s)' % (price_yearly, tax_rate.shorthand)), float(price.yearly)) ) return prices
def _get_plan_prices(current_plan = None): currency = getattr(settings, 'DEFAULT_CURRENCY', 'GBP') symbol = get_currency_symbol(currency) tax_rate = TaxRate.objects.get( chargeable_percent = settings.PAYMENTS_DEFAULT_TAXRATE ) plans = Plan.objects.all() if current_plan: plans = plans.exclude( pk = current_plan.pk ).filter( order__gte = current_plan.order ) prices = {} for p in plans: try: price = p.prices.get(currency = currency) price_monthly = format_price(symbol, price.monthly) price_yearly = format_price(symbol, price.yearly) except Price.DoesNotExist: price_monthly = format_price(symbol, 0) price_yearly = format_price(symbol, 0) prices[p.pk] = ( (1, _(u'Monthly (%s + %s)' % (price_monthly, tax_rate.shorthand)), price.monthly), (12, _(u'Annually (%s + %s)' % (price_yearly, tax_rate.shorthand)), price.yearly) ) return prices
def matrix(self, currency=None): headings = [] features = SortedDict() currency = currency or getattr(settings, 'CURRENCY_CODE', 'GBP') for feature in Feature.objects.select_related().extra( select = { 'values': 'SELECT CONCAT(\'{\', GROUP_CONCAT(CONCAT(\'"\', `plan_id`, \'":"\', `value`, \'"\')), \'}\') FROM `saas_planfeature` WHERE ' \ '`saas_planfeature`.`feature_id` = `saas_feature`.`id`' } ): features[feature.slug] = (feature.name, feature.is_boolean, feature.description, feature.values) rows = [{ 'heading': n, 'columns': c, 'slug': k, 'boolean': b, 'description': d or u'' } for (k, (n, b, d, c)) in features.items()] symbol = helpers.get_currency_symbol(currency) plans = self.with_prices(currency) for plan in plans: h = {'name': plan.name, 'pk': plan.pk} if plan.best_value: h['best'] = True if plan.price_monthly or plan.price_yearly: h.update({ 'price_monthly': helpers.format_price(symbol, plan.price_monthly) or None, 'price_yearly': helpers.format_price(symbol, plan.price_yearly) or None }) headings.append(h) for row in rows: columns = [] column_dict = row['columns'] if column_dict: column_dict = simplejson.loads(row['columns']) else: column_dict = {} for plan in plans: column_item = column_dict.get(unicode(plan.pk), -1) columns.append({ 'value': column_item, 'best': plan.best_value }) row['columns'] = columns return {'headings': headings, 'rows': rows}
def __init__(self, *args, **kwargs): currency = kwargs.pop('currency', getattr(settings, 'DEFAULT_CURRENCY', 'GBP') ) super(SignupForm, self).__init__(*args, **kwargs) if Plan.objects.count() == 1: plan = Plan.objects.get(pk = settings.SAAS_DEFAULT_PLAN) del self.fields['plan'] else: plan = Plan.objects.get( pk = self.initial.get('plan') or settings.SAAS_DEFAULT_PLAN ) tax_rate = TaxRate.objects.get( chargeable_percent = settings.PAYMENTS_DEFAULT_TAXRATE ) symbol = get_currency_symbol(currency) price = plan.prices.get(currency = currency) price_monthly = format_price(symbol, price.monthly) price_yearly = format_price(symbol, price.yearly) self.fields['period'].choices = ( (1, _('Monthly (%s + %s)' % (price_monthly, tax_rate.shorthand))), (12, _('Annually (%s + %s)' % (price_yearly, tax_rate.shorthand))) ) self.free = price.monthly == 0 and price.yearly == 0
def __init__(self, *args, **kwargs): currency = kwargs.pop('currency', getattr(settings, 'DEFAULT_CURRENCY', 'GBP')) self.current_plan = kwargs.pop('current_plan') super(PlanChangeForm, self).__init__(*args, **kwargs) try: payment = self.current_plan.get_payment() del self.fields['payment_gateway'] del self.fields['period'] except Payment.DoesNotExist: pass self.fields['plan'].queryset = self.fields['plan'].queryset.exclude( pk=self.current_plan.plan.pk).filter( order__gte=self.current_plan.plan.order) plan = self.fields['plan'].queryset[0] tax_rate = TaxRate.objects.get( chargeable_percent=settings.PAYMENTS_DEFAULT_TAXRATE) symbol = get_currency_symbol(currency) price = plan.prices.get(currency=currency) price_monthly = format_price(symbol, price.monthly) price_yearly = format_price(symbol, price.yearly) if 'period' in self.fields: self.fields['period'].choices = ( (1, _('Monthly (%s + %s)' % (price_monthly, tax_rate.shorthand))), (12, _('Annually (%s + %s)' % (price_yearly, tax_rate.shorthand))))
def __init__(self, *args, **kwargs): currency = kwargs.pop('currency', getattr(settings, 'DEFAULT_CURRENCY', 'GBP')) self.challenge_words = kwargs.pop('challenge_words', None) super(SignupForm, self).__init__(*args, **kwargs) if Plan.objects.count() == 1: plan = Plan.objects.get(pk=settings.SAAS_DEFAULT_PLAN) del self.fields['plan'] else: plan = Plan.objects.get( pk=self.initial.get('plan') or settings.SAAS_DEFAULT_PLAN) tax_rate = TaxRate.objects.get( chargeable_percent=settings.PAYMENTS_DEFAULT_TAXRATE) symbol = get_currency_symbol(currency) price = plan.prices.get(currency=currency) price_monthly = format_price(symbol, price.monthly) price_yearly = format_price(symbol, price.yearly) self.fields['period'].choices = ( (1, _('Monthly (%s + %s)' % (price_monthly, tax_rate.shorthand))), (12, _('Annually (%s + %s)' % (price_yearly, tax_rate.shorthand)))) if getattr(settings, 'SAAS_NEWSLETTER_OPTIN', False): self.fields['newsletter_optin'] = forms.BooleanField( label=getattr(settings, 'SAAS_NEWSLETTER_OPTIN_PROMPT', u'Subscribe to our newsletter'), required=False, initial=True) if getattr(settings, 'SAAS_TERMS_URL', None): self.fields['terms'] = forms.BooleanField(label=mark_safe( u'I agree to be bound by the <a href="%s">terms and conditions</a>' % settings.SAAS_TERMS_URL)) if getattr(settings, 'SAAS_SIGNUP_CHALLENGE'): if self.challenge_words is None: right_words = settings.SAAS_SIGNUP_CHALLENGE.get( 'CORRECT_WORDS', []) wrong_words = settings.SAAS_SIGNUP_CHALLENGE.get( 'INCORRECT_WORDS', []) word_count = min(4, len(wrong_words)) if word_count > 0: self.challenge_words = [random.choice(right_words) ] + random.sample( wrong_words, word_count) random.shuffle(self.challenge_words) self.fields['challenge'] = forms.ChoiceField( label=settings.SAAS_SIGNUP_CHALLENGE.get( 'LABEL', u'Pick the correct word'), choices=[(w, w) for w in self.challenge_words], )
def __init__(self, *args, **kwargs): currency = kwargs.pop('currency', getattr(settings, 'DEFAULT_CURRENCY', 'GBP') ) self.current_plan = kwargs.pop('current_plan') super(PlanChangeForm, self).__init__(*args, **kwargs) try: payment = self.current_plan.get_payment() del self.fields['payment_gateway'] del self.fields['period'] except Payment.DoesNotExist: pass self.fields['plan'].queryset = self.fields['plan'].queryset.exclude( pk = self.current_plan.plan.pk ).filter( order__gte = self.current_plan.plan.order ) plan = self.fields['plan'].queryset[0] tax_rate = TaxRate.objects.get( chargeable_percent = settings.PAYMENTS_DEFAULT_TAXRATE ) symbol = get_currency_symbol(currency) price = plan.prices.get(currency = currency) price_monthly = format_price(symbol, price.monthly) price_yearly = format_price(symbol, price.yearly) if 'period' in self.fields: self.fields['period'].choices = ( (1, _('Monthly (%s + %s)' % (price_monthly, tax_rate.shorthand))), (12, _('Annually (%s + %s)' % (price_yearly, tax_rate.shorthand))) )
def signup(request): if request.user.is_authenticated(): messages.warning(request, u'You already have an account.') return HttpResponseRedirect( getattr(settings, 'LOGIN_REDIRECT_URL', '/') ) plan = get_object_or_404(Plan, pk = request.GET.get('plan') or settings.SAAS_DEFAULT_PLAN ) if request.GET.get('discount'): code = fix_discount_code(request.GET.get('discount', '')) if not code and request.method == 'GET': messages.warning(request, '%s is not a valid discount code' % request.GET['discount']) else: code = '' form = SignupForm( data = request.POST or None, initial = { 'plan': plan.pk, 'discount_code': code } ) if request.method == 'POST' and form.is_valid(): user = form.save() login(request, user) request.session['PAYMENT_GATEWAY'] = form.cleaned_data['payment_gateway'] return HttpResponseRedirect( reverse('signup_pay') ) currency = getattr(settings, 'DEFAULT_CURRENCY', 'GBP') symbol = get_currency_symbol(currency) tax_rate = TaxRate.objects.get( chargeable_percent = settings.PAYMENTS_DEFAULT_TAXRATE ) prices = {} for p in Plan.objects.all(): try: price = p.prices.get(currency = currency) price_monthly = format_price(symbol, price.monthly) price_yearly = format_price(symbol, price.yearly) except Price.DoesNotExist: price_monthly = format_price(symbol, 0) price_yearly = format_price(symbol, 0) prices[p.pk] = ( (1, _(u'Monthly (%s + %s)' % (price_monthly, tax_rate.shorthand))), (12, _(u'Annually (%s + %s)' % (price_yearly, tax_rate.shorthand))) ) return TemplateResponse( request, 'saas/signup.html', { 'form': form, 'selected_plan': plan, 'next': request.GET.get('next'), 'matrix': Plan.objects.matrix(), 'plan_prices': simplejson.dumps(prices), 'discount': code } )
def matrix(self, currency = None): headings = [] features = SortedDict() currency = currency or getattr(settings, 'CURRENCY_CODE', 'GBP') for feature in Feature.objects.all(): features[feature.slug] = ( feature.name, feature.is_boolean, feature.description ) rows = [ { 'heading': n, 'columns': [], 'slug': k, 'boolean': b, 'description': d or '' } for (k, (n, b, d)) in features.items() ] symbol = helpers.get_currency_symbol(currency) for plan in self.all(): try: price = plan.prices.get(currency = currency) except Price.DoesNotExist: try: price = plan.prices.get( currency = getattr(settings, 'CURRENCY_CODE', 'GBP') ) except Price.DoesNotExist: price = None h = { 'name': plan.name, 'pk': plan.pk } if plan.best_value: h['best'] = True if price: h.update( { 'price_monthly': price and helpers.format_price( symbol, price.monthly ) or None, 'price_yearly': price and helpers.format_price( symbol, price.yearly ) or None } ) headings.append(h) for row in rows: try: feature = plan.features.get(feature__slug = row['slug']) row['columns'].append( { 'best': plan.best_value, 'value': feature.value } ) except: row['columns'].append( { 'best': plan.best_value, 'value': 0 } ) return { 'headings': headings, 'rows': rows }
def get_currency_symbol(self): return helpers.get_currency_symbol(self.currency)
def matrix(self, currency = None): headings = [] features = SortedDict() currency = currency or getattr(settings, 'CURRENCY_CODE', 'GBP') for feature in Feature.objects.select_related().extra( select = { 'values': 'SELECT CONCAT(\'{\', GROUP_CONCAT(CONCAT(\'"\', `plan_id`, \'":"\', `value`, \'"\')), \'}\') FROM `saas_planfeature` WHERE ' \ '`saas_planfeature`.`feature_id` = `saas_feature`.`id`' } ): features[feature.slug] = ( feature.name, feature.is_boolean, feature.description, feature.values ) rows = [ { 'heading': n, 'columns': c, 'slug': k, 'boolean': b, 'description': d or u'' } for (k, (n, b, d, c)) in features.items() ] symbol = helpers.get_currency_symbol(currency) plans = self.with_prices(currency) for plan in plans: h = { 'name': plan.name, 'pk': plan.pk } if plan.best_value: h['best'] = True if plan.price_monthly or plan.price_yearly: h.update( { 'price_monthly': helpers.format_price( symbol, plan.price_monthly ) or None, 'price_yearly': helpers.format_price( symbol, plan.price_yearly ) or None } ) headings.append(h) for row in rows: columns = [] column_dict = row['columns'] if column_dict: column_dict = simplejson.loads(row['columns']) else: column_dict = {} for plan in plans: column_item = column_dict.get(unicode(plan.pk), -1) columns.append( { 'value': column_item, 'best': plan.best_value } ) row['columns'] = columns return { 'headings': headings, 'rows': rows }