# Path: patterns/identify/models.py
# class Identifier(models.Model):
#     name = models.CharField(max_length=255, db_index=True)
#     slug = models.SlugField(blank=True)
#     authority = models.ForeignKey(
#         'Authority', models.CASCADE, blank=True, null=True,
#     )
#     is_primary = models.BooleanField(default=False)
# 
#     # Identifer can contain a pointer to any model
#     content_type = models.ForeignKey(ContentType, models.CASCADE)
#     object_id = models.PositiveIntegerField()
#     content_object = GenericForeignKey()
# 
#     objects = IdentifierManager()
# 
#     @property
#     def url(self):
#         if (not self.authority or not self.authority.object_url):
#             return None
#         else:
#             return self.authority.object_url % self.slug
# 
#     def save(self, *args, **kwargs):
#         if not self.slug:
#             model = self.content_type.model_class()
#             queryset = model.objects.exclude(pk=self.object_id)
#             self.slug = find_unique_slug(self.name, queryset)
# 
#         if self.is_primary and not self.pk:
#             exist = self.content_object.primary_identifier
#             if exist:
#                 # Primary identifier already exists for this object, update
#                 # instead of adding another
#                 self.pk = exist.pk
#                 kwargs.pop('force_insert', None)
#                 kwargs['force_update'] = True
# 
#         super(Identifier, self).save(*args, **kwargs)
# 
#         if self.is_primary:
#             obj = self.content_object
#             if self.name != obj.name or self.slug != obj.slug:
#                 obj.name = self.name
#                 obj.slug = self.slug
#                 obj.save()
# 
#     def __str__(self):
#         return self.name
# 
#     class Meta:
#         db_table = 'wq_identifier'
#         ordering = WQ_IDENTIFIER_ORDER
#         abstract = not INSTALLED


from rest_framework import serializers
from rest_framework.validators import UniqueTogetherValidator
from wq.db.patterns.base import serializers as base
from .models import Identifier


class IdentifierListSerializer(base.TypedAttachmentListSerializer):
    def to_internal_value(self, data):
        data = super(IdentifierListSerializer, self).to_internal_value(data)
        primary = [
            ident for ident in data
            if ident and ident.get('is_primary', None)
        ]
        if not any(primary) and len(data) > 0:
            for ident in data:
                if ident:
                    ident['is_primary'] = True

# Path: patterns/identify/models.py
# class Identifier(models.Model):
#     name = models.CharField(max_length=255, db_index=True)
#     slug = models.SlugField(blank=True)
#     authority = models.ForeignKey(
#         'Authority', models.CASCADE, blank=True, null=True,
#     )
#     is_primary = models.BooleanField(default=False)
# 
#     # Identifer can contain a pointer to any model
#     content_type = models.ForeignKey(ContentType, models.CASCADE)
#     object_id = models.PositiveIntegerField()
#     content_object = GenericForeignKey()
# 
#     objects = IdentifierManager()
# 
#     @property
#     def url(self):
#         if (not self.authority or not self.authority.object_url):
#             return None
#         else:
#             return self.authority.object_url % self.slug
# 
#     def save(self, *args, **kwargs):
#         if not self.slug:
#             model = self.content_type.model_class()
#             queryset = model.objects.exclude(pk=self.object_id)
#             self.slug = find_unique_slug(self.name, queryset)
# 
#         if self.is_primary and not self.pk:
#             exist = self.content_object.primary_identifier
#             if exist:
#                 # Primary identifier already exists for this object, update
#                 # instead of adding another
#                 self.pk = exist.pk
#                 kwargs.pop('force_insert', None)
#                 kwargs['force_update'] = True
# 
#         super(Identifier, self).save(*args, **kwargs)
# 
#         if self.is_primary:
#             obj = self.content_object
#             if self.name != obj.name or self.slug != obj.slug:
#                 obj.name = self.name
#                 obj.slug = self.slug
#                 obj.save()
# 
#     def __str__(self):
#         return self.name
# 
#     class Meta:
#         db_table = 'wq_identifier'
#         ordering = WQ_IDENTIFIER_ORDER
#         abstract = not INSTALLED


# Path: patterns/identify/serializers.py
from rest_framework import serializers
from rest_framework.validators import UniqueTogetherValidator
from wq.db.patterns.base import serializers as base
from .models import Identifier



class IdentifierListSerializer(base.TypedAttachmentListSerializer):
    def to_internal_value(self, data):
        data = super(IdentifierListSerializer, self).to_internal_value(data)
        primary = [
            ident for ident in data
            if ident and ident.get('is_primary', None)
        ]
        if not any(primary) and len(data) > 0:
            for ident in data:
                if ident:
                    ident['is_primary'] = True

                    break