Skip to content

Commit

Permalink
users: allows the 2nd email to be the only one
Browse files Browse the repository at this point in the history
* Makes the main address email optional if patron communication channel
  is email and the additional address email is defined.
* Adds a backend custom validation for this specific case.
* Closes #1499.

Co-Authored-by: Johnny Mariéthoz <Johnny.Mariethoz@rero.ch>
  • Loading branch information
jma committed Dec 15, 2020
1 parent 6492e80 commit 7eaea12
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 6 deletions.
2 changes: 1 addition & 1 deletion data/users.json
Original file line number Diff line number Diff line change
Expand Up @@ -1053,7 +1053,7 @@
"type": {
"$ref": "https://ils.rero.ch/api/patron_types/5"
},
"communication_channel": "email",
"communication_channel": "mail",
"communication_language": "ita"
}
},
Expand Down
16 changes: 15 additions & 1 deletion rero_ils/modules/patrons/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,9 @@ def validate(self, **kwargs):
super(Patron, self).validate(**kwargs)
# We only like to run pids_exist_check if validation_message is True
# and not a string with error from extended_validation
validation_message = True
if self.pid_check:
from ..utils import pids_exists_in_data
validation_message = True
if self.is_patron:
validation_message = pids_exists_in_data(
info='{pid_type} ({pid})'.format(
Expand Down Expand Up @@ -153,9 +153,23 @@ def validate(self, **kwargs):
if subscription_validation_message is not True:
validation_message = subscription_validation_message
break
self._validate_emails()
if validation_message is not True:
raise RecordValidationError(validation_message)

def _validate_emails(self):
"""Check if emails are required.
Check if the user has at least one email if the communication channel
is email.
"""
patron = self.get('patron')
if patron and patron.get('communication_channel') == 'email'\
and self.get('email') is None\
and patron.get('additional_communication_email') is None:
raise RecordValidationError('At least one email should be defined '
'for an email communication channel.')

@classmethod
def create(cls, data, id_=None, delete_pid=False,
dbcommit=False, reindex=False,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
"minLength": 6,
"form": {
"expressionProperties": {
"templateOptions.required": "field.parent.model.roles.some(v => (v === 'librarian' || v === 'system_librarian')) || field.parent.model.patron.communication_channel === 'email'"
"templateOptions.required": "field.parent.model.roles.some(v => (v === 'librarian' || v === 'system_librarian')) || (field.parent.model.patron.communication_channel === 'email' && !field.parent.model.patron.additional_communication_email)"
},
"validation": {
"validators": {
Expand Down Expand Up @@ -246,7 +246,7 @@
},
"communication_channel": {
"title": "Communication channel",
"description": "For the email channel, the user must have an e-mail. Otherwise, no notifications will be sent.",
"description": "For the email channel, the user must have an e-mail or an additional e-mail.",
"type": "string",
"enum": [
"email",
Expand Down
4 changes: 3 additions & 1 deletion tests/api/patrons/test_patrons_rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,9 @@ def test_patrons_post_without_email(client, lib_martigny,
# Create record / POST
del patron_data['pid']
del patron_data['email']
# patron_data['email'] = 'test_librarian@rero.ch'
patron_data['patron']['communication_channel'] = 'mail'
patron_data['patron'][
'additional_communication_email'] = 'test@rero.ch'
patron_data['username'] = 'test_patron'

res, _ = postdata(
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/circulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,7 @@ def patron_sion_without_email(
del patron_sion_data['email']
patron_sion_data['pid'] = 'ptrn10wthoutemail'
patron_sion_data['username'] = 'withoutemail'
patron_sion_data['patron']['communication_channel'] = 'mail'
ptrn = Patron.create(
data=patron_sion_data,
delete_pid=False,
Expand Down
13 changes: 12 additions & 1 deletion tests/ui/patrons/test_patrons_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,19 @@ def test_patron_create_without_email(app, roles, patron_type_children_martigny,
# no data has been created
mailbox.clear()

# create a patron without email
del patron_martigny_data_tmp['email']

# comminication channel require at least one email
patron_martigny_data_tmp['patron']['communication_channel'] = 'email'
with pytest.raises(RecordValidationError):
ptrn = Patron.create(
patron_martigny_data_tmp,
dbcommit=True,
delete_pid=True
)

# create a patron without email
patron_martigny_data_tmp['patron']['communication_channel'] = 'mail'
ptrn = Patron.create(
patron_martigny_data_tmp,
dbcommit=True,
Expand Down

0 comments on commit 7eaea12

Please sign in to comment.