Exemplo n.º 1
0
def list_member_options(request, list_id, email):
    template_name = 'postorius/lists/memberoptions.html'
    client = utils.get_client()
    mm_list = List.objects.get_or_404(fqdn_listname=list_id)
    try:
        mm_member = client.get_member(list_id, email)
        member_prefs = mm_member.preferences
    except Mailman404Error:
        return render(request, template_name, {'nolists': 'true'})
    initial_moderation = dict([
        (key, getattr(mm_member, key)) for key in MemberModeration.base_fields
        ])
    if request.method == 'POST':
        if request.POST.get("formname") == 'preferences':
            moderation_form = MemberModeration(initial=initial_moderation)
            preferences_form = UserPreferences(
                request.POST, initial=member_prefs)
            if preferences_form.is_valid():
                if not preferences_form.has_changed():
                    messages.info(request,
                                  _("No change to the member's preferences."))
                    return redirect('list_member_options', list_id, email)
                for key in preferences_form.fields.keys():
                    member_prefs[key] = preferences_form.cleaned_data[key]
                try:
                    member_prefs.save()
                except HTTPError as e:
                    messages.error(request, e.msg)
                else:
                    messages.success(request, _("The member's preferences have"
                                                " been updated."))
                    return redirect('list_member_options', list_id, email)
        elif request.POST.get("formname") == 'moderation':
            preferences_form = UserPreferences(initial=member_prefs)
            moderation_form = MemberModeration(
                request.POST, initial=initial_moderation)
            if moderation_form.is_valid():
                if not moderation_form.has_changed():
                    messages.info(request,
                                  _("No change to the member's moderation."))
                    return redirect('list_member_options', list_id, email)
                for key in moderation_form.fields.keys():
                    setattr(mm_member, key, moderation_form.cleaned_data[key])
                try:
                    mm_member.save()
                except HTTPError as e:
                    messages.error(request, e.msg)
                else:
                    messages.success(request, _("The member's moderation "
                                                "settings have been updated."))
                    return redirect('list_member_options', list_id, email)
    else:
        preferences_form = UserPreferences(initial=member_prefs)
        moderation_form = MemberModeration(initial=initial_moderation)
    return render(request, template_name, {
        'mm_member': mm_member,
        'list': mm_list,
        'preferences_form': preferences_form,
        'moderation_form': moderation_form,
        })
Exemplo n.º 2
0
def user_list_options(request, list_id):
    utils.set_other_emails(request.user)
    mlist = List.objects.get_or_404(fqdn_listname=list_id)
    mm_user = MailmanUser.objects.get(address=request.user.email)
    subscription = None
    for s in mm_user.subscriptions:
        if s.role == 'member' and s.list_id == list_id:
            subscription = s
            break
    if not subscription:
        raise Http404(_('Subscription does not exist'))
    preferences = subscription.preferences
    if request.method == 'POST':
        form = UserPreferences(request.POST)
        if form.is_valid():
            for key in form.cleaned_data.keys():
                if form.cleaned_data[key] is not None:
                    # None: nothing set yet. Remember to remove this test
                    # when Mailman accepts None as a "reset to default"
                    # value.
                    preferences[key] = form.cleaned_data[key]
            preferences.save()
            messages.success(request, _('Your preferences have been updated.'))
            return redirect('user_list_options', list_id)
        else:
            messages.error(request, _('Something went wrong.'))
    else:
        form = UserPreferences(initial=subscription.preferences)
    user_emails = [request.user.email] + request.user.other_emails
    subscription_form = ChangeSubscriptionForm(
        user_emails, initial={'email': subscription.email})
    return render(request, 'postorius/user/list_options.html',
                  {'form': form, 'list': mlist,
                   'change_subscription_form': subscription_form})
Exemplo n.º 3
0
 def test_form_fields_valid(self):
     form = UserPreferences({
         'acknowledge_posts': 'True',
         'hide_address': 'True',
         'receive_list_copy': 'False',
         'receive_own_postings': 'False',
     })
     self.assertTrue(form.is_valid())
Exemplo n.º 4
0
 def test_form_fields_valid(self):
     form = UserPreferences({
         'acknowledge_posts': 'True',
         'hide_address': 'True',
         'receive_list_copy': 'False',
         'receive_own_postings': 'False',
     })
     self.assertTrue(form.is_valid())
