2019-05-27 10:59:55 +02:00
|
|
|
"""
|
2023-12-05 18:45:07 +01:00
|
|
|
This module stores data for "external account" custom profile field.
|
2019-05-27 10:59:55 +02:00
|
|
|
"""
|
2024-01-29 00:32:21 +01:00
|
|
|
|
2022-09-20 22:45:56 +02:00
|
|
|
from dataclasses import dataclass
|
2022-09-16 12:08:28 +02:00
|
|
|
|
2020-06-21 02:36:20 +02:00
|
|
|
from django.core.exceptions import ValidationError
|
2021-04-16 00:57:30 +02:00
|
|
|
from django.utils.translation import gettext as _
|
2022-09-16 12:08:28 +02:00
|
|
|
from django.utils.translation import gettext_lazy
|
2022-09-19 21:43:34 +02:00
|
|
|
from django_stubs_ext import StrPromise
|
2019-05-27 10:59:55 +02:00
|
|
|
|
|
|
|
from zerver.lib.types import ProfileFieldData
|
2020-06-11 00:54:34 +02:00
|
|
|
from zerver.lib.validator import (
|
|
|
|
check_dict_only,
|
|
|
|
check_external_account_url_pattern,
|
|
|
|
check_required_string,
|
|
|
|
)
|
2019-05-27 10:59:55 +02:00
|
|
|
|
2022-09-20 22:45:56 +02:00
|
|
|
|
2020-03-28 01:25:56 +01:00
|
|
|
# Default external account fields are by default available
|
2019-08-24 13:52:25 +02:00
|
|
|
# to realm admins, where realm admin only need to select
|
|
|
|
# the default field and other values(i.e. name, url) will be
|
|
|
|
# fetch from this dictionary.
|
2022-09-20 22:45:56 +02:00
|
|
|
@dataclass
|
|
|
|
class ExternalAccount:
|
|
|
|
text: str # Field text for admins - custom profile field in org settings view
|
2022-09-19 21:43:34 +02:00
|
|
|
name: StrPromise # Field label or name - user profile in user settings view
|
2022-09-20 22:45:56 +02:00
|
|
|
hint: str # Field hint for realm users
|
|
|
|
url_pattern: str # Field URL linkifier
|
|
|
|
|
|
|
|
|
2019-05-27 10:59:55 +02:00
|
|
|
DEFAULT_EXTERNAL_ACCOUNTS = {
|
2022-09-20 22:45:56 +02:00
|
|
|
"twitter": ExternalAccount(
|
|
|
|
text="Twitter",
|
|
|
|
url_pattern="https://twitter.com/%(username)s",
|
|
|
|
name=gettext_lazy("Twitter username"),
|
|
|
|
hint="",
|
|
|
|
),
|
|
|
|
"github": ExternalAccount(
|
|
|
|
text="GitHub",
|
|
|
|
url_pattern="https://github.com/%(username)s",
|
|
|
|
name=gettext_lazy("GitHub username"),
|
|
|
|
hint="",
|
|
|
|
),
|
2019-05-27 10:59:55 +02:00
|
|
|
}
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2024-07-12 02:30:17 +02:00
|
|
|
def get_default_external_accounts() -> dict[str, dict[str, str]]:
|
2022-09-20 22:45:56 +02:00
|
|
|
return {
|
|
|
|
subtype: {
|
|
|
|
"text": external_account.text,
|
|
|
|
"url_pattern": external_account.url_pattern,
|
|
|
|
"name": str(external_account.name),
|
|
|
|
"hint": external_account.hint,
|
|
|
|
}
|
|
|
|
for subtype, external_account in DEFAULT_EXTERNAL_ACCOUNTS.items()
|
|
|
|
}
|
2022-09-16 12:08:28 +02:00
|
|
|
|
|
|
|
|
2020-06-21 02:36:20 +02:00
|
|
|
def validate_external_account_field_data(field_data: ProfileFieldData) -> ProfileFieldData:
|
2019-05-27 10:59:55 +02:00
|
|
|
field_validator = check_dict_only(
|
2021-02-12 08:20:45 +01:00
|
|
|
[("subtype", check_required_string)],
|
|
|
|
[("url_pattern", check_external_account_url_pattern)],
|
2019-05-27 10:59:55 +02:00
|
|
|
)
|
2021-02-12 08:20:45 +01:00
|
|
|
field_validator("field_data", field_data)
|
2019-05-27 10:59:55 +02:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
field_subtype = field_data.get("subtype")
|
2023-07-23 21:20:53 +02:00
|
|
|
if field_subtype not in DEFAULT_EXTERNAL_ACCOUNTS:
|
2019-05-27 10:59:55 +02:00
|
|
|
if field_subtype == "custom":
|
2023-07-23 21:20:53 +02:00
|
|
|
if "url_pattern" not in field_data:
|
2020-10-23 02:43:28 +02:00
|
|
|
raise ValidationError(_("Custom external account must define URL pattern"))
|
2019-05-27 10:59:55 +02:00
|
|
|
else:
|
2020-06-21 02:36:20 +02:00
|
|
|
raise ValidationError(_("Invalid external account type"))
|
2019-05-27 10:59:55 +02:00
|
|
|
|
2020-06-21 02:36:20 +02:00
|
|
|
return field_data
|