Ejemplo n.º 1
0
class DescribeForm(AkismetSpamCheckFormMixin, AddonFormBase):
    name = TransField(max_length=50)
    slug = forms.CharField(max_length=30)
    summary = TransField(widget=TransTextarea(attrs={'rows': 4}),
                         max_length=250)
    description = TransField(widget=TransTextarea(attrs={'rows': 6}),
                             min_length=10)
    is_experimental = forms.BooleanField(required=False)
    requires_payment = forms.BooleanField(required=False)
    support_url = TransField.adapt(HttpHttpsOnlyURLField)(required=False)
    support_email = TransField.adapt(forms.EmailField)(required=False)

    fields_to_akismet_comment_check = ['name', 'summary', 'description']

    class Meta:
        model = Addon
        fields = ('name', 'slug', 'summary', 'description', 'is_experimental',
                  'support_url', 'support_email', 'requires_payment')

    def __init__(self, *args, **kw):
        super(DescribeForm, self).__init__(*args, **kw)
        content_waffle = waffle.switch_is_active('content-optimization')
        if not content_waffle or self.instance.type != amo.ADDON_EXTENSION:
            description = self.fields['description']
            description.min_length = None
            description.widget.attrs.pop('minlength', None)
            description.validators = [
                validator for validator in description.validators
                if not isinstance(validator, MinLengthValidator)
            ]
            description.required = False
Ejemplo n.º 2
0
class DescribeForm(AddonFormBasic):
    tags = None
    support_url = TransField.adapt(HttpHttpsOnlyURLField)(required=False)
    support_email = TransField.adapt(forms.EmailField)(required=False)

    class Meta:
        model = Addon
        fields = ('name', 'slug', 'summary', 'is_experimental', 'support_url',
                  'support_email')
Ejemplo n.º 3
0
class DescribeForm(AkismetSpamCheckFormMixin, AddonFormBase):
    name = TransField(max_length=50)
    slug = forms.CharField(max_length=30)
    summary = TransField(widget=TransTextarea(attrs={'rows': 4}),
                         max_length=250)
    description = TransField(widget=TransTextarea(attrs={'rows': 6}),
                             min_length=10)
    is_experimental = forms.BooleanField(required=False)
    requires_payment = forms.BooleanField(required=False)
    support_url = TransField.adapt(HttpHttpsOnlyURLField)(required=False)
    support_email = TransField.adapt(forms.EmailField)(required=False)
    has_priv = forms.BooleanField(required=False,
                                  label=_(u'This add-on has a Privacy Policy'),
                                  label_suffix='')
    privacy_policy = TransField(
        widget=TransTextarea(),
        required=False,
        label=_(u'Please specify your add-on\'s Privacy Policy:'))

    fields_to_akismet_comment_check = ['name', 'summary', 'description']

    class Meta:
        model = Addon
        fields = ('name', 'slug', 'summary', 'description', 'is_experimental',
                  'support_url', 'support_email', 'privacy_policy',
                  'requires_payment')

    def __init__(self, *args, **kw):
        kw['initial'] = {
            'has_priv': self._has_field('privacy_policy', kw['instance'])
        }
        super(DescribeForm, self).__init__(*args, **kw)
        content_waffle = waffle.switch_is_active('content-optimization')
        if not content_waffle or self.instance.type != amo.ADDON_EXTENSION:
            description = self.fields['description']
            description.min_length = None
            description.widget.attrs.pop('minlength', None)
            description.validators = [
                validator for validator in description.validators
                if not isinstance(validator, MinLengthValidator)
            ]
            description.required = False

    def _has_field(self, name, instance=None):
        # If there's a policy in any language, this addon has a policy.
        n = getattr(instance or self.instance, u'%s_id' % name)
        return any(map(bool, Translation.objects.filter(id=n)))

    def save(self, commit=True):
        obj = super(DescribeForm, self).save(commit)
        if not self.cleaned_data['has_priv']:
            delete_translation(self.instance, 'privacy_policy')

        return obj
Ejemplo n.º 4
0
class AddonFormSupport(AddonFormBase):
    support_url = TransField.adapt(HttpHttpsOnlyURLField)(required=False)
    support_email = TransField.adapt(forms.EmailField)(required=False)

    class Meta:
        model = Addon
        fields = ('support_email', 'support_url')

    def __init__(self, *args, **kw):
        super(AddonFormSupport, self).__init__(*args, **kw)

    def save(self, addon, commit=True):
        return super(AddonFormSupport, self).save(commit)
Ejemplo n.º 5
0
class AddonFormDetails(AddonFormBase):
    default_locale = forms.TypedChoiceField(choices=LOCALES)
    homepage = TransField.adapt(HttpHttpsOnlyURLField)(required=False)

    class Meta:
        model = Addon
        fields = ('description', 'default_locale', 'homepage')

    def clean(self):
        # Make sure we have the required translations in the new locale.
        required = 'name', 'summary', 'description'
        data = self.cleaned_data
        if not self.errors and 'default_locale' in self.changed_data:
            fields = dict((k, getattr(self.instance, k + '_id'))
                          for k in required)
            locale = self.cleaned_data['default_locale']
            ids = filter(None, fields.values())
            qs = (Translation.objects.filter(locale=locale, id__in=ids,
                                             localized_string__isnull=False)
                  .values_list('id', flat=True))
            missing = [k for k, v in fields.items() if v not in qs]
            # They might be setting description right now.
            if 'description' in missing and locale in data['description']:
                missing.remove('description')
            if missing:
                raise forms.ValidationError(ugettext(
                    'Before changing your default locale you must have a '
                    'name, summary, and description in that locale. '
                    'You are missing %s.') % ', '.join(map(repr, missing)))
        return data
