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 delete(self, request, pk=None): """Delete db repo object""" r = Repo.objects.get(pk=pk) user = request.data['user'] RepoManager(user).delete() r.delete() return Response(status=status.HTTP_204_NO_CONTENT)
def stats(self, request, pk=None): """ Returns current repo stats :param request: request data :param pk: repo id :return: Current repo stats """ user = request.data['user'] b = RepoManager(user).statistics() bs = serializers.serialize('json', b) return Response(bs)
def browse(self, request, pk=None): """ Returns current repo contents :param request: request data :param pk: repo id :return: Current repo contents """ user = request.data['user'] b = RepoManager(user).browse_repository() bs = serializers.serialize('json', b) return Response(bs)
def branches(self, request, pk=None): """ Returns current repo branches :param request: request data :param pk: repo id :return: Current repo branches """ user = request.data['user'] b = RepoManager(user).check_branches() bs = serializers.serialize('json', b) return Response(bs)
def github(self, request, pk=None): """ Upload the given cookbook to a remote github repository :param request: request data :param pk: id of selected cookbook :return: operation status """ cb = CookBook.objects.get(pk=pk) user = request.user.username LOG.info("Uploading cookbook %s to Github" % cb.name) RepoManager(user).upload_coobook(cb.path)
def file(self, request, pk=None): """ Returns current repo file :param request: request data :param pk: repo id :return: Current repo file """ user = request.data['user'] file = request.data['file'] b = RepoManager(user).browse_file(file) bs = serializers.serialize('json', b) return Response(bs)
def create(self, request, *args, **kwargs): """ Generates db repo object """ user = request.data['user'] path = request.data['path'] version = request.data['version'] res = RepoManager(user).create() if not version: version = res r = Repo(user=user, path=path, version=version) r.save() rs = RepoSerializer(r) return Response(rs.data)
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