Commit c92066d8 authored by Jean-Baptiste Pasquier's avatar Jean-Baptiste Pasquier

minor: abstract user model

parent 79584954
Pipeline #5325 passed with stage
in 28 seconds
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from .models import LDPUser
admin.site.register(LDPUser, UserAdmin)
\ No newline at end of file
# -*- coding: utf-8 -*-
# Generated by Django 1.11.20 on 2019-08-28 12:01
# Generated by Django 1.11.20 on 2019-09-12 18:54
from __future__ import unicode_literals
from django.conf import settings
import django.contrib.auth.models
import django.contrib.auth.validators
from django.db import migrations, models
import django.db.models.deletion
import django.utils.timezone
class Migration(migrations.Migration):
......@@ -12,14 +15,41 @@ class Migration(migrations.Migration):
initial = True
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('auth', '0008_alter_user_username_max_length'),
]
operations = [
migrations.CreateModel(
name='LDPUser',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('password', models.CharField(max_length=128, verbose_name='password')),
('last_login', models.DateTimeField(blank=True, null=True, verbose_name='last login')),
('is_superuser', models.BooleanField(default=False, help_text='Designates that this user has all permissions without explicitly assigning them.', verbose_name='superuser status')),
('username', models.CharField(error_messages={'unique': 'A user with that username already exists.'}, help_text='Required. 150 characters or fewer. Letters, digits and @/./+/-/_ only.', max_length=150, unique=True, validators=[django.contrib.auth.validators.UnicodeUsernameValidator()], verbose_name='username')),
('first_name', models.CharField(blank=True, max_length=30, verbose_name='first name')),
('last_name', models.CharField(blank=True, max_length=30, verbose_name='last name')),
('email', models.EmailField(blank=True, max_length=254, verbose_name='email address')),
('is_staff', models.BooleanField(default=False, help_text='Designates whether the user can log into this admin site.', verbose_name='staff status')),
('is_active', models.BooleanField(default=True, help_text='Designates whether this user should be treated as active. Unselect this instead of deleting accounts.', verbose_name='active')),
('date_joined', models.DateTimeField(default=django.utils.timezone.now, verbose_name='date joined')),
('slug', models.SlugField(blank=True, null=True, unique=True)),
('groups', models.ManyToManyField(blank=True, help_text='The groups this user belongs to. A user will get all permissions granted to each of their groups.', related_name='user_set', related_query_name='user', to='auth.Group', verbose_name='groups')),
('user_permissions', models.ManyToManyField(blank=True, help_text='Specific permissions for this user.', related_name='user_set', related_query_name='user', to='auth.Permission', verbose_name='user permissions')),
],
options={
'verbose_name': 'user',
'verbose_name_plural': 'users',
},
managers=[
('objects', django.contrib.auth.models.UserManager()),
],
),
migrations.CreateModel(
name='Account',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('slug', models.SlugField(unique=True)),
('picture', models.URLField(blank=True, null=True)),
('issuer', models.URLField(blank=True, null=True)),
('user', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
......@@ -32,6 +62,7 @@ class Migration(migrations.Migration):
name='ChatProfile',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('slug', models.SlugField(unique=True)),
('jabberID', models.CharField(blank=True, max_length=255, null=True)),
('user', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, related_name='chatProfile', to=settings.AUTH_USER_MODEL)),
],
......
import validators
from django.conf import settings
from django.contrib.auth import get_user_model
from django.contrib.auth.models import AbstractUser
from django.db import models
from django.db.models.signals import post_save
from django.dispatch import receiver
from django.urls import reverse_lazy
from django.utils.translation import ugettext_lazy as _
from djangoldp.models import Model
def name(self):
return self.get_full_name()
class LDPUser(AbstractUser, Model):
slug = models.SlugField(unique=True, blank=True, null=True)
class Meta:
verbose_name = _('user')
verbose_name_plural = _('users')
rdf_type = 'foaf:user'
owner_field = 'id'
lookup_field = 'slug'
def webid(self):
# hack : We user webid as username for external user (since it's an uniq identifier too)
if validators.url(self.username):
webid = self.username
else:
webid = '{0}{1}'.format(settings.BASE_URL, reverse_lazy('user-detail', kwargs={'pk': self.pk}))
return webid
def name(self):
return self.get_full_name()
user_model = get_user_model()
user_model.name = name
user_model.webid = webid
def webid(self):
# hack : We user webid as username for external user (since it's an uniq identifier too)
if validators.url(self.username):
webid = self.username
else:
webid = '{0}{1}'.format(settings.BASE_URL, reverse_lazy('ldpuser-detail', kwargs={'pk': self.pk}))
return webid
class Account(Model):
user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
slug = models.SlugField(unique=True)
picture = models.URLField(blank=True, null=True)
issuer = models.URLField(blank=True, null=True)
......@@ -39,6 +46,7 @@ class Account(Model):
('control_account', 'Control'),
)
rdf_context = {'picture': 'foaf:depiction'}
lookup_field = 'slug'
def __str__(self):
return '{} ({})'.format(self.user.get_full_name(), self.user.username)
......@@ -46,6 +54,7 @@ class Account(Model):
class ChatProfile(Model):
user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name="chatProfile")
slug = models.SlugField(unique=True)
jabberID = models.CharField(max_length=255, blank=True, null=True)
class Meta:
......@@ -54,6 +63,7 @@ class ChatProfile(Model):
('view_chatprofile', 'Read'),
('control_chatprofile', 'Control'),
)
lookup_field = 'slug'
def __str__(self):
return '{} (jabberID: {})'.format(self.user.get_full_name(), self.jabberID)
......@@ -71,13 +81,20 @@ class OPClient(Model):
@receiver(post_save, sender=settings.AUTH_USER_MODEL)
def create_account(sender, instance, created, **kwargs):
if created:
Account.objects.create(user=instance)
chat_profile = ChatProfile.objects.create(user=instance)
Account.objects.create(user=instance, slug=instance.username)
chat_profile = ChatProfile.objects.create(user=instance, slug=instance.username)
if settings.JABBER_DEFAULT_HOST:
chat_profile.jabberID = '{}@{}'.format(instance.username, settings.JABBER_DEFAULT_HOST)
chat_profile.save()
instance.slug = instance.username
instance.save()
else:
try:
instance.slug = instance.username
instance.save()
instance.account.slug = instance.username
instance.account.save()
instance.chatProfile.slug = instance.username
instance.chatProfile.save()
except:
pass
......@@ -3,8 +3,6 @@ from django.conf import settings
from django.urls import reverse_lazy
from django.views.decorators.csrf import csrf_exempt
from djangoldp_account.models import webid
def userinfo(claims, user):
# Populate claims dict.
......@@ -16,4 +14,4 @@ def userinfo(claims, user):
def sub_generator(user):
return webid(user)
return user.webid()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment