From c8cf7ec8ad3d1eb1de624f616520b6e42a994364 Mon Sep 17 00:00:00 2001 From: shon Date: Fri, 22 Mar 2019 15:16:08 +0530 Subject: [PATCH 1/3] create serializers for article and media --- BasicArticle/serializers.py | 2 +- Media/api/__init__.py | 0 Media/api/serializers.py | 8 ++++++++ 3 files changed, 9 insertions(+), 1 deletion(-) create mode 100644 Media/api/__init__.py create mode 100644 Media/api/serializers.py diff --git a/BasicArticle/serializers.py b/BasicArticle/serializers.py index 9dfb6e1a..e5daaa80 100644 --- a/BasicArticle/serializers.py +++ b/BasicArticle/serializers.py @@ -4,4 +4,4 @@ class ArticleSerializer(serializers.HyperlinkedModelSerializer): class Meta: model = Articles - fields = ('id','title', 'body') + fields = ('id','title', 'body', 'image', 'tags') diff --git a/Media/api/__init__.py b/Media/api/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/Media/api/serializers.py b/Media/api/serializers.py new file mode 100644 index 00000000..5761ee58 --- /dev/null +++ b/Media/api/serializers.py @@ -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') \ No newline at end of file From 0b0cce566a002a7b3e5020510ef338baadbeb39a Mon Sep 17 00:00:00 2001 From: shon Date: Fri, 22 Mar 2019 15:17:02 +0530 Subject: [PATCH 2/3] api permission for Community memebership --- Community/api/permissions.py | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 Community/api/permissions.py diff --git a/Community/api/permissions.py b/Community/api/permissions.py new file mode 100644 index 00000000..b53a4bd2 --- /dev/null +++ b/Community/api/permissions.py @@ -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'])) From 52a1a62a1e28229312b40e7328ed691f3a640cec Mon Sep 17 00:00:00 2001 From: shon Date: Fri, 22 Mar 2019 15:17:40 +0530 Subject: [PATCH 3/3] community resource create api --- CollaborationSystem/settings.py | 2 +- Community/api/urls.py | 3 ++- Community/api/views.py | 27 +++++++++++++++++++++++++-- 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/CollaborationSystem/settings.py b/CollaborationSystem/settings.py index a354f48e..5c3b6b93 100644 --- a/CollaborationSystem/settings.py +++ b/CollaborationSystem/settings.py @@ -284,7 +284,7 @@ ), 'DEFAULT_RENDERER_CLASSES': ( - 'rest_framework.renderers.JSONRenderer', + 'rest_framework.renderers.BrowsableAPIRenderer', ) } diff --git a/Community/api/urls.py b/Community/api/urls.py index ab56d825..94b55655 100644 --- a/Community/api/urls.py +++ b/Community/api/urls.py @@ -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\d*)/articles/$', CommunityArticlesApi.as_view(), name='community-articles-api'), url(r'^(?P\d*)/media/(?P[\w\-]+)/$', CommunityMediaApi.as_view(), name='community-media-api'), + url(r'^(?P\d*)/create/(?P[\w\-]+)/$', CreateCommunityResource.as_view(), name='community-resource-create') ] diff --git a/Community/api/views.py b/Community/api/views.py index 3761189e..e4e10fb6 100644 --- a/Community/api/views.py +++ b/Community/api/views.py @@ -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 @@ -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']) \ No newline at end of file + 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() + + \ No newline at end of file