Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1#!/usr/local/bin/python 

2# encoding: utf-8 

3""" 

4*The HTML template module for the `models_transients_element_put.py` resource* 

5 

6:Author: 

7 David Young 

8""" 

9from builtins import zip 

10from builtins import object 

11import sys 

12import os 

13import khufu 

14from datetime import datetime, date, time 

15 

16 

17class models_transients_element_put(object): 

18 """ 

19 The worker class for the models_transients_element_put module 

20 

21 **Key Arguments** 

22 

23 - ``log`` -- logger 

24 - ``request`` -- the pyramid request 

25 """ 

26 

27 def __init__( 

28 self, 

29 log, 

30 request 

31 ): 

32 self.log = log 

33 self.request = request 

34 self.transientBucketId = request.matchdict["elementId"] 

35 self.response = "" 

36 # xt-self-arg-tmpx 

37 

38 log.debug("instansiating a new 'models_transients_element_put' object") 

39 

40 return None 

41 

42 def close(self): 

43 del self 

44 return None 

45 

46 def put(self): 

47 """get the models_transients_element_put object 

48 

49 **Return** 

50 

51 - ``response`` 

52 """ 

53 self.log.debug('starting the ``get`` method') 

54 

55 # move the objects to another list if requested 

56 if "mwl" in self.request.params or "awl" in self.request.params: 

57 self._move_transient_to_another_list() 

58 

59 # change the pi is requested 

60 if set(("piName", "piEmail")) <= set(self.request.params): 

61 self._change_pi_for_object() 

62 

63 if "observationPriority" in self.request.params: 

64 self._set_observational_priority_for_object() 

65 

66 # throw warning if nothing has changed 

67 if len(self.response) == 0: 

68 self.response = "nothing has changed" 

69 

70 self.log.debug('completed the ``get`` method') 

71 

72 return self.response 

73 

74 def _move_transient_to_another_list( 

75 self): 

76 """ create sqlquery for the put request 

77 """ 

78 self.log.debug('starting the ``_create_sqlquery`` method') 

79 transientBucketId = self.transientBucketId 

80 

81 sqlQuery = u""" 

82 select marshallWorkflowLocation, alertWorkflowLocation from pesstoObjects where transientBucketId = %(transientBucketId)s 

83 """ % locals() 

84 objectDataTmp = self.request.db.execute(sqlQuery).fetchall() 

85 objectData = [] 

86 objectData[:] = [dict(list(zip(list(row.keys()), row))) 

87 for row in objectDataTmp] 

88 

89 oldMwl = objectData[0]["marshallWorkflowLocation"] 

90 oldAwl = objectData[0]["alertWorkflowLocation"] 

91 username = self.request.authenticated_userid.replace(".", " ").title() 

92 now = datetime.now() 

93 now = now.strftime("%Y-%m-%d %H:%M:%S") 

94 

95 # CHANGE THE MARSHALL WORKFLOW LOCATION LIST IF REQUESTED 

96 if "mwl" in self.request.params: 

97 mwl = self.request.params["mwl"] 

98 if "snoozed" in self.request.params: 

99 logEntry = "object snoozed by %(username)s" % locals( 

100 ) 

101 snoozed = ", snoozed = 1" 

102 else: 

103 logEntry = "moved from '%(oldMwl)s' to '%(mwl)s' list by %(username)s" % locals( 

104 ) 

105 snoozed = ", snoozed = 0" 

106 

107 sqlQuery = """ 

108 update pesstoObjects set marshallWorkflowLocation = "%(mwl)s" %(snoozed)s where transientBucketId = %(transientBucketId)s 

109 """ % locals() 

110 self.request.db.execute(sqlQuery) 

111 self.request.db.commit() 

112 self.response = self.response + \ 

113 " transientBucketId %(transientBucketId)s moved to the `%(mwl)s` marshallWorkflowLocation<BR>" % locals( 

114 ) 

115 

116 for o, n in zip(["pending observation", "following", "pending classification"], ["classification targets", "followup targets", "queued for classification"]): 

117 logEntry = logEntry.replace(o, n) 

118 

