2019-05-27 10:59:55 +02:00
|
|
|
"""
|
|
|
|
This module stores data for "External Account" custom profile field.
|
|
|
|
"""
|
2020-06-21 02:36:20 +02:00
|
|
|
from django.core.exceptions import ValidationError
|
2019-05-27 10:59:55 +02:00
|
|
|
from django.utils.translation import ugettext as _
|
|
|
|
|
|
|
|
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
|
|
|
|
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.
|
|
|
|
# text: Field text for admins - custom profile field in org settngs view
|
|
|
|
# name: Field label or name - user profile in user settings view
|
|
|
|
# hint: Field hint for realm users
|
|
|
|
# url_patter: Field url linkifier
|
2019-05-27 10:59:55 +02:00
|
|
|
DEFAULT_EXTERNAL_ACCOUNTS = {
|
|
|
|
"twitter": {
|
|
|
|
"text": "Twitter",
|
2019-08-24 13:52:25 +02:00
|
|
|
"url_pattern": "https://twitter.com/%(username)s",
|
|
|
|
"name": "Twitter",
|
|
|
|
"hint": "Enter your Twitter username",
|
2019-05-27 10:59:55 +02:00
|
|
|
},
|
|
|
|
"github": {
|
|
|
|
"text": 'GitHub',
|
2019-08-24 13:52:25 +02:00
|
|
|
"url_pattern": "https://github.com/%(username)s",
|
|
|
|
"name": "GitHub",
|
|
|
|
"hint": "Enter your GitHub username",
|
2019-05-27 10:59:55 +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(
|
|
|
|
[('subtype', check_required_string)],
|
2019-08-03 02:32:09 +02:00
|
|
|
[('url_pattern', check_external_account_url_pattern)],
|
2019-05-27 10:59:55 +02:00
|
|
|
)
|
2020-06-21 02:36:20 +02:00
|
|
|
field_validator('field_data', field_data)
|
2019-05-27 10:59:55 +02:00
|
|
|
|
|
|
|
field_subtype = field_data.get('subtype')
|
|
|
|
if field_subtype not in DEFAULT_EXTERNAL_ACCOUNTS.keys():
|
|
|
|
if field_subtype == "custom":
|
|
|
|
if 'url_pattern' not in field_data.keys():
|
2020-06-21 02:36:20 +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
|