Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
96 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
from rest_framework import generics, serializers | ||
|
||
from kitsune.sumo.api import CORSMixin | ||
from kitsune.products.models import Product, Topic | ||
|
||
|
||
class ProductSerializer(serializers.ModelSerializer): | ||
platforms = serializers.SlugRelatedField(many=True, slug_field='slug') | ||
|
||
class Meta: | ||
model = Product | ||
fields = ('id', 'title', 'slug', 'description', 'platforms', 'visible') | ||
|
||
|
||
class ProductList(CORSMixin, generics.ListAPIView): | ||
"""List all documents.""" | ||
queryset = Product.objects.all() | ||
serializer_class = ProductSerializer | ||
|
||
def get_queryset(self): | ||
queryset = self.queryset | ||
|
||
is_visible = self.request.QUERY_PARAMS.get('is_visible', True) | ||
|
||
if is_visible is not None: | ||
queryset = queryset.filter(visible=is_visible) | ||
|
||
return queryset | ||
|
||
|
||
class TopicSerializer(serializers.ModelSerializer): | ||
parent = serializers.SlugRelatedField(slug_field='slug') | ||
product = serializers.SlugRelatedField(slug_field='slug') | ||
|
||
class Meta: | ||
model = Topic | ||
fields = ('id', 'title', 'slug', 'description', 'parent', 'visible', | ||
'product') | ||
|
||
|
||
class TopicList(CORSMixin, generics.ListAPIView): | ||
queryset = Topic.objects.all() | ||
serializer_class = TopicSerializer | ||
|
||
def get_queryset(self): | ||
queryset = self.queryset | ||
|
||
is_visible = self.request.QUERY_PARAMS.get('is_visible', True) | ||
|
||
if is_visible is not None: | ||
queryset = queryset.filter(visible=is_visible) | ||
|
||
return queryset |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
from django.conf.urls import patterns, url | ||
|
||
from kitsune.products import api | ||
|
||
|
||
# API urls | ||
urlpatterns = patterns( | ||
'', | ||
url(r'^$', api.ProductList.as_view()), | ||
url(r'^(?P<slug>[^/]+)/topics$', api.TopicList.as_view()), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
from rest_framework.exceptions import APIException | ||
|
||
|
||
class CORSMixin(object): | ||
def finalize_response(self, request, response, *args, **kwargs): | ||
response = (super(CORSMixin, self) | ||
.finalize_response(request, response, *args, **kwargs)) | ||
response['Access-Control-Allow-Origin'] = '*' | ||
return response | ||
|
||
|
||
class GenericAPIException(APIException): | ||
def __init__(self, status_code, detail, **kwargs): | ||
self.status_code = status_code | ||
self.detail = detail | ||
for key, val in kwargs.items(): | ||
setattr(self, key, val) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters