diff --git a/bfportal/core/migrations/0081_profile_is_mock_user.py b/bfportal/core/migrations/0081_profile_is_mock_user.py new file mode 100644 index 0000000..f00e388 --- /dev/null +++ b/bfportal/core/migrations/0081_profile_is_mock_user.py @@ -0,0 +1,20 @@ +# Generated by Django 4.1.7 on 2023-07-07 15:25 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + dependencies = [ + ("core", "0080_alter_experiencepagetag_tag"), + ] + + operations = [ + migrations.AddField( + model_name="profile", + name="is_mock_user", + field=models.BooleanField( + default=False, + help_text="If set to true, this user was created by the mock command, and is a fake user", + ), + ), + ] diff --git a/bfportal/core/models/users.py b/bfportal/core/models/users.py index 2f720cd..221fbee 100644 --- a/bfportal/core/models/users.py +++ b/bfportal/core/models/users.py @@ -38,6 +38,11 @@ class Profile(models.Model): user = models.OneToOneField(User, on_delete=models.CASCADE) liked = models.ManyToManyField(ExperiencePage, blank=True) + is_mock_user = models.BooleanField( + default=False, + null=False, + help_text="If set to true, this user was created by the mock command, and is a fake user", + ) hide_username = models.BooleanField( default=False, null=False, @@ -81,7 +86,12 @@ def create_user_profile(sender, instance: User, created, **kwargs): if created: if (group := Group.objects.filter(name="self edit")).exists(): instance.groups.add(group[0]) - Profile.objects.create(user=instance) + new_profile = Profile.objects.create(user=instance) + if getattr(instance, "__mock_user", None): + # custom object variable that is added in `mock` management command + # is not available everywhere, instead use profile.mock_user + new_profile.is_mock_user = True + new_profile.save() @staticmethod @receiver(post_save, sender=User)