prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  User/Contact bug: Signals double saving, overwriting data for test cases
  ### Current Behavior
  
  A bug was introduced [with a PR](https://github.com/cisagov/manage.get.gov/pull/1491) that fixed our oidc login clearing out user information. To fix this, the Contact object was linked to the User object, such that they would remain in sync.
  
  However, this introduced a sneaky double-save bug wherein if signals get called more than once for the same contact object (as we found happens in our test cases), it overrides information when it should not do so.
  
  ![image](https://github.com/cisagov/manage.get.gov/assets/141044360/f9d35949-5138-4337-a189-9e2a843191f5)
  ![image](https://github.com/cisagov/manage.get.gov/assets/141044360/917e6db7-6cfd-48c8-a9b0-e0b8f01f2831)
  
  ### Expected Behavior
  
  When a `Contact` object is saved, the `save()` function checks to see if `self.user` is not None. When it is not, it will update the  `self.user` object with whatever value is located at `self`. It appears that in certain situations, (such as our test cases),  the `User` field is behaving as if it were a one-to-many relationship (one Contact object to many User objects). This should not be the case, and `Contact` should only update one `User` field.
  
  ### Steps to Reproduce
  
  1. Create a new test case in `test_admin.py`, and create 3 fake User objects. Populate them with unique data. Then, log their values. Note that their data seems to get overwritten.
  
  
  ### Environment
  
  _No response_
  
  ### Additional Context
  
  This bug seems to be originating in the `signals.py` file in the ` handle_profile` class. By passing in a flag that disables the save behavior (or just commenting it out), this issue seems to resolve. 
  ![image](https://github.com/cisagov/manage.get.gov/assets/141044360/e19a1508-72c7-405a-96db-3540305dcc4b)
  
  
  ### Issue Links
  
  🔄 Relates to: [#1464 / #1468](https://github.com/cisagov/manage.get.gov/pull/1491)
  🔄 Relates to: [this PR](https://github.com/cisagov/manage.get.gov/pull/1543) (pinpoints where the behavior is occurring but does not solve it)
  
  --- END ISSUE ---
  
  Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
  --- BEGIN FILES ---
  Path: `src/registrar/models/contact.py`
  Content:
  ```
  1 from django.db import models
  2 
  3 from phonenumber_field.modelfields import PhoneNumberField  # type: ignore
  4 
  5 from .utility.time_stamped_model import TimeStampedModel
  6 
  7 
  8 class Contact(TimeStampedModel):
  9 
  10     """Contact information follows a similar pattern for each contact."""
  11 
  12     user = models.OneToOneField(
  13         "registrar.User",
  14         null=True,
  15         blank=True,
  16         on_delete=models.SET_NULL,
  17     )
  18 
  19     first_name = models.TextField(
  20         null=True,
  21         blank=True,
  22         help_text="First name",
  23         verbose_name="first name / given name",
  24         db_index=True,
  25     )
  26     middle_name = models.TextField(
  27         null=True,
  28         blank=True,
  29         help_text="Middle name (optional)",
  30     )
  31     last_name = models.TextField(
  32         null=True,
  33         blank=True,
  34         help_text="Last name",
  35         verbose_name="last name / family name",
  36         db_index=True,
  37     )
  38     title = models.TextField(
  39         null=True,
  40         blank=True,
  41         help_text="Title",
  42         verbose_name="title or role in your organization",
  43     )
  44     email = models.EmailField(
  45         null=True,
  46         blank=True,
  47         help_text="Email",
  48         db_index=True,
  49     )
  50     phone = PhoneNumberField(
  51         null=True,
  52         blank=True,
  53         help_text="Phone",
  54         db_index=True,
  55     )
  56 
  57     def get_formatted_name(self):
  58         """Returns the contact's name in Western order."""
  59         names = [n for n in [self.first_name, self.middle_name, self.last_name] if n]
  60         return " ".join(names) if names else "Unknown"
  61 
  62     def save(self, *args, **kwargs):
  63         # Call the parent class's save method to perform the actual save
  64         super().save(*args, **kwargs)
  65 
  66         # Update the related User object's first_name and last_name
  67         if self.user:
  68             self.user.first_name = self.first_name
  69             self.user.last_name = self.last_name
  70             self.user.save()
  71 
  72     def __str__(self):
  73         if self.first_name or self.last_name:
  74             return self.get_formatted_name()
  75         elif self.email:
  76             return self.email
  77         elif self.pk:
  78             return str(self.pk)
  79         else:
  80             return ""
  81
  ```
  --- END FILES ---
  
  Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.
  
  Here is an example:
  
  ```diff
  diff --git a/examples/server_async.py b/examples/server_async.py
  --- a/examples/server_async.py
  +++ b/examples/server_async.py
  @@ -313,4 +313,4 @@
   
   
   if __name__ == "__main__":
  -    asyncio.run(run_async_server("."), debug=True)
  +    asyncio.run(run_async_server(), debug=True)
  diff --git a/examples/server_sync.py b/examples/server_sync.py
  --- a/examples/server_sync.py
  +++ b/examples/server_sync.py
  @@ -313,5 +313,5 @@
   
   
   if __name__ == "__main__":
  -    server = run_sync_server(".")
  +    server = run_sync_server()
       server.shutdown()
  
  ```
  
golden_diff: diff --git a/src/registrar/models/contact.py b/src/registrar/models/contact.py
  --- a/src/registrar/models/contact.py
  +++ b/src/registrar/models/contact.py
  @@ -64,7 +64,7 @@
           super().save(*args, **kwargs)
   
           # Update the related User object's first_name and last_name
  -        if self.user:
  +        if self.user and (not self.user.first_name or not self.user.last_name):
               self.user.first_name = self.first_name
               self.user.last_name = self.last_name
               self.user.save()
  
verification_info: {"golden_diff": "diff --git a/src/registrar/models/contact.py b/src/registrar/models/contact.py\n--- a/src/registrar/models/contact.py\n+++ b/src/registrar/models/contact.py\n@@ -64,7 +64,7 @@\n         super().save(*args, **kwargs)\n \n         # Update the related User object's first_name and last_name\n-        if self.user:\n+        if self.user and (not self.user.first_name or not self.user.last_name):\n             self.user.first_name = self.first_name\n             self.user.last_name = self.last_name\n             self.user.save()\n", "issue": "User/Contact bug: Signals double saving, overwriting data for test cases\n### Current Behavior\r\n\r\nA bug was introduced [with a PR](https://github.com/cisagov/manage.get.gov/pull/1491) that fixed our oidc login clearing out user information. To fix this, the Contact object was linked to the User object, such that they would remain in sync.\r\n\r\nHowever, this introduced a sneaky double-save bug wherein if signals get called more than once for the same contact object (as we found happens in our test cases), it overrides information when it should not do so.\r\n\r\n![image](https://github.com/cisagov/manage.get.gov/assets/141044360/f9d35949-5138-4337-a189-9e2a843191f5)\r\n![image](https://github.com/cisagov/manage.get.gov/assets/141044360/917e6db7-6cfd-48c8-a9b0-e0b8f01f2831)\r\n\r\n### Expected Behavior\r\n\r\nWhen a `Contact` object is saved, the `save()` function checks to see if `self.user` is not None. When it is not, it will update the  `self.user` object with whatever value is located at `self`. It appears that in certain situations, (such as our test cases),  the `User` field is behaving as if it were a one-to-many relationship (one Contact object to many User objects). This should not be the case, and `Contact` should only update one `User` field.\r\n\r\n### Steps to Reproduce\r\n\r\n1. Create a new test case in `test_admin.py`, and create 3 fake User objects. Populate them with unique data. Then, log their values. Note that their data seems to get overwritten.\r\n\r\n\r\n### Environment\r\n\r\n_No response_\r\n\r\n### Additional Context\r\n\r\nThis bug seems to be originating in the `signals.py` file in the ` handle_profile` class. By passing in a flag that disables the save behavior (or just commenting it out), this issue seems to resolve. \r\n![image](https://github.com/cisagov/manage.get.gov/assets/141044360/e19a1508-72c7-405a-96db-3540305dcc4b)\r\n\r\n\r\n### Issue Links\r\n\r\n\ud83d\udd04 Relates to: [#1464 / #1468](https://github.com/cisagov/manage.get.gov/pull/1491)\r\n\ud83d\udd04 Relates to: [this PR](https://github.com/cisagov/manage.get.gov/pull/1543) (pinpoints where the behavior is occurring but does not solve it)\n", "before_files": [{"content": "from django.db import models\n\nfrom phonenumber_field.modelfields import PhoneNumberField  # type: ignore\n\nfrom .utility.time_stamped_model import TimeStampedModel\n\n\nclass Contact(TimeStampedModel):\n\n    \"\"\"Contact information follows a similar pattern for each contact.\"\"\"\n\n    user = models.OneToOneField(\n        \"registrar.User\",\n        null=True,\n        blank=True,\n        on_delete=models.SET_NULL,\n    )\n\n    first_name = models.TextField(\n        null=True,\n        blank=True,\n        help_text=\"First name\",\n        verbose_name=\"first name / given name\",\n        db_index=True,\n    )\n    middle_name = models.TextField(\n        null=True,\n        blank=True,\n        help_text=\"Middle name (optional)\",\n    )\n    last_name = models.TextField(\n        null=True,\n        blank=True,\n        help_text=\"Last name\",\n        verbose_name=\"last name / family name\",\n        db_index=True,\n    )\n    title = models.TextField(\n        null=True,\n        blank=True,\n        help_text=\"Title\",\n        verbose_name=\"title or role in your organization\",\n    )\n    email = models.EmailField(\n        null=True,\n        blank=True,\n        help_text=\"Email\",\n        db_index=True,\n    )\n    phone = PhoneNumberField(\n        null=True,\n        blank=True,\n        help_text=\"Phone\",\n        db_index=True,\n    )\n\n    def get_formatted_name(self):\n        \"\"\"Returns the contact's name in Western order.\"\"\"\n        names = [n for n in [self.first_name, self.middle_name, self.last_name] if n]\n        return \" \".join(names) if names else \"Unknown\"\n\n    def save(self, *args, **kwargs):\n        # Call the parent class's save method to perform the actual save\n        super().save(*args, **kwargs)\n\n        # Update the related User object's first_name and last_name\n        if self.user:\n            self.user.first_name = self.first_name\n            self.user.last_name = self.last_name\n            self.user.save()\n\n    def __str__(self):\n        if self.first_name or self.last_name:\n            return self.get_formatted_name()\n        elif self.email:\n            return self.email\n        elif self.pk:\n            return str(self.pk)\n        else:\n            return \"\"\n", "path": "src/registrar/models/contact.py"}], "after_files": [{"content": "from django.db import models\n\nfrom phonenumber_field.modelfields import PhoneNumberField  # type: ignore\n\nfrom .utility.time_stamped_model import TimeStampedModel\n\n\nclass Contact(TimeStampedModel):\n\n    \"\"\"Contact information follows a similar pattern for each contact.\"\"\"\n\n    user = models.OneToOneField(\n        \"registrar.User\",\n        null=True,\n        blank=True,\n        on_delete=models.SET_NULL,\n    )\n\n    first_name = models.TextField(\n        null=True,\n        blank=True,\n        help_text=\"First name\",\n        verbose_name=\"first name / given name\",\n        db_index=True,\n    )\n    middle_name = models.TextField(\n        null=True,\n        blank=True,\n        help_text=\"Middle name (optional)\",\n    )\n    last_name = models.TextField(\n        null=True,\n        blank=True,\n        help_text=\"Last name\",\n        verbose_name=\"last name / family name\",\n        db_index=True,\n    )\n    title = models.TextField(\n        null=True,\n        blank=True,\n        help_text=\"Title\",\n        verbose_name=\"title or role in your organization\",\n    )\n    email = models.EmailField(\n        null=True,\n        blank=True,\n        help_text=\"Email\",\n        db_index=True,\n    )\n    phone = PhoneNumberField(\n        null=True,\n        blank=True,\n        help_text=\"Phone\",\n        db_index=True,\n    )\n\n    def get_formatted_name(self):\n        \"\"\"Returns the contact's name in Western order.\"\"\"\n        names = [n for n in [self.first_name, self.middle_name, self.last_name] if n]\n        return \" \".join(names) if names else \"Unknown\"\n\n    def save(self, *args, **kwargs):\n        # Call the parent class's save method to perform the actual save\n        super().save(*args, **kwargs)\n\n        # Update the related User object's first_name and last_name\n        if self.user and (not self.user.first_name or not self.user.last_name):\n            self.user.first_name = self.first_name\n            self.user.last_name = self.last_name\n            self.user.save()\n\n    def __str__(self):\n        if self.first_name or self.last_name:\n            return self.get_formatted_name()\n        elif self.email:\n            return self.email\n        elif self.pk:\n            return str(self.pk)\n        else:\n            return \"\"\n", "path": "src/registrar/models/contact.py"}]}