Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

models.py 2.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. from django.db import models
  2. from django.contrib.auth.models import User
  3. from django.utils import timezone
  4. from taggit.managers import TaggableManager
  5. from datetime import datetime
  6. from croniter import croniter
  7. class CustomUser(models.Model):
  8. user = models.OneToOneField(User, null=True, on_delete=models.CASCADE)
  9. tags = TaggableManager()
  10. class Post(models.Model):
  11. author = models.ForeignKey('auth.User', on_delete=models.CASCADE)
  12. title = models.CharField(max_length=200)
  13. text = models.TextField()
  14. created_date = models.DateTimeField(
  15. default=timezone.now)
  16. published_date = models.DateTimeField(
  17. blank=True, null=True)
  18. tags = TaggableManager()
  19. def publish(self):
  20. self.published_date = timezone.now()
  21. self.save()
  22. def __str__(self):
  23. return self.title
  24. class Report(models.Model):
  25. report_text = models.TextField()
  26. class ScheduledReport(models.Model):
  27. """
  28. Contains email subject and cron expression,to evaluate when the email has to be sent
  29. """
  30. subject = models.CharField(max_length=200)
  31. last_run_at = models.DateTimeField(null=True, blank=True)
  32. next_run_at = models.DateTimeField(null=True, blank=True)
  33. cron_expression = models.CharField(max_length=200)
  34. def save(self, *args, **kwargs):
  35. """
  36. function to evaluate "next_run_at" using the cron expression, so that it is updated once the report is sent.
  37. """
  38. self.last_run_at = datetime.now()
  39. iter = croniter(self.cron_expression, self.last_run_at)
  40. self.next_run_at = iter.get_next(datetime)
  41. super(ScheduledReport, self).save(*args, **kwargs)
  42. def __unicode__(self):
  43. return self.subject
  44. class ScheduledReportGroup(models.Model):
  45. """
  46. Many to many mapping between reports which will be sent out in a scheduled report
  47. """
  48. report = models.ForeignKey(Report, related_name='report', on_delete=models.CASCADE)
  49. scheduled_report = models.ForeignKey(ScheduledReport,
  50. related_name='relatedscheduledreport', on_delete=models.CASCADE)
  51. class ReportRecipient(models.Model):
  52. """
  53. Stores all the recipients of the given scheduled report
  54. """
  55. email = models.EmailField()
  56. scheduled_report = models.ForeignKey(ScheduledReport, related_name='reportrecep', on_delete=models.CASCADE)