def test_form_errors(self): form = MemberForm({}) self.assertFalse(form.is_valid()) self.assertEqual(form.errors['email'][0], 'Please enter an email address.') form = MemberForm({'email': 'invalid.example.com'}) self.assertFalse(form.is_valid()) self.assertEqual(form.errors['email'][0], 'Please enter a valid email address.')
def _non_member_post(self, request, role): """Handle POST for membership roles owner, moderator and non-member. Add memberships if the form is valid otherwise redirect to list_members page with an error message. """ member_form = MemberForm(request.POST) if member_form.is_valid(): try: self.mailing_list.add_role( role=role, address=member_form.cleaned_data['email'], display_name=member_form.cleaned_data['display_name']) messages.success( request, _('%(email)s has been added with the role %(role)s'.format( email=member_form.cleaned_data['email'], role=role))) except HTTPError as e: messages.error(request, e.msg.decode()) else: messages.error(request, member_form.errorsg) return redirect('list_members', self.mailing_list.list_id, role)
def list_members_view(request, list_id, role=None): """Display all members of a given list.""" if role not in ['owner', 'moderator', 'subscriber']: return redirect('list_members', list_id, 'subscriber') mailing_list = List.objects.get_or_404(fqdn_listname=list_id) if request.method == 'POST': if role == 'subscriber': form = MultipleChoiceForm(request.POST) if form.is_valid(): members = form.cleaned_data['choices'] for member in members: mailing_list.unsubscribe(member) messages.success(request, _('The selected members' ' have been unsubscribed')) return redirect('list_members', list_id, role) else: member_form = MemberForm(request.POST) if member_form.is_valid(): try: getattr(mailing_list, 'add_%s' % role)( member_form.cleaned_data['email']) messages.success( request, _('%(email)s has been added' ' with the role %(role)s') % {'email': member_form.cleaned_data['email'], 'role': role}) return redirect('list_members', list_id, role) except HTTPError as e: messages.error(request, _(e.msg)) else: form = MultipleChoiceForm() member_form = MemberForm() context = { 'list': mailing_list, 'role': role, } if role == 'subscriber': context['page_title'] = _('List subscribers') if request.GET.get('q'): query = context['query'] = request.GET['q'] if "*" not in query: query = '*{}*'.format(query) # Proxy the find_members method to insert the query def find_method(count, page): return mailing_list.find_members(query, count=count, page=page) else: find_method = mailing_list.get_member_page context['members'] = paginate( find_method, request.GET.get('page'), request.GET.get('count', 25), paginator_class=MailmanPaginator) if mailing_list.member_count == 0: context['empty_error'] = _('List has no Subscribers') else: context['empty_error'] =\ _('No member was found matching the search') context['form'] = form else: context['member_form'] = member_form if role == 'owner': context['page_title'] = _('List owners') context['members'] = mailing_list.owners context['form_action'] = _('Add owner') elif role == 'moderator': context['page_title'] = _('List moderators') context['members'] = mailing_list.moderators context['empty_error'] = _('List has no moderators') context['form_action'] = _('Add moderator') return render(request, 'postorius/lists/members.html', context)
def list_members_view(request, list_id, role=None): """Display all members of a given list.""" if role not in ['owner', 'moderator', 'subscriber']: return redirect('list_members', list_id, 'subscriber') mailing_list = List.objects.get_or_404(fqdn_listname=list_id) if request.method == 'POST': if role == 'subscriber': form = MultipleChoiceForm(request.POST) if form.is_valid(): members = form.cleaned_data['choices'] for member in members: mailing_list.unsubscribe(member) messages.success(request, _('The selected members' ' have been unsubscribed')) return redirect('list_members', list_id, role) else: member_form = MemberForm(request.POST) if member_form.is_valid(): try: getattr(mailing_list, 'add_%s' % role)( member_form.cleaned_data['email']) messages.success( request, _('%(email)s has been added' ' with the role %(role)s') % {'email': member_form.cleaned_data['email'], 'role': role}) return redirect('list_members', list_id, role) except HTTPError as e: messages.error(request, _(e.msg)) else: form = MultipleChoiceForm() member_form = MemberForm() context = { 'list': mailing_list, 'role': role, } if role == 'subscriber': context['page_title'] = _('List subscribers') if request.GET.get('q'): query = context['query'] = request.GET['q'] if "*" not in query: query = '*{}*'.format(query) # Proxy the find_members method to insert the query def find_method(count, page): return mailing_list.find_members(query, count=count, page=page) else: find_method = mailing_list.get_member_page context['members'] = utils.paginate( request, find_method, count=request.GET.get('count', 25), paginator_class=utils.MailmanPaginator) if len(mailing_list.members) == 0: context['empty_error'] = _('List has no Subscribers') else: context['empty_error'] =\ _('No member was found matching the search') context['count_options'] = [25, 50, 100, 200] context['form'] = form else: context['member_form'] = member_form if role == 'owner': context['page_title'] = _('List owners') context['members'] = mailing_list.owners context['form_action'] = _('Add owner') elif role == 'moderator': context['page_title'] = _('List moderators') context['members'] = mailing_list.moderators context['empty_error'] = _('List has no moderators') context['form_action'] = _('Add moderator') return render(request, 'postorius/lists/members.html', context)