def create(self, request, **kwargs): """ Creates a cookbook db object from a remote url :param request: dict with request values :param kwargs: additional arguments :return: json response with operation status """ # Check data validity if 'upload_url' in request.data.keys(): url = request.data['upload_url'] user = request.user.username LOG.info("Creating Cookbook from %s" % url) else: return Response({'detail': 'Insufficient payload'}, status=status.HTTP_400_BAD_REQUEST) # Download contents to temporary local storage # TODO: better github quota management name, path = LocalStorage().download(url) if not name: return Response('Error downloading %s. Quota exceeded?' % url, status=status.HTTP_400_BAD_REQUEST) # Parse downloaded contents system = LocalStorage().find_system(path) if not system: return Response({'detail': 'No valid cookbook detected for %s' % url}, status=status.HTTP_400_BAD_REQUEST) # Add valid cookbook to user repo m = RepoManager(user) cb_path, version = m.add_cookbook(path) # Generate valid cookbook try: cb = CookBook.objects.get(name=name, user=user) LOG.info("Updating Cookbook {} for user {}".format(name, request.user.id)) # Remove old recipes Recipe.objects.filter(cookbook=cb).delete() except CookBook.DoesNotExist: LOG.info("Generating Cookbook {} for user {}".format(name, request.user.id)) cb = CookBook(user=user, name=name, path=cb_path, version=version, system=system) cb.save() for r in LocalStorage().list_recipes(cb.path): ro = Recipe() ro.name = r ro.cookbook = cb ro.version = RepoManager(user).browse_file(r) ro.system = system ro.user = user ro.save() cbs = CookBookSerializer(cb) resp = Response(cbs.data, status=status.HTTP_201_CREATED) return resp
def create(self, request, **kwargs): """ Creates a cookbook db object from a remote url :param request: dict with request values :param kwargs: additional arguments :return: json response with operation status """ # Check data validity if 'upload_url' in request.data.keys(): url = request.data['upload_url'] user = request.user.username LOG.info("Creating Cookbook from %s" % url) else: return Response({'detail': 'Insufficient payload'}, status=status.HTTP_400_BAD_REQUEST) # Download contents to temporary local storage # TODO: better github quota management name, path = LocalStorage().download(url) if not name: return Response('Error downloading %s. Quota exceeded?' % url, status=status.HTTP_400_BAD_REQUEST) # Parse downloaded contents system = LocalStorage().find_system(path) if not system: return Response( {'detail': 'No valid cookbook detected for %s' % url}, status=status.HTTP_400_BAD_REQUEST) # Add valid cookbook to user repo m = RepoManager(user) cb_path, version = m.add_cookbook(path) # Generate valid cookbook try: cb = CookBook.objects.get(name=name, user=user) LOG.info("Updating Cookbook {} for user {}".format( name, request.user.id)) # Remove old recipes Recipe.objects.filter(cookbook=cb).delete() except CookBook.DoesNotExist: LOG.info("Generating Cookbook {} for user {}".format( name, request.user.id)) cb = CookBook(user=user, name=name, path=cb_path, version=version, system=system) cb.save() for r in LocalStorage().list_recipes(cb.path): ro = Recipe() ro.name = r ro.cookbook = cb ro.version = RepoManager(user).browse_file(r) ro.system = system ro.user = user ro.save() cbs = CookBookSerializer(cb) resp = Response(cbs.data, status=status.HTTP_201_CREATED) return resp