Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

411: add unique constraint on data directory path #412

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions ifcbdb/dashboard/management/commands/adddirectory.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ def handle(self, *args, **options):
except Dataset.DoesNotExist:
self.stderr.write('No such dataset "{}"'.format(dataset_name))
return

# make sure the directory path is not already in the database
existing_path = DataDirectory.objects.filter(dataset=d, path=path).first()
if existing_path:
self.stderr.write('Path "{}" is already in use by dataset "{}"'.format(path, existing_path.dataset.name))
return

# create the directory
dd = DataDirectory(path=path, kind=kind)
dd.dataset = d
Expand Down
17 changes: 17 additions & 0 deletions ifcbdb/dashboard/migrations/0036_datadirectory_unique_path.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 4.2.14 on 2024-10-16 18:52

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('dashboard', '0035_auto_20191114_2039'),
]

operations = [
migrations.AddConstraint(
model_name='datadirectory',
constraint=models.UniqueConstraint(fields=('dataset', 'path'), name='unique path'),
),
]
5 changes: 5 additions & 0 deletions ifcbdb/dashboard/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,11 @@ def get_class_scores_directory(self):
def __str__(self):
return '{} ({})'.format(self.path, self.kind)

class Meta:
constraints = [
models.UniqueConstraint(fields=['dataset', 'path'], name='unique path')
]

class Bin(models.Model):
# bin's permanent identifier (e.g., D20190102T1234_IFCB927)
pid = models.CharField(max_length=64, unique=True)
Expand Down
18 changes: 18 additions & 0 deletions ifcbdb/secure/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ def save(self, commit=True):

class DirectoryForm(forms.ModelForm):

def __init__(self, *args, **kwargs):
self.dataset_id = kwargs.pop("dataset_id", None)

super().__init__(*args, **kwargs)

def clean_path(self):
path = self.cleaned_data["path"]

Expand All @@ -91,6 +96,19 @@ def clean_blacklist(self):

return ",".join([name.strip() for name in blacklist.split(",")])

def clean(self):
data = self.cleaned_data
path = self.cleaned_data.get("path")

# make sure the directory path is not already in the database
existing_path = DataDirectory.objects.filter(dataset_id=self.dataset_id, path=path).first()
if existing_path:
raise forms.ValidationError({
'path': 'Path "{}" is already in use'.format(path)
})

return data

def _match_folder_names(self, value):
return re.match(r'^[A-Za-z0-9,\s]*$', value)

Expand Down
2 changes: 1 addition & 1 deletion ifcbdb/secure/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def edit_directory(request, dataset_id, id):
directory.version = DataDirectory.DEFAULT_VERSION

if request.POST:
form = DirectoryForm(request.POST, instance=directory)
form = DirectoryForm(request.POST, instance=directory, dataset_id=dataset_id)
if form.is_valid():
instance = form.save(commit=False)
if instance.kind == "raw":
Expand Down