def do_remove_realm_custom_profile_field(realm: Realm, field: CustomProfileField) -> None: """ Deleting a field will also delete the user profile data associated with it in CustomProfileFieldValue model. """ field.delete() notify_realm_custom_profile_fields(realm)
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)
def build_customprofile_field(customprofile_field: List[ZerverFieldsT], fields: ZerverFieldsT, customprofilefield_id: int, realm_id: int, custom_field_map: ZerverFieldsT) -> Tuple[ZerverFieldsT, int]: # The name of the custom profile field is not provided in the slack data # Hash keys of the fields are provided # Reference: https://api.slack.com/methods/users.profile.set for field, value in fields.items(): if field not in custom_field_map: slack_custom_fields = ['phone', 'skype'] if field in slack_custom_fields: field_name = field else: field_name = ("slack custom field %s" % str(customprofilefield_id + 1)) customprofilefield = CustomProfileField( id=customprofilefield_id, name=field_name, field_type=1 # For now this is defaulted to 'SHORT_TEXT' # Processing is done in the function 'process_customprofilefields' ) customprofilefield_dict = model_to_dict(customprofilefield, exclude=['realm']) customprofilefield_dict['realm'] = realm_id custom_field_map[field] = customprofilefield_id customprofilefield_id += 1 customprofile_field.append(customprofilefield_dict) return custom_field_map, customprofilefield_id
def build_customprofile_field( customprofile_field: List[ZerverFieldsT], fields: ZerverFieldsT, custom_profile_field_id: int, realm_id: int, slack_custom_field_name_to_zulip_custom_field_id: ZerverFieldsT, ) -> Tuple[ZerverFieldsT, int]: # The name of the custom profile field is not provided in the Slack data # Hash keys of the fields are provided # Reference: https://api.slack.com/methods/users.profile.set for field, value in fields.items(): if field not in slack_custom_field_name_to_zulip_custom_field_id: slack_custom_fields = ["phone", "skype"] if field in slack_custom_fields: field_name = field else: field_name = f"Slack custom field {str(custom_profile_field_id + 1)}" customprofilefield = CustomProfileField( id=custom_profile_field_id, name=field_name, field_type=1, # For now this is defaulted to 'SHORT_TEXT' # Processing is done in the function 'process_customprofilefields' ) customprofilefield_dict = model_to_dict(customprofilefield, exclude=["realm"]) customprofilefield_dict["realm"] = realm_id slack_custom_field_name_to_zulip_custom_field_id[field] = custom_profile_field_id custom_profile_field_id += 1 customprofile_field.append(customprofilefield_dict) return slack_custom_field_name_to_zulip_custom_field_id, custom_profile_field_id
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
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