from django.contrib.auth.mixins import AccessMixin
from django.contrib.auth.views import redirect_to_login
from django.urls import reverse_lazy


class AnonymousLoginRequiredMixin(AccessMixin):
    login_url = reverse_lazy('anonymous_login')
    secure = True

    def get(self, request, *args, **kwargs):
        request.session.setdefault('anonymous_login', False)
        if self.secure and not self.request.session['anonymous_login']:
            if hasattr(self, 'object') and hasattr(self.object, 'is_secure') and not self.object.is_secure:
                return super(AnonymousLoginRequiredMixin, self).get(request, *args, **kwargs)
            return redirect_to_login(self.request.get_full_path(), self.get_login_url(), self.get_redirect_field_name())
        return super(AnonymousLoginRequiredMixin, self).get(request, *args, **kwargs)
