-
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.
feat: implement dumpdata view and admin command
This feature provides ways to serialize the data of an APIS instance to be able to import it in a freshly installed instance later on. The main logic is implemented in the `utils.helper.datadump_serializer` method. It serializes the important models to json - with natural foreign keys. There is both a view ('/api/dumpdata') and an admin command ('apisdumpdata') that output the serialized data as API Response or to stdout. Closes: #273
- Loading branch information
Showing
4 changed files
with
101 additions
and
1 deletion.
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,18 @@ | ||
from django.core.management.base import BaseCommand | ||
|
||
from apis_core.utils.helpers import datadump_serializer | ||
|
||
|
||
class Command(BaseCommand): | ||
help = "Dump APIS data" | ||
|
||
def add_arguments(self, parser): | ||
parser.add_argument( | ||
"args", | ||
metavar="app_labels", | ||
nargs="*", | ||
help=("Optional additional app_labels."), | ||
) | ||
|
||
def handle(self, *app_labels, **options): | ||
print(datadump_serializer(app_labels, "json")) |
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,24 @@ | ||
import json | ||
|
||
from rest_framework.views import APIView | ||
from rest_framework.response import Response | ||
|
||
from apis_core.utils.helpers import datadump_serializer | ||
|
||
|
||
class Dumpdata(APIView): | ||
""" | ||
provide an API endpoint that outputs the datadump of an APIS installation | ||
this is a bit of a hack, becaus we first use the Django JSON serializer to | ||
serialize the data using natural keys, then we use json.loads to so we can | ||
output it as an API reponse. | ||
so basically: serialize -> deserialize -> serialize | ||
""" | ||
|
||
def get(self, request, *args, **kwargs): | ||
params = request.query_params.dict() | ||
app_labels = params.pop("app_labels", []) | ||
if app_labels: | ||
app_labels = app_labels.split(",") | ||
return Response(json.loads(datadump_serializer(app_labels, "json"))) |
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
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