2017-10-22 18:04:59 -04:00
|
|
|
from django.db import models
|
|
|
|
from django.contrib.auth.models import AbstractUser
|
2017-10-26 17:52:26 -04:00
|
|
|
from django.utils.functional import cached_property
|
2017-10-24 06:57:07 -04:00
|
|
|
from meta.models import ModelMeta
|
2017-10-22 18:04:59 -04:00
|
|
|
|
|
|
|
|
2017-10-22 20:53:51 -04:00
|
|
|
def avatar_path(instance, name):
|
|
|
|
return 'avatars/{id}/{name}'.format(id=instance.id, name=name)
|
|
|
|
|
|
|
|
|
2017-10-22 21:59:10 -04:00
|
|
|
class Site(models.Model):
|
|
|
|
name = models.CharField(max_length=100, unique=True)
|
|
|
|
icon = models.CharField(max_length=100)
|
|
|
|
url = models.CharField(max_length=100)
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
ordering = ('name',)
|
|
|
|
|
|
|
|
|
2017-10-24 06:57:07 -04:00
|
|
|
class User(ModelMeta, AbstractUser):
|
2017-10-22 20:53:51 -04:00
|
|
|
avatar = models.ImageField(upload_to=avatar_path)
|
2017-10-22 18:04:59 -04:00
|
|
|
note = models.TextField(blank=True)
|
2017-10-24 21:07:57 -04:00
|
|
|
# This is gonna need to change if I ever decide to add multiple-user support ;)
|
|
|
|
url = '/'
|
|
|
|
|
2017-10-27 01:51:46 -04:00
|
|
|
def get_absolute_url(self):
|
|
|
|
return self.url
|
2017-10-22 21:33:24 -04:00
|
|
|
|
2017-10-24 06:57:07 -04:00
|
|
|
@property
|
|
|
|
def avatar_url(self):
|
|
|
|
return self.avatar.url
|
|
|
|
|
2017-10-26 17:52:26 -04:00
|
|
|
@cached_property
|
2017-10-24 18:34:42 -04:00
|
|
|
def facebook_id(self):
|
|
|
|
try:
|
2017-10-26 17:52:26 -04:00
|
|
|
return self.profiles.filter(site__name='Facebook').values('username')[0]['username']
|
2017-10-24 18:34:42 -04:00
|
|
|
except IndexError:
|
|
|
|
return None
|
|
|
|
|
2017-10-26 17:52:26 -04:00
|
|
|
@cached_property
|
2017-10-24 18:34:42 -04:00
|
|
|
def twitter_username(self):
|
|
|
|
try:
|
2017-10-26 17:52:26 -04:00
|
|
|
return '@' + self.profiles.filter(site__name='Twitter').values('username')[0]['username']
|
2017-10-24 18:34:42 -04:00
|
|
|
except IndexError:
|
|
|
|
return None
|
|
|
|
|
2017-10-24 06:57:07 -04:00
|
|
|
_metadata = {
|
|
|
|
'image': 'avatar_url',
|
|
|
|
'description': 'note',
|
|
|
|
'og_type': 'profile',
|
2017-10-24 18:34:42 -04:00
|
|
|
'og_profile_id': 'facebook_id',
|
|
|
|
'twitter_creator': 'twitter_username',
|
2017-10-24 06:57:07 -04:00
|
|
|
}
|
|
|
|
|
2017-10-22 21:33:24 -04:00
|
|
|
|
2017-10-26 17:52:26 -04:00
|
|
|
class ProfileManager(models.Manager):
|
|
|
|
def get_queryset(self):
|
|
|
|
return super(ProfileManager, self).get_queryset().select_related('site')
|
|
|
|
|
|
|
|
|
2017-10-22 21:59:10 -04:00
|
|
|
class Profile(models.Model):
|
2017-10-26 17:52:26 -04:00
|
|
|
objects = ProfileManager()
|
2017-10-22 21:59:10 -04:00
|
|
|
user = models.ForeignKey(
|
|
|
|
User,
|
|
|
|
related_name='profiles',
|
|
|
|
on_delete=models.CASCADE
|
|
|
|
)
|
|
|
|
site = models.ForeignKey(Site, on_delete=models.CASCADE)
|
|
|
|
username = models.CharField(max_length=100)
|
|
|
|
display_name = models.CharField(max_length=100, blank=True)
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return '{name}@{site}'.format(name=self.username, site=self.site)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
return self.display_name or self.username
|
|
|
|
|
|
|
|
@property
|
|
|
|
def url(self):
|
|
|
|
return self.site.url.format(username=self.username)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
ordering = ('site', 'username')
|
|
|
|
|
|
|
|
|
2017-10-22 21:33:24 -04:00
|
|
|
class Key(models.Model):
|
|
|
|
user = models.ForeignKey(
|
|
|
|
User,
|
|
|
|
related_name='keys',
|
|
|
|
on_delete=models.CASCADE
|
|
|
|
)
|
|
|
|
fingerprint = models.CharField(max_length=40)
|
|
|
|
file = models.FileField(upload_to='keys')
|
|
|
|
|
2017-10-22 21:59:10 -04:00
|
|
|
@property
|
|
|
|
def key_id(self): return self.fingerprint[32:]
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.key_id
|
|
|
|
|
2017-10-22 21:33:24 -04:00
|
|
|
def pretty_print(self):
|
|
|
|
return " ".join(self.fingerprint[i:i+4] for i in range(0, 40, 4))
|