diff --git a/opentech/apply/funds/api_views.py b/opentech/apply/funds/api_views.py new file mode 100644 index 0000000000000000000000000000000000000000..820de510da5b260f7d4239495997a806af9a6d54 --- /dev/null +++ b/opentech/apply/funds/api_views.py @@ -0,0 +1,25 @@ +from rest_framework import generics +from rest_framework import permissions +from django_filters.rest_framework import DjangoFilterBackend + +from .models import ApplicationSubmission +from .serializers import SubmissionListSerializer, SubmissionDetailSerializer +from .permissions import IsApplyStaffUser + + +class SubmissionList(generics.ListAPIView): + queryset = ApplicationSubmission.objects.all() + serializer_class = SubmissionListSerializer + permission_classes = ( + permissions.IsAuthenticated, IsApplyStaffUser, + ) + filter_backends = (DjangoFilterBackend,) + filter_fields = ('round', 'status') + + +class SubmissionDetail(generics.RetrieveAPIView): + queryset = ApplicationSubmission.objects.all() + serializer_class = SubmissionDetailSerializer + permission_classes = ( + permissions.IsAuthenticated, IsApplyStaffUser, + ) diff --git a/opentech/apply/funds/permissions.py b/opentech/apply/funds/permissions.py new file mode 100644 index 0000000000000000000000000000000000000000..ec6f22f83b78b3476cf267333a68149c7c32e0df --- /dev/null +++ b/opentech/apply/funds/permissions.py @@ -0,0 +1,13 @@ +from rest_framework import permissions + + +class IsApplyStaffUser(permissions.BasePermission): + """ + Custom permission to only allow OTF Staff or higher + """ + + def has_permission(self, request, view): + return request.user.is_apply_staff + + def has_object_permission(self, request, view, obj): + return request.user.is_apply_staff diff --git a/opentech/apply/funds/serializers.py b/opentech/apply/funds/serializers.py new file mode 100644 index 0000000000000000000000000000000000000000..acaf39eb7ade84a0165cd27055bf380cb1f4a50c --- /dev/null +++ b/opentech/apply/funds/serializers.py @@ -0,0 +1,15 @@ +from rest_framework import serializers + +from .models import ApplicationSubmission + + +class SubmissionListSerializer(serializers.ModelSerializer): + class Meta: + model = ApplicationSubmission + fields = ('id',) + + +class SubmissionDetailSerializer(serializers.ModelSerializer): + class Meta: + model = ApplicationSubmission + fields = ('id', 'title',) diff --git a/opentech/apply/funds/urls.py b/opentech/apply/funds/urls.py index d9f29072400c042075367adee6bcb78654ff56b0..3f461c32e778ea6bf608389dcc93b73f1d35fe81 100644 --- a/opentech/apply/funds/urls.py +++ b/opentech/apply/funds/urls.py @@ -10,6 +10,7 @@ from .views import ( SubmissionSealedView, SubmissionSearchView, ) +from .api_views import SubmissionList, SubmissionDetail revision_urls = ([ @@ -35,8 +36,14 @@ submission_urls = ([ path('rounds/<int:pk>/', SubmissionsByRound.as_view(), name="by_round"), ], 'submissions') +submission_api_urls = ([ + path('', SubmissionList.as_view(), name='list'), + path('<int:pk>/', SubmissionDetail.as_view(), name='detail'), +], 'submissions-api') + urlpatterns = [ path('submissions/', include(submission_urls)), + path('api/submissions/', include(submission_api_urls)), path('search/', SubmissionSearchView.as_view(), name="search"), ] diff --git a/opentech/settings/base.py b/opentech/settings/base.py index a8146b1edc17c7ebfafe7b7482c437d3c28d4e5c..12e28fd06bc60820f8960b3c3578f0be4d09e31d 100644 --- a/opentech/settings/base.py +++ b/opentech/settings/base.py @@ -116,6 +116,7 @@ INSTALLED_APPS = [ 'django_bleach', 'django_fsm', 'django_pwned_passwords', + 'rest_framework', 'hijack', 'compat', @@ -605,3 +606,16 @@ WEBPACK_LOADER = { COUNTRIES_OVERRIDE = { 'KV': 'Kosovo', } + +# Rest Framework configuration +REST_FRAMEWORK = { + 'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination', + 'PAGE_SIZE': 10, + 'DEFAULT_AUTHENTICATION_CLASSES': ( + 'rest_framework.authentication.BasicAuthentication', + 'rest_framework.authentication.SessionAuthentication', + ), + 'DEFAULT_PERMISSION_CLASSES': ( + 'rest_framework.permissions.IsAuthenticated', + ) +} diff --git a/requirements.txt b/requirements.txt index 25b2bb484519f959a7b91e525edfb1713e75396c..fbc49f76778fdab1ff33dee15eeee2015d01854d 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,5 +1,5 @@ django~=2.0.0 -djangorestframework==3.7.4 +djangorestframework==3.9.0 django-fsm==2.6.0 wagtail~=2.2.0 psycopg2==2.7.3.1