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_post.py` resource* 

5 

6:Author: 

7 David Young 

8""" 

9from builtins import str 

10from builtins import object 

11import sys 

12import os 

13import khufu 

14from datetime import datetime 

15from marshallEngine.feeders.useradded import data, images 

16 

17 

18class models_transients_post(object): 

19 """ 

20 The worker class for the models_transients_post module 

21 

22 **Key Arguments** 

23 

24 - ``log`` -- logger 

25 - ``request`` -- the pyramid request 

26 - ``elementId`` -- the specific element id requests (or False) 

27 

28 """ 

29 

30 def __init__( 

31 self, 

32 log, 

33 request, 

34 elementId=False 

35 ): 

36 self.log = log 

37 self.request = request 

38 self.elementId = elementId 

39 self.response = "" 

40 self.redirectUrl = "" 

41 # xt-self-arg-tmpx 

42 

43 log.debug("instansiating a new 'models_transients_post' object") 

44 

45 return None 

46 

47 def close(self): 

48 del self 

49 return None 

50 

51 def post(self): 

52 """execute the post method on the models_transients_post object 

53 

54 **Return** 

55 

56 - ``response`` -- the reponse to send to the browser 

57 - ``redirectUrl`` -- the URL to redirect to once the transient has been added 

58 

59 """ 

60 self.log.debug('starting the ``post`` method') 

61 

62 elementId = self.elementId 

63 

64 # CHECK THE QUERY STRING PARAMETERS FOR THE CORRECT VARIABLE REQUIRED TO 

65 # ADD A TRANSIENT 

66 if set(("objectRa", "objectDec", "objectName")) <= set(self.request.params): 

67 self._add_new_transient() 

68 

69 # ADD ALERT IF NOTHING WAS ADDED TO THE DATABASE 

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

71 self.response = "Response from <code>" + \ 

72 __name__ + "</code><BR><BR>No Action Was Performed<BR><BR>" 

73 if elementId: 

74 self.response += "The element selected was </code>%(elementId)s</code>" % locals( 

75 ) 

76 else: 

77 self.response += "Resource Context selected (no element)" % locals( 

78 ) 

79 

80 self.log.debug('completed the ``post`` method') 

81 return self.response, self.redirectUrl 

82 

83 def _add_new_transient( 

84 self): 

85 """ add new transient to the marshall 

86 """ 

87 self.log.debug('starting the ``_add_new_transient`` method') 

88 

89 # ASTROCALC UNIT CONVERTER OBJECT 

90 from astrocalc.coords import unit_conversion 

91 from astrocalc.times import conversions 

92 converter = unit_conversion( 

93 log=self.log 

94 ) 

95 # CONVERTER TO CONVERT MJD TO DATE 

96 timeConverter = conversions( 

97 log=self.log 

98 ) 

99 

100 # UNPACK DICTIONARY VALUES TO PARAMS 

101 params = {} 

102 for arg, val in list(self.request.params.items()): 

103 varname = arg 

104 if isinstance(val, ("".__class__, u"".__class__)): 

105 exec(varname + ' = """%s""" ' % (val,), globals(), params) 

106 else: 

107 exec(varname + " = %s" % (val,), globals(), params) 

108 self.log.debug('%s = %s' % (varname, val,)) 

109 

110 # CONVERT RA AND DEC IF REQUIRED 

111 params["objectRa"] = converter.ra_sexegesimal_to_decimal( 

112 ra=params["objectRa"] 

113 ) 

114 params["objectDec"] = converter.dec_sexegesimal_to_decimal( 

115 dec=params["objectDec"] 

116 ) 

117 

118 # GET DATES AND MJD 

119 if isinstance(params["objectDate"], ("".__class__, u"".__class__)) and "-" in params["objectDate"]: 

120 params["mjd"] = timeConverter.ut_datetime_to_mjd( 

121 utDatetime=params["objectDate"]) 

122 elif isinstance(params["objectDate"], float) or isinstance(params["objectDate"], ("".__class__, u"".__class__)): 

123 params["mjd"] = float(params["objectDate"]) 

124 params["objectDate"] = timeConverter.mjd_to_ut_datetime( 

125 mjd=params["mjd"], 

126 sqlDate=True 

127 ) 

128 

129 params["ticketAuthor"] = self.request.authenticated_userid 

130 

131 # add some default null values 

132 if "objectRedshift" not in params or len(str(params["objectRedshift"])) == 0: 

133 params["objectRedshift"] = "null" 

134 if "objectUrl" not in params: 

135 params["objectUrl"] = "null" 

136 else: 

137 params["objectUrl"] = """ '%(objectUrl)s' """ % params 

138 if "objectImageStamp" not in params: 

139 params["objectImageStamp"] = "null" 

140 else: 

141 params[ 

142 "objectImageStamp"] = """ '%(objectImageStamp)s' """ % params 

143 

144 # now add the new transient to the `fs_user_added` table 

145 sqlQuery = u""" 

146 INSERT IGNORE INTO fs_user_added 

147 (candidateID, 

148 targetImageUrl, 

149 objectURL, 

150 survey, 

151 ra_deg, 

152 dec_deg, 

153 discMag, 

154 mag, 

155 observationMJD, 

156 discDate, 

157 suggestedType, 

158 hostZ, 

159 author, 

160 ingested, 

161 summaryRow, 

162 dateCreated, 

163 dateLastModified) VALUES ('%(objectName)s',%(objectImageStamp)s,%(objectUrl)s,'%(objectSurvey)s','%(objectRa)s','%(objectDec)s','%(objectMagnitude)s','%(objectMagnitude)s','%(mjd)s','%(objectDate)s','SN',%(objectRedshift)s,'%(ticketAuthor)s',0,1,NOW(),NOW())""" % params 

164 self.request.db.execute(sqlQuery) 

165 self.request.db.commit() 

166 self.request.db.commit() 

167 

168 # import the new objects in fs_user_added to transientBucket 

169 dbConn = self.request.registry.settings["dbConn"] 

170 # RECONNECT TO DB IF CONNECTION WAS LOST 

171 dbConn.ping(reconnect=True) 

172 

173 # IMPORT THE DATA AND IMAGES 

174 ingester = data( 

175 log=self.log, 

176 settings=self.request.registry.settings["yaml settings"], 

177 dbConn=dbConn 

178 ).ingest(withinLastDays=False) 

179 

180 cacher = images( 

181 log=self.log, 

182 settings=self.request.registry.settings["yaml settings"], 

183 dbConn=dbConn 

184 ).cache(limit=3000) 

185 

186 # Create the redirect URL based on the name of the new object added 

187 self.redirectUrl = self.request.route_path( 

188 'transients_search', _query={'q': params["objectName"]}) 

189 

190 self.log.debug('completed the ``_add_new_transient`` method') 

191 return None 

192 

193 # xt-class-method