def put(self, request, user): serializer = UserNotificationDetailsSerializer(data=request.data) if not serializer.is_valid(): return Response(serializer.errors, status=400) for key, value in serializer.validated_data.items(): try: key = UserOptionsSettingsKey(key) except ValueError: return Response( {"detail": "Unknown key: %s." % key}, status=status.HTTP_400_BAD_REQUEST, ) type = get_type_from_user_option_settings_key(key) if type: NotificationSetting.objects.update_settings( ExternalProviders.EMAIL, type, get_option_value_from_int(type, int(value)), user=user, ) else: user_option, _ = UserOption.objects.get_or_create( key=USER_OPTION_SETTINGS[key]["key"], user=user, project=None, organization=None, ) user_option.update(value=str(int(value))) return self.get(request, user)
def _handle_put_notification_settings(user, notification_type: FineTuningAPIKey, parents, data): with transaction.atomic(): for parent in parents: # We fetched every available parent but only care about the ones in `request.data`. if str(parent.id) not in data: continue # This partitioning always does the same thing because notification_type stays constant. project_option, organization_option = ( (None, parent) if notification_type == FineTuningAPIKey.DEPLOY else (parent, None)) type = get_type_from_fine_tuning_key(notification_type) value = int(data[str(parent.id)]) NotificationSetting.objects.update_settings( ExternalProviders.EMAIL, type, get_option_value_from_int(type, value), user=user, project=project_option, organization=organization_option, ) return Response(status=status.HTTP_204_NO_CONTENT)