Django Import Export Stomp
TLDR: Django plugin for file import/export on top of django-import-export using django-stomp.
Table of Contents
About
Django Import Export Stomp is a django plugin that uses django-stomp to import/export models to spreadsheet-like files (csv, xlsx, etc.).
Codebase
The stack
This application uses at least Python 3.10 with at least Django 5.1.
Engineering standards
Commit hooks
Currently we're using pre-commit.
To configure it simply use the commands below.
pip install pre-commit
pre-commit install
This will automatically lint the staged files using our project standard linters.
Getting Started
This section provides a high-level requirement & quick start guide.
Prerequisites
⚠️
We do not recommend developing the application without docker!
⚠️
Running example application with Docker
-
Clone the repository via
ssh, ie.git clone git@github.com:juntossomosmais/django-import-export-stomp.git. -
Simply running
docker compose up --build exampleis enough to start runningdjango-import-export-stompon a exmaple with Docker.
Testing the application with Docker
-
If you want to run the tests run:
docker compose run integration-tests. -
To run the sonar analysis locally you can use
docker compose up -d sonar-clientand thendocker compose up -d sonar-cli
Documentation
Basic installation
- Install package:
pip install django-import-export-stomp - Add
import_export_stompto yourINSTALLED_APPSin yoursettings.py - Add
author.middlewares.AuthorDefaultBackendMiddlewareto yourMIDDLEWARE_CLASSESin yoursettings.py - Setup django-stomp
Running the consumer
Run python manage.py import_export pubsub to start processing messages from the queues.
Setting up imports
On your settings.py add a IMPORT_EXPORT_MODELS variable:
from import_export_stomp.resources import resource_importer
IMPORT_EXPORT_STOMP_MODELS = {
"Name of your import": {
"app_label": "fake_app",
"model_name": "FakeModel",
"resource": resource_importer(
"tests.resources.fake_app.resources.FakeResource"
), # optional
}
}
By default a dry run of the import is initiated when the import object is created. To instead import the file immediately without a dry-run set the IMPORT_DRY_RUN_FIRST_TIME to False.
IMPORT_DRY_RUN_FIRST_TIME = False
Setting up export
As with imports, a fully configured example project can be found in the example directory.
-
Add a
export_resource_classesclassmethod to the model you want to export.@classmethod def export_resource_classes(cls): return { 'winners': ('Winners resource', WinnersResource), 'winners_all_caps': ('Winners with all caps column resource', WinnersWithAllCapsResource), }
This should return a dictionary of tuples. The keys should be unique unchanging strings, the tuples should consist of a
resource <https://django-import-export.readthedocs.io/en/latest/getting_started.html#creating-import-export-resource>__ and a human friendly description of that resource. -
Add the
create_export_job_actionto the model'sModelAdmin.from django.contrib import admin from import_export.admin_actions import create_export_job_action from . import models @admin.register(models.Winner) class WinnerAdmin(admin.ModelAdmin): list_display = ( 'name', ) actions = ( create_export_job_action, )
-
To customise export queryset you need to add
get_export_querysetto theModelResource.class WinnersResource(ModelResource): class Meta: model = Winner def get_export_queryset(self): """To customise the queryset of the model resource with annotation override""" return self.Meta.model.objects.annotate(device_type=Subquery(FCMDevice.objects.filter( user=OuterRef("pk")).values("type")[:1])
-
Done!
How to use upload with aws s3 presigned url
-
Have
boto3anddjango-storagesinstalled in your project:pip install boto3 django-storages -
Setup django-storages variables -
AWS_STORAGE_BUCKET_NAMEis required. -
Set
IMPORT_EXPORT_STOMP_USE_PRESIGNED_POSTtoTrue. -
Add urls from
import_export_stomp.urlsto yoururls.pyfrom import_export_stomp.urls import urlpatterns as import_export_stomp_urlpatterns urlpatterns = [...] # Your urls urlpatterns += import_export_stomp_urlpatterns
-
Done!
Performing an import
You will find an example django application that uses django-import-export-stomp for importing data. Once you have it running, you can perform an import with the following steps.
-
Navigate to the example applications admin page:
-
Navigate to the ImportJobs table:
-
Create a new import job. There is an example import CSV file in the example/example-data directory. Select that file. Select csv as the file format. We'll be importing to the Winner's model table.
-
Select "Save and continue editing" to save the import job and refresh until you see that a "Summary of changes made by this import" file has been created.
-
You can view the summary if you want. Your import has NOT BEEN PERFORMED YET!
-
Return to the import-jobs table, select the import job we just created, and select the "Perform import" action from the actions drop down.
-
In a short time, your imported Winner object should show up in your Winners table.
Performing an export
-
Perform the basic setup procedure described in the first section.
-
Open up the object list for your model in django admin, select the objects you wish to export, and select the
Export with stompadmin action. -
Select the file format and resource you want to use to export the data.
-
Save the model
-
You will receive an email when the export is done, click on the link in the email
-
Click on the link near the bottom of the page titled
Exported file.
Settings
-
IMPORT_EXPORT_STOMP_MODELSRequired Dict containing all the models that will be imported.{ "Name of your import": { "app_label": "fake_app", "model_name": "FakeModel", "resource": resource_importer( "tests.resources.fake_app.resources.FakeResource" ), # optional -if not present will auto-create } }
-
IMPORT_EXPORT_STOMP_STORAGEStorage class which import/export file field will use. Defaults toNone. Example:storages.backends.s3boto3.S3Boto3Storage -
IMPORT_EXPORT_STOMP_EXCLUDED_FORMATSList of formats to exclude from import/export. Defaults to[]. Example:["csv", "xlsx"] -
IMPORT_EXPORT_STOMP_PROCESSING_QUEUEName of the stomp queue that will be used to publish/consume the messages. Defaults to/queue/django-import-export-stomp-runner -
IMPORT_EXPORT_STOMP_USE_PRESIGNED_POSTEnables upload using presigned post url. Usesboto3anddjango-storages. Defaults toFalse. -
IMPORT_EXPORT_STOMP_PRESIGNED_POST_EXPIRATIONSets signed url expiration time. Defaults to600 -
IMPORT_EXPORT_STOMP_PRESIGNED_FOLDERPrepends a path to the s3 key. Defaults to""
Credits
django-import-export-stomp was based on django-import-export-celery developed by the Czech non-profit auto*mat z.s..
Metadata
Release files for django-import-export-stomp 0.4.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| django_import_export_stomp-0.4.1.tar.gz | 25.8 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| django_import_export_stomp-0.4.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 62.2 kB
Release files / django_import_export_stomp-0.4.1.tar.gz
| Download URL | django_import_export_stomp-0.4.1.tar.gz |
|---|---|
| Size | 25.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
05cd49caa40c9ad2e778662b100ac829cb7f6999de4ba772959372dc82286534
|
|
BLAKE2b-256 checksum How to use checksums |
a60cf26f1e9b25cf61679ccbcab5dca736971b76253f39cf122629aa59b04729
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.5.1 CPython/3.10.19 Linux/6.11.0-1018-azure
|
Release files / django_import_export_stomp-0.4.1-py3-none-any.whl
| Download URL | django_import_export_stomp-0.4.1-py3-none-any.whl |
|---|---|
| Size | 36.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
c29578dc1bd4b5b7c684bc2c1d313ac7f152bde2817e7eb33e731847881e4cdb
|
|
BLAKE2b-256 checksum How to use checksums |
a64aa2cd6d5905bef380e596ad220da9bb668c5e06fbe68a2abc534613a71895
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.5.1 CPython/3.10.19 Linux/6.11.0-1018-azure
|