from django.db import models from django.contrib.auth.models import User from django.utils import timezone from taggit.managers import TaggableManager from datetime import datetime from croniter import croniter class CustomUser(models.Model): user = models.OneToOneField(User, null=True, on_delete=models.CASCADE) tags = TaggableManager() class Post(models.Model): author = models.ForeignKey('auth.User', on_delete=models.CASCADE) title = models.CharField(max_length=200) text = models.TextField() created_date = models.DateTimeField( default=timezone.now) published_date = models.DateTimeField( blank=True, null=True) tags = TaggableManager() def publish(self): self.published_date = timezone.now() self.save() def __str__(self): return self.title class Report(models.Model): report_text = models.TextField() class ScheduledReport(models.Model): """ Contains email subject and cron expression,to evaluate when the email has to be sent """ subject = models.CharField(max_length=200) last_run_at = models.DateTimeField(null=True, blank=True) next_run_at = models.DateTimeField(null=True, blank=True) cron_expression = models.CharField(max_length=200) def save(self, *args, **kwargs): """ function to evaluate "next_run_at" using the cron expression, so that it is updated once the report is sent. """ self.last_run_at = datetime.now() iter = croniter(self.cron_expression, self.last_run_at) self.next_run_at = iter.get_next(datetime) super(ScheduledReport, self).save(*args, **kwargs) def __unicode__(self): return self.subject class ScheduledReportGroup(models.Model): """ Many to many mapping between reports which will be sent out in a scheduled report """ report = models.ForeignKey(Report, related_name='report', on_delete=models.CASCADE) scheduled_report = models.ForeignKey(ScheduledReport, related_name='relatedscheduledreport', on_delete=models.CASCADE) class ReportRecipient(models.Model): """ Stores all the recipients of the given scheduled report """ email = models.EmailField() scheduled_report = models.ForeignKey(ScheduledReport, related_name='reportrecep', on_delete=models.CASCADE)