mirror of
https://github.com/zulip/zulip.git
synced 2025-11-13 02:17:19 +00:00
custom fields: Add support for custom URL field type.
This commit is contained in:
committed by
Tim Abbott
parent
d2128105dd
commit
0d7d94d0db
@@ -68,6 +68,8 @@ exports.add_custom_profile_fields_to_settings = function () {
|
|||||||
type = "choice";
|
type = "choice";
|
||||||
} else if (field.type === 4) {
|
} else if (field.type === 4) {
|
||||||
type = "date";
|
type = "date";
|
||||||
|
} else if (field.type === 5) {
|
||||||
|
type = "url";
|
||||||
} else {
|
} else {
|
||||||
blueslip.error("Undefined field type.");
|
blueslip.error("Undefined field type.");
|
||||||
}
|
}
|
||||||
|
|||||||
20
zerver/migrations/0166_add_url_to_profile_field.py
Normal file
20
zerver/migrations/0166_add_url_to_profile_field.py
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
# Generated by Django 1.11.11 on 2018-04-29 17:25
|
||||||
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('zerver', '0165_add_date_to_profile_field'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.AlterField(
|
||||||
|
model_name='customprofilefield',
|
||||||
|
name='field_type',
|
||||||
|
field=models.PositiveSmallIntegerField(choices=[(1, 'Short Text'), (2, 'Long Text'), (4, 'Date'), (5, 'URL'), (3, 'Choice')], default=1),
|
||||||
|
),
|
||||||
|
]
|
||||||
@@ -32,7 +32,8 @@ from django.db.models.signals import pre_save, post_save, post_delete
|
|||||||
from django.utils.translation import ugettext_lazy as _
|
from django.utils.translation import ugettext_lazy as _
|
||||||
from zerver.lib import cache
|
from zerver.lib import cache
|
||||||
from zerver.lib.validator import check_int, check_float, \
|
from zerver.lib.validator import check_int, check_float, \
|
||||||
check_short_string, check_long_string, validate_choice_field, check_date
|
check_short_string, check_long_string, validate_choice_field, check_date, \
|
||||||
|
check_url
|
||||||
from zerver.lib.name_restrictions import is_disposable_domain
|
from zerver.lib.name_restrictions import is_disposable_domain
|
||||||
from zerver.lib.types import Validator, ExtendedValidator, \
|
from zerver.lib.types import Validator, ExtendedValidator, \
|
||||||
ProfileDataElement, ProfileData, FieldTypeData
|
ProfileDataElement, ProfileData, FieldTypeData
|
||||||
@@ -1898,6 +1899,7 @@ class CustomProfileField(models.Model):
|
|||||||
LONG_TEXT = 2
|
LONG_TEXT = 2
|
||||||
CHOICE = 3
|
CHOICE = 3
|
||||||
DATE = 4
|
DATE = 4
|
||||||
|
URL = 5
|
||||||
|
|
||||||
# These are the fields whose validators require field_data
|
# These are the fields whose validators require field_data
|
||||||
# argument as well.
|
# argument as well.
|
||||||
@@ -1914,6 +1916,7 @@ class CustomProfileField(models.Model):
|
|||||||
(SHORT_TEXT, u'Short Text', check_short_string, str),
|
(SHORT_TEXT, u'Short Text', check_short_string, str),
|
||||||
(LONG_TEXT, u'Long Text', check_long_string, str),
|
(LONG_TEXT, u'Long Text', check_long_string, str),
|
||||||
(DATE, u'Date', check_date, str),
|
(DATE, u'Date', check_date, str),
|
||||||
|
(URL, u'URL', check_url, str),
|
||||||
] # type: FieldTypeData
|
] # type: FieldTypeData
|
||||||
|
|
||||||
ALL_FIELD_TYPES = FIELD_TYPE_DATA + EXTENDED_FIELD_TYPE_DATA
|
ALL_FIELD_TYPES = FIELD_TYPE_DATA + EXTENDED_FIELD_TYPE_DATA
|
||||||
|
|||||||
@@ -266,6 +266,18 @@ class CustomProfileDataTest(ZulipTestCase):
|
|||||||
{'data': ujson.dumps([{"id": field.id, "value": 123}])})
|
{'data': ujson.dumps([{"id": field.id, "value": 123}])})
|
||||||
self.assert_json_error(result, u"value[{}] is not a string".format(field.id))
|
self.assert_json_error(result, u"value[{}] is not a string".format(field.id))
|
||||||
|
|
||||||
|
def test_update_invalid_url(self) -> None:
|
||||||
|
self.login(self.example_email("iago"))
|
||||||
|
realm = get_realm('zulip')
|
||||||
|
field = CustomProfileField.objects.get(name="GitHub profile", realm=realm)
|
||||||
|
|
||||||
|
# Update value of field
|
||||||
|
result = self.client_patch("/json/users/me/profile_data",
|
||||||
|
{'data': ujson.dumps([{"id": field.id, "value": u"abc"}])})
|
||||||
|
self.assert_json_error(
|
||||||
|
result,
|
||||||
|
u"value[{}] is not a URL".format(field.id))
|
||||||
|
|
||||||
def test_update_profile_data(self) -> None:
|
def test_update_profile_data(self) -> None:
|
||||||
self.login(self.example_email("iago"))
|
self.login(self.example_email("iago"))
|
||||||
realm = get_realm('zulip')
|
realm = get_realm('zulip')
|
||||||
@@ -275,6 +287,7 @@ class CustomProfileDataTest(ZulipTestCase):
|
|||||||
('Favorite food', 'short text data'),
|
('Favorite food', 'short text data'),
|
||||||
('Favorite editor', 'vim'),
|
('Favorite editor', 'vim'),
|
||||||
('Birthday', '1909-3-5'),
|
('Birthday', '1909-3-5'),
|
||||||
|
('GitHub profile', 'https://github.com/ABC'),
|
||||||
]
|
]
|
||||||
|
|
||||||
data = []
|
data = []
|
||||||
|
|||||||
@@ -281,6 +281,9 @@ class Command(BaseCommand):
|
|||||||
field_data=field_data)
|
field_data=field_data)
|
||||||
birthday = try_add_realm_custom_profile_field(zulip_realm, "Birthday",
|
birthday = try_add_realm_custom_profile_field(zulip_realm, "Birthday",
|
||||||
CustomProfileField.DATE)
|
CustomProfileField.DATE)
|
||||||
|
favorite_website = try_add_realm_custom_profile_field(zulip_realm, "GitHub profile",
|
||||||
|
CustomProfileField.URL,
|
||||||
|
hint="Or your personal blog's URL")
|
||||||
|
|
||||||
# Fill in values for Iago and Hamlet
|
# Fill in values for Iago and Hamlet
|
||||||
hamlet = get_user("hamlet@zulip.com", zulip_realm)
|
hamlet = get_user("hamlet@zulip.com", zulip_realm)
|
||||||
@@ -290,6 +293,7 @@ class Command(BaseCommand):
|
|||||||
{"id": favorite_food.id, "value": "Apples"},
|
{"id": favorite_food.id, "value": "Apples"},
|
||||||
{"id": favorite_editor.id, "value": "emacs"},
|
{"id": favorite_editor.id, "value": "emacs"},
|
||||||
{"id": birthday.id, "value": "2000-1-1"},
|
{"id": birthday.id, "value": "2000-1-1"},
|
||||||
|
{"id": favorite_website.id, "value": "https://github.com/zulip/zulip"},
|
||||||
])
|
])
|
||||||
do_update_user_custom_profile_data(hamlet, [
|
do_update_user_custom_profile_data(hamlet, [
|
||||||
{"id": phone_number.id, "value": "+0-11-23-456-7890"},
|
{"id": phone_number.id, "value": "+0-11-23-456-7890"},
|
||||||
@@ -297,6 +301,7 @@ class Command(BaseCommand):
|
|||||||
{"id": favorite_food.id, "value": "Dark chocolate"},
|
{"id": favorite_food.id, "value": "Dark chocolate"},
|
||||||
{"id": favorite_editor.id, "value": "vim"},
|
{"id": favorite_editor.id, "value": "vim"},
|
||||||
{"id": birthday.id, "value": "1900-1-1"},
|
{"id": birthday.id, "value": "1900-1-1"},
|
||||||
|
{"id": favorite_website.id, "value": "https://blog.zulig.org"},
|
||||||
])
|
])
|
||||||
else:
|
else:
|
||||||
zulip_realm = get_realm("zulip")
|
zulip_realm = get_realm("zulip")
|
||||||
|
|||||||
Reference in New Issue
Block a user