def post(self, request): user_id = self.request.user.id postdata = json.loads(request.body) # logger.debug("BundleSearch: user_id=%s; search_string=%s.", user_id, search_string) service = BundleService(self.request.user) try: #TODO CHECKING command = postdata['data'][-1].strip("'") items = postdata['data'][:-1] targets = {} for item in items: (key, target) = item.split(':', 1) if key == '': key = target # Set default key to be same as target targets[key] = [target, ''] # TODO PATH # targets[key] = self.parse_target(client, worksheet_uuid, target) new_bundle_uuid = service.derive_bundle('run', targets, postdata['worksheet_uuid'], command) return Response({'uuid': new_bundle_uuid}, content_type="application/json") except Exception as e: logging.error(self.__str__()) logging.error(smart_str(e)) logging.error('') logging.debug('-------------------------') tb = traceback.format_exc() logging.error(tb) logging.debug('-------------------------') return Response(status=service.http_status_from_exception(e))
def get_context_data(self, **kwargs): context = super(BundleListView, self).get_context_data(**kwargs) service = BundleService() results = service.items() context["bundles"] = results bundles = results items = [] for bundle in bundles: item = { "uuid": bundle["uuid"], "details_url": "/bundles/{0}".format(bundle["uuid"]), "name": "", "title": "<title not specified>", "creator": "<creator not specified>", "description": "<description not specified>", } if "metadata" in bundle: metadata = bundle["metadata"] for (key1, key2) in [("title", "name"), ("creator", None), ("description", None)]: if key2 is None: key2 = key1 if key2 in metadata: item[key1] = metadata[key2] items.append(item) context["items"] = items context["items_label"] = "bundles" return context
def get_context_data(self, **kwargs): context = super(BundleDetailView, self).get_context_data(**kwargs) uuid = kwargs.get("uuid") service = BundleService() results = service.item(uuid) context["bundle"] = results return context
def post(self, request, uuid): user = self.request.user if not user.id: return Response(None, status=401) data = json.loads(request.body) worksheet_name = data['name'] worksheet_uuid = data['uuid'] owner_id = data['owner_id'] lines = data['lines'] if not (worksheet_uuid == uuid): return Response(None, status=403) logger.debug("WorksheetUpdate: owner=%s; name=%s; uuid=%s", owner_id, worksheet_name, uuid) service = BundleService(self.request.user) try: service.parse_and_update_worksheet(worksheet_uuid, lines) return Response({}) except Exception as e: logging.error(self.__str__()) logging.error('ERROR') logging.error(smart_str(e)) logging.error('-------------------------') tb = traceback.format_exc() logging.error(tb) logging.error('-------------------------') return Response({'error': smart_str(e)})
def get(self, request, uuid): user_id = self.request.user.id logger.debug("BundleInfo: user_id=%s; uuid=%s.", user_id, uuid) service = BundleService(self.request.user) try: bundle_info = service.get_bundle_info(uuid) target = (uuid, '') info = service.get_target_info(target, 2) # 2 is the depth to retrieve bundle_info['stdout'] = None bundle_info['stderr'] = None #if we have std out or err update it. contents = info.get('contents') if contents: for item in contents: if item['name'] in ['stdout', 'stderr']: lines = service.head_target((uuid, item['name']), 100) if lines: lines = ' '.join(lines) bundle_info[item['name']] = lines bundle_info['edit_permission'] = False if bundle_info['owner_id'] == str(self.request.user.id): bundle_info['edit_permission'] = True return Response(bundle_info, content_type="application/json") except Exception as e: logging.error(self.__str__()) logging.error(smart_str(e)) logging.error('') logging.debug('-------------------------') tb = traceback.format_exc() logging.error(tb) logging.debug('-------------------------') return Response(status=service.http_status_from_exception(e))
def get(self, request, uuid, path=''): user_id = self.request.user.id logger.debug("BundleContent: user_id=%s; uuid=%s; path=%s.", user_id, uuid, path) service = BundleService(self.request.user) try: target = (uuid, path) info = service.get_target_info(target, 2) # 2 is the depth to retrieve info['stdout'] = None info['stderr'] = None #if we have std out or err update it. contents = info.get('contents') if contents: contents = sorted(contents, key=lambda r: r['name']) for item in contents: if item['name'] in ['stdout', 'stderr']: lines = service.head_target((uuid, item['name']), 100) if lines: import base64 lines = ' '.join(map(base64.b64decode, lines)) info[item['name']] = lines return Response(info) except Exception as e: tb = traceback.format_exc() log_exception(self, e, tb) return Response({'error': smart_str(e)})
def get(self, request): user_id = self.request.user.id logger.debug("WorksheetsListApi: user_id=%s.", user_id) service = BundleService(self.request.user) try: worksheets = service.worksheets() user_ids = [] user_id_to_worksheets = {} for worksheet in worksheets: owner_id = worksheet["owner_id"] if owner_id in user_id_to_worksheets: user_id_to_worksheets[owner_id].append(worksheet) else: user_id_to_worksheets[owner_id] = [worksheet] user_ids.append(owner_id) if len(user_ids) > 0: users = ClUser.objects.filter(id__in=user_ids) for user in users: for worksheet in user_id_to_worksheets[user.id]: worksheet["owner"] = user.username return Response(worksheets) except Exception as e: logging.error(self.__str__()) logging.error(smart_str(e)) logging.error("") logging.debug("-------------------------") tb = traceback.format_exc() logging.error(tb) logging.debug("-------------------------") return Response(status=service.http_status_from_exception(e))
def get(self, request, uuid, path=''): user_id = self.request.user.id logger.debug("BundleContent: user_id=%s; uuid=%s; path=%s.", user_id, uuid, path) service = BundleService(self.request.user) try: target = (uuid, path) info = service.get_target_info(target, 2) # 2 is the depth to retrieve info['stdout'] = None info['stderr'] = None #if we have std out or err update it. contents = info.get('contents') if contents: for item in contents: if item['name'] in ['stdout', 'stderr']: lines = service.head_target((uuid, item['name']), 100) if lines: lines = ' '.join(lines) info[item['name']] = lines return Response(info) except Exception as e: logging.error(self.__str__()) logging.error(smart_str(e)) logging.error('') logging.debug('-------------------------') tb = traceback.format_exc() logging.error(tb) logging.debug('-------------------------') return Response({'error': smart_str(e)})
def get(self, request, uuid): user_id = self.request.user.id logger.debug("WorksheetContent: user_id=%s; uuid=%s.", user_id, uuid) service = BundleService(self.request.user) try: worksheet = service.worksheet(uuid, interpreted=True) owner = ClUser.objects.filter(id=worksheet["owner_id"]) # if owner: # owner = owner[0] # else: # pass # TODO throw error # TODO assign owner. # worksheet['owner'] = owner.username return Response(worksheet) except Exception as e: logging.error(self.__str__()) logging.error(smart_str(e)) logging.error("") logging.debug("-------------------------") tb = traceback.format_exc() logging.error(tb) logging.debug("-------------------------") return Response(status=service.http_status_from_exception(e))
def get(self, request, uuid): user_id = self.request.user.id logger.debug("BundleInfo: user_id=%s; uuid=%s.", user_id, uuid) service = BundleService(self.request.user) try: bundle_info = service.get_bundle_info(uuid) target = (uuid, '') info = service.get_target_info(target, 2) # 2 is the depth to retrieve bundle_info['stdout'] = None bundle_info['stderr'] = None #if we have std out or err update it. contents = info.get('contents') if contents: for item in contents: if item['name'] in ['stdout', 'stderr']: lines = service.head_target((uuid, item['name']), 100) if lines: import base64 lines = ' '.join(map(base64.b64decode, lines)) bundle_info[item['name']] = lines bundle_info['edit_permission'] = False if bundle_info['owner_id'] == str(self.request.user.id): bundle_info['edit_permission'] = True return Response(bundle_info, content_type="application/json") except Exception as e: tb = traceback.format_exc() log_exception(self, e, tb) return Response({"error": smart_str(e)}, status=500)
def get_context_data(self, **kwargs): context = super(BundleDetailView, self).get_context_data(**kwargs) uuid = kwargs.get('uuid') service = BundleService(self.request.user) results = service.item(uuid) context['bundle'] = results return context
def get_context_data(self, **kwargs): context = super(BundleListView, self).get_context_data(**kwargs) service = BundleService(self.request.user) results = service.items() context['bundles'] = results bundles = results items = [] for bundle in bundles: item = {'uuid': bundle['uuid'], 'details_url': '/bundles/{0}'.format(bundle['uuid']), 'name': '', 'title': '<title not specified>', 'creator': '<creator not specified>', 'description': '<description not specified>'} if 'metadata' in bundle: metadata = bundle['metadata'] for (key1, key2) in [('title', 'name'), ('creator', None), ('description', None)]: if key2 is None: key2 = key1 if key2 in metadata: item[key1] = metadata[key2] items.append(item) context['items'] = items context['items_label'] = 'bundles' return context
def get(self, request): user_id = self.request.user.id logger.debug("WorksheetsListApi: user_id=%s.", user_id) service = BundleService(self.request.user) try: worksheets = service.worksheets() user_ids = [] user_id_to_worksheets = {} for worksheet in worksheets: owner_id = worksheet['owner_id'] if owner_id in user_id_to_worksheets: user_id_to_worksheets[owner_id].append(worksheet) else: user_id_to_worksheets[owner_id] = [worksheet] user_ids.append(owner_id) if len(user_ids) > 0: users = ClUser.objects.filter(id__in=user_ids) for user in users: for worksheet in user_id_to_worksheets[str(user.id)]: worksheet['owner'] = user.username return Response(worksheets) except Exception as e: tb = traceback.format_exc() log_exception(self, e, tb) return Response({"error": smart_str(e)}, status=500)
def get_context_data(self, **kwargs): context = super(BundleDetailView, self).get_context_data(**kwargs) uuid = kwargs.get('uuid') service = BundleService(self.request.user) bundle_info = service.get_bundle_info(uuid) context['bundle'] = bundle_info return context
def get(self, request, uuid, path): user_id = self.request.user.id service = BundleService() try: content_type = BundleFileContentApi._content_type(path) return StreamingHttpResponse(service.read_file(uuid, path), content_type=content_type) except Exception as e: return Response(status=service.http_status_from_exception(e))
def get(self, request, uuid, path): user_id = self.request.user.id service = BundleService(self.request.user) try: content_type = BundleFileContentApi._content_type(path) return StreamingHttpResponse(service.read_file(uuid, path), content_type=content_type) except Exception as e: return Response(status=service.http_status_from_exception(e))
def get(self, request, uuid): user_id = self.request.user.id logger.debug("BundleInfo: user_id=%s; uuid=%s.", user_id, uuid) service = BundleService(self.request.user) try: item = service.item(uuid) return Response(item, content_type="application/json") except Exception as e: return Response(status=service.http_status_from_exception(e))
def get(self, request, uuid, path): user_id = self.request.user.id logger.debug("BundleContent: user_id=%s; uuid=%s; path=%s.", user_id, uuid, path) service = BundleService(self.request.user) try: items = service.ls(uuid, path) return Response(items) except Exception as e: return Response(status=service.http_status_from_exception(e))
def BundleDownload(request, uuid): service = BundleService(request.user) local_path, temp_path = service.download_target(uuid, return_zip=True) item = service.get_bundle_info(uuid) response = StreamingHttpResponse(service.read_file(uuid, local_path), content_type="zip") response['Content-Disposition'] = 'attachment; filename="%s.zip"' % item['metadata']['name'] return response
def get(self, request): user_id = self.request.user.id logger.debug("WorksheetsListApi: user_id=%s.", user_id) service = BundleService() try: worksheets = service.worksheets() return Response(worksheets) except Exception as e: return Response(status=service.http_status_from_exception(e))
def get(self, request, uuid): user_id = self.request.user.id logger.debug("WorksheetContent: user_id=%s; uuid=%s.", user_id, uuid) service = BundleService() try: worksheet = service.worksheet(uuid) return Response(worksheet) except Exception as e: return Response(status=service.http_status_from_exception(e))
def get(self, request, uuid): user_id = self.request.user.id logger.debug("BundleInfo: user_id=%s; uuid=%s.", user_id, uuid) service = BundleService() try: item = service.item(uuid) return Response(item, content_type="application/json") except Exception as e: return Response(status=service.http_status_from_exception(e))
def get(self, request, uuid, path): user_id = self.request.user.id logger.debug("BundleContent: user_id=%s; uuid=%s; path=%s.", user_id, uuid, path) service = BundleService() try: items = service.ls(uuid, path) return Response(items) except Exception as e: return Response(status=service.http_status_from_exception(e))
def BundleDownload(request, uuid): """ Return a stream with the contents of the bundle (zip file if necessary). This is the same code as BundleFileContentApi. """ service = BundleService(request.user) stream, name, content_type = service.read_target((uuid, "")) response = StreamingHttpResponse(stream, content_type=content_type) response["Content-Disposition"] = 'filename="%s"' % name return response
def get_context_data(self, **kwargs): context = super(BundleDetailView, self).get_context_data(**kwargs) uuid = kwargs.get("uuid") service = BundleService(self.request.user) bundle_info = service.get_bundle_info(uuid) if bundle_info: context["bundle"] = bundle_info context["bundle_title"] = bundle_info.get("metadata", {}).get("name", "") else: context["error"] = "Invalid or inaccessible bundle uuid: " + uuid return context
def get(self, request, uuid, path): service = BundleService(self.request.user) try: stream, name, content_type = service.read_target((uuid, path)) response = StreamingHttpResponse(stream, content_type=content_type) response['Content-Disposition'] = 'filename="%s"' % name return response except Exception as e: tb = traceback.format_exc() log_exception(self, e, tb) return Response({"error": smart_str(e)}, status=500)
def get(self, request): user_id = self.request.user.id logger.debug("WorksheetsListApi: user_id=%s.", user_id) service = BundleService(self.request.user) try: worksheets = service.worksheets() return Response(worksheets) except Exception as e: tb = traceback.format_exc() log_exception(self, e, tb) return Response({"error": smart_str(e)}, status=500)
def get(self, request, uuid): user_id = self.request.user.id logger.debug("WorksheetContent: user_id=%s; uuid=%s.", user_id, uuid) service = BundleService(self.request.user) try: worksheet = service.worksheet(uuid) owner = ClUser.objects.filter(id=worksheet['owner_id'])[0] worksheet['owner'] = owner.username return Response(worksheet) except Exception as e: return Response(status=service.http_status_from_exception(e))
def get(self, request, uuid): user_id = self.request.user.id logger.debug("WorksheetContent: user_id=%s; uuid=%s.", user_id, uuid) service = BundleService(self.request.user) try: worksheet = service.full_worksheet(uuid) return Response(worksheet) except Exception as e: tb = traceback.format_exc() log_exception(self, e, tb) return Response({"error": smart_str(e)}, status=500)
def get(self, request): user_id = self.request.user.id search_string = request.GET.get('spec', '') logger.debug("WorksheetsGetUUIDApi: user_id=%s; spec=%s.", user_id, search_string) service = BundleService(self.request.user) try: worksheet_uuid = service.get_worksheet_uuid(search_string) return Response({'uuid': worksheet_uuid}, content_type="application/json") except Exception as e: tb = traceback.format_exc() log_exception(self, e, tb) return Response({"error": smart_str(e)}, status=500)
def get(self, request): user_id = self.request.user.id worksheet_uuid = request.GET.get('worksheet_uuid', '') logger.debug("WorksheetsGetBundleListApi: user_id=%s; worksheet_uuid=%s.", user_id, worksheet_uuid) service = BundleService(self.request.user) try: bundle_list = service.get_worksheet_bundles(worksheet_uuid) return Response({'bundles': bundle_list}, content_type="application/json") except Exception as e: tb = traceback.format_exc() log_exception(self, e, tb) return Response({"error": smart_str(e)}, status=500)
def get(self, request, uuid, path=''): user_id = self.request.user.id logger.debug("BundleContent: user_id=%s; uuid=%s; path=%s.", user_id, uuid, path) service = BundleService(self.request.user) try: target = (uuid, path) info = service.get_target_info(target) return Response(info) except Exception as e: tb = traceback.format_exc() log_exception(self, e, tb) return Response({'error': smart_str(e)})
def get(self, request, uuid): user_id = self.request.user.id logger.debug("BundleInfo: user_id=%s; uuid=%s.", user_id, uuid) service = BundleService(self.request.user) try: bundle_info = service.get_bundle_info(uuid) bundle_info.update(service.get_bundle_contents(uuid)) return Response(bundle_info, content_type="application/json") except Exception as e: tb = traceback.format_exc() log_exception(self, e, tb) return Response({"error": smart_str(e)}, status=500)
def recent_worksheets(request_user, limit=3): """Used for worksheets in competitions. Issue 1014""" service = BundleService(request_user) unsorted_worksheets = service.worksheets() #if not worksheets: # return worksheets # just incase the list is empty sorted_worksheets = sorted(unsorted_worksheets, key=lambda k: k['id'], reverse=True) worksheets = [(val['uuid'], val['name'], val['owner_name']) for val in sorted_worksheets] return worksheets[0:limit]
def post(self, request): user_id = self.request.user.id service = BundleService(self.request.user) try: source_file = request.FILES['file'] bundle_type = request.POST['bundle_type'] worksheet_uuid = request.POST['worksheet_uuid'] new_bundle_uuid = service.upload_bundle(source_file, bundle_type, worksheet_uuid) return Response({'uuid': new_bundle_uuid}, content_type="application/json") except Exception as e: tb = traceback.format_exc() log_exception(self, e, tb) return Response({"error": smart_str(e)}, status=500)
def get(self, request, uuid, path): # user_id = self.request.user.id service = BundleService(self.request.user) try: content_type, _encoding = mimetypes.guess_type(path) if not content_type: content_type = 'text/plain' return StreamingHttpResponse(service.read_file(uuid, path), content_type=content_type) except Exception as e: tb = traceback.format_exc() log_exception(self, e, tb) return Response({"error": smart_str(e)}, status=500)
def get(self, request): user_id = self.request.user.id bundle_spec = request.GET.get('spec', '') worksheet_uuid = request.GET.get('worksheet_uuid', None) logger.debug("BundleGetUUIDApi: user_id=%s; spec=%s. worksheet_uuid=%s", user_id, bundle_spec, worksheet_uuid) service = BundleService(self.request.user) try: bundle_uuid = service.get_bundle_uuid(bundle_spec, worksheet_uuid) return Response({'uuid': bundle_uuid}, content_type="application/json") except Exception as e: tb = traceback.format_exc() log_exception(self, e, tb) return Response({"error": smart_str(e)}, status=500)
def get(self, request): user_id = self.request.user.id search_string = request.GET.get('search_string', '') logger.debug("WorksheetSearch: user_id=%s; search_string=%s.", user_id, search_string) service = BundleService(self.request.user) try: worksheet_infos = service.search_worksheets( search_string.split(' ')) return Response(worksheet_infos, content_type="application/json") except Exception as e: tb = traceback.format_exc() log_exception(self, e, tb) return Response({"error": smart_str(e)}, status=500)
def post(self, request): user_id = self.request.user.id postdata = json.loads(request.body) # logger.debug("BundleSearch: user_id=%s; search_string=%s.", user_id, search_string) service = BundleService(self.request.user) try: #TODO CHECKING info = {} new_bundle_uuid = service.upload_bundle_url( postdata['url'], info, postdata['worksheet_uuid']) return Response({'uuid': new_bundle_uuid}, content_type="application/json") except Exception as e: tb = traceback.format_exc() log_exception(self, e, tb) return Response({"error": smart_str(e)}, status=500)
def post(self, request): user = self.request.user data = json.loads(request.body) if not data.get('worksheet_uuid', None) or not data.get('command', None): return Response("Must have worksheet uuid and command", status=status.HTTP_400_BAD_REQUEST) service = BundleService(self.request.user) try: data = service.general_command(data['worksheet_uuid'], data['command']) return Response({ 'success': True, 'data': data }) except Exception as e: tb = traceback.format_exc() log_exception(self, e, tb) return Response({"error": smart_str(e)}, status=500)
def post(self, request): user_id = self.request.user.id postdata = json.loads(request.body) # logger.debug("BundleSearch: user_id=%s; search_string=%s.", user_id, search_string) service = BundleService(self.request.user) try: #TODO CHECKING command = postdata['data'][-1].strip("'") items = postdata['data'][:-1] items.append(command) new_bundle_uuid = service.create_run_bundle(items, postdata['worksheet_uuid']) return Response({'uuid': new_bundle_uuid}, content_type="application/json") except Exception as e: tb = traceback.format_exc() log_exception(self, e, tb) return Response({"error": smart_str(e)}, status=500)
def post(self, request): owner = self.request.user if not owner.id: return Response(None, status=401) data = json.loads(request.body) worksheet_name = data['name'] if 'name' in data else '' logger.debug("WorksheetCreation: owner=%s; name=%s", owner.id, worksheet_name) if len(worksheet_name) <= 0: return Response("Invalid name.", status=status.HTTP_400_BAD_REQUEST) service = BundleService(self.request.user) try: data["uuid"] = service.create_worksheet(worksheet_name) logger.debug("WorksheetCreation def: owner=%s; name=%s; uuid", owner.id, data["uuid"]) return Response(data) except Exception as e: return Response(status=service.http_status_from_exception(e))
def get(self, request, uuid): user_id = self.request.user.id logger.debug("WorksheetContent: user_id=%s; uuid=%s.", user_id, uuid) service = BundleService(self.request.user) try: worksheet = service.worksheet(uuid, interpreted=True) owner = ClUser.objects.filter(id=worksheet['owner_id']) if owner: owner = owner[0] worksheet['owner'] = owner.username else: worksheet['owner'] = None return Response(worksheet) except Exception as e: tb = traceback.format_exc() log_exception(self, e, tb) return Response({"error": smart_str(e)}, status=500)
def get(self, request): user_id = self.request.user.id search_string = request.GET.get('search_string', '') worksheet_uuid = request.GET.get( 'worksheet_uuid', None) #if you want to filter it down to worksheet logger.debug("BundleSearch: user_id=%s; search_string=%s.", user_id, search_string) service = BundleService(self.request.user) try: bundle_infos = service.search_bundles(search_string.split(' '), worksheet_uuid) return Response(bundle_infos, content_type="application/json") except Exception as e: tb = traceback.format_exc() log_exception(self, e, tb) return Response({"error": smart_str(e)}, status=500)
def post(self, request): user = self.request.user data = json.loads(request.body) service = BundleService(self.request.user) if data.get('raw_command', None): data['command'] = service.get_command(data['raw_command']) if not data.get('worksheet_uuid', None) or not data.get( 'command', None): return Response("Must have worksheet uuid and command", status=status.HTTP_400_BAD_REQUEST) service = BundleService(self.request.user) # If 'autocomplete' field is set, return a list of completions instead if data.get('autocomplete', False): return Response({ 'completions': service.complete_command(data['worksheet_uuid'], data['command']) }) result = service.general_command(data['worksheet_uuid'], data['command']) if result['exception'] is None: return Response({ 'success': True, 'data': result, 'input_data': data }) else: return Response(result['exception'], status=500)