# Path: api_v3/models/responder.py
# class Responder(models.Model):
#     """Model for ticket responders."""
# 
#     user = models.ForeignKey(
#         settings.AUTH_USER_MODEL, db_index=True,
#         on_delete=models.DO_NOTHING)
#     ticket = models.ForeignKey(
#         'Ticket', related_name='responders', db_index=True,
#         on_delete=models.DO_NOTHING)
#     created_at = models.DateTimeField(auto_now_add=True)
# 
#     class Meta:
#         unique_together = ('user', 'ticket')
# 
#     @classmethod
#     def filter_by_user(cls, user, queryset=None):
#         """Returns any user responder objects.
# 
#         Either related to the tickets he created or he is subscribed to.
#         """
#         if queryset is None:
#             queryset = cls.objects
# 
#         return queryset.filter(
#             # Allow own responder objects
#             models.Q(user=user) |
#             # Allow related to user responder tickets
#             models.Q(ticket__responder_users=user) |
#             # Allow related to user subscribed tickets
#             models.Q(ticket__subscriber_users=user) |
#             # Allow related to user created tickets
#             models.Q(ticket__requester=user)
#         )
# 
# Path: api_v3/serializers/mixins.py
# class ResponderSubscriberSerializer(serializers.ModelSerializer):
# 
#     EMAIL_SUBSCRIBER_ERROR_MESSAGE = 'Email already subscribed.'
#     SUBSCRIBER_ERROR_MESSAGE = 'Subscriber already exists.'
#     RESPONDER_ERROR_MESSAGE = 'User is a responder.'
# 
#     UNIQUENESS_VALIDATORS = [
#         UniqueTogetherValidator(
#             queryset=Subscriber.objects.all(),
#             fields=('user', 'ticket'),
#             message=SUBSCRIBER_ERROR_MESSAGE
#         ),
#         UniqueTogetherValidator(
#             queryset=Responder.objects.all(),
#             fields=('user', 'ticket'),
#             message=RESPONDER_ERROR_MESSAGE
#         )
#     ]
#     EMAIL_UNIQUENESS_VALIDATORS = [
#         UniqueTogetherValidator(
#             queryset=Subscriber.objects.all(),
#             fields=('ticket', 'email'),
#             message=EMAIL_SUBSCRIBER_ERROR_MESSAGE
#         ),
#     ]
# 
#     def run_validation(self, data=empty):
#         """Remap non field error key to the user key to keep consistency."""
#         try:
#             return super(
#                 ResponderSubscriberSerializer, self
#             ).run_validation(data)
#         except ValidationError as error:
#             messages = error.detail.pop(api_settings.NON_FIELD_ERRORS_KEY, None)
# 
#             if messages and self.EMAIL_SUBSCRIBER_ERROR_MESSAGE in messages:
#                 error.detail['email'] = messages
#             else:
#                 error.detail['user'] = messages
# 
#             raise error


from api_v3.models import Responder
from .mixins import ResponderSubscriberSerializer


class ResponderSerializer(ResponderSubscriberSerializer):

    included_serializers = {
        'user': 'api_v3.serializers.ProfileSerializer',
        'ticket': 'api_v3.serializers.TicketSerializer'
    }

    class Meta:
        model = Responder
        fields = ('ticket', 'user')
        resource_name = 'responders'

# Path: api_v3/models/responder.py
# class Responder(models.Model):
#     """Model for ticket responders."""
# 
#     user = models.ForeignKey(
#         settings.AUTH_USER_MODEL, db_index=True,
#         on_delete=models.DO_NOTHING)
#     ticket = models.ForeignKey(
#         'Ticket', related_name='responders', db_index=True,
#         on_delete=models.DO_NOTHING)
#     created_at = models.DateTimeField(auto_now_add=True)
# 
#     class Meta:
#         unique_together = ('user', 'ticket')
# 
#     @classmethod
#     def filter_by_user(cls, user, queryset=None):
#         """Returns any user responder objects.
# 
#         Either related to the tickets he created or he is subscribed to.
#         """
#         if queryset is None:
#             queryset = cls.objects
# 
#         return queryset.filter(
#             # Allow own responder objects
#             models.Q(user=user) |
#             # Allow related to user responder tickets
#             models.Q(ticket__responder_users=user) |
#             # Allow related to user subscribed tickets
#             models.Q(ticket__subscriber_users=user) |
#             # Allow related to user created tickets
#             models.Q(ticket__requester=user)
#         )
# 
# Path: api_v3/serializers/mixins.py
# class ResponderSubscriberSerializer(serializers.ModelSerializer):
# 
#     EMAIL_SUBSCRIBER_ERROR_MESSAGE = 'Email already subscribed.'
#     SUBSCRIBER_ERROR_MESSAGE = 'Subscriber already exists.'
#     RESPONDER_ERROR_MESSAGE = 'User is a responder.'
# 
#     UNIQUENESS_VALIDATORS = [
#         UniqueTogetherValidator(
#             queryset=Subscriber.objects.all(),
#             fields=('user', 'ticket'),
#             message=SUBSCRIBER_ERROR_MESSAGE
#         ),
#         UniqueTogetherValidator(
#             queryset=Responder.objects.all(),
#             fields=('user', 'ticket'),
#             message=RESPONDER_ERROR_MESSAGE
#         )
#     ]
#     EMAIL_UNIQUENESS_VALIDATORS = [
#         UniqueTogetherValidator(
#             queryset=Subscriber.objects.all(),
#             fields=('ticket', 'email'),
#             message=EMAIL_SUBSCRIBER_ERROR_MESSAGE
#         ),
#     ]
# 
#     def run_validation(self, data=empty):
#         """Remap non field error key to the user key to keep consistency."""
#         try:
#             return super(
#                 ResponderSubscriberSerializer, self
#             ).run_validation(data)
#         except ValidationError as error:
#             messages = error.detail.pop(api_settings.NON_FIELD_ERRORS_KEY, None)
# 
#             if messages and self.EMAIL_SUBSCRIBER_ERROR_MESSAGE in messages:
#                 error.detail['email'] = messages
#             else:
#                 error.detail['user'] = messages
# 
#             raise error


# Path: api_v3/serializers/responder.py
from api_v3.models import Responder
from .mixins import ResponderSubscriberSerializer



class ResponderSerializer(ResponderSubscriberSerializer):

    included_serializers = {
        'user': 'api_v3.serializers.ProfileSerializer',
        'ticket': 'api_v3.serializers.TicketSerializer'
    }

    class Meta:
        model = Responder
        fields = ('ticket', 'user')
        resource_name = 'responders'

        validators = ResponderSubscriberSerializer.UNIQUENESS_VALIDATORS