Collectin: make sure collections always have a unique UID.

master
Tom Hacohen 4 years ago
parent 18b3f45b79
commit 5d9b47531b

@ -21,7 +21,10 @@ from django.db.models import Q
from django.utils.functional import cached_property
from django.utils.crypto import get_random_string
from rest_framework import status
from . import app_settings
from .exceptions import EtebaseValidationError
UidValidator = RegexValidator(regex=r'^[a-zA-Z0-9\-_]{20,}$', message='Not a valid UID')
@ -57,6 +60,16 @@ class Collection(models.Model):
return stoken.uid
def validate_unique(self, exclude=None):
super().validate_unique(exclude=exclude)
if exclude is None or 'main_item' in exclude:
return
if self.__class__.objects.filter(owner=self.owner, main_item__uid=self.main_item.uid) \
.exclude(id=self.id).exists():
raise EtebaseValidationError('unique_uid', 'Collection with this uid already exists',
status_code=status.HTTP_409_CONFLICT)
class CollectionItem(models.Model):
uid = models.CharField(db_index=True, blank=False,

@ -297,6 +297,8 @@ class CollectionSerializer(BetterErrorsMixin, serializers.ModelSerializer):
main_item = models.CollectionItem.objects.create(**main_item_data, collection=instance)
instance.main_item = main_item
instance.full_clean()
instance.save()
process_revisions_for_item(main_item, revision_data)

Loading…
Cancel
Save