Last active
August 29, 2015 14:04
-
-
Save neuroticnerd/7198587438c28d800563 to your computer and use it in GitHub Desktop.
API key Django model for use with Oauth Client
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from uuidfield import UUIDField | |
from django.contrib import admin | |
class APIKey(models.Model): | |
apikey = UUIDField(auto=True) | |
url = models.CharField(max_length=255, blank=True, null=True) | |
client = models.ForeignKey(Client, related_name='apikeys') | |
scope = models.CharField(max_length=64, blank=True, null=True) | |
active = models.BooleanField(default=True) | |
deprecate = models.BooleanField(default=True) | |
def save(self, *args, **kwargs): | |
""" | |
The save method gets any deprecateable keys that are | |
currently active with the same scope and invalidates them | |
""" | |
previous = APIKey.objects.filter( | |
client=self.client, deprecate=True, active=True, scope=self.scope) | |
previous.update(active=False) | |
super(APIKey, self).save(*args, **kwargs) | |
def __unicode__(self): | |
rep = ( | |
self.scope, self.active, self.client.name, | |
self.client.id, self.apikey) | |
return u"%s(%s) %s[%s] %s" % rep | |
class APIKeyAdmin(admin.ModelAdmin): | |
readonly_fields = ('apikey',) | |
admin.site.register(APIKey, APIKeyAdmin) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment