On 12/2/06, Beau Hartshorne <[EMAIL PROTECTED]> wrote:
>
> What's the best way make a method call from Django's admin? I'd like
> to set up a link or button that re-sends emails to users who've lost
> them to the Internet or spam traps or whatever.
>

Well, I'm not sure what is the best way to do it, but I'll explain how
I would do it.

First, you need to display the link that will trigger the process. For
example, if you have this model in your app's models.py file:

<<<
class Person(models.Model):
    name = models.CharField(maxlength=80)
    email = models.EmailField()

    class Admin:
        list_display = ('name', 'email', 'reminder_link')

    def reminder_link(self):
        return '<a href="/admin/person/sendreminder/%s/">Send
reminder</a>' % self.id
    reminder_link.allow_tags = True
>>>

There you are telling the admin interface to display three columns,
the 'name', 'email' and a 'reminder_link' property which will cause it
to call a method of that name on the object. Therefore you need to
define that method, which simply returns an HTML portion that should
display a link. You'll need to set an 'allow_tags' property to the
method so that the admin interface does not escape the HTML that it
returns.

Adjust the link that the 'reminder_link' method returns to better fit
your project.

Next, we'll have to handle any request to
'/admin/person/sendreminder/' with a custom view.

You'll have to add an url pattern to your urls.py file:

<<<
urlpatterns = patterns('',
    (r'^admin/person/sendreminder/(\d+)/$', 'myapp.views.send_reminder'),
    (r'^admin/', include('django.contrib.admin.urls')),

    .... your other urls....
)
>>>

You must add the handler for '/admin/person/sendreminder/' before the
default 'admin/' pattern, otherwise the included admin would try to
handle it and would spit an error.

Now you need to create the view that will actually do the work of
sending the reminder. In your views.py file you will define the
'send_reminder' view:

<<<
from django.core.mail import send_mail

def send_reminder(request, person_id):
    person = get_object_or_404(Person, pk=person_id)

    subject = 'Your name reminder'
    body = 'Hey, your name is %s' % person.name
    sender = '[EMAIL PROTECTED]'
    recipient = [person.email]

    send_mail(subject, body, sender, recipient)
    return render_to_response('reminder_sent.html')
>>>

That function send a simple email to the user indicated by the
'person_id' parameter. You would then create 'reminder_sent.html'
template and display a confirmation message.

The only thing left to do is to make sure that only a staff user can
access the '/admin/person/sendreminder/' url. To do that you need to
decorate your view with a 'staff_member_required' decorator like this:

<<<
from django.contrib.admin.views.decorators import staff_member_required
from django.views.decorators.cache import never_cache

def send_reminder(request, person_id):
    .... snip ....
    return render_to_response('reminder_sent.html')
send_reminder = staff_member_required(never_cache(send_reminder))
>>>

And that's it. Hope it can help.

Regards,
Jorge

--~--~---------~--~----~------------~-------~--~----~
 You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to