2 回答
TA贡献1842条经验 获得超21个赞
我get_key在ApiKeyAuthentication中通过覆盖方法找到了解决方案。
class MyApiKeyAuthentication(ApiKeyAuthentication):
def get_key(self, user, api_key):
"""
Attempts to find the API key for the user. Uses ``ApiKey`` by default
but can be overridden.
"""
from tastypie.models import ApiKey
try:
api_key = ApiKey.objects.get(user=user, key=api_key)
current_time = datetime.utcnow()
current_time = current_time.replace(tzinfo=pytz.utc)
week = timedelta(7)
if not (current_time - api_key.created) < week:
api_key.delete()
return self._unauthorized()
else:
api_key.created = current_time
api_key.save()
except ApiKey.DoesNotExist:
return self._unauthorized()
return True
添加回答
举报