|
1 | 1 | import copy |
| 2 | +import datetime |
2 | 3 | import json |
3 | 4 | import logging |
4 | 5 | import time |
@@ -209,3 +210,62 @@ def download_file_anon(request, path): |
209 | 210 | logger.exception('Failed to preview file', extra={'path': path}) |
210 | 211 | return HttpResponseBadRequest('Failed to preview file', |
211 | 212 | content_type='application/json') |
| 213 | + |
| 214 | + |
| 215 | +def search(request): |
| 216 | + search_term = request.GET.get('search_term') |
| 217 | + query={ |
| 218 | + "all": [ |
| 219 | + {"type": "path", |
| 220 | + "args": {"prefix": "/iplant/home/shared"} |
| 221 | + } |
| 222 | + ], |
| 223 | + "any": [ |
| 224 | + {"type": "label", |
| 225 | + "args": {"exact": False, "label": search_term} |
| 226 | + }, |
| 227 | + {"type": "metadata", |
| 228 | + "args": {"value": search_term} |
| 229 | + } |
| 230 | + ] |
| 231 | + } |
| 232 | + |
| 233 | + tc = TerrainClient('anonymous', 'anonymous@cyverse.org') |
| 234 | + list_resp = tc.search(query) |
| 235 | + files=[] |
| 236 | + folders=[] |
| 237 | + matches=[] |
| 238 | + |
| 239 | + for item in list_resp['hits']: |
| 240 | + if item['_type'] == 'file': |
| 241 | + files.append({ |
| 242 | + 'path': item['_source']['path'].lstrip('/'), |
| 243 | + 'label': item['_source']['label'], |
| 244 | + 'filesize': item['_source']['fileSize'], |
| 245 | + 'dateCreated': datetime.datetime.fromtimestamp(item['_source']['dateCreated']/1000.0), |
| 246 | + 'dateModified': datetime.datetime.fromtimestamp(item['_source']['dateModified']/1000.0), |
| 247 | + }) |
| 248 | + matches.append({ |
| 249 | + 'path': item['_source']['path'].lstrip('/'), |
| 250 | + 'label': item['_source']['label'], |
| 251 | + 'filesize': item['_source']['fileSize'], |
| 252 | + 'dateCreated': datetime.datetime.fromtimestamp(item['_source']['dateCreated']/1000.0), |
| 253 | + 'dateModified': datetime.datetime.fromtimestamp(item['_source']['dateModified']/1000.0), |
| 254 | + 'type': item['_type'] |
| 255 | + }) |
| 256 | + |
| 257 | + elif item['_type'] == 'folder': |
| 258 | + folders.append({ |
| 259 | + 'path': item['_source']['path'].lstrip('/'), |
| 260 | + 'label': item['_source']['label'], |
| 261 | + 'dateCreated': datetime.datetime.fromtimestamp(item['_source']['dateCreated']/1000.0), |
| 262 | + 'dateModified': datetime.datetime.fromtimestamp(item['_source']['dateModified']/1000.0), |
| 263 | + }) |
| 264 | + matches.append({ |
| 265 | + 'path': item['_source']['path'].lstrip('/'), |
| 266 | + 'label': item['_source']['label'], |
| 267 | + 'dateCreated': datetime.datetime.fromtimestamp(item['_source']['dateCreated']/1000.0), |
| 268 | + 'dateModified': datetime.datetime.fromtimestamp(item['_source']['dateModified']/1000.0), |
| 269 | + 'type': item['_type'] |
| 270 | + }) |
| 271 | + return render(request, 'sra/search.html',{'folders':folders, 'files':files, 'matches':matches, 'search_term':search_term}) |
0 commit comments