I’mt rying to write a feature from my custom CMS for the admin to switch home page login (username / password) form input boxes from available to unavailable. In my web app, I’ve affectionately named the class model attribute the “nuclear
” option.
Here is how the page should render by default:
Then when the admin switches the ‘nuclear’ Bool to True, the site’s home page should look like this:
The above describes what I am trying to accomplish.
The problem I have is that whether I’ve toggled the Bool checkbox in my Admin Dashboard on or off, there is no change to the login page. Here is my Admin interface showing the “nuclear” check box:
Here is the relevant code block in my Jinja template (and pay particular attention to the first line):
{% if not controls.nuclear %}
<h1> Site-wide access revoked</h1>
{% else %}
<form method='post' action="{% url 'index' %}">
{% csrf_token %}
<h2>
{{form.username.label_tag}}
{{form.username}}
</h2>
<h2>
{{form.password.label_tag}}
{{form.password}}
<input type="submit" value='login'>
</h2>
<input type="hidden" name='next' value="{{next}}">
</form>
{% endif %}
At the first line, {% if not controls.nuclear %}
, if I manually remove not
, then Django will successfully serve the username/password form and restoring not
by adding it back, Django will serve the h1 “Access Revoked” line. So it kind of works. What I am trying to figure out now is how to get the Admin Dashboard checkbox to trigger the change rather than me having to manually enter/remove the negation operator into the template.
I am not sure what I am doing wrong.
Below are some addition snippets from the relevant source code in my project repo.
Here is my views.py. Take note that I am using the LoginView CBV and am over-riding the context_object_name
to 'controls'
which matches what I am using in my template:
class Gateway(LoginView):
model = AuthToggle
fields = '__all__'
context_object_name = 'controls'
template_name = 'registration/login.html'
redirect_authenticated_user = True
def get_success_url(self):
return reverse_lazy('portal')
def dispatch(self, request, *args, **kwargs):
# Overiding the dispatch method to add extra functionality to the loginview
response = super().dispatch(request, *args, **kwargs)
auth_toggle = AuthToggle.objects.first()
if self.request.user.is_authenticated and auth_toggle.is_protected and not request.user.is_staff:
# It is neccessary to store the time in session to set the session expiry + Start session timer
request.session['session_start_time'] = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
request.session.set_expiry(auth_toggle.timeout * 60) # I am converting the minutes in secconds
# Print session start time
notification.messages_print(
'info', 'New session of ' + str(SESSION_TIMEOUT.timeout) + ' minutes has started'
)
print(f"Time session started at: {request.session['session_start_time']}")
elif not self.request.user.is_authenticated and not auth_toggle.is_protected:
return redirect('portal')
return response
Here is my models.py:
class AuthToggle(models.Model):
is_protected = models.BooleanField(default=False)
faravahar = models.BooleanField(default=False)
nuclear = models.BooleanField(default=True)
timeout = models.IntegerField(default=1)
# is_time_session = models.BooleanField(default=False)
# start_time_session = models.DateTimeField(null=True, blank=True)
email = models.EmailField(max_length=50, default='')
def __str__(self):
return "Options"
urls.py:
from django.urls import path,include
from . import views
urlpatterns = [
path('', views.Gateway.as_view(), name='index'), # former
path('portal/', views.portal, name='portal'),
path('logout/', views.EndSession.as_view(), name='logout'),
]