Skip to content

Commit

Permalink
add possibility to specify queryset by filters
Browse files Browse the repository at this point in the history
  • Loading branch information
PetrDlouhy committed Aug 19, 2024
1 parent 7a9ec54 commit 7c4c8d2
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 13 deletions.
11 changes: 4 additions & 7 deletions import_export_celery/admin_actions.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from django.utils import timezone
import json
from uuid import UUID

from django.utils.translation import gettext_lazy as _
Expand Down Expand Up @@ -45,12 +44,10 @@ def create_export_job_action(modeladmin, request, queryset):
ej = ExportJob.objects.create(
app_label=arbitrary_obj._meta.app_label,
model=arbitrary_obj._meta.model_name,
queryset=json.dumps(
[
str(obj.pk) if isinstance(obj.pk, UUID) else obj.pk
for obj in queryset
]
),
queryset=[
str(obj.pk) if isinstance(obj.pk, UUID) else obj.pk
for obj in queryset
],
site_of_origin=request.scheme + "://" + request.get_host(),
)
rurl = reverse(
Expand Down
20 changes: 20 additions & 0 deletions import_export_celery/migrations/0011_alter_exportjob_queryset.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Generated by Django 5.0.7 on 2024-07-29 12:21

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("import_export_celery", "0010_auto_20231013_0904"),
]

operations = [
migrations.AlterField(
model_name="exportjob",
name="queryset",
field=models.JSONField(
verbose_name="JSON list of pks to export or dict of queryset filters"
),
),
]
16 changes: 10 additions & 6 deletions import_export_celery/models/exportjob.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# Copyright (C) 2019 o.s. Auto*Mat
from django.utils import timezone
import json

from author.decorators import with_author

Expand Down Expand Up @@ -67,8 +66,8 @@ def __init__(self, *args, **kwargs):
default="",
)

queryset = models.TextField(
verbose_name=_("JSON list of pks to export"),
queryset = models.JSONField(
verbose_name=_("JSON list of pks to export or dict of queryset filters"),
null=False,
)

Expand Down Expand Up @@ -104,14 +103,19 @@ def get_content_type(self):
return self._content_type

def get_queryset(self):
pks = json.loads(self.queryset)
queryset_spec = self.queryset
if isinstance(queryset_spec, list):
filters = {"pk__in": queryset_spec}
elif isinstance(queryset_spec, dict):
filters = queryset_spec

# If customised queryset for the model exists
# then it'll apply filter on that otherwise it'll
# apply filter directly on the model.
resource_class = self.get_resource_class()
if hasattr(resource_class, "get_export_queryset"):
return resource_class().get_export_queryset().filter(pk__in=pks)
return self.get_content_type().model_class().objects.filter(pk__in=pks)
return resource_class().get_export_queryset().filter(**filters)
return self.get_content_type().model_class().objects.filter(**filters)

def get_resource_choices(self):
return [
Expand Down

0 comments on commit 7c4c8d2

Please sign in to comment.