Exemple #1
0
 def clean_mailed_to(self):
     to = split(self.cleaned_data['mailed_to'])
     if len(to) > MAX:
         raise forms.ValidationError(_(u'You can only send up to %s addresses at a time' % MAX))
     for address in to:
         if not email_re.match(address):
             raise forms.ValidationError(_(u'Invalid e-mail address "%s"') % address)
     return self.cleaned_data['mailed_to']
Exemple #2
0
 def clean_mailed_to(self):
     to = split(self.cleaned_data['mailed_to'])
     if len(to) > MAX:
         raise forms.ValidationError(
             _(u'You can only send up to %s addresses at a time' % MAX))
     for address in to:
         if not email_re.match(address):
             raise forms.ValidationError(
                 _(u'Invalid e-mail address "%s"') % address)
     return self.cleaned_data['mailed_to']
Exemple #3
0
def mail_item_to_friend_send(request):
    """
    Parses the form and sends the email. (Login required, POST required)

    **Context:**

    ``object``
        The model instance to be mailed.

    **Template**

    mailfriend/sent.html
    
    """
    if not request.POST:
        raise Http404, "Only POSTs are allowed"
    form = MailedItemForm(request.POST)
    try:
        content_type, obj = form.check_generic_object()
    except ObjectDoesNotExist:
        raise Http404, "Object does not exist"
    if form.is_valid():
        mailed_item = form.save(commit=False)

        # build full object URL
        site = Site.objects.get_current()
        site_url = 'http://%s/' % site.domain
        url_to_mail = 'http://%s%s' % (site.domain, obj.get_absolute_url())

        # render email
        sending_user = request.user
        subject = "You have received a link"
        message_template = loader.get_template('mailfriend/email_message.txt')
        message_context = Context({
            'site': site,
            'site_url': site_url,
            'object': obj,
            'url_to_mail': url_to_mail,
            'sending_user': sending_user,
        })
        message = message_template.render(message_context)

        # send email
        recipient_list = split(mailed_item.mailed_to)
        if mailed_item.send_to_user_also:
            recipient_list.append(request.user.email)
        if mailed_item.user_email_as_from:
            from_address = request.user.email
        else:
            from_address = settings.DEFAULT_FROM_EMAIL
        EmailMessage(subject,
                     message,
                     from_address,
                     recipient_list,
                     headers={
                         'Reply-To': request.user.email
                     }).send()

        # save email to database
        mailed_item.date_mailed = datetime.datetime.now()
        mailed_item.mailed_by = sending_user
        mailed_item.save()

        context = Context({'object': obj})
        return render_to_response('mailfriend/sent.html',
                                  context,
                                  context_instance=RequestContext(request))

    # form is invalid
    else:
        return render_to_response('mailfriend/form.html', {
            'content_type': content_type,
            'form': form,
            'object': obj,
        },
                                  context_instance=RequestContext(request))
Exemple #4
0
def mail_item_to_friend_send(request):
    """
    Parses the form and sends the email. (Login required, POST required)

    **Context:**

    ``object``
        The model instance to be mailed.

    **Template**

    mailfriend/sent.html

    """
    if not request.POST:
        raise Http404("Only POSTs are allowed")
    form = MailedItemForm(request.POST)
    try:
        content_type, obj = form.check_generic_object()
    except ObjectDoesNotExist:
        raise Http404("Object does not exist")
    if form.is_valid():
        mailed_item = form.save(commit=False)

        # build full object URL
        site = Site.objects.get_current()
        site_url = "http://%s/" % site.domain
        url_to_mail = "http://%s%s" % (site.domain, obj.get_absolute_url())

        # render email
        sending_user = request.user
        subject = "You have received a link"
        message_template = loader.get_template("mailfriend/email_message.txt")
        message_context = Context(
            {
                "site": site,
                "site_url": site_url,
                "object": obj,
                "url_to_mail": url_to_mail,
                "sending_user": sending_user,
            }
        )
        message = message_template.render(message_context)

        # send email
        recipient_list = split(mailed_item.mailed_to)
        if mailed_item.send_to_user_also:
            recipient_list.append(request.user.email)
        if mailed_item.user_email_as_from:
            from_address = request.user.email
        else:
            from_address = settings.DEFAULT_FROM_EMAIL
        EmailMessage(subject, message, from_address, recipient_list, headers={"Reply-To": request.user.email}).send()

        # save email to database
        mailed_item.date_mailed = datetime.datetime.now()
        mailed_item.mailed_by = sending_user
        mailed_item.save()

        context = {"object": obj}
        return render_to_response("mailfriend/sent.html", context, context_instance=RequestContext(request))

    # form is invalid
    else:
        return render_to_response(
            "mailfriend/form.html",
            {"content_type": content_type, "form": form, "object": obj},
            context_instance=RequestContext(request),
        )