from datetime import datetime


class WebRequestMiddleware:
    def __init__(self, get_response):
        self.get_response = get_response

    def __call__(self, request):

        response = self.get_response(request)

        if not '/v1/alert/' in request.path:
            user = getattr(request, 'user', None)
            if user and not user.is_anonymous and user.user_profile:
                user.user_profile.last_web_request = datetime.now()
                user.user_profile.save(update_fields=['last_web_request'])

                # from GeneralApp.temp_data_funcs import add_element_in_tmp_data_list
                # add_element_in_tmp_data_list('user_activity', user.email, 'activities_DT', str(user.user_profile.last_web_request))

        return response