Ejemplo n.º 6
0
class DescribeForm(AddonFormBase):
    name = TransField(max_length=50)
    slug = forms.CharField(max_length=30)
    summary = TransField(widget=TransTextarea(attrs={'rows': 4}),
                         max_length=250)
    is_experimental = forms.BooleanField(required=False)
    support_url = TransField.adapt(HttpHttpsOnlyURLField)(required=False)
    support_email = TransField.adapt(forms.EmailField)(required=False)
    has_priv = forms.BooleanField(
        required=False,
        label=_lazy(u"This add-on has a Privacy Policy"),
        label_suffix='')
    privacy_policy = TransField(
        widget=TransTextarea(),
        required=False,
        label=_lazy(u"Please specify your add-on's Privacy Policy:"))

    class Meta:
        model = Addon
        fields = ('name', 'slug', 'summary', 'is_experimental', 'support_url',
                  'support_email', 'privacy_policy')

    def __init__(self, *args, **kw):
        kw['initial'] = {
            'has_priv': self._has_field('privacy_policy', kw['instance'])
        }
        super(DescribeForm, self).__init__(*args, **kw)

    def clean_name(self):
        return clean_addon_name(self.cleaned_data['name'], self.instance)

    def _has_field(self, name, instance=None):
        # If there's a policy in any language, this addon has a policy.
        n = getattr(instance or self.instance, u'%s_id' % name)
        return any(map(bool, Translation.objects.filter(id=n)))

    def save(self, commit=True):
        obj = super(DescribeForm, self).save(commit)
        if not self.cleaned_data['has_priv']:
            delete_translation(self.instance, 'privacy_policy')

        return obj
Ejemplo n.º 7
0
class AddonFormDetailsUnlisted(AddonFormBase):
    homepage = TransField.adapt(HttpHttpsOnlyURLField)(required=False)

    class Meta:
        model = Addon
        fields = ('description', 'homepage')
Ejemplo n.º 8
0
class AdditionalDetailsForm(AddonFormBase):
    default_locale = forms.TypedChoiceField(choices=LOCALES)
    homepage = TransField.adapt(HttpHttpsOnlyURLField)(required=False)
    tags = forms.CharField(required=False)
    contributions = HttpHttpsOnlyURLField(required=False, max_length=255)

    class Meta:
        model = Addon
        fields = ('default_locale', 'homepage', 'tags', 'contributions')

    def __init__(self, *args, **kw):
        super(AdditionalDetailsForm, self).__init__(*args, **kw)

        if self.fields.get('tags'):
            self.fields['tags'].initial = ', '.join(
                self.get_tags(self.instance))

    def clean_contributions(self):
        if self.cleaned_data['contributions']:
            hostname = urlsplit(self.cleaned_data['contributions']).hostname
            if not hostname.endswith(amo.VALID_CONTRIBUTION_DOMAINS):
                raise forms.ValidationError(ugettext(
                    'URL domain must be one of [%s], or a subdomain.'
                ) % ', '.join(amo.VALID_CONTRIBUTION_DOMAINS))
        return self.cleaned_data['contributions']

    def clean(self):
        # Make sure we have the required translations in the new locale.
        required = 'name', 'summary', 'description'
        if not self.errors and 'default_locale' in self.changed_data:
            fields = dict((k, getattr(self.instance, k + '_id'))
                          for k in required)
            locale = self.cleaned_data['default_locale']
            ids = filter(None, fields.values())
            qs = (Translation.objects.filter(locale=locale, id__in=ids,
                                             localized_string__isnull=False)
                  .values_list('id', flat=True))
            missing = [k for k, v in fields.items() if v not in qs]
            if missing:
                raise forms.ValidationError(ugettext(
                    'Before changing your default locale you must have a '
                    'name, summary, and description in that locale. '
                    'You are missing %s.') % ', '.join(map(repr, missing)))
        return super(AdditionalDetailsForm, self).clean()

    def save(self, addon, commit=False):
        if self.fields.get('tags'):
            tags_new = self.cleaned_data['tags']
            tags_old = [slugify(t, spaces=True) for t in self.get_tags(addon)]

            # Add new tags.
            for t in set(tags_new) - set(tags_old):
                Tag(tag_text=t).save_tag(addon)

            # Remove old tags.
            for t in set(tags_old) - set(tags_new):
                Tag(tag_text=t).remove_tag(addon)

        # We ignore `commit`, since we need it to be `False` so we can save
        # the ManyToMany fields on our own.
        addonform = super(AdditionalDetailsForm, self).save(commit=False)
        addonform.save()

        return addonform