Django app for sending periodic emails? - django

The main functionality of it would be:
check a condition every day
if the condition is true, then send email
Now, I know how to conditionlly send email, but I don`t know how to do make it repeat every day. I have some ideas but not sure about them at all (to add another process? or others?). I appreciate your suggestions and ideas...thx

You could create a custom management command and use a cron job to have it run once a day.
edit:
I have used this before, http://code.google.com/p/django-command-extensions/wiki/JobsScheduling. It is pretty handy if you have multiple tasks to be run on fixed schedules. Instead of having to add a line in crontab for each task you just add new files to the jobs directory.

Related

Execute function at special time using Django

I write task manager, and i want to create bot, which must send message to user (maybe user's email or social account).
I want to user can be able choose task and set time, when bot should send message to him.
My proble is that I don't know what i can user for it, I use django framework, and now i think that celery can help me with it.
Celery might be a bit heavy to start with.
You can try the django_cron to schedule your email tasks or other cron jobs that need to run at a specific time.
It's quite easy to set up following its documentation.

django how to automate functions inside views.py file

Well, this question will surely make the delights of the downvotes brigade and may be tagged as" too broad etc", but it is not!, but precisely because it requires "general" knowledge of how things work, I cannot find an answer in the books I have to ask it.
Having my Django application, yes, I can make it interactive by means of the MVC flow. The issue that I have is when I have methods that are not in connection with an html page (that the user sees) but are methods that are supposed to be running constantly in the background. For example, just to illustrate, imagine a code snippet that queries a DB and sends an email with news every 2 hours. It is just not doing anything because I dont know how to "wake that code snippet up".
I dont have that problem if I am writing a desktop application in just python without Django. If I right click and say, run this file, the code will running in the background alright.
Yes, naturally I have heard of cron jobs etc, but so far I see that you can cron-tab a file but how do I crontab a method inside views.py? but I suppose that is not either the way to go. I am find that you downvote it, as long as I get an answer.
thank you
I've been using a combination of commands and cron jobs for that purpose.
Write your command, set up your cronjob:
30 3 * * * /home/ubuntu/project/env/bin/python /home/ubuntu/project/manage.py command_name
Profit.
If you want to execute periodic tasks in Django, then there are multiple options.
Using Crontab.
There are multiple Django crontab apps are available. (django-crontab)
You just need to add the cron function in your settings file.
CRONJOBS = [
('*/5 * * * *', 'myapp.cron.my_scheduler')
]
cron.py
from myapp.views import send_email
def my_scheduler():
# add logic
send_email()
Using Celery Beat
I personally prefer Celery over crontab . You can check it here

Send Weekly Email Notifications to Users regarding any changes in data

What are ways to send automatic weekly email notifications to users upon any update or changes in the data in a django project ?
I'll describe the simplest solution I'm aware of. There are also much, much more sophisticated approaches to this problem.
Fundamentally, you need three things:
A task runner (I recommend configuring cron jobs with django-kronos)
An SMTP provider (I recommend Mailgun, which is super simple to set up with Django and gives you some test credits out of the box).
Email templates (write yourself & render to string w/ Django -- Mailgun has some good open source templates as well on their blog)
Example: django-kronos provides decorators for registering functions as cron jobs (this assumes your web server is Linux-based). These jobs can be installed as part of your deploy process from the command line:
./manage.py installtasks
For kronos to find tasks, they must be located in cron.py files inside of your apps.
# myapp/cron.py
import kronos
from django.contrib.auth.models import user
from myapp.services import check_for_changes, notify_user_of_change
# Register cron job to run once a week (every Sunday) at midnight
#kronos.register('0 0 * * 0')
def notify_about_changes():
"""Sets up a cron job and runs this service function once a day.
Installed With:
``./manage.py installtasks``
"""
all_my_users = User.objects.all()
for user in all_my_users:
changes = check_for_changes(user)
for change in changes:
notify_user_of_change(user, change)
Note that it's a good practice to keep your task function as "thin" as possible. In your case, you're (a) checking for changes, then (b) notifying about each change. These could each be a function that accepts a user instance as a param, or they could be model methods. In any case, the hard work should be handled elsewhere.
This is a very broad question, so unfortunately the answer is going to be broad as well. The general workflow you're looking for probably starts off with a Signal which you can set up to be activated on update of data.
That signal would then store data about the changes and then use one of many different email options to send email. You could use either built-in Django emails, or use a 3rd party API such as django-mailchimp.
Hopefully this gets you headed in the right direction.

Django setting up a scheduled task without Cron

I know there are many questions asking about this, especially this one: Django - Set Up A Scheduled Job?.
But what I want to understand is, how does a scheduled task inside Django actually works?
My simplistic way to think about it is that there's an infinite loop somewhere, something like this (runs every 60 seconds),
import time
interval=60 #60 seconds
while True:
some_method()
time.sleep(interval)
Question: where do you put this infinite loop? Is there some part of the Django app that just runs in the background alongside the rest of the app?
Thanks!
Django doesn't do scheduled tasks. If you want scheduled tasks, you need a daemon that runs all the time and can launch your task at the appropriate time.
Django only runs when a http request is made. If no one makes a http request for a month, django doesn't run for a month. If there are 45 http requests this second, django will run 45 times this second (in the absence of caching).
You can write scripts in the django framework (called management commands) that get called from some outside service (like cron). That's as close as you'll get to what you want. If that's the case, then the question/answer you reference is the place to get the how tos.
Probably on a unixy system, cron is the simplest outside service to work with. On recent linux systems, cron has a directory /etc/cron.d into which you can drop your app's cron config file, and it will not interfere with any other cron jobs on the system. No editing of existing files necessary.

Alternative to django-celery to perform asynchronous task in Django?

In my admin I have a form allowing to upload a file to fill the DB.
Parsing and filling the DB take a long time, so I'd like to do it asynchronously.
As recommended by several SO users I tried to install python-celery, but I can't manage to do it (I'm on Webfaction).
Is there any simple, easy-to-install alternative?
If webfaction supports cron jobs you can create your own pseudo broker. You could save your long running tasks to the db and in a 'tasks' table, this would allow you to return a response to the user instantaneously. Then there could be a cron that goes through very frequently and looks for uncompleteled tasks and processes them.
I believe this is what django mailer does
https://github.com/jtauber/django-mailer/
https://stackoverflow.com/a/1419640/594589
Try Gearman along with it's python client library
It's very easy to setup and run gearman. Try few examples.