Exemplo n.º 5
0
def list_member_options(request, list_id, email):
    template_name = 'postorius/lists/memberoptions.html'
    mm_list = List.objects.get_or_404(fqdn_listname=list_id)
    try:
        mm_member = mm_list.find_members(address=email)[0]
        member_prefs = mm_member.preferences
    except (ValueError, IndexError):
        raise Http404(_('Member does not exist'))
    except Mailman404Error:
        return render(request, template_name, {'nolists': 'true'})
    initial_moderation = dict([
        (key, getattr(mm_member, key)) for key in MemberModeration.base_fields
        ])
    if request.method == 'POST':
        if request.POST.get("formname") == 'preferences':
            preferences_form = UserPreferences(
                request.POST, preferences=member_prefs)
            if preferences_form.is_valid():
                try:
                    preferences_form.save()
                except HTTPError as e:
                    messages.error(request, e.msg.decode())
                else:
                    messages.success(request, _("The member's preferences"
                                                " have been updated."))
            return redirect('list_member_options', list_id, email)
        elif request.POST.get("formname") == 'moderation':
            moderation_form = MemberModeration(
                request.POST, initial=initial_moderation)
            if moderation_form.is_valid():
                if not moderation_form.has_changed():
                    messages.info(
                        request, _("No change to the member's moderation."))
                    return redirect('list_member_options', list_id, email)
                for key in list(moderation_form.fields.keys()):
                    # In general, it would be a very bad idea to loop over the
                    # fields and try to set them one by one, However,
                    # moderation form has only one field.
                    setattr(mm_member, key, moderation_form.cleaned_data[key])
                try:
                    mm_member.save()
                except HTTPError as e:
                    messages.error(request, e.msg.decode())
                else:
                    messages.success(request, _("The member's moderation "
                                                "settings have been updated."))
                return redirect('list_member_options', list_id, email)
    else:
        preferences_form = UserPreferences(preferences=member_prefs)
        moderation_form = MemberModeration(initial=initial_moderation)
    return render(request, template_name, {
        'mm_member': mm_member,
        'list': mm_list,
        'preferences_form': preferences_form,
        'moderation_form': moderation_form,
        })
Exemplo n.º 6
0
 def test_form_fields_valid(self):
     form = UserPreferences(
         {
             "acknowledge_posts": "True",
             "hide_address": "True",
             "receive_list_copy": "False",
             "receive_own_postings": "False",
         }
     )
     self.assertTrue(form.is_valid())
Exemplo n.º 7
0
 def post(self, request):
     try:
         mm_user = MailmanUser.objects.get(address=request.user.email)
         form = UserPreferences(request.POST)
         if form.is_valid():
             preferences = mm_user.preferences
             for key in form.fields.keys():
                 if form.cleaned_data[key] is not None:
                     # None: nothing set yet. Remember to remove this test
                     # when Mailman accepts None as a "reset to default"
                     # value.
                     preferences[key] = form.cleaned_data[key]
             preferences.save()
             messages.success(request,
                              _('Your preferences have been updated.'))
         else:
             messages.error(request, _('Something went wrong.'))
     except MailmanApiError:
         return utils.render_api_error(request)
     except Mailman404Error as e:
         messages.error(request, e.msg)
     return redirect("user_mailmansettings")
Exemplo n.º 8
0
 def post(self, request):
     try:
         mm_user = MailmanUser.objects.get(address=request.user.email)
         form = UserPreferences(request.POST)
         if form.is_valid():
             preferences = mm_user.preferences
             for key in form.fields.keys():
                 if form.cleaned_data[key] is not None:
                     # None: nothing set yet. Remember to remove this test
                     # when Mailman accepts None as a "reset to default"
                     # value.
                     preferences[key] = form.cleaned_data[key]
             preferences.save()
             messages.success(request,
                              _('Your preferences have been updated.'))
         else:
             messages.error(request, _('Something went wrong.'))
     except MailmanApiError:
         return utils.render_api_error(request)
     except Mailman404Error as e:
         messages.error(request, e.msg)
     return redirect("user_mailmansettings")
Exemplo n.º 9
0
def user_list_options(request, list_id):
    mlist = List.objects.get_or_404(fqdn_listname=list_id)
    mm_user = MailmanUser.objects.get(address=request.user.email)
    subscription = None
    for s in mm_user.subscriptions:
        if s.role == 'member' and s.list_id == list_id:
            subscription = s
            break
    if not subscription:
        raise Http404(_('Subscription does not exist'))
    preferences = subscription.preferences
    if request.method == 'POST':
        form = UserPreferences(request.POST)
        if form.is_valid():
            for key in form.cleaned_data.keys():
                if form.cleaned_data[key] is not None:
                    # None: nothing set yet. Remember to remove this test
                    # when Mailman accepts None as a "reset to default"
                    # value.
                    preferences[key] = form.cleaned_data[key]
            preferences.save()
            messages.success(request, _('Your preferences have been updated.'))
            return redirect('user_list_options', list_id)
        else:
            messages.error(request, _('Something went wrong.'))
    else:
        form = UserPreferences(initial=subscription.preferences)
    user_emails = EmailAddress.objects.filter(
        user=request.user,
        verified=True).order_by("email").values_list("email", flat=True)
    subscription_form = ChangeSubscriptionForm(
        user_emails, initial={'email': subscription.email})
    return render(request, 'postorius/user/list_options.html', {
        'form': form,
        'list': mlist,
        'change_subscription_form': subscription_form
    })