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 ticket header bar for the object ticket* 

5 

6:Author: 

7 David Young 

8""" 

9import sys 

10import os 

11import re 

12import datetime 

13from marshall_webapp.templates.commonelements import commonutils as cu 

14import khufu 

15from fundamentals import times 

16 

17 

18def ticket_header_bar( 

19 log, 

20 request, 

21 discoveryDataDictionary, 

22 objectComments, 

23 atelData, 

24 lightcurveData, 

25 objectHistories): 

26 """get ticket header bar 

27 

28 **Key Arguments** 

29 

30 - ``log`` -- logger 

31 - ``request`` -- the pyramid request 

32 - ``discoveryDataDictionary`` -- a dictionary of the discovery data for this transient. 

33 - ``objectComments`` -- the comments for the object 

34 - ``atelData`` -- the atel matches for the objects displayed on the webpage 

35 - ``lightcurveData`` -- the transient lightcurve data 

36 - ``objectHistories`` -- the object histories 

37 

38 

39 **Return** 

40 

41 - ``ticket_header_bar`` -- the ticket identity bar for the pesssto object 

42 

43 """ 

44 log.debug('starting the ``ticket_header_bar`` function') 

45 

46 ## VARIABLES ## 

47 now = datetime.datetime.now() 

48 currentMagEstimate = discoveryDataDictionary["currentMagnitudeEstimate"] 

49 if currentMagEstimate in [9999, -9999]: 

50 for dataPoint in lightcurveData: 

51 if dataPoint["transientBucketId"] == discoveryDataDictionary["transientBucketId"]: 

52 break 

53 if len(lightcurveData) and (now - dataPoint["observationDate"] < datetime.timedelta(days=7)): 

54 currentMagEstimate = dataPoint["magnitude"] 

55 magWarning = None 

56 if currentMagEstimate and currentMagEstimate > 21.0: 

57 magWarning = _get_magnitude_warning( 

58 log, 

59 currentMag=currentMagEstimate, 

60 transientBucketId=discoveryDataDictionary["transientBucketId"] 

61 ) 

62 

63 if magWarning: 

64 pass 

65 else: 

66 magWarning = "" 

67 

68 # ATEL WARNING IF REQUIRED 

69 atelWarning = None 

70 if discoveryDataDictionary["classifiedFlag"] == 0: 

71 atelWarning = _get_atel_warning( 

72 log, 

73 atelData=atelData, 

74 transientBucketId=discoveryDataDictionary["transientBucketId"] 

75 ) 

76 

77 if atelWarning: 

78 pass 

79 else: 

80 atelWarning = "" 

81 

82 # RESURRECTED OBJECT WARNING 

83 ressurectedWarning = _resurrected_object_warning( 

84 log, 

85 objectHistories, 

86 transientBucketId=discoveryDataDictionary["transientBucketId"] 

87 ) 

88 

89 theseObjectComments = [] 

90 theseObjectComments[:] = [t for t in objectComments if t[ 

91 "pesstoObjectsId"] == discoveryDataDictionary["transientBucketId"]] 

92 

93 latestComment = "" 

94 if len(theseObjectComments): 

95 latestComment = theseObjectComments[0]["comment"].replace("<", "&lt;").replace("&lt;a", "<a").replace( 

96 "&gt;ATEL", ">ATEL").replace("&lt;/a&gt;", "</a>").replace("&lt;/a", "</a").replace("&quot;", '"').replace("&gt;", ">").replace('href=http', 'href="http') 

97 regex = re.compile(r'(href\=\"http[\w\d\.~/:?=]*?)\>', re.S) 

98 latestComment = regex.sub('\g<1>">', latestComment) 

99 # convert bytes to unicode 

100 log.debug("""latestComment: `%(latestComment)s`""" % locals()) 

101 commentDate = theseObjectComments[0]["dateCreated"] 

102 commentAuthor = theseObjectComments[0][ 

103 "commentAuthor"].lower().replace("_", " ").replace(".", " ").title() 

104 commentAuthor = khufu.coloredText( 

105 text="""- %(commentAuthor)s""" % locals(), 

106 color="green", 

107 size=False, # 1-10 

108 pull=False, # "left" | "right" 

109 ) 

110 

111 # FROM DATETIME IMPORT DATETIME, DATE, TIME 

112 now = datetime.datetime.now() 

113 delta = now - commentDate 

114 delta = delta.days 

115 if (delta < 13): 

116 

117 thisDate = times.datetime_relative_to_now(commentDate) 

118 if thisDate[-1:] == "d": 

119 thisDate = thisDate[2:-1] 

120 thisDate = """%(thisDate)s days ago""" % locals() 

121 else: 

122 thisDate = str(commentDate)[:10] 

123 thisDate = thisDate.strip() 

124 if thisDate[:2] == "1 ": 

125 thisDate = thisDate.replace("days", "day") 

126 

127 prefix = khufu.coloredText( 

128 text="<strong>latest comment (%(thisDate)s):</strong>" % locals( 

129 ), 

130 color="blue", 

131 size=False, # 1-10 

132 pull=False, # "left" | "right" 

133 ) 

134 

135 # ADD TEXT COLOR 

136 latestComment = khufu.coloredText( 

137 text=latestComment, 

138 color="cream", 

139 size=False, # 1-10 

140 pull=False, # "left" | "right", 

141 addBackgroundColor=False 

142 ) 

143 

144 latestComment = khufu.well( 

145 wellText="""%(prefix)s %(latestComment)s %(commentAuthor)s""" % locals( 

146 ), 

147 wellSize='small' 

148 ) 

149 

150 # LSQ FORCE PHOTOMETRY WARNING 

151 lsqFPAlert = _get_no_lsq_recalibrated_data_alert( 

152 log=log, 

153 lightcurveData=lightcurveData, 

154 discoveryDataDictionary=discoveryDataDictionary 

155 ) 

156 

157 comment = khufu.grid_row( 

158 responsive=True, 

159 columns="""%(latestComment)s""" % locals(), 

160 htmlId=False, 

161 htmlClass=False, 

162 onPhone=True, 

163 onTablet=True, 

164 onDesktop=True 

165 ) 

166 

167 topbar = khufu.grid_column( 

168 span=12, # 1-12 

169 offset=0, # 1-12 

170 content="""%(ressurectedWarning)s %(magWarning)s %(atelWarning)s %(lsqFPAlert)s %(comment)s """ % locals( 

171 ), 

172 pull=False, # ["right", "left", "center"] 

173 htmlId=False, 

174 htmlClass=False, 

175 onPhone=True, 

176 onTablet=True, 

177 onDesktop=True 

178 ) 

179 

180 topbar = khufu.grid_row( 

181 responsive=True, 

182 columns=topbar, 

183 htmlId=False, 

184 htmlClass="ticketHeader", 

185 onPhone=True, 

186 onTablet=True, 

187 onDesktop=True 

188 ) 

189 

190 return topbar 

191 

192 

193def _get_atel_warning( 

194 log, 

195 atelData, 

196 transientBucketId): 

197 """ get atel warning for ticket 

