Ejemplo n.º 1
0
def verify_mozilla_trademark(name, user, form=None):
    skip_trademark_check = (
        user and user.is_authenticated() and user.email and
        user.email.endswith(amo.ALLOWED_TRADEMARK_SUBMITTING_EMAILS))

    def _check(name):
        name = normalize_string(name, strip_puncutation=True).lower()

        for symbol in amo.MOZILLA_TRADEMARK_SYMBOLS:
            violates_trademark = (
                name.count(symbol) > 1 or (
                    name.count(symbol) >= 1 and not
                    name.endswith(' for {}'.format(symbol))))

            if violates_trademark:
                raise forms.ValidationError(ugettext(
                    u'Add-on names cannot contain the Mozilla or '
                    u'Firefox trademarks.'))

    if not skip_trademark_check:
        if not isinstance(name, dict):
            _check(name)
        else:
            for locale, localized_name in name.items():
                try:
                    _check(localized_name)
                except forms.ValidationError as exc:
                    if form is not None:
                        for message in exc.messages:
                            error_message = LocaleErrorMessage(
                                message=message, locale=locale)
                            form.add_error('name', error_message)
                    else:
                        raise
    return name
Ejemplo n.º 2
0
def verify_mozilla_trademark(name, user, form=None):
    skip_trademark_check = (user and user.is_authenticated
                            and action_allowed_user(
                                user, amo.permissions.TRADEMARK_BYPASS))

    def _check(name):
        name = normalize_string(name, strip_punctuation=True).lower()

        for symbol in amo.MOZILLA_TRADEMARK_SYMBOLS:
            violates_trademark = name.count(symbol) > 1 or (
                name.count(symbol) >= 1
                and not name.endswith(f' for {symbol}'))

            if violates_trademark:
                raise forms.ValidationError(
                    gettext('Add-on names cannot contain the Mozilla or '
                            'Firefox trademarks.'))

    if not skip_trademark_check:
        if not isinstance(name, dict):
            _check(name)
        else:
            for locale, localized_name in name.items():
                try:
                    _check(localized_name)
                except forms.ValidationError as exc:
                    if form is not None:
                        for message in exc.messages:
                            error_message = LocaleErrorMessage(message=message,
                                                               locale=locale)
                            form.add_error('name', error_message)
                    else:
                        raise
    return name
Ejemplo n.º 3
0
 def clean(self):
     message = _(u'Ensure name and summary combined are at most '
                 u'{limit_value} characters (they have {show_value}).')
     super(CombinedNameSummaryCleanMixin, self).clean()
     name_summary_locales = set(
         list(self.cleaned_data.get('name', {}).keys()) +
         self.cleaned_data.get('summary', {}).keys())
     default_locale = self.instance.default_locale.lower()
     name_values = self.cleaned_data.get('name') or {}
     name_default = name_values.get(default_locale) or ''
     summary_values = self.cleaned_data.get('summary') or {}
     summary_default = summary_values.get(default_locale) or ''
     for locale in name_summary_locales:
         val_len = len(
             name_values.get(locale, name_default) +
             summary_values.get(locale, summary_default))
         if val_len > self.MAX_LENGTH:
             if locale == default_locale:
                 # only error in default locale.
                 formatted_message = message.format(
                     limit_value=self.MAX_LENGTH, show_value=val_len)
                 self.add_error(
                     'name',
                     LocaleErrorMessage(message=formatted_message,
                                        locale=locale))
             elif self.should_auto_crop:
                 # otherwise we need to shorten the summary (and or name?)
                 if locale in name_values:
                     # if only default summary need to shorten name instead.
                     max_name_length = (self.fields['name'].max_length
                                        if locale in summary_values else
                                        self.MAX_LENGTH -
                                        len(summary_default))
                     name = name_values[locale][:max_name_length]
                     name_length = len(name)
                     self.cleaned_data.setdefault('name', {})[locale] = name
                 else:
                     name_length = len(name_default)
                 if locale in summary_values:
                     max_summary_length = self.MAX_LENGTH - name_length
                     self.cleaned_data.setdefault('summary', {})[locale] = (
                         summary_values[locale][:max_summary_length])
     return self.cleaned_data
Ejemplo n.º 4
0
 def clean(self):
     message = _('Ensure name and summary combined are at most 70 '
                 'characters (they have {0}).')
     super(CombinedNameSummaryCleanMixin, self).clean()
     name_summary_locales = set(
         self.cleaned_data.get('name', {}).keys() +
         self.cleaned_data.get('summary', {}).keys())
     default_locale = self.instance.default_locale.lower()
     name_values = self.cleaned_data.get('name') or {}
     name_default = name_values.get(default_locale) or ''
     summary_values = self.cleaned_data.get('summary') or {}
     summary_default = summary_values.get(default_locale) or ''
     for locale in name_summary_locales:
         val_len = len(
             name_values.get(locale, name_default) +
             summary_values.get(locale, summary_default))
         if val_len > 70:
             if locale == default_locale or not self.should_auto_crop:
                 # if we're not auto-cropping add an error.
                 self.add_error(
                     'name',
                     LocaleErrorMessage(message=message.format(val_len),
                                        locale=locale))
             else:
                 # otherwise we need to shorten the summary (and or name?)
                 if locale in name_values:
                     # if only default summary need to shorten name instead.
                     max_name_length = (68 if locale in summary_values else
                                        70 - len(summary_default))
                     name = name_values[locale][:max_name_length]
                     name_length = len(name)
                     self.cleaned_data['name'][locale] = name
                 else:
                     name_length = len(name_default)
                 if locale in summary_values:
                     max_summary_length = 70 - name_length
                     self.cleaned_data['summary'][locale] = (
                         summary_values[locale][:max_summary_length])
     return self.cleaned_data