예제 #1
0
def process_email_form(request, content_type_id=None, object_id=None):
    """
    submission processor for email form.
    """
    if not (request.method=='POST' and request.is_ajax()):
        return HttpResponse('Method Not Allowed', status=httplib.METHOD_NOT_ALLOWED, mimetype='text/plain')
    if content_type_id is None:
        content_type_id=request.POST.get('content_type')
    try:
        content_type=ContentType.objects.get(pk=int(content_type_id))
    except (ObjectDoesNotExist, ValueError, TypeError):
        return HttpResponseBadRequest("invalid content type", mimetype='text/plain')
    if object_id is None:
        object_id=request.POST.get('object_id')
    try:
        item=content_type.get_object_for_this_type(pk=int(object_id))
    except (ObjectDoesNotExist, ValueError, TypeError):
        return HttpResponseBadRequest("no such object id")

    form=EmailEventForm(request.POST)
    if not form.is_valid():
        logging.debug("form is invalid.  Errors are: %s", form.errors)
        logging.debug("type of errors: %s", type(form.errors))
        return render_to_json(clean_errors(form.errors), status=httplib.BAD_REQUEST)
    
    cleaned=form.cleaned_data
    message_template=loader.get_template('email_message.txt')
    site=Site.objects.get_current()
    user=request.user if request.user.is_authenticated() else None    
    message_context=Context(dict(
        email_from=cleaned['email_from'],
        subject=cleaned['subject'],
        message=cleaned['message'],
        site=site,
        site_url='http://%s/' % site.domain,
        url='http://%s%s' % (site.domain, item.get_absolute_url()),
        item=item,
        user=user,
        ))
    message=message_template.render(message_context)
    
    recipients=cleaned['email_to'].split(',')
    from_address=cleaned['email_from']
    send_mail(cleaned['subject'], message, from_address, recipients, fail_silently=False, priority='high')
    
    #Update email_count
    email_sent.send(sender=EmailEvent, instance=item)
    
    event=form.save(commit=False)
    event.remote_ip=request.META['REMOTE_ADDR']
    event.content_type_id=content_type.pk
    event.object_id=item.pk
    event.mailed_by=user
    event.save()
    return render_to_response("email_success.txt")
예제 #2
0
    def save(self, fail_silently=False):
        """
        Builds and sends the email message.

        """
        send_mail(priority="high", fail_silently=fail_silently, **self.get_message_dict())
예제 #3
0
    def create_inactive_user(self, username, password, email,
                             send_email=True, profile_callback=None):
        """
        Create a new, inactive ``User``, generates a
        ``RegistrationProfile`` and email its activation key to the
        ``User``, returning the new ``User``.
        
        To disable the email, call with ``send_email=False``.

        The activation email will make use of two templates:

        ``registration/activation_email_subject.txt``
            This template will be used for the subject line of the
            email. It receives one context variable, ``site``, which
            is the currently-active
            ``django.contrib.sites.models.Site`` instance. Because it
            is used as the subject line of an email, this template's
            output **must** be only a single line of text; output
            longer than one line will be forcibly joined into only a
            single line.

        ``registration/activation_email.txt``
            This template will be used for the body of the email. It
            will receive three context variables: ``activation_key``
            will be the user's activation key (for use in constructing
            a URL to activate the account), ``expiration_days`` will
            be the number of days for which the key will be valid and
            ``site`` will be the currently-active
            ``django.contrib.sites.models.Site`` instance.
        
        To enable creation of a custom user profile along with the
        ``User`` (e.g., the model specified in the
        ``AUTH_PROFILE_MODULE`` setting), define a function which
        knows how to create and save an instance of that model with
        appropriate default values, and pass it as the keyword
        argument ``profile_callback``. This function should accept one
        keyword argument:

        ``user``
            The ``User`` to relate the profile to.
        
        """
        new_user = User.objects.create_user(username, email, password)
        new_user.is_active = False
        new_user.save()
        
        registration_profile = self.create_profile(new_user)
        
        if profile_callback is not None:
            profile_callback(user=new_user)
        
        if send_email:
            from courant.core.mailer import send_mail
            current_site = Site.objects.get_current()
            
            subject = render_to_string('registration/activation_email_subject.txt',
                                       { 'site': current_site })
            # Email subject *must not* contain newlines
            subject = ''.join(subject.splitlines())
            
            message = render_to_string('registration/activation_email.txt',
                                       { 'activation_key': registration_profile.activation_key,
                                         'expiration_days': settings.ACCOUNT_ACTIVATION_DAYS,
                                         'site': current_site })
            
            send_mail(subject, message, settings.DEFAULT_FROM_EMAIL, [new_user.email])
        return new_user