# Path: locksmith/auth/models.py
# class ApiKey(models.Model):
#     key = models.CharField(max_length=32, primary_key=True)
#     email = models.EmailField('Email Address')
#     status = models.CharField(max_length=1, choices=KEY_STATUSES, default='U')
# 
#     issued_on = models.DateTimeField(default=datetime.datetime.now, editable=False)
# 
#     def active(self):
#         return status == 'A'
# 
#     def __unicode__(self):
#         return '%s (%s) [%s]' % (self.email, self.key, self.get_status_display())
# 
#     class Meta:
#         db_table = 'locksmith_auth_apikey'


from django.conf import settings
from locksmith.auth.models import ApiKey
from django.http import HttpResponse

QS_PARAM = getattr(settings, 'LOCKSMITH_QS_PARAM', 'apikey')
HTTP_HEADER = getattr(settings, 'LOCKSMITH_HTTP_HEADER', 'HTTP_X_APIKEY')

class APIKeyMiddleware(object):
    def process_request(self, request):
        key = request.GET.get(QS_PARAM, None) or request.META.get(HTTP_HEADER, None)
        if key is not None:
            try:
                request.apikey = ApiKey.objects.get(key=key)

# Path: locksmith/auth/models.py
# class ApiKey(models.Model):
#     key = models.CharField(max_length=32, primary_key=True)
#     email = models.EmailField('Email Address')
#     status = models.CharField(max_length=1, choices=KEY_STATUSES, default='U')
# 
#     issued_on = models.DateTimeField(default=datetime.datetime.now, editable=False)
# 
#     def active(self):
#         return status == 'A'
# 
#     def __unicode__(self):
#         return '%s (%s) [%s]' % (self.email, self.key, self.get_status_display())
# 
#     class Meta:
#         db_table = 'locksmith_auth_apikey'


# Path: locksmith/auth/middleware.py
from django.conf import settings
from locksmith.auth.models import ApiKey
from django.http import HttpResponse


QS_PARAM = getattr(settings, 'LOCKSMITH_QS_PARAM', 'apikey')
HTTP_HEADER = getattr(settings, 'LOCKSMITH_HTTP_HEADER', 'HTTP_X_APIKEY')

class APIKeyMiddleware(object):
    def process_request(self, request):
        key = request.GET.get(QS_PARAM, None) or request.META.get(HTTP_HEADER, None)
        if key is not None:
            try:
                request.apikey = ApiKey.objects.get(key=key)

            except ApiKey.DoesNotExist: