-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
315 lines (285 loc) · 13.4 KB
/
Copy pathserver.py
File metadata and controls
315 lines (285 loc) · 13.4 KB
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
#!/srv/www/engibot/env/bin/python
import ast
import json
import yaml
from tornado import httpserver
from tornado import gen
from tornado.ioloop import IOLoop
import tornado.web
import requests
import resources
import email_format
import db_manage
import send_email
from jira_create import *
from org import get_org_id
from tag_builder import platforms, assemble_tags
from whc import whc
def check_creds(creds):
return creds == whc
class dbManage(tornado.web.RequestHandler):
# Initalize database
def post(self):
if check_creds(tornado.escape.native_str(self.get_argument('creds'))):
db_manage.db_init()
self.write('DB Initialized.')
# Query database
def get(self):
if check_creds(tornado.escape.native_str(self.get_argument('creds'))):
database = 'data/records.sql'
try:
status = tornado.escape.native_str(self.get_argument('status'))
self.write(db_manage.read_db(db_manage.create_connection(database), locator=loc))
except:
status = ''
try:
loc = tornado.escape.native_str(self.get_argument('loc'))
self.write(db_manage.read_db(db_manage.create_connection(database), locator=loc))
except:
try:
owner = tornado.escape.native_str(self.get_argument('owner'))
self.write(db_manage.read_db(db_manage.create_connection(database), owner=owner))
except:
try:
rep = tornado.escape.native_str(self.get_argument('rep'))
self.write(db_manage.read_db(db_manage.create_connection(database), rep=rep))
except:
self.write(db_manage.read_db(db_manage.create_connection(database)))
# Update database records
def put(self):
if check_creds(tornado.escape.native_str(self.get_argument('creds'))):
database = 'data/records.sql'
if tornado.escape.native_str(self.get_argument('new')) == 'T':
account = tornado.escape.native_str(self.get_argument('account'))
oppID = tornado.escape.native_str(self.get_argument('oppID'))
jira = tornado.escape.native_str(self.get_argument('jira'))
owner = tornado.escape.native_str(self.get_argument('owner'))
status = tornado.escape.native_str(self.get_argument('status'))
rep = tornado.escape.native_str(self.get_argument('rep'))
try:
orgID = tornado.escape.native_str(self.get_argument('orgID'))
except:
orgID = ''
rowid = db_manage.create_record(db_manage.create_connection(database), (account, oppID, jira, orgID, owner, rep, status))
self.write('New record created: {}'.format(rowid))
else:
oppID = tornado.escape.native_str(self.get_argument('oppID'))
orgID = tornado.escape.native_str(self.get_argument('orgID'))
db_manage.update_record(db_manage.create_connection(database), (oppID, orgID))
self.write('Updated.')
class CreateJIRA(tornado.web.RequestHandler):
# Generate new JIRA ticket based on salesforce opp and account information.
def post(self):
if check_creds(tornado.escape.native_str(self.get_argument('creds'))):
database = 'data/records.sql'
try:
email = tornado.escape.native_str(self.get_argument("email"))
split = email.split("\n")
for s in split:
if s.startswith("https"):
oppID = s.strip()[-15:]
except:
oppID = tornado.escape.native_str(self.get_argument("opp"))
if oppID.startswith("https"):
oppID = oppID[-15:].strip()
if oppID:
# Status change of se request
jira, rep, se, account = createJIRA(oppID)
resources.sfdc_details(oppID, status="In Progress")
response = {
'jira': jira,
'oppID': oppID,
'rep': rep,
'owner': se,
'account': account
}
self.write(response)
rowid = db_manage.create_record(db_manage.create_connection(database), (account, oppID, jira, '', se, rep, 'Open'))
else:
self.write("Error")
else:
self.write('Invalid Credentials.')
class UpdateJIRA(tornado.web.RequestHandler):
# Client implementation details updated in JIRA ticket
def post(self):
if check_creds(tornado.escape.native_str(self.get_argument('creds'))):
default_params = """{
}"""
default_traffic = """{
}"""
param_dict = ast.literal_eval(self.get_argument('custom', default_params))
traffic_dict = ast.literal_eval(self.get_argument('traffic', default_traffic))
oppID = tornado.escape.native_str(self.get_argument('oppID'))
orgID = tornado.escape.native_str(self.get_argument('orgID'))
s = tornado.escape.native_str(self.get_argument('s', 'No'))
jira = tornado.escape.native_str(self.get_argument('jira'))
update, server, tech = updateJIRA(oppID, jira, param_dict, traffic_dict, s, orgID)
response = {
'params': param_dict,
'traffic': traffic_dict,
'server': server,
'tech': tech
}
self.write(json.dumps(response))
else:
self.write('Invalid Credentials.')
class Instructions(tornado.web.RequestHandler):
# Generate implementation instruction email based on client use case
def get(self):
if check_creds(tornado.escape.native_str(self.get_argument('creds'))):
database = 'data/records.sql'
loc = tornado.escape.native_str(self.get_argument('oppID'))
tags = tornado.escape.native_str(self.get_argument('tags', ''))
tech = tornado.escape.native_str(self.get_argument('tech'))
print(tech)
record = db_manage.read_db(db_manage.create_connection(database), locator=loc)[0]
account, oppID, jira, orgID, owner, rep = record[0], record[1], record[2], record[3], record[4], record[5]
email = email_format.compile_email(oppID, orgID, tags, tech=tech)
self.write(email)
else:
self.write('Invalid Credentials.')
class SendEmail(tornado.web.RequestHandler):
# Sends email...
def post(self):
if check_creds(tornado.escape.native_str(self.get_argument('creds'))):
body = tornado.escape.native_str(self.get_argument('body'))
user = tornado.escape.native_str(self.get_argument('user'))
orgID = tornado.escape.native_str(self.get_argument('orgID'))
to = tornado.escape.native_str(self.get_argument('to'))
cc = tornado.escape.native_str(self.get_argument('cc'))
subject = tornado.escape.native_str(self.get_argument('sub'))
attachments = tornado.escape.native_str(self.get_argument('attachments'))
cfg = ''
with open('/home/{}/config.yml'.format(user), 'r') as config_file:
cfg = yaml.load(config_file)
owner = cfg['gmail']['email']
pw = cfg['gmail']['password']
send_email.py_mail(subject, body, to, owner, attachments, pw, cc)
self.write("Email sent to {}!".format(to))
else:
self.write('Invalid Credentials.')
# Grab details from salesforce for email header info.
def get(self):
if check_creds(tornado.escape.native_str(self.get_argument('creds'))):
oppID = tornado.escape.native_str(self.get_argument('oppID'))
account, opp, req, rep, se = resources.sfdc_details(oppID)
sf = resources.sfdc_login()
res_dictionary = {'contacts': {}, 'type': req['Implementation_Type__c']}
print(req['Tech_Contact__c'])
try:
contact = sf.Contact.get(req['Tech_Contact__c'])
res_dictionary['contacts']['tech'] = [contact['Name'], contact['Email']]
except:
print('No tech contact')
if req['Business_Contact__c'] != req['Tech_Contact__c']:
try:
contact = sf.Contact.get(req['Business_Contact__c'])
res_dictionary['contacts']['business'] = [contact['Name'], contact['Email']]
except:
print('No business contact')
self.write(res_dictionary)
else:
self.write('Invalid Credentials.')
class Platforms(tornado.web.RequestHandler):
# List known platform macros
def get(self):
if check_creds(tornado.escape.native_str(self.get_argument('creds'))):
self.write(platforms())
else:
self.write('Invalid Credentials.')
class modJIRA(tornado.web.RequestHandler):
# Comment or reassign JIRA tasks and subtasks
def post(self):
if check_creds(tornado.escape.native_str(self.get_argument('creds'))):
response = ''
jira = tornado.escape.native_str(self.get_argument('jira'))
comment = tornado.escape.native_str(self.get_argument('comment'))
reassign = tornado.escape.native_str(self.get_argument('reassign', ''))
response += post_comment(jira, comment) + '\n'
if reassign != '':
response = reassign_task(jira, reassign, comment)
self.write(response)
else:
self.write('Invalid Credentials.')
# Read JIRA ticket comments and summary
def get(self):
if check_creds(tornado.escape.native_str(self.get_argument('creds'))):
jira = tornado.escape.native_str(self.get_argument('jira'))
response = read_issue(jira)
self.write(response)
else:
self.write('Invalid Credentials.')
# Resolve JIRA tasks
def put(self):
if check_creds(tornado.escape.native_str(self.get_argument('creds'))):
jira = tornado.escape.native_str(self.get_argument('jira'))
oppID = tornado.escape.native_str(self.get_argument('oppID'))
task_type = tornado.escape.native_str(self.get_argument('task'))
response = resolve_issue(jira, oppID, task_type)
self.write(response)
else:
self.write('Invalid Credentials.')
class Tag(tornado.web.RequestHandler):
# Generate custom client JS tags with keys and macros based on platform. Return in multiple formats (html, plain text, JSON)
def get(self):
if check_creds(tornado.escape.native_str(self.get_argument('creds'))):
try:
pfms = tornado.escape.native_str(self.get_argument('pfm')).split(',')
org = tornado.escape.native_str(self.get_argument('org'))
except:
self.write('No orgID (org) or platform (pfm) passed in.')
return
if len(org) != 20:
self.write('Invalid orgID - must be 20 characters.')
return
oppID = tornado.escape.native_str(self.get_argument('opp', 'False'))
img = ast.literal_eval(tornado.escape.native_str(self.get_argument('img', 'True')))
js = ast.literal_eval(tornado.escape.native_str(self.get_argument('js', 'True')))
devices = tornado.escape.native_str(self.get_argument('devices', 'desktop,mobile_web,in-app')).split(',')
formats = tornado.escape.native_str(self.get_argument('formats', 'video,banner')).split(',')
imp_type = tornado.escape.native_str(self.get_argument('imp_type', 'display')).split(',')
custom_params = tornado.escape.native_str(self.get_argument('custom_params', '')).split(',')
custom_dictionary = {}
if custom_params[0] != '':
for p in custom_params:
split = p.split('|')
custom_dictionary[split[0]] = split[1]
try:
inst = tornado.escape.native_str(self.get_argument('inst'))
inst = True
except:
inst = False
try:
comment = tornado.escape.native_str(self.get_argument('comment'))
comment = True
except:
comment = False
try:
html = tornado.escape.native_str(self.get_argument('html'))
html = True
except:
html = False
response = assemble_tags(pfms, org, js, img, devices, formats, custom_params, oppID, inst, html, comment)
self.write(response)
else:
self.write('Invalid Credentials.')
class Application(tornado.web.Application):
def __init__(self):
handlers = [
(r"/CreateJIRA/?", CreateJIRA),
(r"/UpdateJIRA/?", UpdateJIRA),
(r"/modJIRA/?", modJIRA),
(r"/Org/?", Account),
(r"/Tag/?", Tag),
(r"/Platforms/?", Platforms),
(r"/Instructions/?", Instructions),
(r"/dbManage/?", dbManage),
(r"/SendEmail/?", SendEmail),
]
tornado.web.Application.__init__(self, handlers)
def main():
app = Application()
app.listen()
IOLoop.instance().start()
if __name__ == '__main__':
main()