Skip to content

Commit f3c27b3

Browse files
committed
feat #4138 Changes requested in review
1 parent 22f9199 commit f3c27b3

File tree

7 files changed

+20
-27
lines changed

7 files changed

+20
-27
lines changed

src/core/forms/forms.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import uuid
77
import json
88
import os
9+
import warnings
910

1011
from django import forms
1112
from django.db.models import Q
@@ -131,7 +132,7 @@ def __init__(self, *args, **kwargs):
131132

132133
class JournalContactForm(ContactPersonForm):
133134
def __init__(self, *args, **kwargs):
134-
return DeprecationWarning("Use ContactPersonForm instead.")
135+
warnings.warn("Use ContactPersonForm instead.")
135136
super().__init__(*args, **kwargs)
136137

137138

src/core/logic.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1289,8 +1289,6 @@ def get_contact_form(request, contact_person_id):
12891289

12901290

12911291
def send_contact_message(contact_form, request):
1292-
# Todo: do we need this if the field is bleached rich text?
1293-
body = contact_form.cleaned_data["body"].replace("\n", "<br>")
12941292
sender_email = contact_form.cleaned_data["sender"]
12951293
contact_person = get_object_or_404(
12961294
models.ContactPerson,
@@ -1318,7 +1316,7 @@ def send_contact_message(contact_form, request):
13181316
"from": sender_email,
13191317
"to": recipient_email,
13201318
"subject": contact_form.cleaned_data["subject"],
1321-
"body": body,
1319+
"body": contact_form.cleaned_data["body"],
13221320
"custom_reply_to": contact_form.cleaned_data["sender"],
13231321
},
13241322
log_dict=log_dict,

src/core/migrations/0110_rename_contacts_contactperson_and_more.py

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,6 @@
99
logger = get_logger(__name__)
1010

1111

12-
def normalize_email(email):
13-
"""
14-
Copied from Django 4.2's AccountManager.
15-
"""
16-
email = email or ""
17-
try:
18-
email_name, domain_part = email.strip().rsplit("@", 1)
19-
except ValueError:
20-
pass
21-
else:
22-
email = email_name + "@" + domain_part.lower()
23-
return email
24-
25-
2612
def connect_contact_person_to_account(apps, schema_editor):
2713
ContactPerson = apps.get_model("core", "ContactPerson")
2814
Account = apps.get_model("core", "Account")
@@ -34,26 +20,34 @@ def connect_contact_person_to_account(apps, schema_editor):
3420
if matching_accounts.exists():
3521
account = matching_accounts.first()
3622
else:
37-
normalized_email = normalize_email(email)
23+
normalized_email = Account.objects.normalize_email(email)
3824
account = Account.objects.create(
3925
email=normalized_email,
4026
username=normalized_email,
4127
first_name=" ".join(contact_person.name.split()[:-1]),
4228
last_name=contact_person.name.split()[-1],
4329
)
4430
contact_person.account = account
31+
contact_person.email = ""
4532
contact_person.save()
4633

4734

4835
def infer_contact_message_target(apps, account):
4936
Journal = apps.get_model("journal", "Journal")
5037
Press = apps.get_model("press", "Press")
38+
ContactPerson = apps.get_model("core", "ContactPerson")
39+
40+
contact_person_records = ContactPerson.objects.filter(account=account)
41+
is_staff = account.is_staff
5142
journals_where_editor = Journal.objects.filter(
5243
accountrole__user=account,
5344
accountrole__role__slug="editor",
5445
)
55-
is_staff = account.is_staff
56-
if is_staff and journals_where_editor.count() == 0:
46+
47+
if contact_person_records.count() == 1:
48+
# This person must have been contacted on the same site as their ContactPerson record.
49+
target = contact_person_records.first().object
50+
elif is_staff and journals_where_editor.count() == 0:
5751
# This person must have been contacted in their press staff capacity.
5852
target = Press.objects.first()
5953
elif not is_staff and journals_where_editor.count() == 1:
@@ -83,7 +77,7 @@ def move_contact_message_to_log_entry(apps, schema_editor):
8377
if matching_accounts.exists():
8478
account = matching_accounts.first()
8579
else:
86-
normalized_email = normalize_email(email)
80+
normalized_email = Account.objects.normalize_email(email)
8781
account = Account.objects.create(
8882
email=normalized_email,
8983
username=normalized_email,

src/core/models.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,8 @@ def create(self, **kwargs):
393393

394394

395395
class AccountManager(BaseUserManager):
396+
use_in_migrations = True
397+
396398
def create_user(self, username=None, password=None, email=None, **kwargs):
397399
"""Creates a user from the given username or email
398400
In Janeway, users rely on email addresses to log in. For compatibility

src/journal/logic.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ def set_article_image(request, article):
397397

398398

399399
def send_contact_message(new_contact, request):
400-
raise DeprecationWarning(
400+
warnings.warn(
401401
"`journal.logic.send_contact_message` is deprecated. "
402402
"Use `core.logic.send_contact_message` instead."
403403
)

src/utils/install/journal_defaults.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5628,7 +5628,7 @@
56285628
"type": "rich-text"
56295629
},
56305630
"value": {
5631-
"default": "<p>{{ body|safe }}</p><hr><p>The above was sent from the <a href=\"{% stateless_site_url site 'contact' %}\">contact page for {{ site.name }}</a> by {{ from }}.</p>"
5631+
"default": "<p>{{ body|safe|linebreaksbr }}</p><hr><p>The above was sent from the <a href=\"{% stateless_site_url site 'contact' %}\">contact page for {{ site.name }}</a> by {{ from }}.</p>"
56325632
},
56335633
"editable_by": [
56345634
"editor",

src/utils/models.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@
2020
from django.utils.translation import gettext_lazy as _
2121

2222
from utils.logger import get_logger
23-
from utils.shared import get_ip_address
2423
from utils.importers.up import get_input_value_by_name
25-
from utils.shared import get_ip_address
2624

2725
logger = get_logger(__name__)
2826

@@ -215,7 +213,7 @@ def viewable_as_contact_message_by(self, account):
215213
"""
216214
if self.types == "Contact Message":
217215
if Addressee.objects.filter(
218-
email__iexact=account.email,
216+
email__iexact=account.__class__.objects.normalize_email(account.email),
219217
).exists():
220218
return True
221219
return False

0 commit comments

Comments
 (0)