예제 #1
0
def list_new(request, template='postorius/lists/new.html'):
    """
    Add a new mailing list.
    If the request to the function is a GET request an empty form for
    creating a new list will be displayed. If the request method is
    POST the form will be evaluated. If the form is considered
    correct the list gets created and otherwise the form with the data
    filled in before the last POST request is returned. The user must
    be logged in to create a new list.
    """
    mailing_list = None
    choosable_domains = [('', _('Choose a Domain'))]
    choosable_domains += _get_choosable_domains(request)
    choosable_styles = _get_choosable_styles(request)
    if request.method == 'POST':
        form = ListNew(choosable_domains, choosable_styles, request.POST)
        if form.is_valid():
            # grab domain
            domain = Domain.objects.get_or_404(
                mail_host=form.cleaned_data['mail_host'])
            # creating the list
            try:
                mailing_list = domain.create_list(
                    form.cleaned_data['listname'],
                    style_name=form.cleaned_data['list_style'])
                mailing_list.add_owner(form.cleaned_data['list_owner'])
                list_settings = mailing_list.settings
                if form.cleaned_data['description']:
                    list_settings["description"] = \
                        form.cleaned_data['description']
                list_settings["advertised"] = form.cleaned_data['advertised']
                list_settings.save()
                messages.success(request, _("List created"))
                return redirect("list_summary",
                                list_id=mailing_list.list_id)
            # TODO catch correct Error class:
            except HTTPError as e:
                # Right now, there is no good way to detect that this is a
                # duplicate mailing list request other than checking the
                # reason for 400 error.
                if e.reason == b'Mailing list exists':
                    form.add_error(
                        'listname', _('Mailing List already exists.'))
                    return render(request, template, {'form': form})
                # Otherwise just render the generic error page.
                return render(request, 'postorius/errors/generic.html',
                              {'error': e.msg.decode()})
        else:
            messages.error(request, _('Please check the errors below'))
    else:
        form = ListNew(choosable_domains, choosable_styles, initial={
            'list_owner': request.user.email,
            'advertised': True,
            })
    return render(request, template, {'form': form})
예제 #2
0
 def test_form_fields_list_invalid(self):
     form = ListNew({
         'listname': 'xy#z',
         'mail_host': 'mailman.most-desirable.org',
         'list_owner': 'mailman.most-desirable.org',
         'advertise': 'abcd',
         'description': 'The Most Desirable organization',
     })
     self.assertFalse(form.is_valid())
예제 #3
0
 def test_form_fields_list(self):
     form = ListNew(
         [("mailman.most-desirable.org", "mailman.most-desirable.org")], {
             'listname': 'xyz',
             'mail_host': 'mailman.most-desirable.org',
             'list_owner': '*****@*****.**',
             'advertised': 'True',
             'description': 'The Most Desirable organization',
         })
     self.assertTrue(form.is_valid(), form.errors)
예제 #4
0
파일: list.py 프로젝트: aisworld/postorius
def list_new(request, template='postorius/lists/new.html'):
    """
    Add a new mailing list.
    If the request to the function is a GET request an empty form for
    creating a new list will be displayed. If the request method is
    POST the form will be evaluated. If the form is considered
    correct the list gets created and otherwise the form with the data
    filled in before the last POST request is returned. The user must
    be logged in to create a new list.
    """
    mailing_list = None
    choosable_domains = [('', _('Choose a Domain'))]
    choosable_domains += _get_choosable_domains(request)
    if request.method == 'POST':
        form = ListNew(choosable_domains, request.POST)
        if form.is_valid():
            # grab domain
            domain = Domain.objects.get_or_404(
                mail_host=form.cleaned_data['mail_host'])
            # creating the list
            try:
                mailing_list = domain.create_list(
                    form.cleaned_data['listname'])
                mailing_list.add_owner(form.cleaned_data['list_owner'])
                list_settings = mailing_list.settings
                list_settings["description"] = form.cleaned_data['description']
                list_settings["advertised"] = form.cleaned_data['advertised']
                list_settings.save()
                messages.success(request, _("List created"))
                return redirect("list_summary",
                                list_id=mailing_list.list_id)
            # TODO catch correct Error class:
            except HTTPError as e:
                return render(request, 'postorius/errors/generic.html',
                              {'error': e})
        else:
            messages.error(request, _('Please check the errors below'))
    else:
        form = ListNew(choosable_domains,
                       initial={'list_owner': request.user.email})
    return render(request, template, {'form': form})