119 sqlQuery = u"""insert ignore into transients_history_logs ( 

120 transientBucketId, 

121 dateCreated, 

122 log 

123 ) 

124 VALUES ( 

125 %s, 

126 "%s", 

127 "%s" 

128 )""" % (transientBucketId, now, logEntry) 

129 self.request.db.execute(sqlQuery) 

130 self.request.db.commit() 

131 

132 # RESET PRIORITY IF REQUIRED 

133 if mwl == "following": 

134 sqlQuery = """ 

135 update pesstoObjects set observationPriority = 2 where transientBucketId = %(transientBucketId)s 

136 """ % locals() 

137 self.request.db.execute(sqlQuery) 

138 self.request.db.commit() 

139 

140 # RESET THE LAST TIME REVIEWE IF REQUIRED 

141 if mwl == "archive": 

142 now = datetime.now() 

143 now = now.strftime("%Y-%m-%d %H:%M:%S") 

144 sqlQuery = """ 

145 update pesstoObjects set lastReviewedMagDate = "%(now)s" where transientBucketId = %(transientBucketId)s 

146 """ % locals() 

147 self.request.db.execute(sqlQuery) 

148 self.request.db.commit() 

149 

150 # CHANGE THE ALERT WORKFLOW LOCATION LIST IF REQUESTED 

151 if "awl" in self.request.params: 

152 awl = self.request.params["awl"] 

153 sqlQuery = """ 

154 update pesstoObjects set alertWorkflowLocation = "%(awl)s", snoozed = 0 where transientBucketId = %(transientBucketId)s 

155 """ % locals() 

156 self.request.db.execute(sqlQuery) 

157 self.request.db.commit() 

158 self.response = self.response + \ 

159 " transientBucketId %(transientBucketId)s moved to the `%(awl)s` alertWorkflowLocation<BR>" % locals( 

160 ) 

161 

162 logEntry = "moved from '%(oldAwl)s' to '%(awl)s' list by %(username)s" % locals( 

163 ) 

164 for o, n in zip(["pending observation", "following", "pending classification"], ["classification targets", "followup targets", "queued for classification"]): 

165 logEntry = logEntry.replace(o, n) 

166 sqlQuery = u"""insert ignore into transients_history_logs ( 

167 transientBucketId, 

168 dateCreated, 

169 log 

170 ) 

171 VALUES ( 

172 %s, 

173 "%s", 

174 "%s" 

175 )""" % (transientBucketId, now, logEntry) 

176 self.request.db.execute(sqlQuery) 

177 self.request.db.commit() 

178 

179 self.log.debug('completed the ``_create_sqlquery`` method') 

180 return None 

181 

182 def _change_pi_for_object( 

183 self): 

184 """ change pi for object 

185 """ 

186 self.log.debug('starting the ``_change_pi_for_object`` method') 

187 

188 piName = self.request.params["piName"].strip() 

189 piEmail = self.request.params["piEmail"].strip() 

190 transientBucketId = self.transientBucketId 

191 username = self.request.authenticated_userid.replace(".", " ").title() 

192 now = datetime.now() 

193 now = now.strftime("%Y-%m-%d %H:%M:%S") 

194 

195 sqlQuery = """ 

196 select pi_name, pi_email from pesstoObjects where transientBucketId = %(transientBucketId)s 

197 """ % locals() 

198 objectDataTmp = self.request.db.execute(sqlQuery).fetchall() 

199 objectData = [] 

200 objectData[:] = [dict(list(zip(list(row.keys()), row))) 

201 for row in objectDataTmp] 

202 oldPiName = objectData[0]["pi_name"] 

203 oldPiEmail = objectData[0]["pi_email"] 

204 

205 # CHANGE THE PI IN THE DATABASE 

206 sqlQuery = """ 

207 update pesstoObjects set pi_name = "%(piName)s", pi_email = "%(piEmail)s" where transientBucketId = %(transientBucketId)s 

208 """ % locals() 

209 self.request.db.execute(sqlQuery) 

210 self.request.db.commit() 

211 

212 self.response = self.response + \ 

213 "changed the PI of transient #%(transientBucketId)s to '%(piName)s' (%(piEmail)s)" % locals( 

214 ) 

215 

216 if oldPiName: 

217 logEntry = "PI changed from %(oldPiName)s (%(oldPiEmail)s) to %(piName)s (%(piEmail)s) by %(username)s" % locals( 

218 ) 

