def upload(request): user_file = UserFile(user_file=request.FILES["file"]) user_file.save() content = _getMultipartData(user_file.user_file.path, 0, append_new_line=True) ss_response = _send_request('POST', '/REST.svc/v3/file', { "Accept": "application/json", "Content-Type": _getMultipartContentType(), }, body=content, user=request.user) headers = ss_response.getheaders() response = ss_response.read() ss_id = json.loads(response) ## This is the old File::Parser bit parser_response = _send_request('GET', '/REST.svc/v3/file/%s/parser' % ss_id, { "Accept": "application/json", }, user=request.user) parser_data = parser_response.read() json_values = json.loads(parser_data) json_values["sol_id"] = user_file.id json_values["ss_id"] = ss_id json_response = json.dumps(json_values) return HttpResponse(json_response)
def parser(request, ss_id, sol_id): if request.method == "PUT": json_data = json.loads(request.body) if json_data["delimiter"] == "\\t": json_data["delimiter"] = "\t" json_data["parser"]["delimiter"] = json_data["delimiter"] json_data["parser"]["has_column_headers"] = json_data["has_header"] if not json_data["has_header"]: json_data["columns"] = [] parser_response = _send_request('PUT', '/REST.svc/v3/file/%s/parser' % ss_id, { "Accept": "application/json", "Content-type": "application/json", }, body=json.dumps(json_data), user=UserService().get_user()) else: ## This is the old File::Parser bit parser_response = _send_request('GET', '/REST.svc/v3/file/%s/parser' % ss_id, { "Accept": "application/json", }, user=UserService().get_user()) parser_data = parser_response.read() json_values = json.loads(parser_data) json_values["sol_id"] = sol_id json_values["ss_id"] = ss_id json_response = json.dumps(json_values) return HttpResponse(json_response, content_type="application/json; charset=utf-8")
def set_access(self, accounts, emails, user_from): data = { 'is_shared': False, 'is_public': False, 'authorized_viewers': accounts, } if len(accounts) or len(emails): data['is_shared'] = True schema = self.schema name = self.name url = '/REST.svc/v2/db/dataset/%s/%s/permissions' % (urllib.quote(schema), urllib.quote(name)) _send_request('PUT', url, { "Accept": "application/json", "Content-Type": "application/json", }, body=json.dumps(data), user=schema) stored_dataset, was_created = Dataset.objects.get_or_create(schema = self.schema, name = self.name) DatasetEmailAccess.objects.filter(dataset=stored_dataset, is_active=False, email__in=emails).update(is_active = True) DatasetEmailAccess.objects.filter(dataset=stored_dataset, is_active=True).exclude(email__in=emails).update(is_active = False) for email in emails: res, created = DatasetEmailAccess.objects.get_or_create(dataset=stored_dataset, is_active=True, email=email) if created: self.send_email_notification(res, user_from)
def dataset_permissions(request, schema, table_name): response = _send_request('GET', '/REST.svc/v1/user/%s' % (request.user), { "Accept": "application/json" }) code = response.status content = response.read() person_data = json.loads(content) person_schema = person_data['schema'] if (person_schema != schema): return HttpResponse(status = 403) dataset = Dataset(schema=schema, name=table_name) if request.method == "PUT": json_data = json.loads(request.raw_post_data) accounts = json_data["accounts"] emails = json_data["emails"] dataset.set_access(accounts, emails, request.user) return HttpResponse("") access = dataset.get_all_access() return HttpResponse(json.dumps(access))
def upload(request): user_file = UserFile(user_file=request.FILES["file"]) user_file.save() content = _getMultipartData(user_file.user_file.path, 0, append_new_line=True) # Javerage is just here until we get off yui - the cookies for auth # aren't reliable behind the flash uploader ss_response = _send_request('POST', '/REST.svc/v3/file', { "Accept": "application/json", "Content-Type": _getMultipartContentType(), }, body=content, user="******") headers = ss_response.getheaders() response = ss_response.read() ss_id = json.loads(response) json_response = json.dumps({ "sol_id": user_file.id, "ss_id": ss_id }) return HttpResponse(json_response)
def set_server_access(self, access): schema = self.schema name = self.name url = '/REST.svc/v2/db/dataset/%s/%s/permissions' % (urllib.quote(schema), urllib.quote(name)) response = _send_request('PUT', url, { "Accept": "application/json", "Content-Type": "application/json", }, body=json.dumps(access), user=schema)
def set_access(self, accounts, emails, user_from): data = { 'is_shared': False, 'is_public': False, 'authorized_viewers': accounts, } if len(accounts) or len(emails): data['is_shared'] = True schema = self.schema name = self.name url = '/REST.svc/v2/db/dataset/%s/%s/permissions' % ( urllib.quote(schema), urllib.quote(name)) _send_request('PUT', url, { "Accept": "application/json", "Content-Type": "application/json", }, body=json.dumps(data), user=schema) stored_dataset, was_created = Dataset.objects.get_or_create( schema=self.schema, name=self.name) DatasetEmailAccess.objects.filter( dataset=stored_dataset, is_active=False, email__in=emails).update(is_active=True) DatasetEmailAccess.objects.filter( dataset=stored_dataset, is_active=True).exclude(email__in=emails).update(is_active=False) for email in emails: res, created = DatasetEmailAccess.objects.get_or_create( dataset=stored_dataset, is_active=True, email=email) if created: self.send_email_notification(res, user_from)
def _get_server_data(self): schema = self.schema name = self.name url = '/REST.svc/v2/db/dataset/%s/%s/permissions' % (urllib.quote(schema), urllib.quote(name)) response = _send_request('GET', url, { "Accept": "application/json", "Content-Type": "application/json", }, user=schema) data = json.loads(response.read()) return data
def _generate_credentials(): api_key = hashlib.md5("%s%s%s" % (time.time(), random.randint(0, sys.maxint), os.getpid())).hexdigest() user = UserService().get_user() data = json.dumps({ 'key': api_key }) ss_response = _send_request('PUT','/REST.svc/v1/api_key/%s' % (user), { "Accept": "application/json", "Content-Type": "application/json", }, body=data, user=user) response = HttpResponse(ss_response.read()) response.status_code = ss_response.status return response
def set_server_access(self, access): schema = self.schema name = self.name url = '/REST.svc/v2/db/dataset/%s/%s/permissions' % ( urllib.quote(schema), urllib.quote(name)) response = _send_request('PUT', url, { "Accept": "application/json", "Content-Type": "application/json", }, body=json.dumps(access), user=schema)
def _get_server_data(self): schema = self.schema name = self.name url = '/REST.svc/v2/db/dataset/%s/%s/permissions' % ( urllib.quote(schema), urllib.quote(name)) response = _send_request('GET', url, { "Accept": "application/json", "Content-Type": "application/json", }, user=schema) data = json.loads(response.read()) return data
def proxy(request, path): request_url = '/'+urllib.quote(path) if request.GET: request_url = request_url + "?" for arg in request.GET: request_url = request_url + "%s=%s&" % (urllib.quote(arg), urllib.quote(request.GET[arg])) body = request.read() ss_response = _send_request(request.META['REQUEST_METHOD'], request_url, { "Accept": "application/json", "Content-Type": "application/json", }, body=body, user=UserService().get_user()) headers = ss_response.getheaders() response = HttpResponse(ss_response.read()) for header in headers: name = header[0] value = header[1] if name == "x-powered-by": continue if name == "client-peer": continue if name == "x-aspnet-version": continue if name == "server": continue if name == "transfer-encoding": continue if name == "connection": continue # django if name == "location": response[name] = "/sqlshare/proxy/"+value else: response[name] = value response.status_code = ss_response.status return response
def credentials(request): user = UserService().get_user() if request.META['REQUEST_METHOD'] == 'GET': ss_response = _send_request('GET','/REST.svc/v1/api_key/%s' % (user), { "Accept": "application/json", "Content-Type": "application/json", }, user=user) if ss_response.status == 404: return _generate_credentials() response = HttpResponse(ss_response.read()) return response else: return _generate_credentials()
def stream_upload(request): body = request.read() body_json = json.loads(body) has_error = False ss_id = body_json["ss_id"] user_file = UserFile.objects.get(pk=body_json["sol_id"]) total_chunks = _getChunkCount(user_file) yield '{"total":%s, "progress":"' % total_chunks chunk_count = 1 content = _getMultipartData(user_file.user_file.path, chunk_count) while content is not None: ss_response = _send_request('POST', '/REST.svc/v3/file/%s' % ss_id, { "Accept": "application/json", "Content-Type": _getMultipartContentType(), }, body=content, user=request.user) headers = ss_response.getheaders() response = ss_response.read() if ss_response.status != 200: body = ss_response.read() has_error = True yield '", "error":%s, "code":"%s"}' % (body, ss_response.status) break yield "." chunk_count += 1 content = _getMultipartData(user_file.user_file.path, chunk_count) if not has_error: put_json = {} put_json["parser"] = {} if body_json["parser"]["delimiter"] == "\\t": put_json["parser"]["delimiter"] = "\t" else: put_json["parser"]["delimiter"] = body_json["parser"]["delimiter"] if body_json["parser"]["has_column_headers"]: put_json["parser"]["has_column_headers"] = True else: put_json["parser"]["has_column_headers"] = False if body_json["is_public"]: put_json["is_public"] = True else: put_json["is_public"] = False put_json["table_name"] = body_json["table_name"] put_json["columns"] = body_json["columns"] put_json["dataset_name"] = body_json["dataset_name"] put_json["description"] = body_json["description"] put_json["sample_data"] = None put_response = _send_request("PUT", "/REST.svc/v3/file/%s/database" % ss_id, { "Accept": "application/json", "Content-Type": "application/json", }, body=json.dumps(put_json), user=request.user) if put_response.status != 202: body = put_response.read() yield '","error":%s, "code":"%s"}' % (body, put_response.status) yield '"}'