|
9 | 9 | def connect_contact_person_to_account(apps, schema_editor): |
10 | 10 | ContactPerson = apps.get_model("core", "ContactPerson") |
11 | 11 | Account = apps.get_model("core", "Account") |
12 | | - languages = ["cy", "de", "en", "en-us", "en_us", "es", "fr", "it", "nl"] |
13 | 12 | for contact_person in ContactPerson.objects.all(): |
14 | | - account, created = Account.objects.get_or_create( |
| 13 | + matching_accounts = Account.objects.filter( |
15 | 14 | username=contact_person.email.lower(), |
16 | | - defaults={ |
17 | | - "first_name": " ".join(contact_person.name.split()[:-1]), |
18 | | - "last_name": contact_person.name.split()[-1], |
19 | | - "email": contact_person.email, |
20 | | - }, |
21 | 15 | ) |
| 16 | + if matching_accounts.exists(): |
| 17 | + account = matching_accounts.first() |
| 18 | + else: |
| 19 | + account = Account.objects.create( |
| 20 | + email=contact_person.email, |
| 21 | + username=contact_person.email.lower(), |
| 22 | + first_name=" ".join(contact_person.name.split()[:-1]), |
| 23 | + last_name=contact_person.name.split()[-1], |
| 24 | + ) |
22 | 25 | contact_person.account = account |
23 | | - for language in languages: |
24 | | - with translation.override(language): |
25 | | - language_var = "name_{}".format(language) |
26 | | - setattr(contact_person, language_var, "") |
27 | | - contact_person.email = "" |
28 | 26 | contact_person.save() |
29 | 27 |
|
30 | 28 |
|
31 | 29 | def connect_contact_message_to_account(apps, schema_editor): |
32 | 30 | ContactMessage = apps.get_model("core", "ContactMessage") |
33 | 31 | Account = apps.get_model("core", "Account") |
34 | 32 | for contact_message in ContactMessage.objects.all(): |
35 | | - account, created = Account.objects.get_or_create( |
| 33 | + matching_accounts = Account.objects.filter( |
36 | 34 | username=contact_message.recipient.lower(), |
37 | | - defaults={ |
38 | | - "email": contact_message.recipient, |
39 | | - }, |
40 | 35 | ) |
| 36 | + if matching_accounts.exists(): |
| 37 | + account = matching_accounts.first() |
| 38 | + else: |
| 39 | + account = Account.objects.create( |
| 40 | + email=contact_message.recipient, |
| 41 | + username=contact_message.recipient.lower(), |
| 42 | + ) |
41 | 43 | contact_message.account = account |
42 | 44 | contact_message.recipient = "" |
43 | 45 | contact_message.client_ip = "" |
|
0 commit comments