lemoncurry/users/models.py

158 lines
4.2 KiB
Python
Raw Normal View History

2017-10-22 18:04:59 -04:00
from django.db import models
from django.contrib.auth.models import AbstractUser, UserManager as DjangoUserManager
from django.contrib.sites.models import Site as DjangoSite
from django.utils.functional import cached_property
from meta.models import ModelMeta
from urllib.parse import urljoin
from lemoncurry import utils
2017-10-22 18:04:59 -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)
domain = models.CharField(max_length=100, blank=True)
url_template = models.CharField(max_length=100)
def format(self, username=''):
return self.url_template.format(domain=self.domain, username=username)
@property
def url(self):
return self.format()
2017-10-22 21:59:10 -04:00
def __str__(self):
return self.name
class Meta:
ordering = ('name',)
2017-10-22 21:59:10 -04:00
class UserManager(DjangoUserManager):
def get_queryset(self):
return super(UserManager, self).get_queryset().prefetch_related('keys', 'profiles')
class User(ModelMeta, AbstractUser):
objects = UserManager()
avatar = models.ImageField(upload_to=avatar_path)
2017-10-22 18:04:59 -04:00
note = models.TextField(blank=True)
xmpp = models.EmailField(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 = '/'
@property
def name(self):
return '{0} {1}'.format(self.first_name, self.last_name)
def get_absolute_url(self):
return self.url
2017-10-22 21:33:24 -04:00
@property
def full_url(self):
base = 'https://' + DjangoSite.objects.get_current().domain
return urljoin(base, self.url)
@property
def description(self):
return utils.to_plain(self.note)
@property
def avatar_url(self):
return self.avatar.url
@cached_property
def facebook_id(self):
for p in self.profiles.all():
if p.site.name == 'Facebook':
return p.username
return None
@cached_property
def twitter_username(self):
for p in self.profiles.all():
if p.site.name == 'Twitter':
return '@' + p.username
return None
@property
def json_ld(self):
base = 'https://' + DjangoSite.objects.get_current().domain
return {
'@context': 'http://schema.org',
'@type': 'Person',
'@id': self.full_url,
'url': self.full_url,
'name': self.name,
'email': self.email,
'image': urljoin(base, self.avatar.url),
'givenName': self.first_name,
'familyName': self.last_name,
'sameAs': [profile.url for profile in self.profiles.all()]
}
_metadata = {
'image': 'avatar_url',
'description': 'description',
'og_type': 'profile',
'og_profile_id': 'facebook_id',
'twitter_creator': 'twitter_username',
}
2017-10-22 21:33:24 -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):
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):
if self.site.domain:
return self.name + '@' + self.site.domain
return self.name
2017-10-22 21:59:10 -04:00
@property
def name(self):
return self.display_name or self.username
@property
def url(self):
return self.site.format(username=self.username)
2017-10-22 21:59:10 -04:00
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))