219 else: 

220 logEntry = "%(piName)s (%(piEmail)s) assigned as PI of this object by by %(username)s" % locals( 

221 ) 

222 

223 sqlQuery = u"""insert ignore into transients_history_logs ( 

224 transientBucketId, 

225 dateCreated, 

226 log 

227 ) 

228 VALUES ( 

229 %s, 

230 "%s", 

231 "%s" 

232 )""" % (transientBucketId, now, logEntry) 

233 self.request.db.execute(sqlQuery) 

234 self.request.db.commit() 

235 

236 self.log.debug('completed the ``_change_pi_for_object`` method') 

237 return None 

238 

239 def _set_observational_priority_for_object( 

240 self): 

241 """ change the observational priority for an object 

242 """ 

243 self.log.debug( 

244 'completed the ````_set_observational_priority_for_object`` method') 

245 

246 observationPriority = self.request.params[ 

247 "observationPriority"].strip() 

248 transientBucketId = self.transientBucketId 

249 username = self.request.authenticated_userid.replace(".", " ").title() 

250 now = datetime.now() 

251 now = now.strftime("%Y-%m-%d %H:%M:%S") 

252 

253 # GET OLD DATA 

254 sqlQuery = """ 

255 select observationPriority, marshallWorkflowLocation from pesstoObjects where transientBucketId = %(transientBucketId)s 

256 """ % locals() 

257 objectDataTmp = self.request.db.execute(sqlQuery).fetchall() 

258 objectData = [] 

259 objectData[:] = [dict(list(zip(list(row.keys()), row))) 

260 for row in objectDataTmp] 

261 oldobservationPriority = objectData[0]["observationPriority"] 

262 mwl = objectData[0]["marshallWorkflowLocation"] 

263 

264 # CHANGE THE OBSERVATIONPRIORITY IN THE DATABASE 

265 sqlQuery = """ 

266 update pesstoObjects set observationPriority = "%(observationPriority)s" where transientBucketId = %(transientBucketId)s 

267 """ % locals() 

268 self.request.db.execute(sqlQuery) 

269 self.request.db.commit() 

270 

271 # RESPONSE 

272 self.response = self.response + \ 

273 "changed the observational priority of transient #%(transientBucketId)s to '%(observationPriority)s'" % locals( 

274 ) 

275 

276 observationPriority = int(observationPriority) 

277 oldobservationPriority = int(oldobservationPriority) 

278 

279 if mwl == "following": 

280 for n, w in zip([1, 2, 3, 4], ["CRITICAL", "IMPORTANT", "USEFUL", "NONE"]): 

281 if n == oldobservationPriority: 

282 oldobservationPriority = w 

283 

284 for n, w in zip([1, 2, 3, 4], ["CRITICAL", "IMPORTANT", "USEFUL", "NONE"]): 

285 if n == observationPriority: 

286 observationPriority = w 

287 

288 # LOG ENTRY 

289 logEntry = "observation priority changed from %(oldobservationPriority)s to %(observationPriority)s by %(username)s" % locals( 

290 ) 

291 

292 else: 

293 for n, w in zip([1, 2, 3], ["HIGH", "MEDIUM", "LOW"]): 

294 if n == oldobservationPriority: 

295 oldobservationPriority = w 

296 

297 for n, w in zip([1, 2, 3], ["HIGH", "MEDIUM", "LOW"]): 

298 if n == observationPriority: 

299 observationPriority = w 

300 

301 # LOG ENTRY 

302 logEntry = "classification priority changed from %(oldobservationPriority)s to %(observationPriority)s by %(username)s" % locals( 

303 ) 

304 

305 sqlQuery = u"""insert ignore into transients_history_logs ( 

306 transientBucketId, 

307 dateCreated, 

308 log 

309 ) 

310 VALUES ( 

311 %s, 

312 "%s", 

313 "%s" 

314 )""" % (transientBucketId, now, logEntry) 

315 self.request.db.execute(sqlQuery) 

316 self.request.db.commit() 

317 

318 self.log.debug( 

319 'completed the ``_set_observational_priority_for_object`` method') 

320 return None 

321 

322 # xt-class-method