class DashboardWidgetSerializer(CamelSnakeSerializer): # Is a string because output serializers also make it a string. id = serializers.CharField(required=False) title = serializers.CharField(required=False, max_length=255) display_type = serializers.ChoiceField( choices=DashboardWidgetDisplayTypes.as_text_choices(), required=False) interval = serializers.CharField(required=False, max_length=10) queries = DashboardWidgetQuerySerializer(many=True, required=False) def validate_display_type(self, display_type): return DashboardWidgetDisplayTypes.get_id_for_type_name(display_type) validate_id = validate_id def validate_interval(self, interval): if parse_stats_period(interval) is None: raise serializers.ValidationError("Invalid interval") return interval def validate(self, data): if not data.get("id"): if not data.get("queries"): raise serializers.ValidationError({ "queries": "One or more queries are required to create a widget" }) if not data.get("title"): raise serializers.ValidationError( {"title": "Title is required during creation."}) if data.get("display_type") is None: raise serializers.ValidationError({ "displayType": "displayType is required during creation." }) return data
class DashboardWidgetSerializer(CamelSnakeSerializer): # Is a string because output serializers also make it a string. id = serializers.CharField(required=False) title = serializers.CharField(required=False, max_length=255) display_type = serializers.ChoiceField( choices=DashboardWidgetDisplayTypes.as_text_choices(), required=False) interval = serializers.CharField(required=False, max_length=10) queries = DashboardWidgetQuerySerializer(many=True, required=False) widget_type = serializers.ChoiceField( choices=DashboardWidgetTypes.as_text_choices(), required=False) def validate_display_type(self, display_type): return DashboardWidgetDisplayTypes.get_id_for_type_name(display_type) def validate_widget_type(self, widget_type): return DashboardWidgetTypes.get_id_for_type_name(widget_type) validate_id = validate_id def validate_interval(self, interval): if parse_stats_period(interval) is None: raise serializers.ValidationError("Invalid interval") return interval def validate(self, data): query_errors = [] has_query_error = False if data.get("queries"): # Check each query to see if they have an issue or discover error depending on the type of the widget for query in data.get("queries"): if (data.get("widget_type") == DashboardWidgetTypes.ISSUE and "issue_query_error" in query): query_errors.append(query["issue_query_error"]) has_query_error = True elif ("widget_type" not in data or data.get("widget_type") == DashboardWidgetTypes.DISCOVER ) and "discover_query_error" in query: query_errors.append(query["discover_query_error"]) has_query_error = True else: query_errors.append({}) if has_query_error: raise serializers.ValidationError({"queries": query_errors}) if not data.get("id"): if not data.get("queries"): raise serializers.ValidationError({ "queries": "One or more queries are required to create a widget" }) if not data.get("title"): raise serializers.ValidationError( {"title": "Title is required during creation."}) if data.get("display_type") is None: raise serializers.ValidationError({ "displayType": "displayType is required during creation." }) return data
class DashboardWidgetSerializer(CamelSnakeSerializer): # Is a string because output serializers also make it a string. id = serializers.CharField(required=False) title = serializers.CharField(required=False) display_type = serializers.ChoiceField( choices=DashboardWidgetDisplayTypes.as_text_choices(), required=False) queries = DashboardWidgetQuerySerializer(many=True, required=False) def validate_display_type(self, display_type): return DashboardWidgetDisplayTypes.get_id_for_type_name(display_type) validate_id = validate_id def validate(self, data): if not data.get("id") and not data.get("queries"): raise serializers.ValidationError( "One or more queries are required to create a widget") return data
def is_table_display_type(display_type): return (display_type == DashboardWidgetDisplayTypes.as_text_choices()[ DashboardWidgetDisplayTypes.TABLE][0])