custom fields: Add support for custom URL field type.

This commit is contained in:
Yashashvi Dave
2018-04-25 22:50:58 +05:30
committed by Tim Abbott
parent d2128105dd
commit 0d7d94d0db
5 changed files with 44 additions and 1 deletions

View File

@@ -68,6 +68,8 @@ exports.add_custom_profile_fields_to_settings = function () {
type = "choice";
} else if (field.type === 4) {
type = "date";
} else if (field.type === 5) {
type = "url";
} else {
blueslip.error("Undefined field type.");
}

View 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),
),
]

View File

@@ -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 zerver.lib import cache
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.types import Validator, ExtendedValidator, \
ProfileDataElement, ProfileData, FieldTypeData
@@ -1898,6 +1899,7 @@ class CustomProfileField(models.Model):
LONG_TEXT = 2
CHOICE = 3
DATE = 4
URL = 5
# These are the fields whose validators require field_data
# argument as well.
@@ -1914,6 +1916,7 @@ class CustomProfileField(models.Model):
(SHORT_TEXT, u'Short Text', check_short_string, str),
(LONG_TEXT, u'Long Text', check_long_string, str),
(DATE, u'Date', check_date, str),
(URL, u'URL', check_url, str),
] # type: FieldTypeData
ALL_FIELD_TYPES = FIELD_TYPE_DATA + EXTENDED_FIELD_TYPE_DATA

View File

@@ -266,6 +266,18 @@ class CustomProfileDataTest(ZulipTestCase):
{'data': ujson.dumps([{"id": field.id, "value": 123}])})
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:
self.login(self.example_email("iago"))
realm = get_realm('zulip')
@@ -275,6 +287,7 @@ class CustomProfileDataTest(ZulipTestCase):
('Favorite food', 'short text data'),
('Favorite editor', 'vim'),
('Birthday', '1909-3-5'),
('GitHub profile', 'https://github.com/ABC'),
]
data = []

View File

@@ -281,6 +281,9 @@ class Command(BaseCommand):
field_data=field_data)
birthday = try_add_realm_custom_profile_field(zulip_realm, "Birthday",
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
hamlet = get_user("hamlet@zulip.com", zulip_realm)
@@ -290,6 +293,7 @@ class Command(BaseCommand):
{"id": favorite_food.id, "value": "Apples"},
{"id": favorite_editor.id, "value": "emacs"},
{"id": birthday.id, "value": "2000-1-1"},
{"id": favorite_website.id, "value": "https://github.com/zulip/zulip"},
])
do_update_user_custom_profile_data(hamlet, [
{"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_editor.id, "value": "vim"},
{"id": birthday.id, "value": "1900-1-1"},
{"id": favorite_website.id, "value": "https://blog.zulig.org"},
])
else:
zulip_realm = get_realm("zulip")