root jira workflow cat RECR_send_email_to_c andidate Interview 31 Отка

  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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
[root@jira workflow]# cat RECR_send_email_to_candidate
# Interview (31)
# Отказ (41)
# Test (51)
# Final (71)
# Offer (91)
# Написать письмо (241)
# Анкета СБ (251)
# Написать заказчику (271)
# Документы на выход (291)
if transientVars['actionId'] in [71, 31, 51, 41, 91, 241, 251, 271, 291, 301]:
log.warn('debug: 0 | %s | %s'%(issue.key, transientVars['actionId']))
from com.atlassian.jira.component import ComponentAccessor
cfM = ComponentAccessor.getCustomFieldManager()
def gcfv(id, fromIssue=issue):
return fromIssue.getCustomFieldValue(cfM.getCustomFieldObject(id))
def scfv(id, value, toIssue=issue):
toIssue.setCustomFieldValue(cfM.getCustomFieldObject(id), value)
log.warn('debug: 1 | %s | %s | %s'%(issue.key, transientVars['actionId'], gcfv(10194)))
## Если "Отправлять письмо" == Да
if (gcfv(10194) and gcfv(10194).getOptionId() == 10212) or transientVars['actionId'] in [241, 251, 271, 301]:
from com.atlassian.jira.config.util import JiraHome
import time
import datetime
from java.text import SimpleDateFormat
import signature
## Define managers and utils
userUtil = ComponentAccessor.getUserUtil()
linkManager = ComponentAccessor.getIssueLinkManager()
attachmentManager = ComponentAccessor.getAttachmentManager()
## Define common vars
jira_home_path = ComponentAccessor.getComponentOfType(JiraHome).getHomePath()
current_user = ComponentAccessor.getJiraAuthenticationContext().getUser()
# Текст письма
text = gcfv(10016)
if not text: text = ''
# Candidate issue
for link in linkManager.getInwardLinks(issue.getId()):
linkedIssue = link.getSourceObject()
if linkedIssue.getProjectObject().getKey() == 'BR':
candidate_issue = linkedIssue
candidate_email = gcfv(10001, candidate_issue)
candidate_name = gcfv(10010, candidate_issue)
if not candidate_email:
raise NameError(u'Невозможно отправить письмо - карточка кандидата не содержит E-mail!')
# Подпись рекрутёра
sign = ''
if current_user.getName() in signature.HR: sign = '\n'+signature.HR[current_user.getName()]
####
def textReplaceAll(what, to=''):
global text
for i in range(0, text.count(what)):
text = text.replace(what, to)
# Отправитель и адресат
sender = current_user.getEmailAddress()
to = candidate_email
def create_email(body):
import email; from email.MIMEText import MIMEText;
html = u'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'
html += u'<html xmlns="http://www.w3.org/1999/xhtml"><head><meta charset="utf-8"></head>'
html += u'<body>' + body + u'</body></html>'
msg = email.MIMEMultipart.MIMEMultipart('related')
msg.set_charset('utf-8')
msg['Subject'] = subj
msg['Importance'] = 'high'
msg.attach(MIMEText(body.encode('utf-8').replace('\n', '<br/>'), 'html', 'utf-8'))
return msg
def attach_file(msg, File, filename, contentID=None):
from email.mime.application import MIMEApplication
from email import Encoders
from email.MIMEBase import MIMEBase
import urllib
part = MIMEBase('application', 'octet-stream')
part.set_payload(File)
Encoders.encode_base64(part)
if contentID:
part.add_header('Content-ID', contentID)
part.add_header('Content-Disposition', u'attachment; filename*=UTF-8\'\''+urllib.quote_plus(str(filename)).replace('+', '%20'))
msg.attach(part)
return msg
def attach_issue_attachments(msg):
attachents = attachmentManager.getAttachments(issue)
for attachment in attachents:
z = (datetime.datetime.now()-datetime.timedelta(seconds=2)).strftime('%Y-%m-%d %H:%M:%S.0')
if time.strptime(str(attachment.getCreated()), '%Y-%m-%d %H:%M:%S.0') > time.strptime(z, '%Y-%m-%d %H:%M:%S.0'):
File = open(jira_home_path+'/data/attachments/' + issue.getProjectObject().getKey() + '/' + issue.getKey() + '/' +str(attachment.getId()), 'rb').read()
msg = attach_file(msg, File, attachment.getFilename())
return msg
###########################################################################################
if transientVars['actionId'] in [71, 31]:
office = gcfv(10168)
date = gcfv(10175)
timem = gcfv(10131)
subj = u'Интервью в Mail.Ru Group'
textReplaceAll(u'%Дата_встречи%', SimpleDateFormat('dd.MM.yyyy').format(date))
textReplaceAll(u'%Время_встречи%', str(timem))
maps = {10154:'voronezh.gif', 10152:'skylight.jpg', 10156:'m100.jpg', 10153: 'st.petersburg.png', 10155:'nnovgorod.jpg'}
img = None; imgTag = ''
if office.getOptionId() in maps > -1:
img = maps[office.getOptionId()]
imgTag = '\n\n<img src="cid:map">'
msg = create_email(text+imgTag+sign)
if img: msg = attach_file(msg, open('/data/jira-home/data/images/'+img).read(), 'map.png', 'map')
###########################################################################################
if transientVars['actionId'] == 51:
subj = u'Тест от Mail.Ru Group'
msg = create_email(text+sign)
msg = attach_issue_attachments(msg)
###########################################################################################
if transientVars['actionId'] == 41:
subj = u'Переговоры в Mail.Ru Group'
msg = create_email(text+sign)
###########################################################################################
if transientVars['actionId'] == 91:
subj = u'Предложение о работе, Mail.Ru Group'
msg = create_email(text+sign)
msg = attach_issue_attachments(msg)
###########################################################################################
if transientVars['actionId'] == 241:
v = gcfv(10139)
subj = v if v else u'Переговоры в Mail.Ru Group'
msg = create_email(text+sign)
msg = attach_issue_attachments(msg)
###########################################################################################
if transientVars['actionId'] == 251:
subj = u'Анкета от Mail.Ru Group'
msg = create_email(text+sign)
msg = attach_file(msg, open('/data/jira-home/data/anketa_sb.doc').read(), 'Анкета для заполнения.doc')
###########################################################################################
if transientVars['actionId'] == 271 or transientVars['actionId'] == 301:
v = gcfv(20702) # Тема письма
subj = v if v else u'[RECR] '+issue.getSummary()
to = gcfv(10001)
msg = create_email(text+sign)
msg = attach_issue_attachments(msg)
###########################################################################################
if transientVars['actionId'] == 291:
date = gcfv(10173)
subj = u'Список документов для трудоустройства (компания MAIL.RU)'
textReplaceAll(u'%Дата_выхода%', SimpleDateFormat('dd.MM.yyyy').format(date))
msg = create_email(text+sign)
msg = attach_file(msg, open('/data/jira-home/data/anketa_sb.doc').read(), 'Анкета для заполнения.doc')
## Send email
import smtplib
log.warn('debug: 2 | %s | %s | %s | %s'%(issue.key, transientVars['actionId'], sender, to))
if to:
if isinstance(to, (str, unicode)):
to = [to,]
server = smtplib.SMTP('localhost')
for i in to:
msg['To'] = i
server.sendmail(sender, [i], msg.as_string())
log.warn('debug: 3 | %s | %s | send passed'%(issue.key, transientVars['actionId']))
# Дубликат для рекрутёра
msg['To'] = sender
msg['Subject'] = '[RECR] %s | %s'%(msg['Subject'], issue.getSummary())
server.sendmail(sender, [sender], msg.as_string())
log.warn('debug: 4 | %s | %s | duplicate passed'%(issue.key, transientVars['actionId']))
server.quit()
else:
raise(Exception(u'Не удалось получить e-mail адресата!'))