Ejemplo n.º 1
0
def send_mail_to_all_members_view(request):
    if request.user.is_staff:
        raise Http404
    is_coordinator = request.user.is_superuser or request.user.is_staff or Staff.objects.filter(
        customer_responsible_id=request.user.customer.id, is_coordinator=True, is_active=True
    ).order_by('?').first() is not None
    if request.method == 'POST':
        form = MembersContactValidationForm(request.POST)  # A form bound to the POST data
        if form.is_valid():  # All validation rules pass
            to_email_customer = []
            if is_coordinator:
                qs = Customer.objects.filter(is_active=True, represent_this_buyinggroup=False, may_order=True)
            else:
                qs = Customer.objects.filter(is_active=True, accept_mails_from_members=True,
                                             represent_this_buyinggroup=False, may_order=True)
            for customer in qs:
                if customer.user_id != request.user.id:
                    to_email_customer.append(customer.user.email)
                if customer.email2 is not None and customer.email2 != EMPTY_STRING:
                    to_email_customer.append(customer.email2)
            to = (request.user.email,)
            email = EmailMessage(
                strip_tags(form.cleaned_data.get('subject')),
                strip_tags(form.cleaned_data.get('message')),
                from_email=settings.DEFAULT_FROM_EMAIL,
                to=to,
                bcc=to_email_customer
            )
            send_email(email=email)
            email = form.fields["your_email"]
            email.initial = request.user.email
            email.widget.attrs['readonly'] = True
            recipient = form.fields["recipient"]
            if is_coordinator:
                recipient.initial = _("All members as coordinator")
            else:
                recipient.initial = _("All members accepting to show they mail address")
            recipient.widget.attrs['readonly'] = True
            return render(request, "repanier/send_mail_to_all_members.html",
                          {'form': form, 'update': '2'})
    else:
        form = MembersContactValidationForm()  # An unbound form
        email = form.fields["your_email"]
        email.initial = request.user.email
        email.widget.attrs['readonly'] = True
        recipient = form.fields["recipient"]
        if is_coordinator:
            recipient.initial = _("All members as coordinator")
        else:
            recipient.initial = _("All members accepting to show they mail address")
        recipient.widget.attrs['readonly'] = True

    return render(request, "repanier/send_mail_to_all_members.html",
                  {'form': form, 'coordinator': is_coordinator})
def send_mail_to_coordinators_view(request):
    if request.user.is_staff:
        raise Http404
    if request.method == "POST":
        form = CoordinatorsContactValidationForm(request.POST)
        if form.is_valid():
            to_email_staff = []
            selected_staff_members = form.cleaned_data.get("staff")
            for staff in Staff.objects.filter(
                is_active=True, is_contributor=False, id__in=selected_staff_members
            ).order_by("?"):
                to_email_staff.append(staff.user.email)

            if len(to_email_staff) > 0:
                to_email_customer = [request.user.email]
                email = EmailMessage(
                    strip_tags(form.cleaned_data.get("subject")),
                    strip_tags(form.cleaned_data.get("message")),
                    from_email=settings.DEFAULT_FROM_EMAIL,
                    to=to_email_customer,
                    cc=to_email_staff,
                )
                send_email(email=email)
                # return HttpResponseRedirect('/')
                email = form.fields["your_email"]
                email.initial = request.user.email
                email.widget.attrs["readonly"] = True
                return render(request, "repanier/send_mail_to_coordinators.html", {"form": form, "update": "2"})
            else:
                return render(request, "repanier/send_mail_to_coordinators.html", {"form": form, "update": "1"})
    else:
        form = CoordinatorsContactValidationForm()

        email = form.fields["your_email"]
        email.initial = request.user.email
        email.widget.attrs["readonly"] = True

    return render(request, "repanier/send_mail_to_coordinators.html", {"form": form})