def create(self, request, *args, **kwargs): if self.request.user.is_anonymous: raise exceptions.NotAuthenticated() itask_data = { 'library': request.POST.get('library') not in ['false', False], # NOTE: 'filename' here comes from 'name' (!) in the POST data 'filename': request.POST.get('name', None), 'destination': request.POST.get('destination', None), } if 'base64Encoded' in request.POST: encoded_str = request.POST['base64Encoded'] encoded_substr = encoded_str[encoded_str.index('base64') + 7:] itask_data['base64Encoded'] = encoded_substr elif 'file' in request.data: encoded_xls = to_str(base64.b64encode(request.data['file'].read())) itask_data['base64Encoded'] = encoded_xls if 'filename' not in itask_data: itask_data['filename'] = request.data['file'].name elif 'url' in request.POST: itask_data['single_xls_url'] = request.POST['url'] import_task = ImportTask.objects.create(user=request.user, data=itask_data) # Have Celery run the import in the background import_in_background.delay(import_task_uid=import_task.uid) return Response( { 'uid': import_task.uid, 'url': reverse('api_v2:importtask-detail', kwargs={'uid': import_task.uid}, request=request), 'status': ImportTask.PROCESSING, }, status.HTTP_201_CREATED)
def test_import_asset_base64_xls(self): encoded_xls = base64.b64encode(self.asset.to_xls_io().read()) task_data = { 'base64Encoded': 'base64:{}'.format(to_str(encoded_xls)), 'name': 'I was imported via base64-encoded XLS!', } self._post_import_task_and_compare_created_asset_to_source(task_data, self.asset)
def kludgy_is_xml_equal(*args): """ Compare strings after removing newlines and whitespace between tags. Returns True if all strings are equal after this manipulation """ xml_strings = list(args) for i, xml in enumerate(xml_strings): xml = to_str(xml).replace('\n', '') xml = re.sub(r'>\s+<', '><', xml) xml_strings[i] = xml return len(set(xml_strings)) == 1
def _run_task(self, messages): self.status = self.PROCESSING self.save(update_fields=['status']) dest_item = dest_kls = has_necessary_perm = False if 'destination' in self.data and self.data['destination']: _d = self.data.get('destination') (dest_kls, dest_item) = _resolve_url_to_asset_or_collection(_d) necessary_perm = 'change_%s' % dest_kls if not dest_item.has_perm(self.user, necessary_perm): raise exceptions.PermissionDenied('user cannot update %s' % dest_kls) else: has_necessary_perm = True if 'url' in self.data: self._load_assets_from_url( messages=messages, url=self.data.get('url'), destination=dest_item, destination_kls=dest_kls, has_necessary_perm=has_necessary_perm, ) return if 'single_xls_url' in self.data: # TODO: merge with `url` handling above; currently kept separate # because `_load_assets_from_url()` uses complex logic to deal with # multiple XLS files in a directory structure within a ZIP archive response = requests.get(self.data['single_xls_url']) response.raise_for_status() encoded_xls = to_str(base64.b64encode(response.content)) self.data['base64Encoded'] = encoded_xls if 'base64Encoded' in self.data: self._parse_b64_upload( base64_encoded_upload=self.data['base64Encoded'], filename=self.data.get('filename', None), messages=messages, library=self.data.get('library', False), destination=dest_item, destination_kls=dest_kls, has_necessary_perm=has_necessary_perm, ) return raise Exception( 'ImportTask data must contain `base64Encoded`, `url`, or ' '`single_xls_url`')
def _run_task(self, messages): self.status = self.PROCESSING self.save(update_fields=['status']) dest_item = has_necessary_perm = False if 'destination' in self.data and self.data['destination']: _d = self.data.get('destination') dest_item = _resolve_url_to_asset(_d) if not dest_item.has_perm(self.user, PERM_CHANGE_ASSET): raise exceptions.PermissionDenied('user cannot update asset') else: has_necessary_perm = True if 'url' in self.data: # Retrieve file name from URL self._load_assets_from_url( messages=messages, url=self.data.get('url'), destination=dest_item, has_necessary_perm=has_necessary_perm, ) return # Get filename try: filename = self.data['filename'] except KeyError: filename = None if 'single_xls_url' in self.data: # Retrieve file name from URL # TODO: merge with `url` handling above; currently kept separate # because `_load_assets_from_url()` uses complex logic to deal with # multiple XLS files in a directory structure within a ZIP archive response = requests.get(self.data['single_xls_url']) response.raise_for_status() encoded_xls = to_str(base64.b64encode(response.content)) # if filename is empty or None, try to retrieve # file name from the response headers if not filename: filename_from_header = parse_options_header( response.headers['Content-Disposition']) try: filename = filename_from_header[1]['filename'] except (TypeError, IndexError, KeyError): pass self.data['base64Encoded'] = encoded_xls if 'base64Encoded' in self.data: # When a file is uploaded as base64, # no name is provided in the encoded string # We should rely on self.data.get(:filename:) self._parse_b64_upload( base64_encoded_upload=self.data['base64Encoded'], filename=filename, messages=messages, library=self.data.get('library', False), destination=dest_item, has_necessary_perm=has_necessary_perm, ) return raise Exception( 'ImportTask data must contain `base64Encoded`, `url`, or ' '`single_xls_url`')
def get_asset_file_content(self, url): response = self.client.get(url) streaming_content = [ to_str(chunk) for chunk in response.streaming_content ] return ''.join(streaming_content)
def _split_formpack_csv(line, sep=";", quote='"'): return [field.strip(quote) for field in to_str(line).split(sep)]
def remove_whitespace(str_): return re.sub(r'>\s+<', '><', to_str(str_))
def test_no_intercom_for_non_matching_user(self): self.assertTrue( self.client.login(username='******', password='******')) response = self.client.get(reverse('kpi-root')) lines = [line.strip() for line in to_str(response.content).split('\n')] self.assertFalse("window.IntercomAppId = 'arm&leg';" in lines)