SciPost Code Repository
Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
SciPost
Manage
Activity
Members
Labels
Plan
Issues
118
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
SciPost
SciPost
Commits
f8bf1f2a
Commit
f8bf1f2a
authored
7 months ago
by
George Katsikas
Browse files
Options
Downloads
Patches
Plain Diff
allow queryset resolution in json email config
parent
dfa1f2ee
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
scipost_django/mails/core.py
+37
-16
37 additions, 16 deletions
scipost_django/mails/core.py
with
37 additions
and
16 deletions
scipost_django/mails/core.py
+
37
−
16
View file @
f8bf1f2a
...
@@ -272,14 +272,22 @@ class MailEngine:
...
@@ -272,14 +272,22 @@ class MailEngine:
was_list
=
isinstance
(
emails
,
list
)
was_list
=
isinstance
(
emails
,
list
)
emails
=
emails
if
was_list
else
[
emails
]
emails
=
emails
if
was_list
else
[
emails
]
valid_emails
=
[
valid_emails
:
list
[
str
|
list
[
str
]]
=
[
valid_entry
valid_entry
for
entry
in
emails
for
entry
in
emails
if
(
valid_entry
:
=
self
.
_validate_email_addresses
(
entry
))
if
(
valid_entry
:
=
self
.
_validate_email_addresses
(
entry
))
]
]
# Chain list of lists to a single list.
flattened_valid_emails
=
[]
for
entry
in
valid_emails
:
if
isinstance
(
entry
,
list
):
flattened_valid_emails
.
extend
(
entry
)
else
:
flattened_valid_emails
.
append
(
entry
)
# Remove duplicate recipients from email list
# Remove duplicate recipients from email list
valid_emails
=
list
(
set
(
valid_emails
))
valid_emails
=
list
(
set
(
flattened_
valid_emails
))
if
len
(
valid_emails
)
==
0
:
if
len
(
valid_emails
)
==
0
:
raise
ConfigurationError
(
raise
ConfigurationError
(
...
@@ -294,24 +302,37 @@ class MailEngine:
...
@@ -294,24 +302,37 @@ class MailEngine:
"""
"""
Return email address given raw email, email prefix or database relation given in `entry`.
Return email address given raw email, email prefix or database relation given in `entry`.
"""
"""
# Separate entry from possible filter function.
entry
,
filter_func
=
entry
.
split
(
"
|
"
)
if
"
|
"
in
entry
else
(
entry
,
""
)
filter_func
,
args
=
(
filter_func
.
split
(
"
:
"
)
if
"
:
"
in
filter_func
else
(
filter_func
,
"
email
"
)
)
# Email string
if
re
.
match
(
"
[^@]+@[^@]+\.[^@]+
"
,
entry
):
if
re
.
match
(
"
[^@]+@[^@]+\.[^@]+
"
,
entry
):
# Email string
return
entry
return
entry
#
if the email address is given as a prefix of the form `[recipient]@`, add domain name:
#
Domain prefixed `[recipient]@`
elif
re
.
match
(
"
[^@]+@$
"
,
entry
):
elif
re
.
match
(
"
[^@]+@$
"
,
entry
):
return
"
%s%s
"
%
(
entry
,
get_current_domain
())
return
f
"
{
entry
}{
get_current_domain
()
}
"
elif
self
.
template_variables
[
"
object
"
]:
# Database relation
mail_to
=
self
.
template_variables
[
"
object
"
]
elif
obj
:
=
self
.
template_variables
[
"
object
"
]:
for
attr
in
entry
.
split
(
"
|
"
)[
0
].
split
(
"
.
"
):
obj
=
self
.
template_variables
[
"
object
"
]
print
(
obj
,
entry
,
filter_func
,
args
)
# Recurse through object properties to get the email address.
for
attr
in
entry
.
split
(
"
.
"
):
try
:
try
:
mail_to
=
getattr
(
mail_to
,
attr
)
obj
=
getattr
(
obj
,
attr
)
if
inspect
.
ismethod
(
mail_to
):
if
isinstance
(
obj
,
models
.
Manager
):
mail_to
=
mail_to
()
obj
=
list
(
obj
.
values_list
(
args
,
flat
=
True
))
elif
inspect
.
ismethod
(
obj
):
obj
=
obj
()
except
AttributeError
:
except
AttributeError
:
# Invalid property/mail
# Allow None values
if
entry
.
endswith
(
"
|None
"
):
if
filter_func
==
"
None
"
:
# Allow None values
return
None
return
None
raise
KeyError
(
"
The property (%s) does not exist.
"
%
entry
)
raise
KeyError
(
"
The property (%s) does not exist.
"
%
entry
)
return
mail_to
return
obj
raise
KeyError
(
"
Neither an email adress nor db instance is given.
"
)
raise
KeyError
(
"
Neither an email address nor db instance is given.
"
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment