Skip to content

Commit

Permalink
feat(core): add boolean field to profile to mark a user as mock user
Browse files Browse the repository at this point in the history
  • Loading branch information
MagneticNeedle committed Jul 7, 2023
1 parent c904893 commit 5b4b700
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
20 changes: 20 additions & 0 deletions bfportal/core/migrations/0081_profile_is_mock_user.py
Original file line number Diff line number Diff line change
@@ -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",
),
),
]
12 changes: 11 additions & 1 deletion bfportal/core/models/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 5b4b700

Please sign in to comment.