Пример #1
0
 def clean_recipients(self):
     """Check no filter prohibits the exchange."""
     recipients = self.cleaned_data['recipients']
     exchange_filter = getattr(self, 'exchange_filter', None)
     if exchange_filter:
         errors = []
         filtered_names = []
         recipients_list = recipients[:]
         for u in recipients_list:
             try:
                 reason = exchange_filter(self.instance.sender, u, recipients_list)
                 if reason is not None:
                     recipients.remove(u)
                     filtered_names.append(
                         self.error_messages[
                             'filtered_user_with_reason' if reason else 'filtered_user'
                         ].format(username=get_user_name(u), reason=reason)
                     )
             except forms.ValidationError as e:
                 recipients.remove(u)
                 errors.extend(e.messages)
         if filtered_names:
             errors.append(self.error_messages['filtered'].format(users=', '.join(filtered_names)))
         if errors:
             raise forms.ValidationError(errors)
     return recipients
Пример #2
0
 def clean_recipients(self):
     """Check no filter prohibits the exchange."""
     recipients = self.cleaned_data['recipients']
     exchange_filter = getattr(self, 'exchange_filter', None)
     if exchange_filter:
         errors = []
         filtered_names = []
         recipients_list = recipients[:]
         for u in recipients_list:
             try:
                 reason = exchange_filter(self.instance.sender, u,
                                          recipients_list)
                 if reason is not None:
                     recipients.remove(u)
                     filtered_names.append(
                         self.error_messages['filtered_user_with_reason' if
                                             reason else 'filtered_user'].
                         format(username=get_user_name(u), reason=reason))
             except forms.ValidationError as e:
                 recipients.remove(u)
                 errors.extend(e.messages)
         if filtered_names:
             errors.append(self.error_messages['filtered'].format(
                 users=', '.join(filtered_names)))
         if errors:
             raise forms.ValidationError(errors)
     return recipients
Пример #3
0
 def clean(self, value):
     """Check names are valid and filter them."""
     names = super(BasicCommaSeparatedUserField, self).clean(value)
     if not names:
         return []
     user_model = get_user_model()
     name_user_as = getattr(settings, 'POSTMAN_NAME_USER_AS',
                            user_model.USERNAME_FIELD)
     users = list(
         user_model.objects.filter(
             is_active=True, **{'{0}__in'.format(name_user_as): names}))
     unknown_names = set(names) ^ set([get_user_name(u) for u in users])
     errors = []
     if unknown_names:
         errors.append(self.error_messages['unknown'].format(
             users=', '.join(unknown_names)))
     if self.user_filter:
         filtered_names = []
         for u in users[:]:
             try:
                 reason = self.user_filter(u)
                 if reason is not None:
                     users.remove(u)
                     filtered_names.append(
                         self.error_messages['filtered_user_with_reason' if
                                             reason else 'filtered_user'].
                         format(username=get_user_name(u), reason=reason))
             except ValidationError as e:
                 users.remove(u)
                 errors.extend(e.messages)
         if filtered_names:
             errors.append(self.error_messages['filtered'].format(
                 users=', '.join(filtered_names)))
     if errors:
         raise ValidationError(errors)
     return users
Пример #4
0
    def clean(self):
        """Check that the recipient is correctly initialized and no filter prohibits the exchange."""
        if not self.recipient:
            raise forms.ValidationError(ugettext("Undefined recipient."))

        exchange_filter = getattr(self, 'exchange_filter', None)
        if exchange_filter and isinstance(self.recipient, get_user_model()):
            try:
                reason = exchange_filter(self.instance.sender, self.recipient, None)
                if reason is not None:
                    raise forms.ValidationError(self.error_messages['filtered'].format(
                        users=self.error_messages[
                            'filtered_user_with_reason' if reason else 'filtered_user'
                        ].format(username=get_user_name(self.recipient), reason=reason)
                    ))
            except forms.ValidationError as e:
                raise forms.ValidationError(e.messages)
        return super(BaseReplyForm, self).clean()
Пример #5
0
    def clean(self):
        """Check that the recipient is correctly initialized and no filter prohibits the exchange."""
        if not self.recipient:
            raise forms.ValidationError(ugettext("Undefined recipient."))

        exchange_filter = getattr(self, 'exchange_filter', None)
        if exchange_filter and isinstance(self.recipient, get_user_model()):
            try:
                reason = exchange_filter(self.instance.sender, self.recipient,
                                         None)
                if reason is not None:
                    raise forms.ValidationError(
                        self.error_messages['filtered'].format(
                            users=self.
                            error_messages['filtered_user_with_reason'
                                           if reason else 'filtered_user'].
                            format(username=get_user_name(self.recipient),
                                   reason=reason)))
            except forms.ValidationError as e:
                raise forms.ValidationError(e.messages)
        return super(BaseReplyForm, self).clean()