Skip to content

Commit 1dfdd93

Browse files
committed
feat: merge mail-types into single function
1 parent 5c514c6 commit 1dfdd93

File tree

3 files changed

+27
-34
lines changed

3 files changed

+27
-34
lines changed

tapir/accounts/forms.py

+8-4
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,22 @@
66

77
from tapir import settings
88
from tapir.accounts.models import TapirUser
9-
from tapir.core.tapir_email_base import get_mails_not_mandatory, get_mails_mandatory
9+
from tapir.core.tapir_email_base import (
10+
get_mail_types,
11+
)
1012
from tapir.settings import PERMISSION_COOP_ADMIN, GROUP_VORSTAND
1113
from tapir.utils.forms import DateInputTapir, TapirPhoneNumberField
1214

1315

1416
class TapirUserSelfUpdateForm(forms.ModelForm):
1517
mandatory_mails = forms.MultipleChoiceField(
1618
required=False,
17-
choices=get_mails_mandatory(default="both"),
19+
choices=get_mail_types(enabled_by_default="both", mandatory=True),
1820
label=_("Mandatory Emails"),
1921
widget=CheckboxSelectMultiple(),
20-
initial=[m[0] for m in get_mails_mandatory(default="both")],
22+
initial=[
23+
m[0] for m in get_mail_types(enabled_by_default="both", mandatory=True)
24+
],
2125
)
2226

2327
def __init__(self, *args, **kwargs):
@@ -29,7 +33,7 @@ class Meta:
2933
fields = ["usage_name", "pronouns", "additional_mails"]
3034
widgets = {
3135
"additional_mails": forms.widgets.CheckboxSelectMultiple(
32-
choices=get_mails_not_mandatory(default="both")
36+
choices=get_mail_types(enabled_by_default="both", mandatory=False)
3337
)
3438
}
3539

tapir/accounts/models.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from tapir import utils, settings
1717
from tapir.coop.config import get_ids_of_users_registered_to_a_shift_with_capability
1818
from tapir.core.config import help_text_displayed_name
19-
from tapir.core.tapir_email_base import get_mails_not_mandatory
19+
from tapir.core.tapir_email_base import get_mails_not_mandatory, get_mail_types
2020
from tapir.log.models import UpdateModelLogEntry
2121
from tapir.settings import (
2222
PERMISSIONS,
@@ -62,7 +62,7 @@ class TapirUserManager(UserManager.from_queryset(TapirUserQuerySet)):
6262

6363

6464
def get_mails_not_mandatory_and_default():
65-
return [m[0] for m in get_mails_not_mandatory(default=True)]
65+
return [m[0] for m in get_mail_types(mandatory=False, enabled_by_default=True)]
6666

6767

6868
class TapirUser(AbstractUser):

tapir/core/tapir_email_base.py

+17-28
Original file line numberDiff line numberDiff line change
@@ -17,46 +17,35 @@
1717

1818
all_emails: Dict[str, Type[TapirEmailBase]] = {}
1919

20-
enabled_by_default_options = Literal[True, False, "both"]
20+
MAIL_OPTIONS_ = Literal[True, False, "both"]
2121

2222

23-
def get_all_emails(
24-
default: enabled_by_default_options = True,
25-
) -> list[Type[TapirEmailBase]]:
23+
def get_mail_types(
24+
enabled_by_default: MAIL_OPTIONS_ = True,
25+
mandatory: MAIL_OPTIONS_ = True,
26+
) -> List[Tuple[str, str]]:
2627
"""
2728
default="both" returns both, default and non-default mails.
2829
default=False returns mails not being sent by default
2930
"""
30-
if default != "both":
31-
return [
32-
mail
33-
for mail in TapirEmailBase.__subclasses__()
34-
if mail.enabled_by_default is default
35-
]
36-
else:
37-
return [mail for mail in TapirEmailBase.__subclasses__()]
38-
39-
40-
def get_mails_not_mandatory(
41-
default: enabled_by_default_options = "both",
42-
) -> List[Tuple[str, str]]:
43-
return [
44-
(mail.get_unique_id(), mail.get_name())
45-
for mail in get_all_emails(default=default)
46-
if mail.mandatory is False
47-
]
4831

32+
def filter_mail(mail):
33+
return (
34+
enabled_by_default == "both"
35+
or mail.enabled_by_default is enabled_by_default
36+
) and (mandatory == "both" or mail.mandatory is mandatory)
4937

50-
def get_mails_mandatory(
51-
default: enabled_by_default_options = "both",
52-
) -> List[Tuple[str, str]]:
5338
return [
5439
(mail.get_unique_id(), mail.get_name())
55-
for mail in get_all_emails(default=default)
56-
if mail.mandatory is True
40+
for mail in TapirEmailBase.__subclasses__()
41+
if filter_mail(mail)
5742
]
5843

5944

45+
def get_mails_not_mandatory() -> List[Tuple[str, str]]:
46+
return get_mail_types(mandatory=False, enabled_by_default="both")
47+
48+
6049
class TapirEmailBase:
6150
enabled_by_default = True # mails are opt-out by default
6251
mandatory = True # mails are mandatory by default
@@ -145,7 +134,7 @@ def send_to_share_owner(self, actor: User | None, recipient: ShareOwner):
145134

146135
def user_wants_to_or_has_to_receive_mail(self, user: TapirUser):
147136
return (self.get_unique_id() in user.additional_mails) | (
148-
self.get_unique_id() in [x[0] for x in get_mails_mandatory()]
137+
self.get_unique_id() in [x[0] for x in get_mail_types(mandatory=True)]
149138
)
150139

151140
def send_to_tapir_user(self, actor: User | None, recipient: TapirUser):

0 commit comments

Comments
 (0)