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

Include create method to ConnectViewset #1683

Merged
merged 1 commit into from
Sep 4, 2019
Merged
Show file tree
Hide file tree
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
11 changes: 10 additions & 1 deletion onadata/apps/api/tests/viewsets/test_connect_viewset.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def setUp(self):
self.view = ConnectViewSet.as_view({
"get": "list",
"post": "reset",
"delete": "expire"
"delete": "expire",
})
self.data = {
'url': 'http://testserver/api/v1/profiles/bob',
Expand All @@ -51,6 +51,15 @@ def setUp(self):
'api_token': self.user.auth_token.key,
}

def test_generate_auth_token(self):
self.view = ConnectViewSet.as_view({
"post": "create",
})
request = self.factory.post("/", **self.extra)
request.session = self.client.session
response = self.view(request)
self.assertEqual(response.status_code, 201)

def test_regenerate_auth_token(self):
self.view = ConnectViewSet.as_view({
"get": "regenerate_auth_token",
Expand Down
56 changes: 33 additions & 23 deletions onadata/apps/api/viewsets/connect_viewset.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from rest_framework.decorators import action
from rest_framework.exceptions import ParseError
from rest_framework.response import Response
from rest_framework import mixins

from onadata.apps.api.models.temp_token import TempToken
from onadata.apps.api.permissions import ConnectViewsetPermissions
Expand All @@ -23,8 +24,33 @@
from onadata.settings.common import DEFAULT_SESSION_EXPIRY_TIME


class ConnectViewSet(AuthenticateHeaderMixin, CacheControlMixin, ETagsMixin,
ObjectLookupMixin, viewsets.GenericViewSet):
def user_profile_w_token_response(request, status):
""" Returns authenticated user profile"""

if request and not request.user.is_anonymous:
session = getattr(request, "session")
if not session.session_key:
# login user to create session token
# TODO cannot call this without calling authenticate first or
# setting the backend, commented for now.
# login(request, request.user)
session.set_expiry(DEFAULT_SESSION_EXPIRY_TIME)

try:
user_profile = request.user.profile
except UserProfile.DoesNotExist:
user_profile, _ = UserProfile.objects.get_or_create(
user=request.user)

serializer = UserProfileWithTokenSerializer(
instance=user_profile, context={"request": request})

return Response(serializer.data, status=status)


class ConnectViewSet(mixins.CreateModelMixin, AuthenticateHeaderMixin,
CacheControlMixin, ETagsMixin, ObjectLookupMixin,
viewsets.GenericViewSet):
"""
This endpoint allows you retrieve the authenticated user's profile info.
"""
Expand All @@ -33,28 +59,12 @@ class ConnectViewSet(AuthenticateHeaderMixin, CacheControlMixin, ETagsMixin,
permission_classes = (ConnectViewsetPermissions, )
serializer_class = UserProfileWithTokenSerializer

def list(self, request, *args, **kwargs):
""" Returns authenticated user profile"""

if request and not request.user.is_anonymous:
session = getattr(request, "session")
if not session.session_key:
# login user to create session token
# TODO cannot call this without calling authenticate first or
# setting the backend, commented for now.
# login(request, request.user)
session.set_expiry(DEFAULT_SESSION_EXPIRY_TIME)

try:
user_profile = request.user.profile
except UserProfile.DoesNotExist:
user_profile, _ = UserProfile.objects.get_or_create(
user=request.user)

serializer = UserProfileWithTokenSerializer(
instance=user_profile, context={"request": request})
# pylint: disable=R0201
def create(self, request, *args, **kwargs):
return user_profile_w_token_response(request, status.HTTP_201_CREATED)

return Response(serializer.data)
def list(self, request, *args, **kwargs):
return user_profile_w_token_response(request, status.HTTP_200_OK)

@action(methods=['GET'], detail=True)
def starred(self, request, *args, **kwargs):
Expand Down