From 937413fac9bb9247f85ae563d3e198b40edd124e Mon Sep 17 00:00:00 2001 From: Nick Krichevsky Date: Fri, 7 Jul 2023 14:28:39 -0400 Subject: [PATCH] Add Poster model to serve as generic user in the fediverse --- ...te_poster_remove_localuser_display_name.py | 33 +++++++++++++ ...ser_display_name_alter_localuser_poster.py | 47 +++++++++++++++++++ printpub/user/models/__init__.py | 2 + .../user/{models.py => models/local_user.py} | 9 ++-- printpub/user/models/poster.py | 10 ++++ 5 files changed, 98 insertions(+), 3 deletions(-) create mode 100644 printpub/user/migrations/0002_intermediate_poster_remove_localuser_display_name.py create mode 100644 printpub/user/migrations/0003_remove_localuser_display_name_alter_localuser_poster.py create mode 100644 printpub/user/models/__init__.py rename printpub/user/{models.py => models/local_user.py} (73%) create mode 100644 printpub/user/models/poster.py diff --git a/printpub/user/migrations/0002_intermediate_poster_remove_localuser_display_name.py b/printpub/user/migrations/0002_intermediate_poster_remove_localuser_display_name.py new file mode 100644 index 0000000..b669571 --- /dev/null +++ b/printpub/user/migrations/0002_intermediate_poster_remove_localuser_display_name.py @@ -0,0 +1,33 @@ +# Generated by Django 4.2.3 on 2023-07-07 18:21 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + replaces = [('user', '0002_poster_remove_localuser_display_name_and_more'), ('user', '0003_alter_localuser_display_name')] + + dependencies = [ + ('user', '0001_initial'), + ] + + operations = [ + migrations.CreateModel( + name='Poster', + fields=[ + ('id', models.AutoField(primary_key=True, serialize=False)), + ('display_name', models.CharField(max_length=128)), + ], + ), + migrations.AddField( + model_name='localuser', + name='poster', + field=models.OneToOneField(null=True, on_delete=django.db.models.deletion.RESTRICT, to='user.poster'), + ), + migrations.AlterField( + model_name='localuser', + name='display_name', + field=models.CharField(max_length=128, null=True), + ), + ] diff --git a/printpub/user/migrations/0003_remove_localuser_display_name_alter_localuser_poster.py b/printpub/user/migrations/0003_remove_localuser_display_name_alter_localuser_poster.py new file mode 100644 index 0000000..a1acea2 --- /dev/null +++ b/printpub/user/migrations/0003_remove_localuser_display_name_alter_localuser_poster.py @@ -0,0 +1,47 @@ +# Generated by Django 4.2.3 on 2023-07-07 18:23 + +from django.db import migrations, models +import django.db.models.deletion + + +def make_poster_with_display_name(apps, schema_editor): + User = apps.get_model("user", "LocalUser") + Poster = apps.get_model("user", "Poster") + + for user in User.objects.all(): + poster = Poster(display_name=user.display_name) + poster.save() + user.poster = poster + user.save() + + +def restore_display_names(apps, schema_editor): + User = apps.get_model("user", "LocalUser") + + for user in User.objects.all(): + user.display_name = user.poster.display_name + user.save() + + +class Migration(migrations.Migration): + dependencies = [ + ( + "user", + "0002_intermediate_poster_remove_localuser_display_name", + ), + ] + + operations = [ + migrations.RunPython(make_poster_with_display_name, restore_display_names), + migrations.RemoveField( + model_name="localuser", + name="display_name", + ), + migrations.AlterField( + model_name="localuser", + name="poster", + field=models.OneToOneField( + on_delete=django.db.models.deletion.RESTRICT, to="user.poster" + ), + ), + ] diff --git a/printpub/user/models/__init__.py b/printpub/user/models/__init__.py new file mode 100644 index 0000000..2dbc2ee --- /dev/null +++ b/printpub/user/models/__init__.py @@ -0,0 +1,2 @@ +from printpub.user.models.local_user import LocalUser +from printpub.user.models.poster import Poster diff --git a/printpub/user/models.py b/printpub/user/models/local_user.py similarity index 73% rename from printpub/user/models.py rename to printpub/user/models/local_user.py index e628b76..7fb181c 100644 --- a/printpub/user/models.py +++ b/printpub/user/models/local_user.py @@ -2,18 +2,21 @@ from django.db import models from django.contrib.auth import models as auth_models +from . import poster + + class LocalUser(auth_models.AbstractUser): - REQUIRED_FIELDS = ["display_name"] + REQUIRED_FIELDS = ["poster"] first_name = None last_name = None - display_name = models.CharField(max_length=128) + poster = models.OneToOneField(poster.Poster, on_delete=models.RESTRICT) def get_full_name(self) -> str: """ Override. We do not use first/last name so we must give something else """ - return self.display_name + return self.poster.display_name def get_short_name(self) -> str: """ diff --git a/printpub/user/models/poster.py b/printpub/user/models/poster.py new file mode 100644 index 0000000..4bc9124 --- /dev/null +++ b/printpub/user/models/poster.py @@ -0,0 +1,10 @@ +from django.db import models + + +class Poster(models.Model): + """ + A Poster is someone who can create a post in the fediverse, whether they belong to this server or not. + """ + + id = models.AutoField(primary_key=True) + display_name = models.CharField(max_length=128)