Streamline saves on user create

This commit is contained in:
Mouse Reeve 2021-08-03 07:43:03 -07:00
parent 62befa9295
commit ee7bdc956a
3 changed files with 16 additions and 13 deletions

View file

@ -243,7 +243,6 @@ class User(OrderedCollectionPageMixin, AbstractUser):
# generate a username that uses the domain (webfinger format)
actor_parts = urlparse(self.remote_id)
self.username = "%s@%s" % (self.username, actor_parts.netloc)
super().save(*args, **kwargs)
# this user already exists, no need to populate fields
if not created:
@ -276,7 +275,7 @@ class User(OrderedCollectionPageMixin, AbstractUser):
self.key_pair = KeyPair.objects.create(
remote_id="%s/#main-key" % self.remote_id
)
self.save(broadcast=False)
self.save(broadcast=False, update_fields=["key_pair"])
shelves = [
{

View file

@ -178,12 +178,15 @@ def update_rank_on_shelving(sender, instance, *args, **kwargs):
@receiver(signals.post_save, sender=models.User)
# pylint: disable=unused-argument, too-many-arguments
def add_new_user(sender, instance, created, **kwargs):
def add_new_user(sender, instance, created, update_fields=None, **kwargs):
"""a new user, wow how cool"""
# a new user is found, create suggestions for them
if created and instance.local:
rerank_suggestions_task.delay(instance.id)
if update_fields and not "discoverable" in update_fields:
return
# this happens on every save, not just when discoverability changes, annoyingly
if instance.discoverable:
rerank_user_task.delay(instance.id, update_only=False)

View file

@ -11,16 +11,17 @@ class Activitystreams(TestCase):
def setUp(self):
"""use a test csv"""
self.local_user = models.User.objects.create_user(
"mouse", "mouse@mouse.mouse", "password", local=True, localname="mouse"
)
self.another_user = models.User.objects.create_user(
"nutria",
"nutria@nutria.nutria",
"password",
local=True,
localname="nutria",
)
with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"):
self.local_user = models.User.objects.create_user(
"mouse", "mouse@mouse.mouse", "password", local=True, localname="mouse"
)
self.another_user = models.User.objects.create_user(
"nutria",
"nutria@nutria.nutria",
"password",
local=True,
localname="nutria",
)
with patch("bookwyrm.models.user.set_remote_server.delay"):
self.remote_user = models.User.objects.create_user(
"rat",