Use atomic when creating new users

This commit is contained in:
Mouse Reeve 2021-09-07 10:09:28 -07:00
parent 7bad2a34c4
commit 5a224b5aa4

View file

@ -274,6 +274,7 @@ class User(OrderedCollectionPageMixin, AbstractUser):
transaction.on_commit(lambda: set_remote_server.delay(self.id)) transaction.on_commit(lambda: set_remote_server.delay(self.id))
return return
with transaction.atomic():
# populate fields for local users # populate fields for local users
link = site_link() link = site_link()
self.remote_id = f"{link}/user/{self.localname}" self.remote_id = f"{link}/user/{self.localname}"
@ -294,10 +295,25 @@ class User(OrderedCollectionPageMixin, AbstractUser):
# create keys and shelves for new local users # create keys and shelves for new local users
self.key_pair = KeyPair.objects.create( self.key_pair = KeyPair.objects.create(
remote_id="%s/#main-key" % self.remote_id remote_id=f"{self.remote_id}/#main-key"
) )
self.save(broadcast=False, update_fields=["key_pair"]) self.save(broadcast=False, update_fields=["key_pair"])
self.create_shelves()
def delete(self, *args, **kwargs):
"""deactivate rather than delete a user"""
self.is_active = False
# skip the logic in this class's save()
super().save(*args, **kwargs)
@property
def local_path(self):
"""this model doesn't inherit bookwyrm model, so here we are"""
return "/user/%s" % (self.localname or self.username)
def create_shelves(self):
"""default shelves for a new user"""
shelves = [ shelves = [
{ {
"name": "To Read", "name": "To Read",
@ -321,17 +337,6 @@ class User(OrderedCollectionPageMixin, AbstractUser):
editable=False, editable=False,
).save(broadcast=False) ).save(broadcast=False)
def delete(self, *args, **kwargs):
"""deactivate rather than delete a user"""
self.is_active = False
# skip the logic in this class's save()
super().save(*args, **kwargs)
@property
def local_path(self):
"""this model doesn't inherit bookwyrm model, so here we are"""
return "/user/%s" % (self.localname or self.username)
class KeyPair(ActivitypubMixin, BookWyrmModel): class KeyPair(ActivitypubMixin, BookWyrmModel):
"""public and private keys for a user""" """public and private keys for a user"""