Add support for social profiles

This commit is contained in:
Danielle McLean 2017-10-23 12:59:10 +11:00
parent 75f2575c30
commit 97948b957d
Signed by: 00dani
GPG key ID: 5A5D2D1AFF12EEC5
7 changed files with 137 additions and 1 deletions

View file

@ -23,6 +23,9 @@
<li>
<a class="u-email" rel="me" href="mailto:{{ user.email }}"><i class="fa fa-envelope"></i> {{ user.email }}</a>
</li>
{% for profile in user.profiles.all %}<li>
<a class="u-url" rel="me" href="{{ profile.url }}"><i class="{{ profile.site.icon }}"></i> {{ profile.name }}</a>
</li>{% endfor %}
</ul>
</div>
</article>

View file

@ -0,0 +1 @@
default_app_config = 'users.apps.UsersConfig'

View file

@ -1,6 +1,6 @@
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
from .models import Key, User
from .models import Key, Profile, Site, User
class KeyInline(admin.TabularInline):
@ -8,13 +8,20 @@ class KeyInline(admin.TabularInline):
extra = 1
class ProfileInline(admin.TabularInline):
model = Profile
extra = 1
class UserAdmin(BaseUserAdmin):
fieldsets = BaseUserAdmin.fieldsets + (
('Profile', {'fields': ('avatar', 'note')}),
)
inlines = (
KeyInline,
ProfileInline,
)
admin.site.register(Site)
admin.site.register(User, UserAdmin)

View file

@ -3,3 +3,4 @@ from django.apps import AppConfig
class UsersConfig(AppConfig):
name = 'users'
verbose_name = 'Users and Profiles'

View file

@ -0,0 +1,55 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.6 on 2017-10-23 01:43
from __future__ import unicode_literals
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('users', '0003_key'),
]
operations = [
migrations.CreateModel(
name='Profile',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('username', models.CharField(max_length=100)),
('display_name', models.CharField(blank=True, max_length=100)),
],
options={
'ordering': ('site', 'username'),
},
),
migrations.CreateModel(
name='Site',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=100, unique=True)),
('icon', models.CharField(max_length=100)),
('url', models.CharField(max_length=100)),
],
options={
'ordering': ('name',),
},
),
migrations.AddField(
model_name='profile',
name='site',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='users.Site'),
),
migrations.AddField(
model_name='profile',
name='user',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL),
),
migrations.AddField(
model_name='user',
name='profiles',
field=models.ManyToManyField(through='users.Profile', to='users.Site'),
),
]

View file

@ -0,0 +1,26 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.6 on 2017-10-23 01:58
from __future__ import unicode_literals
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('users', '0004_auto_20171023_0143'),
]
operations = [
migrations.RemoveField(
model_name='user',
name='profiles',
),
migrations.AlterField(
model_name='profile',
name='user',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='profiles', to=settings.AUTH_USER_MODEL),
),
]

View file

@ -6,11 +6,48 @@ def avatar_path(instance, name):
return 'avatars/{id}/{name}'.format(id=instance.id, name=name)
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',)
class User(AbstractUser):
avatar = models.ImageField(upload_to=avatar_path)
note = models.TextField(blank=True)
class Profile(models.Model):
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')
class Key(models.Model):
user = models.ForeignKey(
User,
@ -20,5 +57,11 @@ class Key(models.Model):
fingerprint = models.CharField(max_length=40)
file = models.FileField(upload_to='keys')
@property
def key_id(self): return self.fingerprint[32:]
def __str__(self):
return self.key_id
def pretty_print(self):
return " ".join(self.fingerprint[i:i+4] for i in range(0, 40, 4))