# 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/models/subscriber.py
# class Subscriber(models.Model):
#     """Ticket subscriber."""
# 
#     user = models.ForeignKey(
#         settings.AUTH_USER_MODEL, db_index=True, null=True,
#         on_delete=models.DO_NOTHING)
#     ticket = models.ForeignKey(
#         'Ticket', related_name='subscribers', db_index=True,
#         on_delete=models.DO_NOTHING)
#     created_at = models.DateTimeField(auto_now_add=True)
#     email = models.CharField(max_length=512, blank=True, null=True)
# 
#     class Meta:
#         unique_together = (
#             ('user', 'email', 'ticket')
#         )
# 
#     @classmethod
#     def filter_by_user(cls, user, queryset=None):
#         """Returns any user subscriber objects.
# 
#         A subscriber is very much similar to a responder, but:
#             * it can not do ticket updates
#             * it can not add other people to a ticket.
# 
#         Either related to the tickets he created or he is subscribed to.
#         """
#         if queryset is None:
#             queryset = cls.objects
# 
#         return queryset.filter(
#             # Allow own subscriber 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)
#         )


from rest_framework.exceptions import ValidationError
from rest_framework.fields import empty
from rest_framework.validators import UniqueTogetherValidator
from rest_framework.settings import api_settings
from rest_framework_json_api import serializers
from api_v3.models import Responder, Subscriber



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(

# 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/models/subscriber.py
# class Subscriber(models.Model):
#     """Ticket subscriber."""
# 
#     user = models.ForeignKey(
#         settings.AUTH_USER_MODEL, db_index=True, null=True,
#         on_delete=models.DO_NOTHING)
#     ticket = models.ForeignKey(
#         'Ticket', related_name='subscribers', db_index=True,
#         on_delete=models.DO_NOTHING)
#     created_at = models.DateTimeField(auto_now_add=True)
#     email = models.CharField(max_length=512, blank=True, null=True)
# 
#     class Meta:
#         unique_together = (
#             ('user', 'email', 'ticket')
#         )
# 
#     @classmethod
#     def filter_by_user(cls, user, queryset=None):
#         """Returns any user subscriber objects.
# 
#         A subscriber is very much similar to a responder, but:
#             * it can not do ticket updates
#             * it can not add other people to a ticket.
# 
#         Either related to the tickets he created or he is subscribed to.
#         """
#         if queryset is None:
#             queryset = cls.objects
# 
#         return queryset.filter(
#             # Allow own subscriber 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
from rest_framework.exceptions import ValidationError
from rest_framework.fields import empty
from rest_framework.validators import UniqueTogetherValidator
from rest_framework.settings import api_settings
from rest_framework_json_api import serializers
from api_v3.models import Responder, Subscriber




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(),