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

Conceptual fix for #111 (add possibility to specify queryset by filters) #130

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading