Example #1
0
def try_update_realm_custom_profile_field(
    realm: Realm,
    field: CustomProfileField,
    name: str,
    hint: str = "",
    field_data: Optional[ProfileFieldData] = None,
) -> None:
    field.name = name
    field.hint = hint
    if (field.field_type == CustomProfileField.SELECT
            or field.field_type == CustomProfileField.EXTERNAL_ACCOUNT):
        field.field_data = orjson.dumps(field_data or {}).decode()
    field.save()
    notify_realm_custom_profile_fields(realm)
Example #2
0
def try_add_realm_default_custom_profile_field(
        realm: Realm, field_subtype: str) -> CustomProfileField:
    field_data = DEFAULT_EXTERNAL_ACCOUNTS[field_subtype]
    custom_profile_field = CustomProfileField(
        realm=realm,
        name=field_data["name"],
        field_type=CustomProfileField.EXTERNAL_ACCOUNT,
        hint=field_data["hint"],
        field_data=orjson.dumps(dict(subtype=field_subtype)).decode(),
    )
    custom_profile_field.save()
    custom_profile_field.order = custom_profile_field.id
    custom_profile_field.save(update_fields=["order"])
    notify_realm_custom_profile_fields(realm)
    return custom_profile_field
Example #3
0
def try_add_realm_custom_profile_field(
    realm: Realm,
    name: str,
    field_type: int,
    hint: str = "",
    field_data: Optional[ProfileFieldData] = None,
) -> CustomProfileField:
    custom_profile_field = CustomProfileField(realm=realm, name=name, field_type=field_type)
    custom_profile_field.hint = hint
    if (
        custom_profile_field.field_type == CustomProfileField.SELECT
        or custom_profile_field.field_type == CustomProfileField.EXTERNAL_ACCOUNT
    ):
        custom_profile_field.field_data = orjson.dumps(field_data or {}).decode()

    custom_profile_field.save()
    custom_profile_field.order = custom_profile_field.id
    custom_profile_field.save(update_fields=["order"])
    notify_realm_custom_profile_fields(realm)
    return custom_profile_field