Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#2037] Change file_upload endpoint permissions #2044

Merged
merged 1 commit into from
Mar 8, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 24 additions & 17 deletions akvo/rest/views/indicator_period_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@
IndicatorPeriodDataCommentSerializer)
from ..viewsets import PublicProjectViewSet

from django.http import HttpResponseForbidden

from rest_framework import status
from rest_framework.decorators import api_view, permission_classes
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response


Expand Down Expand Up @@ -80,7 +82,6 @@ class IndicatorPeriodDataCommentViewSet(PublicProjectViewSet):


@api_view(['POST'])
@permission_classes((IsAuthenticated, ))
def indicator_upload_file(request, pk=None):
"""
Special API call for directly uploading a file.
Expand All @@ -91,18 +92,24 @@ def indicator_upload_file(request, pk=None):
update = IndicatorPeriodData.objects.get(pk=pk)
upload_file = request.FILES['file']

# TODO: Permissions
# user = request.user

file_type = request.POST.copy()['type']
if file_type == 'photo':
update.photo = upload_file
update.save(update_fields=['photo'])
return Response({'file': update.photo.url})
elif file_type == 'file':
update.file = upload_file
update.save(update_fields=['file'])
return Response({'file': update.file.url})

# TODO: Error response
return Response({})
# Permissions
user = getattr(request, 'user', None)
if not user:
return Response({'error': 'User is not logged in'}, status=status.HTTP_403_FORBIDDEN)

if not user.has_perm('rsr.change_project', update.period.indicator.result.project):
return Response({'error': 'User has no permission to place an update'},
status=status.HTTP_403_FORBIDDEN)

try:
file_type = request.POST.copy()['type']
if file_type == 'photo':
update.photo = upload_file
update.save(update_fields=['photo'])
return Response({'file': update.photo.url})
elif file_type == 'file':
update.file = upload_file
update.save(update_fields=['file'])
return Response({'file': update.file.url})
except Exception as e:
return Response({'error': str(e)}, status=status.HTTP_400_BAD_REQUEST)