198 

199 **Key Arguments** 

200 

201 - ``log`` -- logger 

202 - ``atelData`` -- the atel matches for the objects displayed on the webpage 

203 - ``transientBucketId`` -the unquie id for the transient in the marshall database 

204 

205 

206 **Return** 

207 

208 - ``warning`` or None -- the atel warning 

209 

210 """ 

211 log.debug('starting the ``_get_atel_warning`` function') 

212 ## VARIABLES ## 

213 rows = [] 

214 for dataPoint in atelData: 

215 if dataPoint["transientBucketId"] == transientBucketId: 

216 row = dataPoint 

217 rows.append(row) 

218 

219 atelLinks = [] 

220 for row in rows: 

221 thisName = row["name"].replace("atel_", "") 

222 atelLink = khufu.a( 

223 content="""ATel %(thisName)s""" % locals(), 

224 href=row["surveyObjectUrl"], 

225 openInNewTab=True 

226 ) 

227 atelLinks.append(atelLink) 

228 

229 atels = "" 

230 if len(atelLinks): 

231 atels = """this object many have been classified already - see """ 

232 count = 1 

233 for link in atelLinks: 

234 atels = "%(atels)s %(link)s" % locals() 

235 if count != len(atelLinks): 

236 atels = "%(atels)s, " % locals() 

237 count += 1 

238 log.debug('completed the ``_get_atel_warning`` function') 

239 

240 atels = khufu.alert( 

241 alertText=atels, 

242 alertHeading='Warning', 

243 extraPadding=False, 

244 alertLevel='error' 

245 ) 

246 

247 return atels 

248 

249 log.debug('completed the ``_get_atel_warning`` function') 

250 return None 

251 

252 

253def _get_magnitude_warning( 

254 log, 

255 currentMag, 

256 transientBucketId): 

257 """ get atel warning for ticket 

