Пример #1
0
    def get(self, request, *args, **kwargs):
        # Return a the list of user's repositories directly from GitHub
        try:
            token = SocialToken.objects.get(
                account__user=request.user, account__provider='github'
            )
        except Exception:
            msg = (
                "Failed to connect to GitHub account for Galaxy user {} "
                "You must first authenticate with GitHub."
                .format(request.user.username)
            )
            logger.error(msg)
            return HttpResponseBadRequest({'detail': msg})

        try:
            gh_api = Github(token.token)
            gh_api.get_api_status()
        except GithubException as e:
            msg = (
                "Failed to connect to GitHub API. This is most likely a "
                "temporary error, please try again in a few minutes. {} - {}"
                .format(e.data, e.status)
            )
            logger.error(msg)
            return HttpResponseBadRequest({'detail': msg})

        try:
            ghu = gh_api.get_user()
        except Exception:
            msg = "Failed to get GitHub authorized user."
            logger.error(msg)
            return HttpResponseBadRequest({'detail': msg})
        try:
            user_repos = ghu.get_repos()
        except Exception:
            msg = "Failed to get user repositories from GitHub."
            logger.error(msg)
            return HttpResponseBadRequest({'detail': msg})

        try:
            celerytasks.refresh_existing_user_repos(token.token, ghu)
        except Exception as exc:
            logger.error("Error: refresh_user_repos - {0}".format(exc.message))
            raise

        try:
            celerytasks.update_user_repos(user_repos, request.user)
        except Exception as exc:
            logger.error("Error: update_user_repos - {0}".format(exc.message))
            raise

        qs = request.user.repositories.all()
        serializer = serializers.RepositorySerializer(qs, many=True)
        return Response(serializer.data, status=status.HTTP_200_OK)
Пример #2
0
    def get(self, request):
        namespace = request.GET.get('namespace', None)
        name = request.GET.get('name', None)

        if not name or not namespace:
            raise exceptions.ValidationError(
                detail='namespace and name parameters are required')

        try:
            repo = models.Repository.objects.get(
                provider_namespace__namespace__name=namespace, name=name)
            namespace_obj = models.Namespace.objects.get(name=namespace)
            content = models.Content.objects.filter(
                repository__name__iexact=name,
                repository__provider_namespace__namespace__name__iexact=
                namespace  # noqa
            )

            data = {
                'repository':
                v1_serializers.RepositorySerializer(repo).data,
                'namespace':
                v1_serializers.NamespaceSerializer(namespace_obj).data,
                'content':
                v1_serializers.ContentSerializer(content, many=True).data
            }
            return response.Response({'type': 'repository', 'data': data})
        except django_exceptions.ObjectDoesNotExist:
            pass

        try:
            collection = models.Collection.objects.get(
                namespace__name__iexact=namespace, name__iexact=name)

            collection_import = models.CollectionImport.objects.get(
                imported_version=collection.latest_version)

            data = {
                'collection':
                internal_serializers.CollectionDetailSerializer(
                    collection).data,
                'collection_import':
                v2_serializers.CollectionImportSerializer(
                    collection_import).data
            }
            return response.Response({'type': 'collection', 'data': data})
        except django_exceptions.ObjectDoesNotExist:
            raise exceptions.NotFound(
                detail="No collection or repository could be found " +
                "matching the name {}.{}".format(namespace, name))
Пример #3
0
    def get(self, request):
        try:
            page = int(request.GET.get('page', 1))
            page_size = int(request.GET.get('page_size', 10))
        except ValueError:
            raise exceptions.ValidationError(
                detail='Pagination values must be numbers')

        namespace = request.GET.get('namespace', None)
        package_type = request.GET.get('type', None)
        order = request.GET.get('order', 'name')

        self._validate_order(order)

        if not namespace:
            raise exceptions.ValidationError(
                detail='The namespace parameter is required')

        collection_filters = {
            'namespace__name__iexact': namespace,
        }

        repo_filters = {
            'provider_namespace__namespace__name__iexact': namespace
        }

        # Names are split by ' ' and then applied as an AND filter, so
        # name="bob the angry" is interpreted as "bob" & "the" & "angry"
        name_filter = []
        if 'name' in request.GET:
            name_filter = [
                Q(name__icontains=name)
                for name in request.GET['name'].split()
            ]

        # Avoid loading models if the type is set.
        if package_type == 'collection':
            repos = models.Repository.objects.none()
            repo_count = 0
        else:
            repos = models.Repository.objects.filter(
                **repo_filters).order_by(order)
            if len(name_filter) > 0:
                repos = repos.filter(
                    functools.reduce(operator.and_, name_filter))
            repo_count = repos.count()

        if package_type == 'repository':
            collections = models.Collection.objects.none()
            collection_count = 0
        else:
            collections = models.Collection.objects.filter(
                **collection_filters).order_by(order)
            if len(name_filter) > 0:
                collections = collections.filter(
                    functools.reduce(operator.and_, name_filter))

            collection_count = collections.count()

        start = (page * page_size) - page_size
        end = page * page_size

        collection_results = collections[start:end]

        # If the collections fill the whole page, don't bother loading the
        # repos
        if len(collection_results) == page_size:
            repo_results = []
        else:
            # Django gets cranky if you try to slice a queryset with a negative
            # index
            r_start = max(0, start - collection_count)

            repo_results = repos[r_start:end - collection_count]

        result = {
            'collection': {
                'count':
                collection_count,
                'results':
                internal_serializers.CollectionListSerializer(
                    collection_results, many=True).data,
            },
            'repository': {
                'count':
                repo_count,
                'results':
                v1_serializers.RepositorySerializer(
                    repo_results,
                    many=True,
                ).data,
            }
        }

        return response.Response(result)