1

I'm building a django app that has users come to my site, enter in a string of text, their email, a friend's email and a date they would like to be emailed on.

How would I go about having the text they entered emailed to the them on the date they requested in the date_returned field? Any specific apps? Loops? Etc

Thank you,

My Models.py looks like:

class bet(models.Model):
name = models.CharField(max_length=100)
email_1 = models.EmailField()
email_2 = models.EmailField()
wager = models.CharField(max_length=300)
date_submitted = models.DateField(_("Date"), auto_now_add=True) 
date_returned = models.DateField(null=True)

def __unicode__(self):
    return self.name

class BetForm(ModelForm):
name = forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'Bet Name'}),      max_length=100)
email_1 = forms.EmailField(widget=forms.TextInput(attrs={'placeholder': 'Your Email'}))
email_2 = forms.EmailField(widget=forms.TextInput(attrs={'placeholder': 'Your Friend\'s Email'}))
wager = forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'What\'s The Wager?'}), max_length=200)
date_returned = forms.DateField(widget=SelectDateWidget())
class Meta:
    model=bet
Questifer
  • 1,113
  • 3
  • 18
  • 48

3 Answers3

0

You can write a custom management command in django for this. Then you will need to schedule a daily cron job which runs this command.

You should add a BooleanField as well to check if email has been already sent.

Please have a look at a related question - Django - Set Up A Scheduled Job?

Community
  • 1
  • 1
pankaj28843
  • 2,458
  • 17
  • 34
0

In django and python it is easy to send an email so I won't get into that. Your problem of sending an email at some particular event is not the job of a web application. I would recommend setting up a cron job that calls a django command (https://docs.djangoproject.com/en/dev/howto/custom-management-commands/). Write a simple command to dequeue events that are about to occur and send out the appropriate email. Have the cron run at a regular enough interval to simulate real time.

models.py

class Bet(models.Model):
    name = models.CharField(max_length=100)
    email_1 = models.EmailField()
    email_2 = models.EmailField()
    wager = models.CharField(max_length=300)
    date_submitted = models.DateField(_("Date"), auto_now_add=True) 
    date_returned = models.DateField(null=True)
    email_sent = model.BooleanField(default=False)

dequeueemail.py

from django.core.management.base import BaseCommand, CommandError
from app_name.models import bet 

class Command(BaseCommand):
    def handle(self, *args, **options):
        for bet in bet.objects.filter(date_returned__gt=datetime.datetime.now(),email_sent=False):
            #python code to send email
            bet.email_sent=True
            bet.save()

crontab

* * * * * python manage.py dequeueemail

If you want to hack this without actually installing a cron check this app out (http://code.google.com/p/django-cron/)

Victor 'Chris' Cabral
  • 2,135
  • 1
  • 16
  • 33
0

Judging by the Celery tag on the question you may be open to a solution using Celery, so I'll pose one. Similar to a cron job that runs only once once, Celery can delay tasks until a given date time: http://celery.readthedocs.org/en/latest/userguide/calling.html#eta-and-countdown

It's fairly straight forward. Just supply the keyword arg eta with a datetime to your .apply_async call and the task will be executed at that time.

Loren Abrams
  • 1,002
  • 8
  • 9