apache / superset

Apache Superset is a Data Visualization and Data Exploration Platform
https://superset.apache.org/
Apache License 2.0
62.62k stars 13.82k forks source link

Bad export filenames #30308

Open FraterCRC opened 1 month ago

FraterCRC commented 1 month ago

Bug description

If you have a model with name in any other than english language you get filename '_{id}' which will not be exported

it is obvious from this code def get_filename(model_name: str, model_id: int, skip_id: bool = False) -> str: slug = secure_filename(model_name) filename = slug if skipid else f"{slug}{model_id}" return filename if slug else str(model_id)

How to reproduce the bug

  1. Go to charts
  2. Сreate chart "Пивко"
  3. Try to import the chart
  4. You get file with name _{id} in charts folder of zip archive
  5. Files with names starting with _ and - are not imported

Screenshots/recordings

No response

Superset version

master / latest-dev

Python version

3.9

Node version

16

Browser

Safari

Additional context

Suggest to do smthing like:

def get_filename(model_name: str, model_id: int, skip_id: bool = False) -> str: slug = secure_filename(modelname) slug = slug.replace('-', '').lstrip() filename = f"{slug}{model_id}" if slug else "{model_id}" return filename

Checklist

dosubot[bot] commented 1 month ago

Hey @FraterCRC! I'm here to help you with any bugs, questions, or contributions you have for Superset. Let's tackle this together!

To address the issue where non-English model names result in filenames like '_{id}' that are not exported, you can modify the get_filename function to handle non-English characters more gracefully. Here's an improved version of the function:

import unicodedata
import re
from werkzeug.utils import secure_filename

def slugify(value: str) -> str:
    value = unicodedata.normalize('NFKD', value).encode('ascii', 'ignore').decode('ascii')
    value = re.sub(r'[^\w\s-]', '', value).strip().lower()
    return re.sub(r'[-\s]+', '-', value)

def get_filename(model_name: str, model_id: int, skip_id: bool = False) -> str:
    slug = slugify(model_name)
    filename = slug if skip_id else f"{slug}_{model_id}"
    return filename if slug else str(model_id)

This slugify function normalizes the string to ASCII, removes non-word characters, and replaces spaces and hyphens with a single hyphen. This should handle non-English characters more gracefully and avoid the issue of filenames like '_{id}'.

In the context of Superset, you can see that the get_filename function is used in the ExportDashboardsCommand class to generate filenames for exported dashboards. By modifying the get_filename function as shown above, you can ensure that non-English model names are handled correctly when exporting dashboards [1][2].

To continue talking to Dosu, mention @dosu.

rusackas commented 1 month ago

Assigning to @yousoph since she's been taking stock of what is/isn't being exported properly.

sfirke commented 1 month ago

Looks closely related to https://github.com/apache/superset/pull/22118, see the discussion in comments there - is @EugeneTorap still actively contributing to Superset?