forked from bkroeze/bmprofile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
56 lines (44 loc) · 1.7 KB
/
models.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
from django.db import models
from django.contrib.auth.models import User
from django.conf import settings
from signedauth.models import UserKey
import logging
log = logging.getLogger(__name__)
class BmProfileManager(models.Manager):
def force_for_user(self, user):
try:
profile = self.get(user=user)
except BmProfile.DoesNotExist:
log.debug('creating profile for %s', user)
profile = BmProfile(user=user)
profile.save()
return profile
class BmProfile(models.Model):
user = models.ForeignKey(User)
playaname = models.CharField(max_length=100, default = '', blank=True)
userkey = models.ForeignKey(UserKey, null=True)
api_allowed = models.NullBooleanField('API Allowed', default=False)
objects = BmProfileManager()
@property
def name(self):
if not self.playaname:
name = self.user.get_full_name()
if not name:
name = self.user.username
return name
return self.playaname
def save(self, *args, **kwargs):
if not self.playaname:
self.playaname = self.user.username
super(BmProfile, self).save()
if not self.userkey:
log.debug('creating userkey for %s', self.user.username)
key = UserKey(label='key', user=self.user)
key.save()
self.userkey = key
self.save()
def __unicode__(self):
return u'BmProfile: %s' % self.playaname
# maybe monkeypatch user to use BmProfile as its profile without forcing all users to have keys
if settings.AUTH_PROFILE_MODULE == 'bmprofile.BmProfile':
User.profile = property(lambda u: BmProfile.objects.force_for_user(u))