I am trying to check and display user online status with django and django rest framework.
I have tried this article: Django - detect if user is online / offline But did not work on APIview only works on views with render and Httpresponse.
Updated
Here is the code for the middleware class
import datetime
from django.core.cache import cache
from django.conf import settings
from django.utils.deprecation import MiddlewareMixin
class ActiveUserMiddleware(MiddlewareMixin):
def process_request(self, request):
current_user = request.user
if request.user.is_authenticated:
now = datetime.datetime.now()
cache.set('seen_%s' % (current_user.username), now, settings.USER_LASTSEEN_TIMEOUT)
The APIView class ProfilePersonal(APIView): permission_classes = (permissions.IsAuthenticated,)
serializer_class = UserProfileSerializer
def get(self):
try:
queryset = Account.objects.get()
except Account.DoesNotExist:
return Response(data={'non_field_errors': 'Request could not be validated.'},
status=status.HTTP_400_BAD_REQUEST)
serializer_data = self.serializer_class(queryset)
return Response(
data=serializer_data,
status=status.HTTP_200_OK
)