models py from django db import models from django contrib auth models

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#models.py
from django.db import models
from django.contrib.auth.models import User
import datetime
from service.managers import CheckRecords
class AlertDate(models.Model):
name = models.CharField(max_length=30)
days = models.IntegerField()
class Group(models.Model):
name = models.CharField(max_length=30)
user = models.ManyToManyField(User, filter_interface=True)
class Service(models.Model):
name = models.CharField(max_length=50)
class Client(models.Model):
name = models.CharField(max_length=50)
class Record(models.Model):
service = models.ForeignKey(Service)
client = models.ForeignKey(Client)
description = models.TextField(blank=True)
information = models.TextField(blank=True)
start_date = models.DateField()
end_date = models.DateField()
alert_group = models.ManyToManyField(Group, filter_interface=True)
alert = models.ForeignKey(AlertDate)
objects = models.Manager()
check = CheckRecords()
#managers.py
from django.db import models
from models import Record
from lib.functions import send_report
import datetime
class CheckRecords(models.Manager):
def expired(self):
records = self.filter(end_date__lt=datetime.date.today())
for record in records:
emails = []
for group in record.alert_group.all():
for user in group.user.all():
emails.append(user.email)
send_report(record, emails)
def in_expire_zone(self):
records = self.all()
for record in records:
if (record.end_date - datetime.timedelta(days=record.alert.days)) == datetime.date.today():
emails = []
for group in record.alert_group.all():
for user in group.user.all():
emails.append(user.email)
send_report(record, emails)
def every_week(self):
records = self.filter(end_date__lt=datetime.date.today())
for record in records:
days_left = record.end_date - datetime.date.today()
if days_left.days < record.alert.days and not days_left.days % 7:
emails = []
for group in record.alert_group.all():
for user in record.user.all():
emails.append(user.email)
send_report(record, emails)
#views.py
from django.shortcuts import render_to_response
from service.models import Record
def check_alert(request):
Record.check.expired()
Record.check.in_expire_zone()
Record.check.every_week()
return render_to_response('check.html')