def clean(self): cleaned_data = super().clean() parent_tool = cleaned_data.get("parent_tool") category = cleaned_data.get("_category") location = cleaned_data.get("_location") phone_number = cleaned_data.get("_phone_number") primary_owner = cleaned_data.get("_primary_owner") image = cleaned_data.get("_image") # only resize if an image is present and has changed if image and not isinstance(image, FieldFile): from NEMO.utilities import resize_image # resize image to 500x500 maximum cleaned_data['_image'] = resize_image(image, 500) if parent_tool: if parent_tool.id == self.instance.id: self.add_error( 'parent_tool', 'You cannot select the parent to be the tool itself.') # in case of alternate tool, remove everything except parent_tool and name data = dict([(k, v) for k, v in self.cleaned_data.items() if k == "parent_tool" or k == "name"]) # an alternate tool is never visible data['visible'] = False return data else: if not category: self.add_error('_category', 'This field is required.') if not location: self.add_error('_location', 'This field is required.') if not phone_number: self.add_error('_phone_number', 'This field is required.') if not primary_owner: self.add_error('_primary_owner', 'This field is required.') post_usage_questions = cleaned_data.get("_post_usage_questions") # Validate _post_usage_questions JSON format if post_usage_questions: try: loads(post_usage_questions) except ValueError as error: self.add_error( "_post_usage_questions", "This field needs to be a valid JSON string") policy_off_between_times = cleaned_data.get( "_policy_off_between_times") policy_off_start_time = cleaned_data.get("_policy_off_start_time") policy_off_end_time = cleaned_data.get("_policy_off_end_time") if policy_off_between_times and (not policy_off_start_time or not policy_off_end_time): if not policy_off_start_time: self.add_error("_policy_off_start_time", "Start time must be specified") if not policy_off_end_time: self.add_error("_policy_off_end_time", "End time must be specified")
def save_task_images(request, task: Task) -> List[TaskImages]: task_images: List[TaskImages] = [] try: images_form = TaskImagesForm(request.POST, request.FILES) if images_form.is_valid() and images_form.cleaned_data['image'] is not None: for image_memory_file in request.FILES.getlist('image'): resized_image = resize_image(image_memory_file, 350) image = TaskImages(task=task) image.image.save(resized_image.name, ContentFile(resized_image.read()), save=False) image.save() task_images.append(image) except Exception as e: tasks_logger.exception(e) return task_images