Commits

Dejan Noveski committed 1fccf23

Don't allow creation of system folders if there's a user folder already created with the same name

Comments (0)

Files changed (1)

postman/models.py

     def save(self, *args, **kwargs):
         from django.core.exceptions import ValidationError
 
-        system_folders = SkipInboxFolder.objects.filter(
+        # If admin tries to add a system folder, we must make sure that admin wont over-ride
+        # other user's folder.
+        if self.user is None:
+            if SkipInboxFolder.objects.filter(name = self.name).count() > 0:
+                raise ValidationError('A folder with that name already exists. Please consider a different name.')
+        else:
+            # If there's a system folder with this name, don't save it.
+            saved_folders = SkipInboxFolder.objects.filter(
                 models.Q(name = self.name), models.Q(models.Q(user=self.user) | models.Q(user=None))).count()
+            if saved_folders > 0:
+                raise ValidationError('A system folder with that name already exists, or you already have this folder.')
 
-        if system_folders > 0:
-            raise ValidationError('A system folder with that name already exists. Use that one, or change the name.')
         return super(SkipInboxFolder, self).save(*args, **kwargs)
 
     class Meta: