3

I'm Junior Django Dev. Got my first project. Doing quite well but senior dev that teaches me went on vacations....

I have a Task in my company to create a function that will remind all people in specyfic Group, 5 days before event by sending mail.

There is a TournamentModel that contains a tournament_start_date for instance '10.08.2018'.

Player can join tournament, when he does he joins django group "Registered".

I have to create a function (job?) that will check tournament_start_date and if tournament begins in 5 days, this function will send emails to all people in "Registered" Group... automatically.

How can I do this? What should I use? How to run it and it will automatically check? I'm learning python/django for few months... but I meet jobs fot the first time ;/

I will appreciate any help.

Adrian Kurzeja
  • 797
  • 1
  • 11
  • 27
  • At first you might want to educate yourself about [management commands](https://docs.djangoproject.com/en/2.0/howto/custom-management-commands/). – Klaus D. Jul 17 '18 at 11:01
  • That's way too broad, sorry. A couple hints nonetheless: 1/ you can get tournaments starting in 5 days with an ordinary ORM lookup on start_date, 2/ write your code as a management command, 3/ use your system scheduler (cron on unix-like systems) to run it once a day. – bruno desthuilliers Jul 17 '18 at 11:01
  • Add the Mail at one cronjob that will be schedule to check it everyday, when one event will be valid in condition (so 5 days to the event start) that will trigger your email, look for schedule tasks on Windows or Cron Jobs in Linux – Diego Vinícius Jul 17 '18 at 14:52

2 Answers2

4

If you break this down into two parts, it will be easier. The parts are

  1. Send the reminder emails
  2. Run the script every day

For the first part, I would begin by installing the outstanding django-extensions package so that it is easy to run scripts. (Check your INSTALLED_APPS, you may have it already.) Having the python-dateutil package will be helpful too.

Then I would write my script. Let's say your app is called "myapp". Create a directory under "myapp" called "scripts", and create an empty __init__.py in there.

Now create your script file in "myapp/scripts/remind_players.py". It will look something like this:

from datetime import date
from datutil.relativedelta import relativedelta
from django.mail import send_mail  # I think this is right.
from myapp.models import Tournament

def run():
    # find our tournaments that start five days from now.
    for tournament in Tournament.objects.filter(tournament_start_date=date.today() + relativedelta(days=5)):

        # find the players that are registered for the tournament
        for player in tournament.registered_player_set():
            send_mail(...)

To run your script manually, you would run

python manage.py runscript remind_players

(PROTIP: make sure that your test database only contains your own email address. It would be embarrassing to have your test system sending out emails to everyone.)

(PROTIP: you can also configure Django to put email messages in the logs, rather than actually sending them.)

For the second part, you'll have to find a way to schedule the job to run every day. Usually, your operating system will provide a way to do that. If you're on Unix/Linux, you'll use "cron". If you're on Windows, you'll use "Scheduled Tasks". (There are other options for scheduling, but these are the most common.)

Chris Curvey
  • 9,738
  • 10
  • 48
  • 70
1

You can set this mail send function as cron job。You can schedule it by crontab or Celery if Your team has used it.

Hayden
  • 449
  • 4
  • 13