def createFromKylo(self, request, *args, **kwargs): try: data = kwargs.get('data') data = convert_to_string(data) if 'datasource_details' in data: data['input_file'] = None if "datasetname" in data['datasource_details']: datasource_details = json.loads(data['datasource_details']) data['name'] = datasource_details['datasetname'] else: data['name'] = data.get( 'name', data.get('datasource_type', "H") + "_" + str(random.randint(1000000, 10000000))) # question: why to use user.id when it can take, id, pk, object. # answer: I tried. Sighhh but it gave this error "Incorrect type. Expected pk value, received User." data['created_by'] = request['user'] serializer = DatasetSerializer(data=data, context={"request": request}) if serializer.is_valid(): dataset_object = serializer.save() dataset_object.create() return Response(serializer.data) return creation_failed_exception(serializer.errors) except Exception as err: return creation_failed_exception(err)
def get_retrieve_data(viewset=None): try: instance = viewset.get_object_from_all() except: return creation_failed_exception("File Doesn't exist.") if instance is None: return creation_failed_exception("File Doesn't exist.") serializer = viewset.serializer_class(instance=instance) return Response(serializer.data)
def update(self, request, *args, **kwargs): data = request.data data = convert_to_string(data) if 'name' in data and not name_check(data['name']): return creation_failed_exception( "Name not correct. Only digits, letter, undescore and hypen allowed. No empty. Less then 100 characters." ) if 'name' in data: datasetname_list = [] dataset_query = Dataset.objects.filter( deleted=False, created_by_id=request.user.id) for index, i in enumerate(dataset_query): datasetname_list.append(i.name) if data['name'] in datasetname_list: return creation_failed_exception( "Dataset file name already exists!.") try: instance = self.get_object_from_all() if 'deleted' in data: if data['deleted'] == True: print('let us delete') instance.deleted = True instance.save() clean_up_on_delete.delay(instance.slug, Dataset.__name__) return JsonResponse({'message': 'Deleted'}) except: return update_failed_exception("File Doesn't exist.") if 'subsetting' in data: if data['subsetting'] == True: subset_dataset_list = [] dataset_query = Dataset.objects.filter( deleted=False, created_by_id=request.user.id) for index, i in enumerate(dataset_query): subset_dataset_list.append(i.name) if data['name'] in subset_dataset_list: return creation_failed_exception( "Dataset file name already exists!.") return self.subsetting(request, instance) # question: do we need update method in views/ as well as in serializers? # answer: Yes. LoL serializer = self.serializer_class(instance=instance, data=data, partial=True) if serializer.is_valid(): serializer.save() return Response(serializer.data) return update_failed_exception(serializer.errors)
def clean_last_name(self): if self.cleaned_data["last_name"].strip() == '': raise ValidationError("Last name is required.") #---------------Last Name check Validations----------- should_proceed = name_check(self.cleaned_data["last_name"]) if should_proceed < 0: if should_proceed == -1: return creation_failed_exception("Last Name is empty.") if should_proceed == -2: return creation_failed_exception("Last Name is very large.") if should_proceed == -3: return creation_failed_exception( "Last Name have special_characters.") #-------------------------------------------------------- return self.cleaned_data["last_name"]
def clean_username(self): username = self.cleaned_data['username'].lower() r = User.objects.filter(username=username) if r.count(): raise ValidationError("Username already exists") #---------------User Name check Validations----------- should_proceed = name_check(username) if should_proceed < 0: if should_proceed == -1: return creation_failed_exception("UserName is empty.") if should_proceed == -2: return creation_failed_exception("UserName is very large.") if should_proceed == -3: return creation_failed_exception( "UserName have invalid_characters.") #-------------------------------------------------------- return username
def subsetting(self, request, instance=None): if instance is None: return creation_failed_exception("File Doesn't exist.") # try: data = request.data data = convert_to_string(data) temp_details = dict() if instance.datasource_type == "fileUpload": temp_details['input_file'] = instance.input_file temp_details['datasource_type'] = instance.datasource_type temp_details['file_remote'] = instance.file_remote temp_details['name'] = data.get('name', temp_details['input_file'].name) else: temp_details['input_file'] = None temp_details['datasource_details'] = instance.datasource_details temp_details['datasource_type'] = instance.datasource_type temp_details['name'] = data.get( 'name', data.get('datasource_type', "NoName") + "_" + str(random.randint(1000000, 10000000))) temp_details['created_by'] = request.user.id serializer = DatasetSerializer(data=temp_details, context={"request": self.request}) if serializer.is_valid(): dataset_object = serializer.save() if 'filter_settings' in data: dataset_object.create_for_subsetting( data['filter_settings'], data.get('transformation_settings', {}), instance.get_input_file(), instance.get_metadata_url_config()) else: return creation_failed_exception( {'error': 'no filter_settings'}) return Response(serializer.data)
def create(self, request, *args, **kwargs): # try: if 'data' in kwargs: data = kwargs.get('data') self.request = request else: data = request.data data = convert_to_string(data) response = [] try: if data['mode'] == 'autoML': self.mode = 'autoML' elif data['mode'] == 'analyst': self.mode = 'analyst' else: self.mode = 'analyst' self.save() except: pass try: data['created_by'] = request.user.id except: # data['created_by'] = None pass if 'name' in data: should_proceed = name_check(data['name']) if should_proceed < 0: if should_proceed == -1: return creation_failed_exception("Name is empty.") elif should_proceed == -2: return creation_failed_exception("Name is very large.") elif should_proceed == -3: return creation_failed_exception( "Name have special_characters.") if 'input_file' in data: # data['input_file'] = request.FILES.get('input_file') files = request.FILES.getlist('input_file') for file in files: data['input_file'] = file data['datasource_type'] = 'fileUpload' if data['input_file'] is None: data['name'] = data.get( 'name', data.get('datasource_type', "H") + "_" + str(random.randint(1000000, 10000000))) else: data['name'] = data['input_file'].name[:-4].replace( '.', '_') datasetname_list = [] dataset_query = Dataset.objects.filter( deleted=False, created_by_id=request.user.id) for index, i in enumerate(dataset_query): datasetname_list.append(i.name) if data['name'] in datasetname_list: return creation_failed_exception( "Dataset file name already exists!.") serializer = DatasetSerializer( data=data, context={"request": self.request}) if serializer.is_valid(): dataset_object = serializer.save() dataset_object.create() response.append(serializer.data) else: return creation_failed_exception(serializer.errors) return Response(response) elif 'datasource_details' in data: data['input_file'] = None if "datasetname" in data['datasource_details']: datasource_details = json.loads(data['datasource_details']) data['name'] = datasource_details['datasetname'] else: data['name'] = data.get( 'name', data.get('datasource_type', "H") + "_" + str(random.randint(1000000, 10000000))) # question: why to use user.id when it can take, id, pk, object. # answer: I tried. Sighhh but it gave this error "Incorrect type. Expected pk value, received User." serializer = DatasetSerializer(data=data, context={"request": self.request}) if serializer.is_valid(): dataset_object = serializer.save() dataset_object.create() return Response(serializer.data) return creation_failed_exception(serializer.errors)