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

5 

6:Author: 

7 David Young 

8""" 

9from __future__ import absolute_import 

10from builtins import zip 

11from builtins import object 

12import sys 

13import os 

14import khufu 

15import re 

16from marshall_webapp.models.xmatches_searches import models_xmatches_searches_get 

17from marshall_webapp.models.xmatches_searches.element import models_xmatches_element_searches_get 

18 

19class templates_xmatches_searches(object): 

20 """ 

21 The worker class for the templates_xmatches_searches module 

22 

23 **Key Arguments** 

24 

25 - ``log`` -- logger 

26 - ``request`` -- the pyramid request 

27 - ``elementId`` -- the element id of the resource requested (or false) 

28  

29 """ 

30 

31 def __init__( 

32 self, 

33 log, 

34 request, 

35 elementId=False, 

36 format=False 

37 ): 

38 self.log = log 

39 self.request = request 

40 self.elementId = elementId 

41 # xt-self-arg-tmpx 

42 

43 log.debug( 

44 "instansiating a new 'templates_xmatches_searches' object") 

45 

46 return None 

47 

48 def get(self): 

49 """get the templates_xmatches_searches object 

50 

51 **Return** 

52 

53 - ``webpage`` -- the webpage 

54  

55 """ 

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

57 

58 from marshall_webapp.templates.commonelements.pagetemplates import defaultpagetemplate 

59 

60 if self.elementId == False: 

61 xmatches_searches = models_xmatches_searches_get( 

62 log=self.log, 

63 request=self.request 

64 ) 

65 self.catalogues = xmatches_searches.get() 

66 

67 from marshall_webapp.templates.commonelements.pagetemplates import defaultpagetemplate 

68 

69 # add text color 

70 text = khufu.coloredText( 

71 text="<BR>Click on the <em>associated transient</em> links to reveal matched transient tickets<br><br>", 

72 color="grey", 

73 size=4, # 1-10 

74 pull="right", # "left" | "right", 

75 addBackgroundColor=False 

76 ) 

77 

78 maincontent = text + self.create_table() 

79 

80 webpage = defaultpagetemplate( 

81 log=self.log, 

82 request=self.request, 

83 bodyId=False, 

84 pageTitle="ePESSTO+ Marshall", 

85 topNavBar=False, 

86 sideBar="xmatches", 

87 mainContent=maincontent, 

88 relativePathFromDocRoot=False, 

89 thisPageName="xmatches" 

90 ) 

91 else: 

92 from .templates_resources_transients import templates_resources_transients 

93 webpage = templates_resources_transients( 

94 log=self.log, 

95 request=self.request, 

96 tcsCatalogueId=self.elementId 

97 ).get() 

98 

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

100 return webpage 

101 

102 def create_table( 

103 self): 

104 """create table 

105 

106 **Return** 

107 

108 - ``table`` 

109  

110 """ 

111 self.log.debug('starting the ``create_table`` method') 

112 

113 columnsNames = ["Catalogue", "Sources", "Number Rows", 

114 "Associated Transients", "Top Rank Associated Transients"] 

115 columnsNamesDB = ["table_name", "object_types", "number_of_rows", 

116 "all_transient_associations", "top_ranked_transient_associations"] 

117 

118 for row in self.catalogues: 

119 row["table_name"] = row["table_name"].replace("tcs_cat_", "") 

120 matchObject = re.finditer( 

121 r'.*_(v\d.*)', 

122 row["table_name"], 

123 flags=0 # re.S 

124 ) 

125 for match in matchObject: 

126 replaceMe = match.group(1).replace("_", ".") 

127 row["table_name"] = row["table_name"].replace( 

128 match.group(1), replaceMe) 

129 

130 row["table_name"] = row["table_name"].replace( 

131 "_", " ").replace(" final", "") 

132 

133 for k, v in list(dict(row).items()): 

134 if isinstance(v, float): 

135 row[k] = "{:,.0f}".format(v) 

136 

137 href = self.request.route_path( 

138 'xmatches_element_searches', elementId=row["table_id"]) 

139 row["all_transient_associations"] = khufu.a( 

140 content=row["all_transient_associations"], 

141 href=href 

142 ) 

143 

144 href = self.request.route_path( 

145 'xmatches_element_searches', elementId=row["table_id"], ) 

146 row["all_transient_associations"] = khufu.a( 

147 content=row["all_transient_associations"], 

148 href=href 

149 ) 

150 

151 href = self.request.route_path('xmatches_element_searches', elementId=row[ 

152 "table_id"], _query={'tcsRank': 1}) 

153 row["top_ranked_transient_associations"] = khufu.a( 

154 content=row["top_ranked_transient_associations"], 

155 href=href 

156 ) 

157 

158 import khufu.tables.sortable_table as sortable_table 

159 # build a sortable table 

160 table = sortable_table( 

161 currentPageUrl=self.request.path_qs, 

162 columnsToDisplay=columnsNamesDB, 

163 tableRowsDictionary=self.catalogues, 

164 log=self.log, 

165 defaultSort=False 

166 ) 

167 

168 # REPLACE COLUMN NAMES WITH DISPLAY NAMES (E.G. FOR WHEN MYSQL COLUMN 

169 # NAMES ARE USED) 

170 nd = table.modifyDisplayNameDict 

171 for o, n in zip(columnsNamesDB, columnsNames): 

172 nd[o] = n 

173 

174 table = table.get() 

175 

176 self.log.debug('completed the ``create_table`` method') 

177 return table 

178 

179 # xt-class-method