Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Importing files #71

Open
pniedzwiedzinski opened this issue Feb 28, 2020 · 0 comments
Open

Importing files #71

pniedzwiedzinski opened this issue Feb 28, 2020 · 0 comments
Labels
migrate-2-django Issues related to migration from flask to django on backend

Comments

@pniedzwiedzinski
Copy link
Member

For that, we can use this: https://github.com/django-import-export/django-import-export

I looked through the code and there's a Format API for creating custom parsers. The drawback of this is that there is nothing about that class in the docs. So we could contribute to this project by adding the docs and examples.

I've made some research about this but it's not yet tested:

Format

Example Format (copied from https://github.com/django-import-export/django-import-export/blob/master/import_export/formats/base_formats.py)

class XLS(TablibFormat):
    TABLIB_MODULE = 'tablib.formats._xls'
    CONTENT_TYPE = 'application/vnd.ms-excel'

    def create_dataset(self, in_stream):
        """
        Create dataset from first sheet.
        """
        import xlrd
        xls_book = xlrd.open_workbook(file_contents=in_stream)
        dataset = tablib.Dataset()
        sheet = xls_book.sheets()[0]

        dataset.headers = sheet.row_values(0)
        for i in range(1, sheet.nrows):
            dataset.append(sheet.row_values(i))
        return dataset
  • TABLIB_MODULE - we don't actually want it because we will be using our own parser instead of tablib, and also we will use Format instead of TablibFormat
  • create_dataset - this function takes in_stream which is the content of the imported file. This function returns data in form of tablib.Dataset. So the task sounds simple -> convert our parser, so it will return substitutions in this format.
    http://docs.python-tablib.org/en/master/tutorial/#creating-a-dataset
    The headers of the dataset should match the field names.
@pniedzwiedzinski pniedzwiedzinski added the migrate-2-django Issues related to migration from flask to django on backend label Feb 28, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
migrate-2-django Issues related to migration from flask to django on backend
Projects
None yet
Development

No branches or pull requests

1 participant