258 

259 **Key Arguments** 

260 

261 - ``log`` -- logger 

262 - ``currentMag`` -- the current magnitude estimate of the object 

263 - ``transientBucketId`` -the unquie id for the transient in the marshall database 

264 

265 

266 **Return** 

267 

268 - ``warning`` or None -- the atel warning 

269 

270 """ 

271 log.debug('starting the ``_get_magnitude_warning`` function') 

272 

273 if currentMag in [9999, -9999]: 

274 text = "not enough data to determine a current magnitude" 

275 alertLevel = 'info' 

276 alertHeading = "Alert" 

277 else: 

278 text = "this object is too faint to take a classification spectrum - please consider archiving it" 

279 alertLevel = "error" 

280 alertHeading = "Warning" 

281 

282 alert = khufu.alert( 

283 alertText=text, 

284 alertHeading=alertHeading, 

285 extraPadding=False, 

286 alertLevel=alertLevel 

287 ) 

288 return alert 

289 

290 log.debug('completed the ``_get_magnitude_warning`` function') 

291 return None 

292 

293 

294def _get_no_lsq_recalibrated_data_alert( 

295 log, 

296 lightcurveData, 

297 discoveryDataDictionary): 

298 """ get no lsq recalibrated data alert 

299 

300 **Key Arguments** 

301 

302 - ``log`` -- logger 

303 - ``discoveryDataDictionary`` -- dictionary of the transient's discovery data 

304 - ``lightcurveData`` -- the transient lightcurve data 

305 

306 

307 **Return** 

308 

309 - ``alert`` -- alert for when LSQ object has no recalibrated data yet 

310 

311 """ 

312 log.debug('starting the ``_get_no_lsq_recalibrated_data_alert`` function') 

313 

314 alert = "" 

315 for row in lightcurveData: 

316 if row["transientBucketId"] == discoveryDataDictionary["transientBucketId"]: 

317 if row["survey"] and row["survey"].lower() == "lsq-discoveries": 

318 name = discoveryDataDictionary["masterName"] 

319 alert = khufu.alert( 

320 alertText='lsq forced photometry not yet available for %(name)s' % locals( 

321 ), 

322 alertHeading='Note', 

323 extraPadding=False, 

324 # [ "warning" | "error" | "success" | "info" ] 

325 alertLevel='info' 

326 ) 

327 break 

328 

329 log.debug('completed the ``_get_no_lsq_recalibrated_data_alert`` function') 

330 return alert 

331 

332 

333def _resurrected_object_warning( 

334 log, 

335 objectHistories, 

336 transientBucketId): 

337 """ get no lsq recalibrated data alert 

338 

339 **Key Arguments** 

340 

341 - ``log`` -- logger 

342 - ``transientBucketId`` -- the transientBucketId 

343 - ``objectHistories`` -- the transient lightcurve data 

344 

345 

346 **Return** 

347 

348 - ``alert`` -- alert for when LSQ object has no recalibrated data yet 

349 

350 """ 

351 log.debug('starting the ``_resurrected_object_warning`` function') 

352 

353 notification = False 

354 for row in objectHistories: 

355 if row["transientBucketId"] == transientBucketId: 

356 if "marshall's object resurrector" in row["log"]: 

357 notification = True 

358 else: 

359 notification = False 

360 

361 if notification is True: 

362 notification = khufu.alert( 

363 alertText='Resurrected from archive - possibly on the rise again', 

364 alertHeading='Note', 

365 extraPadding=False, 

366 # [ "warning" | "error" | "success" | "info" ] 

367 alertLevel='success' 

368 ) 

369 else: 

370 notification = "" 

371 

372 log.debug('completed the ``_resurrected_object_warning`` function') 

373 return notification