basic import working
This commit is contained in:
@ -45,6 +45,7 @@ from rest_framework.parsers import MultiPartParser
|
||||
from rest_framework.renderers import JSONRenderer, TemplateHTMLRenderer
|
||||
from rest_framework.response import Response
|
||||
from rest_framework.throttling import AnonRateThrottle
|
||||
from rest_framework.views import APIView
|
||||
from rest_framework.viewsets import ViewSetMixin
|
||||
from treebeard.exceptions import InvalidMoveToDescendant, InvalidPosition, PathOverflow
|
||||
|
||||
@ -1417,6 +1418,53 @@ def import_files(request):
|
||||
return Response({'error': True, 'msg': form.errors}, status=status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
|
||||
class ImportOpenData(APIView):
|
||||
permission_classes = [CustomIsAdmin & CustomTokenHasReadWriteScope]
|
||||
|
||||
def get(self, request, format=None):
|
||||
response = requests.get('https://raw.githubusercontent.com/TandoorRecipes/open-tandoor-data/main/build/meta.json')
|
||||
metadata = json.loads(response.content)
|
||||
return Response(metadata)
|
||||
|
||||
def post(self, request, *args, **kwargs):
|
||||
# serializer load data ?
|
||||
# TODO validate data
|
||||
# TODO add option to handle merging of existing data (merge, override, ignore)
|
||||
print(request.data)
|
||||
selected_version = request.data['selected_version']
|
||||
selected_datatypes = request.data['selected_datatypes']
|
||||
|
||||
response = requests.get(f'https://raw.githubusercontent.com/TandoorRecipes/open-tandoor-data/main/build/{selected_version}.json') # TODO catch 404, timeout, ...
|
||||
data = json.loads(response.content)
|
||||
|
||||
unit_name_list = []
|
||||
for u in list(data['unit'].keys()):
|
||||
unit_name_list.append(data['unit'][u]['name'])
|
||||
unit_name_list.append(data['unit'][u]['plural_name'])
|
||||
|
||||
existing_units = Unit.objects.filter(space=request.space).filter(Q(name__in=unit_name_list) | Q(plural_name__in=unit_name_list)).values_list('name', 'plural_name')
|
||||
existing_units = [item for sublist in existing_units for item in sublist]
|
||||
|
||||
insert_list = []
|
||||
for u in list(data['unit'].keys()):
|
||||
if not (data['unit'][u]['name'] in existing_units or data['unit'][u]['plural_name'] in existing_units):
|
||||
insert_list.append(Unit(
|
||||
name=data['unit'][u]['name'],
|
||||
plural_name=data['unit'][u]['plural_name'],
|
||||
base_unit=data['unit'][u]['base_unit'] if data['unit'][u]['base_unit'] != '' else None,
|
||||
space=request.space
|
||||
))
|
||||
|
||||
Unit.objects.bulk_create(insert_list)
|
||||
|
||||
# TODO hardcode insert order?
|
||||
# TODO split into update/create lists with multiple parameters per datatype
|
||||
print(existing_units)
|
||||
return Response({
|
||||
'test': existing_units
|
||||
})
|
||||
|
||||
|
||||
def get_recipe_provider(recipe):
|
||||
if recipe.storage.method == Storage.DROPBOX:
|
||||
return Dropbox
|
||||
|
Reference in New Issue
Block a user