from django.conf import settings
from django.http import HttpResponse, HttpResponseBadRequest
from django.shortcuts import get_object_or_404
from django.views.decorators.http import require_POST
from locksmith.common import get_signature
from locksmith.auth.models import ApiKey

def verify_signature(post):
    return get_signature(post, settings.LOCKSMITH_SIGNING_KEY) == post['signature']

@require_POST
def create_key(self, request):
    if not verify_signature(request.POST):
        return HttpResponseBadRequest('bad signature')
    Key.objects.create(key=request.POST['key'],
                                  email=request.POST['email'],
                                  status=request.POST['status'])
    return HttpResponse('OK')

@require_POST
def update_key(self, request, get_by='key'):
    if not verify_signature(request.POST):
        return HttpResponseBadRequest('bad signature')
    # get the key
    if get_by == 'key':
        key = get_object_or_404(Key, key=request.POST['key'])
    elif get_by == 'email':
        key = get_object_or_404(Key, email=request.POST['email'])

    # update key
    key.key = request.POST['key']
    key.email = request.POST['email']
    key.status = request.POST['status']
    key.save()

    return HttpResponse('OK')

@@ -411,30 +411,24 @@
  create_key(
-self, 
 request):%0A  
@@ -421,24 +421,24 @@
 y(request):%0A
+
     if not v
@@ -527,16 +527,19 @@
 e')%0A    
+Api
 Key.obje
@@ -600,24 +600,16 @@
         
-        
 email=re
@@ -625,32 +625,24 @@
 T%5B'email'%5D,%0A
-        
             
@@ -727,24 +727,24 @@
 equire_POST%0A
+
 def update_k
@@ -750,14 +750,8 @@
 key(
-self, 
 requ

remove self, from views