예제 #1
0
파일: fields.py 프로젝트: su-danny/famdates
    def clean(self, value):
        super(CommaSeparatedUserField, self).clean(value)
        if not value:
            return ''
        if isinstance(value, (list, tuple)):
            return value

        names = set(value.split(','))
        names_set = set([name.strip() for name in names if name.strip()])
        users = list(User.objects.filter(**{'%s__in' % get_username_field(): names_set}))
        unknown_names = names_set ^ set([getattr(user, get_username_field()) for user in users])

        recipient_filter = self._recipient_filter
        invalid_users = []
        if recipient_filter is not None:
            for r in users:
                if recipient_filter(r) is False:
                    users.remove(r)
                    invalid_users.append(getattr(r, get_username_field()))

        if unknown_names or invalid_users:
            raise forms.ValidationError(_(u"The following usernames are incorrect: %(users)s") % {
            'users': ', '.join(list(unknown_names) + invalid_users)})

        return users
예제 #2
0
    def clean(self, value):
        super(CommaSeparatedUserField, self).clean(value)
        if not value:
            return ''
        if isinstance(value, (list, tuple)):
            return value

        names = set(value.split(','))
        names_set = set([name.strip() for name in names if name.strip()])
        users = list(
            User.objects.filter(
                **{'%s__in' % get_username_field(): names_set}))
        unknown_names = names_set ^ set(
            [getattr(user, get_username_field()) for user in users])

        recipient_filter = self._recipient_filter
        invalid_users = []
        if recipient_filter is not None:
            for r in users:
                if recipient_filter(r) is False:
                    users.remove(r)
                    invalid_users.append(getattr(r, get_username_field()))

        if unknown_names or invalid_users:
            raise forms.ValidationError(
                _(u"The following usernames are incorrect: %(users)s") %
                {'users': ', '.join(list(unknown_names) + invalid_users)})

        return users
예제 #3
0
 def prepare_value(self, value):
     if value is None:
         value = ''
     elif isinstance(value, (list, tuple)):
         value = (', '.join(
             [getattr(user, get_username_field()) for user in value]))
     return value
예제 #4
0
 def render(self, name, value, attrs=None):
     if value is None:
         value = ''
     elif isinstance(value, (list, tuple)):
         value = (', '.join(
             [getattr(user, get_username_field()) for user in value]))
     return super(CommaSeparatedUserInput, self).render(name, value, attrs)
예제 #5
0
def compose(request,
            recipient=None,
            form_class=ComposeForm,
            template_name='messages/compose.html',
            success_url=None,
            recipient_filter=None):
    """
    Displays and handles the ``form_class`` form to compose new messages.
    Required Arguments: None
    Optional Arguments:
        ``recipient``: username of a `django.contrib.auth` User, who should
                       receive the message, optionally multiple usernames
                       could be separated by a '+'
        ``form_class``: the form-class to use
        ``template_name``: the template to use
        ``success_url``: where to redirect after successfull submission
    """
    if request.method == "POST":
        sender = request.user
        form = form_class(request.POST, recipient_filter=recipient_filter)
        if form.is_valid():
            form.save(sender=request.user)
            messages.info(request, _(u"Message successfully sent."))
            if success_url is None:
                success_url = reverse('messages_inbox')
            if request.GET.has_key('next'):
                success_url = request.GET['next']
            return HttpResponseRedirect(success_url)
    else:
        form = form_class()
        if recipient is not None:
            recipients = [
                u for u in User.objects.filter(
                    **{
                        '%s__in' % get_username_field():
                        [r.strip() for r in recipient.split('+')]
                    })
            ]
            form.fields['recipient'].initial = recipients
    return render_to_response(template_name, {
        'form': form,
    },
                              context_instance=RequestContext(request))
예제 #6
0
파일: fields.py 프로젝트: su-danny/famdates
 def render(self, name, value, attrs=None):
     if value is None:
         value = ''
     elif isinstance(value, (list, tuple)):
         value = (', '.join([getattr(user, get_username_field()) for user in value]))
     return super(CommaSeparatedUserInput, self).render(name, value, attrs)