Skip to content

Commit

Permalink
Merge branch 'api-design' into n-level
Browse files Browse the repository at this point in the history
  • Loading branch information
abhijitbonik committed Mar 22, 2019
2 parents d108964 + 52a1a62 commit 67aa773
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 5 deletions.
2 changes: 1 addition & 1 deletion BasicArticle/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
class ArticleSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Articles
fields = ('id','title', 'body')
fields = ('id','title', 'body', 'image', 'tags')
2 changes: 1 addition & 1 deletion CollaborationSystem/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@
),

'DEFAULT_RENDERER_CLASSES': (
'rest_framework.renderers.JSONRenderer',
'rest_framework.renderers.BrowsableAPIRenderer',
)
}

Expand Down
7 changes: 7 additions & 0 deletions Community/api/permissions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from rest_framework import permissions
from Community.models import CommunityMembership, Community

class IsCommunityMember(permissions.BasePermission):

def has_permission(self, request, view):
return CommunityMembership.objects.filter(user=request.user, community=Community.objects.get(pk=view.kwargs['pk']))
3 changes: 2 additions & 1 deletion Community/api/urls.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from django.conf.urls import url
from .views import CommunityListsApi, CommunityArticlesApi, CommunityMediaApi
from .views import CommunityListsApi, CommunityArticlesApi, CommunityMediaApi, CreateCommunityResource

urlpatterns = [
url(r'^list/$', CommunityListsApi.as_view(), name='community-list-api'),
url(r'^(?P<pk>\d*)/articles/$', CommunityArticlesApi.as_view(), name='community-articles-api'),
url(r'^(?P<pk>\d*)/media/(?P<type>[\w\-]+)/$', CommunityMediaApi.as_view(), name='community-media-api'),
url(r'^(?P<pk>\d*)/create/(?P<resource_type>[\w\-]+)/$', CreateCommunityResource.as_view(), name='community-resource-create')
]
27 changes: 25 additions & 2 deletions Community/api/views.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
from Community.models import Community, CommunityArticles, CommunityMedia
from .serializers import CommunitySerializer, CommunityArticlesSerializer, CommunityMediaSerializer
from rest_framework import generics

from BasicArticle.serializers import ArticleSerializer
from BasicArticle.models import Articles
from Media.api.serializers import MediaCreateSerializer
from Media.models import Media
from rest_framework.permissions import IsAdminUser
from .permissions import IsCommunityMember
class CommunityListsApi(generics.ListAPIView):
queryset = Community.objects.all()
serializer_class = CommunitySerializer
Expand All @@ -17,4 +22,22 @@ class CommunityMediaApi(generics.ListAPIView):
serializer_class = CommunityMediaSerializer

def get_queryset(self):
return CommunityMedia.objects.filter(media__state__name='publish', media__mediatype=self.kwargs['type'], community = self.kwargs['pk'])
return CommunityMedia.objects.filter(media__state__name='publish', media__mediatype=self.kwargs['type'], community = self.kwargs['pk'])


class CreateCommunityResource(generics.CreateAPIView):
permission_classes = (IsAdminUser, IsCommunityMember,)

def get_serializer_class(self):
if self.kwargs['resource_type'] == 'article':
return ArticleSerializer
elif self.kwargs['resource_type'] == 'media':
return MediaCreateSerializer

def get_queryset(self):
if self.kwargs['resource_type'] == 'article':
return Article.objects.all()
elif self.kwargs['resource_type'] == 'media':
return Media.objects.all()


Empty file added Media/api/__init__.py
Empty file.
8 changes: 8 additions & 0 deletions Media/api/serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from rest_framework import serializers
from Media.models import Media


class MediaCreateSerializer(serializers.ModelSerializer):
class Meta:
model = Media
fields = ('mediatype', 'title', 'mediafile', 'medialink')

0 comments on commit 67aa773

Please sign in to comment.