# Path: emailcongress/models.py
# class Token(EmailCongressModel):
# 
#     class Meta:
#         permissions = (
#             ("api", "Can access the API."),
#         )
# 
#     key = models.CharField(max_length=64, unique=True, null=True, db_index=True)
#     object_id = models.PositiveIntegerField(db_index=True)
#     content_type = models.ForeignKey(ContentType, db_index=True)
#     content_object = GenericForeignKey()
# 
#     objects = TokenManager()
# 
#     def __str__(self):
#         return self.key
# 
#     @property
#     def user(self):
#         """
#         Convenience method for getting the django auth user from this Token if it exists.
# 
#         @return: the django auth user or None
#         @rtype: (DjangoUser|None)
#         """
#         if type(self.content_object) == User:
#             return self.content_object.django_user
#         else:
#             return None
# 
#     def save(self, *args, **kwargs):
#         """
#         Generates a key if one doesn't exist before saving the token.
#         """
#         if not self.key:
#             self.key = self.uid_creator()
#         super().save(*args, **kwargs)
# 
#     def reset(self, key=None):
#         self.key = key if key is not None else self.uid_creator()
#         self.save()
#         return self.key
# 
#     def link(self):
#         return utils.construct_link(settings.PROTOCOL,
#                                     settings.HOSTNAME,
#                                     reverse('validate', kwargs={'token': self.key}))
# 
#     @classmethod
#     def uid_creator(cls):
#         """
#         Creates a 64 character string uid, checks for collisions in input class, and returns a uid.
# 
#         @return: 64 character alphanumeric string
#         @rtype: str
#         """
#         while True:
#             potential_token = uuid.uuid4().hex + uuid.uuid4().hex
#             if cls.objects.filter(key=potential_token).count() == 0:
#                 return potential_token
# 
#     @classmethod
#     def convert_token(cls, key):
#         """
#         Converts a token to user, the user's default information, and a message.
# 
#         @param key: the string key to convert to other models
#         @type key: str
#         @return: tuple of message, user message info, and user
#         @rtype: (Message, UserMessageInfo, User)
#         """
#         msg, umi, user = None, None, None
#         token = cls.objects.filter(key=key).first()
# 
#         if token is not None:
#             item = token.content_object
#             if type(item) is User:
#                 user = item
#                 umi = user.default_info
#             elif type(item) is Message:
#                 msg = item
#                 umi = msg.user_message_info
#                 user = umi.user
# 
#         return msg, umi, user
# 
#     @staticmethod
#     def delete_content_object(sender, instance, **kwargs):
#         try:
#             instance.content_object.delete()
#         except:
#             pass


from rest_framework import HTTP_HEADER_ENCODING, exceptions
from rest_framework.authentication import TokenAuthentication as rest_framework_TokenAuthentication
from django.utils.translation import ugettext_lazy as _
from emailcongress.models import Token


class TokenAuthentication(rest_framework_TokenAuthentication):
    """
    Simple token based authentication.

    A custom token model may be used, but must have the following properties.

    * key -- The string identifying the token
    * user -- The user to which the token belongs

    http://stackoverflow.com/questions/27043349/how-to-use-custom-token-model-in-django-rest-framework
    """


# Path: emailcongress/models.py
# class Token(EmailCongressModel):
# 
#     class Meta:
#         permissions = (
#             ("api", "Can access the API."),
#         )
# 
#     key = models.CharField(max_length=64, unique=True, null=True, db_index=True)
#     object_id = models.PositiveIntegerField(db_index=True)
#     content_type = models.ForeignKey(ContentType, db_index=True)
#     content_object = GenericForeignKey()
# 
#     objects = TokenManager()
# 
#     def __str__(self):
#         return self.key
# 
#     @property
#     def user(self):
#         """
#         Convenience method for getting the django auth user from this Token if it exists.
# 
#         @return: the django auth user or None
#         @rtype: (DjangoUser|None)
#         """
#         if type(self.content_object) == User:
#             return self.content_object.django_user
#         else:
#             return None
# 
#     def save(self, *args, **kwargs):
#         """
#         Generates a key if one doesn't exist before saving the token.
#         """
#         if not self.key:
#             self.key = self.uid_creator()
#         super().save(*args, **kwargs)
# 
#     def reset(self, key=None):
#         self.key = key if key is not None else self.uid_creator()
#         self.save()
#         return self.key
# 
#     def link(self):
#         return utils.construct_link(settings.PROTOCOL,
#                                     settings.HOSTNAME,
#                                     reverse('validate', kwargs={'token': self.key}))
# 
#     @classmethod
#     def uid_creator(cls):
#         """
#         Creates a 64 character string uid, checks for collisions in input class, and returns a uid.
# 
#         @return: 64 character alphanumeric string
#         @rtype: str
#         """
#         while True:
#             potential_token = uuid.uuid4().hex + uuid.uuid4().hex
#             if cls.objects.filter(key=potential_token).count() == 0:
#                 return potential_token
# 
#     @classmethod
#     def convert_token(cls, key):
#         """
#         Converts a token to user, the user's default information, and a message.
# 
#         @param key: the string key to convert to other models
#         @type key: str
#         @return: tuple of message, user message info, and user
#         @rtype: (Message, UserMessageInfo, User)
#         """
#         msg, umi, user = None, None, None
#         token = cls.objects.filter(key=key).first()
# 
#         if token is not None:
#             item = token.content_object
#             if type(item) is User:
#                 user = item
#                 umi = user.default_info
#             elif type(item) is Message:
#                 msg = item
#                 umi = msg.user_message_info
#                 user = umi.user
# 
#         return msg, umi, user
# 
#     @staticmethod
#     def delete_content_object(sender, instance, **kwargs):
#         try:
#             instance.content_object.delete()
#         except:
#             pass


# Path: api/authenticators.py
from rest_framework import HTTP_HEADER_ENCODING, exceptions
from rest_framework.authentication import TokenAuthentication as rest_framework_TokenAuthentication
from django.utils.translation import ugettext_lazy as _
from emailcongress.models import Token



class TokenAuthentication(rest_framework_TokenAuthentication):
    """
    Simple token based authentication.

    A custom token model may be used, but must have the following properties.

    * key -- The string identifying the token
    * user -- The user to which the token belongs

    http://stackoverflow.com/questions/27043349/how-to-use-custom-token-model-in-django-rest-framework
    """


    model = Token