mirror of https://github.com/zulip/zulip.git
custom fields: Add support for custom date field type.
This commit is contained in:
parent
94d787aa2e
commit
4033f210af
|
@ -66,6 +66,8 @@ function add_custom_profile_fields_to_settings() {
|
|||
type = "text";
|
||||
} else if (field.type === 3) {
|
||||
type = "choice";
|
||||
} else if (field.type === 4) {
|
||||
type = "date";
|
||||
} else {
|
||||
blueslip.error("Undefined field type.");
|
||||
}
|
||||
|
|
|
@ -32,6 +32,7 @@ from django.core.validators import validate_email, URLValidator
|
|||
from typing import Callable, Iterable, Optional, Tuple, TypeVar, Text, cast, \
|
||||
Dict
|
||||
|
||||
from datetime import datetime
|
||||
from zerver.lib.request import JsonableError
|
||||
from zerver.lib.types import Validator, ProfileFieldData
|
||||
|
||||
|
@ -67,6 +68,15 @@ def check_capped_string(max_length: int) -> Callable[[str, object], Optional[str
|
|||
def check_long_string(var_name: str, val: object) -> Optional[str]:
|
||||
return check_capped_string(500)(var_name, val)
|
||||
|
||||
def check_date(var_name: str, val: object) -> Optional[str]:
|
||||
if not isinstance(val, str):
|
||||
return _('%s is not a string') % (var_name,)
|
||||
try:
|
||||
datetime.strptime(val, '%Y-%m-%d')
|
||||
except ValueError:
|
||||
return _('%s is not a date') % (var_name,)
|
||||
return None
|
||||
|
||||
def check_int(var_name: str, val: object) -> Optional[str]:
|
||||
if not isinstance(val, int):
|
||||
return _('%s is not an integer') % (var_name,)
|
||||
|
|
|
@ -0,0 +1,20 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# Generated by Django 1.11.11 on 2018-04-29 17:24
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('zerver', '0164_stream_history_public_to_subscribers'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterField(
|
||||
model_name='customprofilefield',
|
||||
name='field_type',
|
||||
field=models.PositiveSmallIntegerField(choices=[(1, 'Short Text'), (2, 'Long Text'), (4, 'Date'), (3, 'Choice')], default=1),
|
||||
),
|
||||
]
|
|
@ -32,7 +32,7 @@ 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_short_string, check_long_string, validate_choice_field, check_date
|
||||
from zerver.lib.name_restrictions import is_disposable_domain
|
||||
from zerver.lib.types import Validator, ExtendedValidator, \
|
||||
ProfileDataElement, ProfileData, FieldTypeData
|
||||
|
@ -1897,6 +1897,7 @@ class CustomProfileField(models.Model):
|
|||
SHORT_TEXT = 1
|
||||
LONG_TEXT = 2
|
||||
CHOICE = 3
|
||||
DATE = 4
|
||||
|
||||
# These are the fields whose validators require field_data
|
||||
# argument as well.
|
||||
|
@ -1912,6 +1913,7 @@ class CustomProfileField(models.Model):
|
|||
# Type, Name, Validator, Converter
|
||||
(SHORT_TEXT, u'Short Text', check_short_string, str),
|
||||
(LONG_TEXT, u'Long Text', check_long_string, str),
|
||||
(DATE, u'Date', check_date, str),
|
||||
] # type: FieldTypeData
|
||||
|
||||
ALL_FIELD_TYPES = FIELD_TYPE_DATA + EXTENDED_FIELD_TYPE_DATA
|
||||
|
|
|
@ -253,6 +253,19 @@ class CustomProfileDataTest(ZulipTestCase):
|
|||
result,
|
||||
u"value[{}] is too long (limit: 50 characters).".format(field.id))
|
||||
|
||||
def test_update_invalid_date(self) -> None:
|
||||
self.login(self.example_email("iago"))
|
||||
realm = get_realm('zulip')
|
||||
field = CustomProfileField.objects.get(name="Birthday", realm=realm)
|
||||
|
||||
# Update value of field
|
||||
result = self.client_patch("/json/users/me/profile_data",
|
||||
{'data': ujson.dumps([{"id": field.id, "value": u"a-b-c"}])})
|
||||
self.assert_json_error(result, u"value[{}] is not a date".format(field.id))
|
||||
result = self.client_patch("/json/users/me/profile_data",
|
||||
{'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_profile_data(self) -> None:
|
||||
self.login(self.example_email("iago"))
|
||||
realm = get_realm('zulip')
|
||||
|
@ -261,6 +274,7 @@ class CustomProfileDataTest(ZulipTestCase):
|
|||
('Biography', 'long text data'),
|
||||
('Favorite food', 'short text data'),
|
||||
('Favorite editor', 'vim'),
|
||||
('Birthday', '1909-3-5'),
|
||||
]
|
||||
|
||||
data = []
|
||||
|
|
|
@ -279,6 +279,8 @@ class Command(BaseCommand):
|
|||
"Favorite editor",
|
||||
CustomProfileField.CHOICE,
|
||||
field_data=field_data)
|
||||
birthday = try_add_realm_custom_profile_field(zulip_realm, "Birthday",
|
||||
CustomProfileField.DATE)
|
||||
|
||||
# Fill in values for Iago and Hamlet
|
||||
hamlet = get_user("hamlet@zulip.com", zulip_realm)
|
||||
|
@ -287,12 +289,14 @@ class Command(BaseCommand):
|
|||
{"id": biography.id, "value": "Betrayer of Othello."},
|
||||
{"id": favorite_food.id, "value": "Apples"},
|
||||
{"id": favorite_editor.id, "value": "emacs"},
|
||||
{"id": birthday.id, "value": "2000-1-1"},
|
||||
])
|
||||
do_update_user_custom_profile_data(hamlet, [
|
||||
{"id": phone_number.id, "value": "+0-11-23-456-7890"},
|
||||
{"id": biography.id, "value": "Prince of Denmark, and other things!"},
|
||||
{"id": favorite_food.id, "value": "Dark chocolate"},
|
||||
{"id": favorite_editor.id, "value": "vim"},
|
||||
{"id": birthday.id, "value": "1900-1-1"},
|
||||
])
|
||||
else:
|
||||
zulip_realm = get_realm("zulip")
|
||||
|
|
Loading…
Reference in New Issue