review changes #14187

This commit is contained in:
Abhimanyu Saharan 2023-11-10 02:21:01 +05:30
parent 8e9e81243a
commit 5a9739f563
2 changed files with 9 additions and 9 deletions

View File

@ -2,6 +2,7 @@ import logging
import os import os
from django.conf import settings from django.conf import settings
from django.core.exceptions import ValidationError
from django.db import models from django.db import models
from django.urls import reverse from django.urls import reverse
from django.utils.translation import gettext as _ from django.utils.translation import gettext as _
@ -84,6 +85,14 @@ class ManagedFile(SyncedDataMixin, models.Model):
self.file_path = os.path.basename(self.data_path) self.file_path = os.path.basename(self.data_path)
self.data_file.write_to_disk(self.full_path, overwrite=True) self.data_file.write_to_disk(self.full_path, overwrite=True)
def clean(self):
super().clean()
# Ensure that the file root and path make a unique pair
if self._meta.model.objects.filter(file_root=self.file_root, file_path=self.file_path).exclude(pk=self.pk).exists():
raise ValidationError(
f"A {self._meta.verbose_name.lower()} with this file path already exists ({self.file_root}/{self.file_path}).")
def delete(self, *args, **kwargs): def delete(self, *args, **kwargs):
# Delete file from disk # Delete file from disk
try: try:

View File

@ -2,7 +2,6 @@ import inspect
import logging import logging
from functools import cached_property from functools import cached_property
from django.core.exceptions import ValidationError
from django.db import models from django.db import models
from django.urls import reverse from django.urls import reverse
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
@ -77,14 +76,6 @@ class ScriptModule(PythonModuleMixin, JobsMixin, ManagedFile):
return scripts return scripts
def clean(self):
super().clean()
# Ensure that the file root and path make a unique pair
if self.file_root and self.file_path:
if ScriptModule.objects.filter(file_root=self.file_root, file_path=self.file_path).exclude(pk=self.pk).exists():
raise ValidationError(f"A script module with this file path already exists ({self.file_root}/{self.file_path}).")
def save(self, *args, **kwargs): def save(self, *args, **kwargs):
self.file_root = ManagedFileRootPathChoices.SCRIPTS self.file_root = ManagedFileRootPathChoices.SCRIPTS
return super().save(*args, **kwargs) return super().save(*args, **kwargs)