class UpsellForm(happyforms.Form): upsell_of = AddonChoiceField(queryset=Addon.objects.none(), required=False, label=_lazy(u'This is a paid upgrade of'), empty_label=_lazy(u'Not an upgrade')) def __init__(self, *args, **kw): self.addon = kw.pop('addon') self.user = kw.pop('user') kw.setdefault('initial', {}) if self.addon.upsold: kw['initial']['upsell_of'] = self.addon.upsold.free super(UpsellForm, self).__init__(*args, **kw) self.fields['upsell_of'].queryset = (self.user.addons.exclude( pk=self.addon.pk, status=amo.STATUS_DELETED).filter(premium_type__in=amo.ADDON_FREES, type=self.addon.type)) def save(self): current_upsell = self.addon.upsold new_upsell_app = self.cleaned_data.get('upsell_of') if new_upsell_app: # We're changing the upsell or creating a new one. if not current_upsell: # If the upsell is new or we just deleted the old upsell, # create a new upsell. log.debug('[1@%s] Creating app upsell' % self.addon.pk) current_upsell = AddonUpsell(premium=self.addon) # Set the upsell object to point to the app that we're upselling. current_upsell.free = new_upsell_app current_upsell.save() elif current_upsell: # We're deleting the upsell. log.debug('[1@%s] Deleting the app upsell' % self.addon.pk) current_upsell.delete()
class TransactionFilterForm(happyforms.Form): app = AddonChoiceField(queryset=None, required=False, label=_lazy(u'App')) transaction_type = forms.ChoiceField( required=False, label=_lazy(u'Transaction Type'), choices=[(None, '')] + amo.MKT_TRANSACTION_CONTRIB_TYPES.items()) transaction_id = forms.CharField( required=False, label=_lazy(u'Transaction ID')) current_year = datetime.today().year years = [current_year - x for x in range(current_year - 2012)] date_from = forms.DateTimeField( required=False, widget=SelectDateWidget(years=years), label=_lazy(u'From')) date_to = forms.DateTimeField( required=False, widget=SelectDateWidget(years=years), label=_lazy(u'To')) def __init__(self, *args, **kwargs): self.apps = kwargs.pop('apps', []) super(TransactionFilterForm, self).__init__(*args, **kwargs) self.fields['app'].queryset = self.apps
class UpsellForm(happyforms.Form): price = forms.ModelChoiceField(queryset=Price.objects.active(), label=_lazy(u'App Price'), empty_label=None, required=True) make_public = forms.TypedChoiceField(choices=APP_PUBLIC_CHOICES, widget=forms.RadioSelect(), label=_lazy( u'When should your app be ' 'made available for sale?'), coerce=int, required=False) free = AddonChoiceField( queryset=Addon.objects.none(), required=False, empty_label='', # L10n: "App" is a paid version of this app. "from" is this app. label=_lazy(u'App to upgrade from'), widget=forms.Select()) def __init__(self, *args, **kw): self.extra = kw.pop('extra') self.request = kw.pop('request') self.addon = self.extra['addon'] if 'initial' not in kw: kw['initial'] = {} kw['initial']['make_public'] = amo.PUBLIC_IMMEDIATELY if self.addon.premium: kw['initial']['price'] = self.addon.premium.price super(UpsellForm, self).__init__(*args, **kw) self.fields['free'].queryset = (self.extra['amo_user'].addons.exclude( pk=self.addon.pk).filter(premium_type__in=amo.ADDON_FREES, status__in=amo.VALID_STATUSES, type=self.addon.type)) if len(self.fields['price'].choices) > 1: # Tier 0 (Free) should not be the default selection. self.initial['price'] = (Price.objects.active().exclude( price='0.00')[0]) def clean_make_public(self): return (amo.PUBLIC_WAIT if self.cleaned_data.get('make_public') else None) def save(self): if 'price' in self.cleaned_data: premium = self.addon.premium if not premium: premium = AddonPremium() premium.addon = self.addon premium.price = self.cleaned_data['price'] premium.save() upsell = self.addon.upsold if self.cleaned_data['free']: # Check if this app was already a premium version for another app. if upsell and upsell.free != self.cleaned_data['free']: upsell.delete() if not upsell: upsell = AddonUpsell(premium=self.addon) upsell.free = self.cleaned_data['free'] upsell.save() elif upsell: upsell.delete() self.addon.update(make_public=self.cleaned_data['make_public'])
class PremiumForm(happyforms.Form): """ The premium details for an addon, which is unfortunately distributed across a few models. """ premium_type = forms.TypedChoiceField(label=_lazy(u'Premium Type'), coerce=lambda x: int(x), choices=amo.ADDON_PREMIUM_TYPES.items(), widget=forms.RadioSelect()) price = forms.ModelChoiceField(queryset=Price.objects.active(), label=_('Add-on Price'), empty_label=None, required=False) do_upsell = forms.TypedChoiceField(coerce=lambda x: bool(int(x)), choices=APP_UPSELL_CHOICES, widget=forms.RadioSelect(), required=False) free = AddonChoiceField(queryset=Addon.objects.none(), required=False, empty_label='') text = forms.CharField(widget=forms.Textarea(), required=False) def __init__(self, *args, **kw): self.extra = kw.pop('extra') self.request = kw.pop('request') self.addon = self.extra['addon'] kw['initial'] = { 'premium_type': self.addon.premium_type, 'do_upsell': 0, } if self.addon.premium: kw['initial']['price'] = self.addon.premium.price upsell = self.addon.upsold if upsell: kw['initial'].update({ 'text': upsell.text, 'free': upsell.free, 'do_upsell': 1, }) super(PremiumForm, self).__init__(*args, **kw) if self.addon.is_webapp(): self.fields['price'].label = _(u'App Price') self.fields['do_upsell'].choices = APP_UPSELL_CHOICES self.fields['free'].queryset = (self.extra['amo_user'].addons .exclude(pk=self.addon.pk) .filter(premium_type__in=amo.ADDON_FREES, status__in=amo.VALID_STATUSES, type=self.addon.type)) if (not self.initial.get('price') and len(list(self.fields['price'].choices)) > 1): # Tier 0 (Free) should not be the default selection. self.initial['price'] = (Price.objects.active() .exclude(price='0.00')[0]) # For the wizard, we need to remove some fields. for field in self.extra.get('exclude', []): del self.fields[field] def clean_price(self): if (self.cleaned_data.get('premium_type') in amo.ADDON_PREMIUMS and not self.cleaned_data['price']): raise_required() return self.cleaned_data['price'] def clean_text(self): if (self.cleaned_data['do_upsell'] and not self.cleaned_data['text']): raise_required() return self.cleaned_data['text'] def clean_free(self): if (self.cleaned_data['do_upsell'] and not self.cleaned_data['free']): raise_required() return self.cleaned_data['free'] def save(self): if 'price' in self.cleaned_data: premium = self.addon.premium if not premium: premium = AddonPremium() premium.addon = self.addon premium.price = self.cleaned_data['price'] premium.save() upsell = self.addon.upsold if (self.cleaned_data['do_upsell'] and self.cleaned_data['text'] and self.cleaned_data['free']): # Check if this app was already a premium version for another app. if upsell and upsell.free != self.cleaned_data['free']: upsell.delete() if not upsell: upsell = AddonUpsell(premium=self.addon) upsell.text = self.cleaned_data['text'] upsell.free = self.cleaned_data['free'] upsell.save() elif not self.cleaned_data['do_upsell'] and upsell: upsell.delete() self.addon.premium_type = self.cleaned_data['premium_type'] self.addon.save() # If they checked later in the wizard and then decided they want # to keep it free, push to pending. if (not self.addon.needs_paypal() and self.addon.is_incomplete()): self.addon.mark_done()
class PremiumForm(happyforms.Form): """ The premium details for an addon, which is unfortunately distributed across a few models. """ premium_type = forms.TypedChoiceField( label=_lazy(u'Premium Type'), coerce=lambda x: int(x), choices=amo.ADDON_PREMIUM_TYPES.items(), widget=forms.RadioSelect()) price = forms.ModelChoiceField(queryset=Price.objects.active(), label=_lazy(u'App Price'), empty_label=None, required=False) free = AddonChoiceField(queryset=Addon.objects.none(), required=False, label=_lazy(u'This is a paid upgrade of'), empty_label=_lazy(u'Not an upgrade')) currencies = forms.MultipleChoiceField( widget=forms.CheckboxSelectMultiple, required=False, label=_lazy(u'Supported Non-USD Currencies')) def __init__(self, *args, **kw): self.extra = kw.pop('extra') self.request = kw.pop('request') self.addon = self.extra['addon'] kw['initial'] = { 'premium_type': self.addon.premium_type, } if self.addon.premium: kw['initial']['price'] = self.addon.premium.price upsell = self.addon.upsold if upsell: kw['initial']['free'] = upsell.free if self.addon.premium and waffle.switch_is_active('currencies'): kw['initial']['currencies'] = self.addon.premium.currencies super(PremiumForm, self).__init__(*args, **kw) if waffle.switch_is_active('currencies'): choices = (PriceCurrency.objects.values_list('currency', flat=True).distinct()) self.fields['currencies'].choices = [(k, k) for k in choices if k] if self.addon.is_webapp(): self.fields['premium_type'].label = _lazy(u'Will your app ' 'use payments?') self.fields['price'].label = _(u'App Price') self.fields['free'].queryset = (self.extra['amo_user'].addons.exclude( pk=self.addon.pk).filter(premium_type__in=amo.ADDON_FREES, status__in=amo.VALID_STATUSES, type=self.addon.type)) if (not self.initial.get('price') and len(list(self.fields['price'].choices)) > 1): # Tier 0 (Free) should not be the default selection. self.initial['price'] = (Price.objects.active().exclude( price='0.00')[0]) # For the wizard, we need to remove some fields. for field in self.extra.get('exclude', []): del self.fields[field] def clean_price(self): if (self.cleaned_data.get('premium_type') in amo.ADDON_PREMIUMS and not self.cleaned_data['price']): raise_required() return self.cleaned_data['price'] def clean_free(self): return self.cleaned_data['free'] def save(self): if 'price' in self.cleaned_data: premium = self.addon.premium if not premium: premium = AddonPremium() premium.addon = self.addon premium.price = self.cleaned_data['price'] premium.save() upsell = self.addon.upsold if self.cleaned_data['free']: # Check if this app was already a premium version for another app. if upsell and upsell.free != self.cleaned_data['free']: upsell.delete() if not upsell: upsell = AddonUpsell(premium=self.addon) upsell.free = self.cleaned_data['free'] upsell.save() elif not self.cleaned_data['free'] and upsell: upsell.delete() # Check for free -> paid for already public apps. premium_type = self.cleaned_data['premium_type'] if (self.addon.premium_type == amo.ADDON_FREE and premium_type in amo.ADDON_PREMIUMS and self.addon.status == amo.STATUS_PUBLIC): # Free -> paid for public apps trigger re-review. log.info(u'[Webapp:%s] (Re-review) Public app, free -> paid.' % (self.addon)) RereviewQueue.flag(self.addon, amo.LOG.REREVIEW_FREE_TO_PAID) self.addon.premium_type = premium_type if self.addon.premium and waffle.switch_is_active('currencies'): currencies = self.cleaned_data['currencies'] self.addon.premium.update(currencies=currencies) self.addon.save() # If they checked later in the wizard and then decided they want # to keep it free, push to pending. if not self.addon.needs_paypal() and self.addon.is_incomplete(): self.addon.mark_done()