コード例 #1
0
ファイル: endpoints.py プロジェクト: dan56g/pinax-api
class ArticleTagEndpointSet(api.DjangoModelEndpointSetMixin, api.ResourceEndpointSet):
    """
    Handle ArticleTag retrieval.
    """

    url = api.url(
        base_name="articletag",
        base_regex=r"articletags",
        lookup={
            "field": "tag",
            "regex": r"\w+"
        }
    )
    middleware = {
        "authentication": [
            api.authentication.Anonymous(),
        ]
    }

    def list(self, request):
        """
        Identifier: List all tags
        """
        ArticleTagResource = api.registry["articletag"]
        return self.render(ArticleTagResource.from_queryset(self.get_queryset()))

    def retrieve(self, request, pk):
        """
        Identifier: Retrieve a tag
        """
        resource = self.resource_class(self.obj)
        return self.render(resource)
コード例 #2
0
ファイル: endpoints.py プロジェクト: pombredanne/kel-api
class ScopedInstanceEndpointSet(api.ResourceEndpointSet):

    url = api.url(base_name="instance",
                  base_regex=r"instances",
                  lookup={
                      "field": "instance",
                      "regex": r"[a-zA-Z0-9_-]+"
                  })
    middleware = {
        "authentication": [
            KelIdentityAuthentication(),
        ],
        "permissions": {
            ensure_user_belongs("site"),
        },
    }

    def get_queryset(self):
        return self.request.user.instances().filter(site=self.site)

    def prepare(self):
        self.resource_group = self.get_object_or_404(
            self.request.user.resource_groups(),
            name=self.kwargs["resource_group"],
        )
        self.site = self.get_object_or_404(
            self.request.user.sites().filter(
                resource_group=self.resource_group),
            name=self.kwargs["site"],
        )
        if self.requested_method in ["retrieve", "update", "destroy"]:
            self.instance = self.get_object_or_404(
                self.get_queryset().filter(site=self.site),
                name=self.kwargs["service"],
            )

    def list(self, request, *args, **kwargs):
        return self.render(
            self.resource_class.from_queryset(self.get_queryset()))

    def create(self, request, *args, **kwargs):
        with self.validate(self.resource_class) as resource:
            resource.save(create_kwargs={
                "site": self.site,
            })
        return self.render_create(resource)

    def retrieve(self, request, *args, **kwargs):
        resource = self.resource_class(self.instance)
        return self.render(resource)

    def update(self, request, *args, **kwargs):
        with self.validate(self.resource_class, obj=self.instance) as resource:
            resource.save()
        return self.render(resource)

    def destroy(self, request, *args, **kwargs):
        self.instance.delete()
        return self.render_delete()
コード例 #3
0
ファイル: endpoints.py プロジェクト: dan56g/pinax-api
class ArticleEndpointSet(api.DjangoModelEndpointSetMixin, api.ResourceEndpointSet):
    """
    Handle Article retrieval.
    """
    url = api.url(
        base_name="article",
        base_regex=r"articles",
        lookup={
            "field": "pk",
            "regex": r"\d+"
        }
    )
    relationships = {
        "tags": ArticleTagCollectionEndpointSet,
        "author": ArticleAuthorEndpointSet,
    }
    middleware = {
        "authentication": [
            api.authentication.Anonymous(),
        ]
    }

    def create(self, request):
        with self.validate(self.resource_class) as resource:
            resource.save()
            return self.render_create(resource)

    def list(self, request):
        """
        Identifier: List all Articles, optionally filtered by tag
        """
        qs = self.get_queryset()
        tag_querystring = request.GET.get("tag", "")
        if tag_querystring:
            qs = qs.filter(articletag__name__in=[tag_querystring])
        return self.render(self.resource_class.from_queryset(qs))

    def retrieve(self, request, pk):
        """
        Identifier: Retrieve an Article
        """
        resource = self.resource_class(obj=self.obj)
        return self.render(resource)

    def update(self, request, pk):
        """
        Update an Article
        """
        with self.validate(self.resource_class, obj=self.obj) as resource:
            resource.save()
            return self.render(resource)

    def destroy(self, request, pk):
        """
        Delete an Article
        """
        self.obj.delete()
        return self.render_delete()
コード例 #4
0
ファイル: endpoints.py プロジェクト: paltman/api
class ScopedResourceGroupEndpointSet(api.ResourceEndpointSet):

    url = api.url(base_name="resource-group",
                  base_regex=r"resource-groups",
                  lookup={
                      "field": "resource_group",
                      "regex": r"[a-zA-Z0-9_-]+"
                  })
    middleware = {
        "authentication": [
            KelIdentityAuthentication(),
        ],
        "permissions": {
            ensure_token_match("abc", check_methods=["create"]),
            ensure_user_belongs(
                "resource_group",
                check_methods=["retrieve", "update", "destroy"]),
        },
    }
    relationships = {
        "sites": ResourceGroupSiteCollectionRelationship,
    }

    def get_queryset(self):
        return self.request.user.resource_groups()

    def prepare(self):
        if self.requested_method in ["retrieve", "update", "destroy"]:
            self.resource_group = self.get_object_or_404(
                self.get_queryset(),
                name=self.kwargs["resource_group"],
            )

    def list(self, request, *args, **kwargs):
        return self.render(
            self.resource_class.from_queryset(self.get_queryset()))

    def create(self, request, *args, **kwargs):
        with self.validate(self.resource_class) as resource:
            resource.save(create_kwargs={
                "owner": request.user,
            })
        return self.render_create(resource)

    def retrieve(self, request, *args, **kwargs):
        resource = self.resource_class(self.resource_group)
        return self.render(resource)

    def update(self, request, *args, **kwargs):
        with self.validate(self.resource_class,
                           obj=self.resource_group) as resource:
            resource.save()
        return self.render(resource)

    def destroy(self, request, *args, **kwargs):
        self.resource_group.delete()
        return self.render_delete()
コード例 #5
0
ファイル: endpoints.py プロジェクト: pombredanne/kel-api
class BlobEndpointSet(api.ResourceEndpointSet):

    url = api.url(base_name="blob",
                  base_regex=r"blobs",
                  lookup={
                      "field": "blob",
                      "regex": r"[a-f0-9]+"
                  })
    middleware = {
        "authentication": [
            api.authentication.Anonymous(),
        ]
    }

    def create(self, request, *args, **kwargs):
        with self.validate(self.resource_class) as resource:
            resource.save()
        return self.render_create(resource)