Skip to content

Commit e7c4f6c

Browse files
committed
feat #4138 Upgrade and refactor contact system
1 parent 894ee44 commit e7c4f6c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+1278
-255
lines changed

src/core/admin.py

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ class AccountAdmin(UserAdmin):
131131
admin_utils.RepositoryRoleInline,
132132
admin_utils.EditorialGroupMemberInline,
133133
admin_utils.StaffGroupMemberInline,
134+
admin_utils.ContactPersonInline,
134135
admin_utils.PasswordResetInline,
135136
]
136137

@@ -558,35 +559,51 @@ def _journal(self, obj):
558559
return obj.group.journal if obj else ""
559560

560561

561-
class ContactsAdmin(admin.ModelAdmin):
562-
list_display = ("name", "email", "role", "object", "sequence")
562+
class ContactPersonAdmin(admin.ModelAdmin):
563+
list_display = ("_name", "_email", "role", "object", "sequence")
563564
list_filter = (
564565
admin_utils.GenericRelationJournalFilter,
565566
admin_utils.GenericRelationPressFilter,
566567
)
567-
search_fields = ("name", "email", "role")
568+
search_fields = (
569+
"account__first_name",
570+
"account__middle_name",
571+
"account__last_name",
572+
"account__email",
573+
"role",
574+
)
575+
raw_id_fields = ("account",)
568576

577+
def _name(self, obj):
578+
return obj.account.full_name() if obj and obj.account else ""
569579

570-
class ContactAdmin(admin.ModelAdmin):
580+
def _email(self, obj):
581+
return obj.account.email if obj and obj.account else ""
582+
583+
584+
class ContactMessageAdmin(admin.ModelAdmin):
571585
list_display = (
572586
"subject",
573587
"sender",
574-
"recipient",
575-
"client_ip",
588+
"account",
576589
"date_sent",
577590
"object",
578591
)
579592
list_filter = (
580593
admin_utils.GenericRelationJournalFilter,
581594
admin_utils.GenericRelationPressFilter,
582595
"date_sent",
583-
"recipient",
596+
"account",
584597
)
585598
search_fields = (
586599
"subject",
587600
"sender",
588-
"recipient",
601+
"account__first_name",
602+
"account__middle_name",
603+
"account__last_name",
604+
"account__email",
589605
)
606+
raw_id_fields = ("account",)
590607
date_hierarchy = "date_sent"
591608

592609

@@ -766,8 +783,8 @@ def _person(self, obj):
766783
(models.Workflow, WorkflowAdmin),
767784
(models.WorkflowLog, WorkflowLogAdmin),
768785
(models.LoginAttempt, LoginAttemptAdmin),
769-
(models.Contacts, ContactsAdmin),
770-
(models.Contact, ContactAdmin),
786+
(models.ContactPerson, ContactPersonAdmin),
787+
(models.ContactMessage, ContactMessageAdmin),
771788
(models.AccessRequest, AccessRequestAdmin),
772789
(models.Organization, OrganizationAdmin),
773790
(models.OrganizationName, OrganizationNameAdmin),

src/core/forms/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
GetResetTokenForm,
2121
JournalArticleForm,
2222
JournalAttributeForm,
23-
JournalContactForm,
23+
ContactMessageForm,
24+
ContactPersonForm,
2425
JournalImageForm,
2526
JournalStylingForm,
2627
JournalSubmissionForm,

src/core/forms/forms.py

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,18 +84,16 @@ def clean(self):
8484
return cleaned_data
8585

8686

87-
class JournalContactForm(JanewayTranslationModelForm):
87+
class ContactPersonForm(JanewayTranslationModelForm):
8888
def __init__(self, *args, **kwargs):
8989
next_sequence = kwargs.pop("next_sequence", None)
90-
super(JournalContactForm, self).__init__(*args, **kwargs)
90+
super().__init__(*args, **kwargs)
9191
if next_sequence:
9292
self.fields["sequence"].initial = next_sequence
9393

9494
class Meta:
95-
model = models.Contacts
95+
model = models.ContactPerson
9696
fields = (
97-
"name",
98-
"email",
9997
"role",
10098
"sequence",
10199
)
@@ -105,6 +103,34 @@ class Meta:
105103
)
106104

107105

106+
class ContactMessageForm(forms.ModelForm, CaptchaForm):
107+
def __init__(self, *args, **kwargs):
108+
subject = kwargs.pop("subject", None)
109+
account = kwargs.pop("account", None)
110+
contact_people = kwargs.pop("contact_people", None)
111+
super().__init__(*args, **kwargs)
112+
self.fields["account"].required = True
113+
self.fields["account"].choices = [
114+
(person.account.pk, person.account.full_name()) for person in contact_people
115+
]
116+
117+
if subject:
118+
self.fields["subject"].initial = subject
119+
120+
if account:
121+
self.fields["account"].initial = account.pk
122+
123+
class Meta:
124+
model = models.ContactMessage
125+
fields = ("account", "sender", "subject", "body")
126+
127+
128+
class JournalContactForm(ContactPersonForm):
129+
def __init__(self, *args, **kwargs):
130+
return DeprecationWarning("Use ContactPersonForm instead.")
131+
super().__init__(*args, **kwargs)
132+
133+
108134
class EditorialGroupForm(JanewayTranslationModelForm):
109135
def __init__(self, *args, **kwargs):
110136
next_sequence = kwargs.pop("next_sequence", None)

