realm: Add registry of realm property types.

This makes it possible for us to do some convenient validation for
developers, checking whether the correct types are passed for each
each realm property.
This commit is contained in:
Tim Abbott
2017-03-23 17:44:29 -07:00
parent 09f66b5c6d
commit a98bce98c6
2 changed files with 20 additions and 1 deletions

View File

@@ -422,9 +422,11 @@ def active_humans_in_realm(realm):
def do_set_realm_property(realm, name, value):
# type: (Realm, Text, Union[Text, bool, int]) -> None
# type: (Realm, str, Union[Text, bool, int]) -> None
"""Takes in a realm object, the name of an attribute to update, and the value to update.
"""
property_type = Realm.property_types[name]
assert isinstance(value, property_type)
setattr(realm, name, value)
realm.save(update_fields=[name])
event = dict(

View File

@@ -144,6 +144,23 @@ class Realm(ModelReprMixin, models.Model):
default=2**31 - 1) # type: BitHandler
waiting_period_threshold = models.PositiveIntegerField(default=0) # type: int
# Define the types of the various automatically managed properties
property_types = dict(
add_emoji_by_admins_only=bool,
create_stream_by_admins_only=bool,
default_language=Text,
description=Text,
email_changes_disabled=bool,
invite_required=bool,
invite_by_admins_only=bool,
inline_image_preview=bool,
inline_url_embed_preview=bool,
name=Text,
name_changes_disabled=bool,
restricted_to_domain=bool,
waiting_period_threshold=int,
)
ICON_FROM_GRAVATAR = u'G'
ICON_UPLOADED = u'U'
ICON_SOURCES = (