| 147 | return value |
| 148 | |
| 149 | def update(self, instance: "User", validated_data: Any) -> Any: |
| 150 | |
| 151 | # Update current_organization and current_team |
| 152 | current_organization = validated_data.pop("set_current_organization", None) |
| 153 | current_team = validated_data.pop("set_current_team", None) |
| 154 | if current_organization: |
| 155 | if current_team and not current_organization.teams.filter(pk=current_team.pk).exists(): |
| 156 | raise serializers.ValidationError( |
| 157 | {"set_current_team": ["Team must belong to the same organization in set_current_organization."]} |
| 158 | ) |
| 159 | |
| 160 | validated_data["current_organization"] = current_organization |
| 161 | validated_data["current_team"] = current_team if current_team else current_organization.teams.first() |
| 162 | elif current_team: |
| 163 | validated_data["current_team"] = current_team |
| 164 | validated_data["current_organization"] = current_team.organization |
| 165 | |
| 166 | if ( |
| 167 | "email" in validated_data |
| 168 | and validated_data["email"].lower() != instance.email.lower() |
| 169 | and is_email_available() |
| 170 | ): |
| 171 | send_email_change_emails.delay( |
| 172 | timezone.now().isoformat(), instance.first_name, instance.email, validated_data["email"] |
| 173 | ) |
| 174 | |
| 175 | # Update password |
| 176 | current_password = validated_data.pop("current_password", None) |
| 177 | password = self.validate_password_change( |
| 178 | cast(User, instance), current_password, validated_data.pop("password", None) |
| 179 | ) |
| 180 | |
| 181 | if validated_data.get("notification_settings"): |
| 182 | validated_data["partial_notification_settings"] = validated_data.pop("notification_settings") |
| 183 | |
| 184 | updated_attrs = list(validated_data.keys()) |
| 185 | instance = cast(User, super().update(instance, validated_data)) |
| 186 | |
| 187 | if password: |
| 188 | instance.set_password(password) |
| 189 | instance.save() |
| 190 | update_session_auth_hash(self.context["request"], instance) |
| 191 | updated_attrs.append("password") |
| 192 | |
| 193 | report_user_updated(instance, updated_attrs) |
| 194 | |
| 195 | return instance |
| 196 | |
| 197 | def to_representation(self, instance: Any) -> Any: |
| 198 | user_identify.identify_task.delay(user_id=instance.id) |