-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotification.py
164 lines (144 loc) · 8.99 KB
/
notification.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/usr/bin/env python
""" Prupose: To Send Notifications based on certain task conditions.
Author: Joshua Holt
Date: 12-09-2009
Last Modified: 02-14-2010
"""
import time,datetime
#from google.appengine.api import xmpp # importing this lib allows jabber chat messages to be sent.
from google.appengine.api import mail
from google.appengine.ext import db
# Data Model Imports
from models import User, Task, Project
def should_notify(currentUserId, task, action, wantsNotifications = True):
"""Determines if a notification should be sent"""
currentUserKey = db.Key.from_path('User', int(currentUserId))
currentUser = db.get(currentUserKey)
if task.submitterId != None:
submitterKey = db.Key.from_path('User', int(task.submitterId))
submitter = db.get(submitterKey)
if task.assigneeId != None:
assigneeKey = db.Key.from_path('User', int(task.assigneeId))
assignee = db.get(assigneeKey)
if wantsNotifications == False or task.name == "New Task":
retVal = False
elif task.submitterId != None and int(currentUserId) == int(task.submitterId) and task.assigneeId != None and int(currentUserId) == int(task.assigneeId):
retVal = False
else:
retVal = True
return retVal
def send_notification(taskId, currentUserId, action, name, ttype, priority, status, validation, submitterId, assigneeId, effort, projectId, description):
"""sends email notification"""
# Get information about this task and the assignee and submitter
task = None;
if action != "deleted":
task_key = db.Key.from_path('Task', int(taskId))
task = db.get(task_key)
currentUser_key = db.Key.from_path('User', int(currentUserId))
currentUser = db.get(currentUser_key)
# get all watches for task (like user authentication in main.py)
watcherIds = []; watcherEmails = []
q = db.GqlQuery("SELECT * FROM Watch WHERE taskId = %s" % taskId)
#result = q.fetch()
#if len(result) != 0:
# for each watch, push userId into array
for watch in q:
watcherIds.append(watch.userId)
# for each watcher, push email (if they have one) into array
for watcherId in watcherIds:
watcher_key = db.Key.from_path('User', int(watcherId))
watcher = db.get(watcher_key)
if(watcher.email != None and watcher.email != '' and watcher.email != 'None'):
watcherEmails.append(watcher.email)
# create list of emails for watchers
watcherSendList = ''
if(len(watcherEmails) != 0):
watcherSendList = '; '.join(watcherEmails)
if action == "deleted" or task != None:
submitter = None; assignee = None;
if action == "deleted" or (task != None and task.submitterId != None):
sid = task.submitterId if action != "deleted" else submitterId
if sid != 'None':
submitter_key = db.Key.from_path('User', int(sid))
submitter = db.get(submitter_key)
if action == "deleted" or (task != None and task.assigneeId != None):
aid = task.assigneeId if action != "deleted" else assigneeId
if aid != 'None':
assignee_key = db.Key.from_path('User', int(aid))
assignee = db.get(assignee_key)
if submitter != None or assignee != None:
message = mail.EmailMessage(sender="Tasks Server <[email protected]>", subject="Task #%s %s by %s" % (taskId, action if name != "New Task" else "created", currentUser.name))
message.to = ';'; message.cc = ';';
if assignee != None and assignee.email != '' and assignee.email != 'None' and assignee.email != None and assigneeId != currentUserId:
message.to = "%s" % assignee.email
if submitter != None and submitter.email != '' and submitter.email != 'None' and submitter.email != None and submitterId != currentUserId:
message.cc = "%s" % submitter.email
newName = task.name if task != None and task.name != None else "Unspecified"
oldName = newName if name == "New Task" else name
message.body = "Name:\t\t%s\n\n" % oldName if action == "deleted" or newName == oldName else "Name:\t\t%s\n=>\t\t%s\n\n" % (oldName, newName)
newType = "'" + task.type.replace('_','') + "'" if task != None and task.type != None else "Unspecified"
oldType = "'" + ttype.replace('_','') + "'" if ttype != 'None' else "Unspecified"
if name == "New Task":
oldType = newType
message.body += "Type:\t\t%s\n" % oldType if action == "deleted" or newType == oldType else "Type:\t\t%s => %s\n" % (oldType, newType)
newPriority = "'" + task.priority.replace('_','') + "'" if task != None and task.priority != None else "Unspecified"
oldPriority = "'" + priority.replace('_','') + "'" if priority != 'None' else "Unspecified"
if name == "New Task":
oldPriority = newPriority
message.body += "Priority:\t\t%s\n" % oldPriority if action == "deleted" or newPriority == oldPriority else "Priority:\t\t%s => %s\n" % (oldPriority, newPriority)
newStatus = "'" + task.developmentStatus.replace('_','') + "'" if task != None and task.developmentStatus != None else "Unspecified"
oldStatus = "'" + status.replace('_','') + "'" if status != 'None' else "Unspecified"
if name == "New Task":
oldStatus = newStatus
message.body += "Status:\t\t%s\n" % oldStatus if action == "deleted" or newStatus == oldStatus else "Status:\t\t%s => %s\n" % (oldStatus, newStatus)
newValidation = "'" + task.validation.replace('_','') + "'" if task != None and task.validation != None else "Unspecified"
oldValidation = "'" + validation.replace('_','') + "'" if validation != 'None' else "Unspecified"
if name == "New Task":
oldValidation = newValidation
message.body += "Validation:\t%s\n" % oldValidation if action == "deleted" or newValidation == oldValidation else "Validation:\t%s => %s\n" % (oldValidation, newValidation)
newSubmitter = db.get(db.Key.from_path('User', int(task.submitterId))) if task != None and task.submitterId != None else None
newSubmitterName = "'" + newSubmitter.name + "'" if newSubmitter != None else "Unassigned"
if submitterId != 'None' and submitterId != '':
oldSubmitter = db.get(db.Key.from_path('User', int(submitterId)))
else:
oldSubmitter = None
oldSubmitterName = "'" + oldSubmitter.name + "'" if oldSubmitter != None else "Unassigned"
if name == "New Task":
oldSubmitterName = newSubmitterName
message.body += "\nSubmitter:\t%s\n" % oldSubmitterName if action == "deleted" or newSubmitterName == oldSubmitterName else "\nSubmitter:\t%s => %s\n" % (oldSubmitterName, newSubmitterName)
newAssignee = db.get(db.Key.from_path('User', int(task.assigneeId))) if task != None and task.assigneeId != None else None
newAssigneeName = "'" + newAssignee.name + "'" if newAssignee != None else "Unassigned"
if assigneeId != 'None' and assigneeId != '':
oldAssignee = db.get(db.Key.from_path('User', int(assigneeId)))
else:
oldAssignee = None
oldAssigneeName = "'" + oldAssignee.name + "'" if oldAssignee != None else "Unassigned"
if name == "New Task":
oldAssigneeName = newAssigneeName
message.body += "Assignee:\t%s\n" % oldAssigneeName if action == "deleted" or newAssigneeName == oldAssigneeName else "Assignee:\t%s => %s\n" % (oldAssigneeName, newAssigneeName)
newEffort = "'" + task.effort + "'" if task != None and task.effort != None else "Unspecified"
oldEffort = "'" + effort + "'" if effort != 'None' else "Unspecified"
if name == "New Task":
oldEffort = newEffort
message.body += "\nEffort:\t\t%s\n" % oldEffort if action == "deleted" or newEffort == oldEffort else "\nEffort:\t\t%s => %s\n" % (oldEffort, newEffort)
newProject = db.get(db.Key.from_path('Project', int(task.projectId))) if task != None and task.projectId != None else None
newProjectName = "'" + newProject.name + "'" if newProject != None else "Unallocated"
if projectId != 'None' and projectId != '':
oldProject = db.get(db.Key.from_path('Project', int(projectId)))
else:
oldProject = None
oldProjectName = "'" + oldProject.name + "'" if oldProject != None else "Unallocated"
if name == "New Task":
oldProjectName = newProjectName
message.body += "Project:\t\t%s\n" % oldProjectName if action == "deleted" or newProjectName == oldProjectName else "Project:\t\t%s => %s\n" % (oldProjectName, newProjectName)
newDescription = task.description if task != None and task.description != None and task.description != '' else "Unspecified"
oldDescription = description if description != 'None' and description != '' else "Unspecified"
if name == "New Task":
oldDescription = newDescription
message.body += "\nDescription:\n%s\n" % oldDescription if action == "deleted" or newDescription == oldDescription else "\nDescription:\n%s\n\n=>\n\n%s\n" % (oldDescription, newDescription)
if watcherSendList != '':
message.cc += watcherSendList if message.cc == ';' else "; %s" % watcherSendList
if message.to == ';' and message.cc != ';':
message.to = message.cc; message.cc = ';'
if message.to != ';' or message.cc != ';':
message.send()