diff --git a/_modules/apis_core/urls.html b/_modules/apis_core/urls.html index 53da8110c..a1cad6bff 100644 --- a/_modules/apis_core/urls.html +++ b/_modules/apis_core/urls.html @@ -103,6 +103,7 @@
# from apis_core.apis_vocabularies.api_views import UserViewSet
from apis_core.utils import caching
from apis_core.apis_metainfo.viewsets import UriToObjectViewSet
+from apis_core.core.views import Dumpdata
app_name = "apis_core"
@@ -193,7 +194,6 @@ Source code for apis_core.urls
from apis_core.apis_entities.api_views import GetEntityGeneric
-
urlpatterns = [
path("", TemplateView.as_view(template_name="base.html"), name="apis_index"),
path("admin/", admin.site.urls),
@@ -258,6 +258,7 @@ Source code for apis_core.urls
GetEntityGeneric.as_view(),
name="GetEntityGeneric",
),
+ path("api/dumpdata", Dumpdata.as_view()),
]
if "apis_fulltext_download" in settings.INSTALLED_APPS:
diff --git a/_modules/apis_core/utils/helpers.html b/_modules/apis_core/utils/helpers.html
index 26d5a3286..6f05c12cc 100644
--- a/_modules/apis_core/utils/helpers.html
+++ b/_modules/apis_core/utils/helpers.html
@@ -96,7 +96,10 @@ Source code for apis_core.utils.helpers
from apis_core.apis_entities.models import TempEntityClass
from apis_core.apis_relations.models import Property
+from django.apps import apps
+from django.db import DEFAULT_DB_ALIAS, router
from django.contrib.contenttypes.models import ContentType
+from django.core import serializers
@@ -170,6 +173,69 @@ Source code for apis_core.utils.helpers
return members[0][1]
return None
+
+
+
+[docs]
+def datadump_get_objects(models: list = [], *args, **kwargs):
+ for model in models:
+ if not model._meta.proxy and router.allow_migrate_model(
+ DEFAULT_DB_ALIAS, model
+ ):
+ objects = model._default_manager
+ queryset = objects.using(DEFAULT_DB_ALIAS).order_by(model._meta.pk.name)
+ yield from queryset.iterator()
+
+
+
+
+[docs]
+def datadump_get_queryset(additional_app_labels: list = []):
+ """
+ This method is loosely based on the `dumpdata` admin command.
+ It iterates throug the relevant app models and exports them using
+ a serializer and natural foreign keys.
+ Data exported this way can be reimported into a newly created Django APIS app
+ """
+
+ # get all APIS apps and all APIS models
+ apis_app_labels = ["apis_relations", "apis_metainfo"]
+ apis_app_models = [
+ model for model in apps.get_models() if model._meta.app_label in apis_app_labels
+ ]
+
+ # create a list of app labels we want to iterate
+ # this allows to extend the apps via the ?app_labels= parameter
+ app_labels = set(apis_app_labels)
+ app_labels |= set(additional_app_labels)
+
+ # look for models that inherit from APIS models and add their
+ # app label to app_labels
+ for model in apps.get_models():
+ if any(map(lambda x: issubclass(model, x), apis_app_models)):
+ app_labels.add(model._meta.app_label)
+
+ # now go through all app labels
+ app_list = {}
+ for app_label in app_labels:
+ app_config = apps.get_app_config(app_label)
+ app_list[app_config] = None
+
+ models = serializers.sort_dependencies(app_list.items(), allow_cycles=True)
+
+ yield from datadump_get_objects(models)
+
+
+
+
+[docs]
+def datadump_serializer(additional_app_labels: list = [], serialier_format="json"):
+ return serializers.serialize(
+ serialier_format,
+ datadump_get_queryset(additional_app_labels),
+ use_natural_foreign_keys=True,
+ )
+
diff --git a/genindex.html b/genindex.html
index 9b746b694..067dc5877 100644
--- a/genindex.html
+++ b/genindex.html
@@ -1030,6 +1030,12 @@ C
D