def validate_folder(self):
"""Validates whether a folder can be created.
Performs two types of validation:
1. Checks if a DB entry is present.
2. Checks if a physical folder exists in the system."""
unicoded_title = "".join((i if ord(i) < 128 else '_') for i in unidecode(self.title))
parent_folder = self.folder
if parent_folder:
if ImageFolder.objects.filter(folder=parent_folder, title=self.title).count() > 0:
raise ValidationError("Folder exists in the DB!", code='db')
folder_path = os.path.join(settings.MEDIA_ROOT, parent_folder.path, unicoded_title)
if os.path.isdir(folder_path):
raise ValidationError("Folder exists in the OS!", code='os')
else:
if ImageFolder.objects.filter(folder__isnull=True, title=self.title).count() > 0:
raise ValidationError("Folder exists in the DB!", code='db')
folder_path = os.path.join(settings.MEDIA_ROOT, IMAGES_FOLDER_NAME, unicoded_title)
if os.path.isdir(folder_path):
raise ValidationError("Folder exists in the OS!", code='os')
评论列表
文章目录