Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In django How to schedule a daily report to be sent out as email

I want to know how exactly I can schedule a report to be generated and sent out as email of the visitor details log table on a daily basis at a particular time. The visitor details like name, in and out time, purpose of visit needs to be sent out as an email. Using django 1.6.5 on linux.

I am aware of cron Django - Set Up A Scheduled Job? https://docs.djangoproject.com/en/dev/howto/custom-management-commands/ but don't seem to get the things together.

I can create template and view in django admin gui using all the model admin options. I can also generate csv using actions in admin panel. But I want the report to be generated automatically everyday and sent out as email without logging in django. I need complete code solution for that, as I am not clear how this can be done. Please help

like image 455
user956424 Avatar asked Oct 14 '14 11:10

user956424


1 Answers

First create custom management command like:

class Command(BaseCommand):

commands = ['sendreport',]
args = '[command]'
help = 'Send report'

def handle(self, *args, **options):

    '''
    Get completed sessions, send invite to vote
    '''

    reports = Log.objects.filter(date__gt=datetime.today(),date__lt=(datetime.today()+timedelta(days=2)))
    for report in reports:
        send_notification(_("Report"), _("log:%s")%report.text, '[email protected]' )

To create email text and send

Then you can add cronjob, something like

0 0 * * * /pathtovirtualenv/python manage.py sendreport

To run this command every night

like image 196
Michael Plakhov Avatar answered Nov 12 '22 02:11

Michael Plakhov