# Path: realestate/api/models.py
# class ApiKeys(models.Model):
#     description = models.CharField(max_length=100, blank=True, default='', verbose_name=_('Description'))
#     key = models.CharField(max_length=40, primary_key=True)
#     created = models.DateTimeField(auto_now_add=True)
# 
#     class Meta:
#         verbose_name = _('API Key')
#         verbose_name_plural = _('API Keys')
# 
#     def save(self, *args, **kwargs):
#         if not self.key:
#             self.key = self.generate_key()
#         return super(ApiKeys, self).save(*args, **kwargs)
# 
#     def generate_key(self):
#         return binascii.hexlify(os.urandom(20))
# 
#     def __unicode__(self):
#         return self.key


from django.contrib.auth.models import AnonymousUser
from rest_framework.authentication import BaseAuthentication
from rest_framework import exceptions
from realestate.api.models import ApiKeys
from django.utils.translation import ugettext as _


class ApiKeyAuthentication(BaseAuthentication):
    def authenticate(self, request):
        key = request.META.get('HTTP_X_API_KEY')
        if not key:
            raise exceptions.AuthenticationFailed(_('The API Key is missing'))

        try:
            api_key = ApiKeys.objects.get(key=key)
            user = AnonymousUser()

# Path: realestate/api/models.py
# class ApiKeys(models.Model):
#     description = models.CharField(max_length=100, blank=True, default='', verbose_name=_('Description'))
#     key = models.CharField(max_length=40, primary_key=True)
#     created = models.DateTimeField(auto_now_add=True)
# 
#     class Meta:
#         verbose_name = _('API Key')
#         verbose_name_plural = _('API Keys')
# 
#     def save(self, *args, **kwargs):
#         if not self.key:
#             self.key = self.generate_key()
#         return super(ApiKeys, self).save(*args, **kwargs)
# 
#     def generate_key(self):
#         return binascii.hexlify(os.urandom(20))
# 
#     def __unicode__(self):
#         return self.key


# Path: realestate/api/authentication.py
from django.contrib.auth.models import AnonymousUser
from rest_framework.authentication import BaseAuthentication
from rest_framework import exceptions
from realestate.api.models import ApiKeys
from django.utils.translation import ugettext as _



class ApiKeyAuthentication(BaseAuthentication):
    def authenticate(self, request):
        key = request.META.get('HTTP_X_API_KEY')
        if not key:
            raise exceptions.AuthenticationFailed(_('The API Key is missing'))

        try:
            api_key = ApiKeys.objects.get(key=key)
            user = AnonymousUser()

        except ApiKeys.DoesNotExist: