-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodel.py
96 lines (77 loc) · 3.27 KB
/
model.py
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
"""This module contains the model."""
from google.appengine.ext import ndb
class Subscriber(ndb.Model):
"""Represents a single person subscribed to a team.
The keyname is the mail+team.
"""
name = ndb.StringProperty()
mail = ndb.StringProperty()
status = ndb.StringProperty() # subscribe | unsubscribe
role = ndb.StringProperty() # admin | None
team = ndb.StringProperty() # e.g., GFW, POTICO
@classmethod
def get_or_insert(cls, **data):
data['team'] = data['team'].lower()
data['mail'] = data['mail'].lower()
key_name = '%s+%s' % (data['mail'], data['team'])
return super(Subscriber, cls).get_or_insert(key_name, **data)
@classmethod
def subscribed(cls, team):
"""Return all subsribers for supplied team."""
return cls.query(
cls.status == 'subscribe',
cls.team == team.lower()
).fetch(100)
class SubscriberUpdate(ndb.Model):
"""Represents a subscriber update and used to create a digest email.
The keyname is mail+date+team"""
name = ndb.StringProperty() # Subsriber name
mail = ndb.StringProperty() # Subscriber mail
team = ndb.StringProperty() # The WRI team name
message = ndb.TextProperty()
date = ndb.DateTimeProperty()
sent = ndb.BooleanProperty() # True if update email sent
error = ndb.StringProperty() # The mail error if one occurred
@classmethod
def get_or_insert(cls, name, mail, team, date):
key_name = '%s+%s+%s' % (team, mail, date.isoformat())
return super(SubscriberUpdate, cls).get_or_insert(
key_name, name=name, mail=mail, team=team.lower(), date=date)
@classmethod
def get_updates(cls, date, team):
"""Get SubscriberUpdate models for supplied date and team."""
return cls.query(
cls.date == date,
cls.team == team.lower()
).order(-cls.name).fetch(100)
class Update(ndb.Model):
"""Represents an update event for a team and date."""
date = ndb.DateTimeProperty()
digest_sent = ndb.BooleanProperty(default=False)
updates_sent = ndb.BooleanProperty(default=False)
team = ndb.StringProperty() # The WRI team name
@classmethod
def get_or_insert(cls, team, date):
key_name = '%s+%s' % (team, date.isoformat())
return super(Update, cls).get_or_insert(
key_name, team=team, date=date)
@classmethod
def latest(cls, team):
"""Returns the latest Update entity for a team."""
return cls.query(
cls.team == team.lower()
).order(-cls.date).get()
class SubscriberDigest(ndb.Model):
"""Ensures a subscriber received a digest email for a given Update date.
The keyname is email+date+team.
"""
mail = ndb.StringProperty() # Subscriber email
date = ndb.DateTimeProperty() # The date of the Update
team = ndb.StringProperty() # The WRI team name
sent = ndb.BooleanProperty(default=False) # True if subscriber got digest
error = ndb.StringProperty() # The mail error if one occurred
@classmethod
def get_or_insert(cls, mail, team, date):
key_name = '%s+%s+%s' % (team, mail, date.isoformat())
return super(SubscriberDigest, cls).get_or_insert(
key_name, mail=mail, team=team.lower(), date=date)