text: # coding: utf-8
  from __future__ import unicode_literals, print_function, division, absolute_import
  
  from rest_framework import viewsets
  from rest_framework.response import Response
  
  from onadata.apps.api.permissions import ConnectViewsetPermissions
  from onadata.apps.main.models.user_profile import UserProfile
  from onadata.libs.mixins.object_lookup_mixin import ObjectLookupMixin
  from onadata.libs.serializers.user_profile_serializer import (
      UserProfileWithTokenSerializer)
  from onadata.settings.common import DEFAULT_SESSION_EXPIRY_TIME
  
  
  class ConnectViewSet(ObjectLookupMixin, viewsets.GenericViewSet):
      """This endpoint allows you retrieve the authenticated user's profile info.
  
  ## Retrieve profile
  > Example
  >
  >       curl -X GET https://example.com/api/v1/user
  
  > Response:
  
  >       {
              "api_token": "76121138a080c5ae94f318a8b9be91e7ebebb484",
              "city": "Nairobi",
              "country": "Kenya",
              "gravatar": "avatar.png",
              "name": "Demo User",
              "organization": "",
              "require_auth": false,
              "twitter": "",
              "url": "http://localhost:8000/api/v1/profiles/demo",
              "user": "http://localhost:8000/api/v1/users/demo",
              "username": "demo",
              "website": ""
  }
  
  """
      lookup_field = 'user'
      queryset = UserProfile.objects.all()
      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)
  
          serializer = UserProfileWithTokenSerializer(
              instance=UserProfile.objects.get_or_create(user=request.user)[0],
              context={"request": request})
  
          return Response(serializer.data)
  