src/core/include_urls.py

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,11 @@
120120
press_views.IdentifierManager.as_view(),
121121
name="press_identifier_manager",
122122
),
123+
re_path(
124+
r"^press/contact/$",
125+
press_views.contact,
126+
name="press_contact",
127+
),
123128
# Notes
124129
re_path(
125130
r"^article/(?P<article_id>\d+)/note/(?P<note_id>\d+)/delete/$",
@@ -256,22 +261,31 @@
256261
core_views.article_image_edit,
257262
name="core_article_image_edit",
258263
),
259-
# Journal Contacts
260-
re_path(r"^manager/contacts/$", core_views.contacts, name="core_journal_contacts"),
264+
# Contact People
261265
re_path(
262-
r"^manager/contacts/add/$",
263-
core_views.edit_contacts,
264-
name="core_new_journal_contact",
266+
r"^manager/contacts/$",
267+
core_views.contact_people,
268+
name="core_contact_people",
265269
),
266270
re_path(
267-
r"^manager/contacts/(?P<contact_id>\d+)/$",
268-
core_views.edit_contacts,
269-
name="core_journal_contact",
271+
r"^manager/contacts/order/$",
272+
core_views.contact_people_reorder,
273+
name="core_contact_people_reorder",
270274
),
271275
re_path(
272-
r"^manager/contacts/order/$",
273-
core_views.contacts_order,
274-
name="core_journal_contacts_order",
276+
r"^manager/contacts/search/$",
277+
core_views.PotentialContactListView.as_view(),
278+
name="core_contact_person_search",
279+
),
280+
re_path(
281+
r"^manager/contacts/add/(?P<account_id>\d+)/$",
282+
core_views.contact_person_create,
283+
name="core_contact_person_create",
284+
),
285+
re_path(
286+
r"^manager/contacts/(?P<contact_person_id>\d+)/$",
287+
core_views.contact_person_update,
288+
name="core_contact_person_update",
275289
),
276290
# Editorial Team
277291
re_path(

src/core/logic.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1221,3 +1221,59 @@ def create_organization_name(request):
12211221
% {"organization": organization_name},
12221222
)
12231223
return organization_name
1224+
1225+
1226+
def get_contact_form(request):
1227+
subject = request.GET.get("subject", "")
1228+
contact_people = request.site_type.contact_people
1229+
recipient_email = request.GET.get("recipient", "")
1230+
account = None
1231+
if recipient_email:
1232+
try:
1233+
account = models.Account.objects.get(
1234+
contactperson__pk__in=[cp.pk for cp in contact_people],
1235+
email__iexact=recipient_email,
1236+
)
1237+
except models.Account.DoesNotExist:
1238+
pass
1239+
1240+
if request.method == "POST":
1241+
contact_form = forms.ContactMessageForm(
1242+
request.POST,
1243+
contact_people=contact_people,
1244+
)
1245+
else:
1246+
contact_form = forms.ContactMessageForm(
1247+
subject=subject,
1248+
contact_people=contact_people,
1249+
account=account,
1250+
)
1251+
return contact_form, contact_people
1252+
1253+
1254+
def send_contact_message(contact_form, request):
1255+
message = contact_form.save(commit=False)
1256+
message.content_type = request.model_content_type
1257+
message.object_id = request.site_type.pk
1258+
message.save()
1259+
body = message.body.replace("\n", "<br>")
1260+
notify_helpers.send_email_with_body_from_setting_template(
1261+
request=request,
1262+
template="contact_message",
1263+
subject=message.subject,
1264+
to=message.account.email,
1265+
context={
1266+
"site": request.journal or request.press,
1267+
"from": message.sender,
1268+
"to": message.account.email,
1269+
"subject": message.subject,
1270+
"body": body,
1271+
"custom_reply_to": message.sender,
1272+
},
1273+
)
1274+
messages.add_message(
1275+
request,
1276+
messages.SUCCESS,
1277+
_("Your message has been sent to %(recipient)s.")
1278+
% {"recipient": message.account.full_name()},
1279+
)

src/core/middleware.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -221,9 +221,11 @@ def process_request(request):
221221
"cms_page",
222222
"cms_nav",
223223
"website_index",
224-
"core_journal_contacts",
225-
"core_journal_contact",
226-
"core_journal_contacts_order",
224+
"core_contact_people",
225+
"core_contact_person_search",
226+
"core_contact_person_create",
227+
"core_contact_person_update",
228+
"core_contact_people_reorder",
227229
"contact",
228230
"core_edit_settings_group",
229231
]

0 commit comments

Comments
 (0)