5

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

Community
  • 1
  • 1
user956424
  • 1,611
  • 2
  • 37
  • 67
  • 1
    You mention a link to a right question. Read the answers carefully, they shall help – stalk Oct 14 '14 at 11:31
  • This is clearly a duplicate of the linked question. Unless you give a good reason why the answers to that question don't work, I will close this as a duplicate. – Daniel Roseman Oct 14 '14 at 12:09

1 Answers1

10

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, 'my@email.com' )

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

Michael Plakhov
  • 2,292
  • 2
  • 19
  • 21