Add Poster model to serve as generic user in the fediverse

master
Nick Krichevsky 2023-07-07 14:28:39 -04:00
parent 905d5a02d0
commit 937413fac9
5 changed files with 98 additions and 3 deletions

View File

@ -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),
),
]

View File

@ -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"
),
),
]

View File

@ -0,0 +1,2 @@
from printpub.user.models.local_user import LocalUser
from printpub.user.models.poster import Poster

View File

@ -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:
"""

View File

@ -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)