def _save_resource_file(self, provider, file_): # Load file contents if isinstance(file_, dict): file_name = file_['name'] content = base64.b64decode(file_['data']) else: file_name = file_.name file_.seek(0) content = file_.read() # Check file name if not is_valid_file(file_name): raise ValueError('Invalid file name format: Unsupported character') # Create provider dir for assets if it does not exists provider_dir = os.path.join(settings.MEDIA_ROOT, 'assets', provider) if not os.path.isdir(provider_dir): os.mkdir(provider_dir) file_path = os.path.join(provider_dir, file_name) resource_path = file_path[file_path.index(settings.MEDIA_DIR):] if resource_path.startswith('/'): resource_path = resource_path[1:] # Check if the file already exists if os.path.exists(file_path): res = Resource.objects.get(resource_path=resource_path) if res.product_id is not None: # If the resource has product_id field, it means that a product # spec has been created, so it cannot be overridden raise ConflictError('The provided digital asset file (' + file_name + ') already exists') res.delete() # Create file with open(file_path, "wb") as f: f.write(content) self.rollback_logger['files'].append(file_path) site = settings.SITE return resource_path, url_fix( urljoin(site, '/charging/' + resource_path))
def _save_resource_file(self, provider, file_): # Load file contents if isinstance(file_, dict): file_name = file_['name'] content = base64.b64decode(file_['data']) else: file_name = file_.name file_.seek(0) content = file_.read() # Check file name if not is_valid_file(file_name): raise ValueError('Invalid file name format: Unsupported character') # Create provider dir for assets if it does not exists provider_dir = os.path.join(settings.MEDIA_ROOT, 'assets', provider) if not os.path.isdir(provider_dir): os.mkdir(provider_dir) file_path = os.path.join(provider_dir, file_name) resource_path = file_path[file_path.index(settings.MEDIA_DIR):] if resource_path.startswith('/'): resource_path = resource_path[1:] # Check if the file already exists if os.path.exists(file_path): res = Resource.objects.get(resource_path=resource_path) if len(res.state): # If the resource has state field, it means that a product # spec has been created, so it cannot be overridden raise ConflictError('The provided digital asset (' + file_name + ') already exists') res.delete() # Create file with open(file_path, "wb") as f: f.write(content) self.rollback_logger['files'].append(file_path) return resource_path
def _save_resource_file(provider, name, version, file_): # Load file contents if isinstance(file_, dict): f_name = file_["name"] content = base64.b64decode(file_["data"]) else: f_name = file_.name content = file_.read() # Check file name if not is_valid_file(f_name): raise ValueError("Invalid file name format: Unsupported character") # Create file file_name = provider + "__" + name + "__" + version + "__" + f_name path = os.path.join(settings.MEDIA_ROOT, "resources") file_path = os.path.join(path, file_name) f = open(file_path, "wb") f.write(content) f.close() return settings.MEDIA_URL + "resources/" + file_name
def _save_resource_file(provider, name, version, file_): # Load file contents if isinstance(file_, dict): f_name = file_['name'] content = base64.b64decode(file_['data']) else: f_name = file_.name file_.seek(0) content = file_.read() # Check file name if not is_valid_file(f_name): raise ValueError('Invalid file name format: Unsupported character') # Create file file_name = provider + '__' + name + '__' + version + '__' + f_name path = os.path.join(settings.MEDIA_ROOT, 'resources') file_path = os.path.join(path, file_name) f = open(file_path, "wb") f.write(content) f.close() return settings.MEDIA_URL + 'resources/' + file_name
def register_resource(provider, data, file_=None): # Check if the resource already exists existing = True current_organization = provider.userprofile.current_organization try: Resource.objects.get(name=data['name'], provider=current_organization, version=data['version']) except: existing = False if existing: raise Exception('The resource already exists') if not re.match(re.compile(r'^(?:[1-9]\d*\.|0\.)*(?:[1-9]\d*|0)$'), data['version']): raise Exception('Invalid version format') if not is_valid_id(data['name']): raise Exception('Invalid name format') resource_data = { 'name': data['name'], 'version': data['version'], 'description': data['description'], 'content_type': data['content_type'] } if file_ is None: if 'content' in data: resource = data['content'] # Check file name format if not is_valid_file(resource['name']): raise Exception('Invalid file name format: Unsupported character') #decode the content and save the media file file_name = current_organization.name + '__' + data['name'] + '__' + data['version'] + '__' + resource['name'] path = os.path.join(settings.MEDIA_ROOT, 'resources') file_path = os.path.join(path, file_name) f = open(file_path, "wb") dec = base64.b64decode(resource['data']) f.write(dec) f.close() resource_data['content_path'] = settings.MEDIA_URL + 'resources/' + file_name resource_data['link'] = '' elif 'link' in data: # Add the download link # Check link format if not is_valid_url(data['link']): raise Exception('Invalid Resource link format') resource_data['link'] = data['link'] resource_data['content_path'] = '' else: #decode the content and save the media file # Check file name format if not is_valid_file(file_.name): raise Exception('Invalid file name format: Unsupported character') file_name = current_organization.name + '__' + data['name'] + '__' + data['version'] + '__' + file_.name path = os.path.join(settings.MEDIA_ROOT, 'resources') file_path = os.path.join(path, file_name) f = open(file_path, "wb") f.write(file_.read()) f.close() resource_data['content_path'] = settings.MEDIA_URL + 'resources/' + file_name resource_data['link'] = '' Resource.objects.create( name=resource_data['name'], provider=current_organization, version=resource_data['version'], description=resource_data['description'], download_link=resource_data['link'], resource_path=resource_data['content_path'], content_type=resource_data['content_type'], state='created' )