-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This view provides a way to serialize the whole database using json - getting a fixture with natural foreign keys. This fixture lets us reimport the database in a new installation. Closes: #273
- Loading branch information
Showing
2 changed files
with
76 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
from django.http import HttpResponse | ||
from django.views import View | ||
from django.contrib.auth.mixins import UserPassesTestMixin | ||
|
||
from django.apps import apps | ||
from django.core import serializers | ||
from django.db import DEFAULT_DB_ALIAS, router | ||
|
||
from apis_core.apis_entities.models import AbstractEntity | ||
|
||
|
||
class Dumpdata(UserPassesTestMixin, View): | ||
""" | ||
This view is loosely based on the `dumpdata` admin command. | ||
It iterates throug the relevant app models and exports them using | ||
the json serializer and natural foreign keys. | ||
This way we can reimport this into a newly created Django APIS app | ||
""" | ||
|
||
def test_func(self): | ||
return self.request.user.is_staff | ||
|
||
def get_objects(self, models, *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() | ||
|
||
def get(self, request, *args, **kwargs): | ||
# 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(request.GET.get("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) | ||
|
||
headers = { | ||
"Content-Type": "application/json", | ||
} | ||
hr = HttpResponse(headers=headers) | ||
|
||
serializers.serialize( | ||
"json", | ||
self.get_objects(models), | ||
indent=2, | ||
use_natural_foreign_keys=True, | ||
stream=hr, | ||
) | ||
|
||
return hr |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters